Skip to content

Commit b92c164

Browse files
committed
Merge branch 'master' into feat/ml-experementa
2 parents 51a0f5d + 4e5acfa commit b92c164

File tree

15 files changed

+378
-191
lines changed

15 files changed

+378
-191
lines changed

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@
138138
"webpack-bundle-analyzer": "^4.5.0",
139139
"webpack-cli": "^4.10.0",
140140
"webpack-dev-server": "^4.10.0",
141-
"webpack-merge": "^5.7.3"
141+
"webpack-merge": "^5.7.3",
142+
"workbox-webpack-plugin": "^7.1.0"
142143
},
143144
"dependencies": {
144145
"@apollo/client": "^3.9.4",
@@ -256,6 +257,11 @@
256257
"videostream": "^3.2.2",
257258
"web3": "1.2.4",
258259
"web3-utils": "^4.2.1",
260+
"workbox-core": "^7.1.0",
261+
"workbox-expiration": "^7.1.0",
262+
"workbox-precaching": "^7.1.0",
263+
"workbox-routing": "^7.1.0",
264+
"workbox-strategies": "^7.1.0",
259265
"worker-url": "^1.1.0"
260266
},
261267
"prec-commit": [

src/components/loader/loader.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,23 @@ const Bootloader = () => {
209209
};
210210

211211
function bootstrap() {
212+
if ('serviceWorker' in navigator) {
213+
console.log('Going to install service worker');
214+
window.addEventListener('load', () => {
215+
console.log('Starting to load service worker');
216+
navigator.serviceWorker
217+
.register('/service-worker.js')
218+
.then((registration) => {
219+
console.log('service worker registered: ', registration);
220+
})
221+
.catch((registrationError) => {
222+
console.log('service worker registration failed: ', registrationError);
223+
});
224+
});
225+
} else {
226+
console.log('No service worker is available');
227+
}
228+
212229
let _a;
213230
const assets =
214231
((_a =
@@ -229,9 +246,8 @@ function bootstrap() {
229246

230247
progressData.innerHTML = `Loading: <span>${Math.round(
231248
progress * 100
232-
)}%</span>. <br/> Network speed: <span>${
233-
Math.round(e.networkSpeed * 100) / 100
234-
} kbps</span>`;
249+
)}%</span>. <br/> Network speed: <span>${Math.round(e.networkSpeed * 100) / 100
250+
} kbps</span>`;
235251

236252
// console.log(e.loaded, e.loaded / e.totalSize); // @TODO
237253
})

src/containers/Search/SearchResults.tsx

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
1-
import { useState, useEffect } from 'react';
2-
import { useParams } from 'react-router-dom';
1+
import { useEffect, useState } from 'react';
32
import InfiniteScroll from 'react-infinite-scroll-component';
4-
import { useDevice } from 'src/contexts/device';
5-
import { IpfsContentType } from 'src/services/ipfs/types';
3+
import { useParams } from 'react-router-dom';
4+
import Display from 'src/components/containerGradient/Display/Display';
65
import Spark from 'src/components/search/Spark/Spark';
76
import Loader2 from 'src/components/ui/Loader2';
8-
import { getIpfsHash } from 'src/utils/ipfs/helpers';
9-
import { PATTERN_IPFS_HASH } from 'src/constants/patterns';
10-
import Display from 'src/components/containerGradient/Display/Display';
7+
import { useDevice } from 'src/contexts/device';
8+
import { IpfsContentType } from 'src/services/ipfs/types';
119

12-
import { encodeSlash } from '../../utils/utils';
10+
import useIsOnline from 'src/hooks/useIsOnline';
1311
import ActionBarContainer from './ActionBarContainer';
14-
import FirstItems from './_FirstItems.refactor';
15-
import useSearchData from './hooks/useSearchData';
16-
import { LinksTypeFilter, SortBy } from './types';
1712
import Filters from './Filters/Filters';
1813
import styles from './SearchResults.module.scss';
14+
import FirstItems from './_FirstItems.refactor';
1915
import { initialContentTypeFilterState } from './constants';
2016
import { getSearchQuery } from 'src/utils/search/utils';
17+
import useSearchData from './hooks/useSearchData';
18+
import { LinksTypeFilter, SortBy } from './types';
2119

2220
const sortByLSKey = 'search-sort';
2321

2422
function SearchResults() {
2523
const { query: q, cid } = useParams();
24+
const isOnline = useIsOnline();
25+
const defaultMessage = isOnline
26+
? 'there are no answers or questions to this particle <br /> be the first and create one'
27+
: "ther's nothing to show, wait until you're online";
2628

2729
const query = q || cid || '';
2830

@@ -151,10 +153,7 @@ function SearchResults() {
151153
<p>{error.message}</p>
152154
</Display>
153155
) : (
154-
<Display color="white">
155-
there are no answers or questions to this particle <br /> be the
156-
first and create one
157-
</Display>
156+
<Display color="white">{defaultMessage}</Display>
158157
)}
159158
</div>
160159

src/containers/application/Header/SwitchAccount/SwitchAccount.tsx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
import React, { useMemo, useRef } from 'react';
21
import cx from 'classnames';
3-
import { Link, useLocation } from 'react-router-dom';
2+
import React, { useMemo, useRef } from 'react';
43
import { usePopperTooltip } from 'react-popper-tooltip';
4+
import { Link, useLocation } from 'react-router-dom';
55
import { Transition } from 'react-transition-group';
66

7+
import usePassportByAddress from 'src/features/passport/hooks/usePassportByAddress';
78
import useOnClickOutside from 'src/hooks/useOnClickOutside';
89
import { routes } from 'src/routes';
9-
import usePassportByAddress from 'src/features/passport/hooks/usePassportByAddress';
1010

11-
import { useAppSelector } from 'src/redux/hooks';
1211
import Pill from 'src/components/Pill/Pill';
12+
import { useBackend } from 'src/contexts/backend/backend';
1313
import { useSigningClient } from 'src/contexts/signerClient';
14+
import useIsOnline from 'src/hooks/useIsOnline';
15+
import { useAppSelector } from 'src/redux/hooks';
1416
import BroadcastChannelSender from 'src/services/backend/channels/BroadcastChannelSender';
15-
import { useBackend } from 'src/contexts/backend/backend';
16-
import { AvataImgIpfs } from '../../../portal/components/avataIpfs';
17-
import styles from './SwitchAccount.module.scss';
18-
import networkStyles from '../SwitchNetwork/SwitchNetwork.module.scss';
1917
import useMediaQuery from '../../../../hooks/useMediaQuery';
2018
import robot from '../../../../image/temple/robot.png';
19+
import { AvataImgIpfs } from '../../../portal/components/avataIpfs';
2120
import Karma from '../../Karma/Karma';
21+
import networkStyles from '../SwitchNetwork/SwitchNetwork.module.scss';
22+
import styles from './SwitchAccount.module.scss';
2223

2324
// should be refactored
2425
function AccountItem({
@@ -91,6 +92,7 @@ function AccountItem({
9192
function SwitchAccount() {
9293
const { signerReady } = useSigningClient();
9394
const { isIpfsInitialized } = useBackend();
95+
const isOnline = useIsOnline();
9496
const mediaQuery = useMediaQuery('(min-width: 768px)');
9597

9698
const [controlledVisible, setControlledVisible] = React.useState(false);
@@ -179,9 +181,10 @@ function SwitchAccount() {
179181
</button>
180182
)}
181183
{isReadOnly && <Pill color="yellow" text="read only" />}
182-
{!isReadOnly && !signerReady && (
184+
{!isReadOnly && !signerReady && isOnline && (
183185
<Pill color="red" text="switch keplr" />
184186
)}
187+
{!isOnline && <Pill color="red" text="offline" />}
185188
<Karma address={useGetAddress} />
186189
</div>
187190
)}
@@ -195,8 +198,10 @@ function SwitchAccount() {
195198
>
196199
<div
197200
className={cx(styles.containerAvatarConnect, {
198-
[styles.containerAvatarConnectFalse]: !isIpfsInitialized,
199-
[styles.containerAvatarConnectTrueGreen]: isIpfsInitialized,
201+
[styles.containerAvatarConnectFalse]:
202+
!isIpfsInitialized || !isOnline,
203+
[styles.containerAvatarConnectTrueGreen]:
204+
isIpfsInitialized && isOnline,
200205
})}
201206
>
202207
<AvataImgIpfs

src/containers/portal/components/avataIpfs/AvataImgIpfs.tsx

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,24 @@
1-
import { useQuery } from '@tanstack/react-query';
2-
import { useEffect, useState } from 'react';
3-
import styles from './styles.module.scss';
41
import useQueueIpfsContent from 'src/hooks/useQueueIpfsContent';
52
import cx from 'classnames';
3+
import { useEffect, useState } from 'react';
4+
import styles from './styles.module.scss';
65

76
const getRoboHashImage = (addressCyber: string) =>
87
`https://robohash.org/${addressCyber}`;
98

109
function AvataImgIpfs({ img = '', cidAvatar, addressCyber, ...props }) {
1110
const { fetchWithDetails } = useQueueIpfsContent();
12-
13-
const [avatar, setAvatar] = useState<string | null>(null);
14-
const { data } = useQuery(
15-
['getAvatar', cidAvatar],
16-
async () => {
17-
const response = await fetchWithDetails!(cidAvatar, 'image');
18-
return response?.content || '';
19-
},
20-
21-
{
22-
enabled: Boolean(fetchWithDetails && cidAvatar),
23-
staleTime: 10 * (60 * 1000), // 10 mins
24-
cacheTime: 15 * (60 * 1000), // 15 mins
25-
retry: 0,
26-
}
27-
);
11+
const [avatar, setAvatar] = useState<string | undefined>(undefined);
2812

2913
useEffect(() => {
30-
if (!data) {
31-
setAvatar(null);
32-
} else {
33-
setAvatar(data);
14+
if (!fetchWithDetails || !cidAvatar) {
15+
return;
3416
}
35-
}, [data]);
17+
18+
fetchWithDetails(cidAvatar, 'image').then((value) => {
19+
setAvatar(value?.content);
20+
});
21+
}, [fetchWithDetails, cidAvatar]);
3622

3723
const avatarImage =
3824
avatar ||

src/containers/warp/Warp.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,14 @@ function Warp() {
103103
}, [setAdviser, tab]);
104104

105105
useEffect(() => {
106-
const [{ coinDecimals }] = traseDenom(tokenA);
106+
const [{ coinDecimals }] = tracesDenom(tokenA);
107107
setTokenACoinDecimals(coinDecimals);
108-
}, [traseDenom, tokenA]);
108+
}, [tracesDenom, tokenA]);
109109

110110
useEffect(() => {
111-
const [{ coinDecimals }] = traseDenom(tokenB);
111+
const [{ coinDecimals }] = tracesDenom(tokenB);
112112
setTokenBCoinDecimals(coinDecimals);
113-
}, [traseDenom, tokenB]);
113+
}, [tracesDenom, tokenB]);
114114

115115
useEffect(() => {
116116
if (firstEffectOccured.current) {

src/contexts/signerClient.tsx

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ import React, {
55
useMemo,
66
useState,
77
} from 'react';
8+
import _ from 'lodash';
89
import { SigningCyberClient } from '@cybercongress/cyber-js';
910
import configKeplr, { getKeplr } from 'src/utils/keplrUtils';
1011
import { OfflineSigner } from '@cybercongress/cyber-js/build/signingcyberclient';
1112
import { Option } from 'src/types';
12-
import { useAppSelector } from 'src/redux/hooks';
13+
import { useAppDispatch, useAppSelector } from 'src/redux/hooks';
14+
import { Keplr } from '@keplr-wallet/types';
15+
import { addAddressPocket, setDefaultAccount } from 'src/redux/features/pocket';
16+
import { accountsKeplr } from 'src/utils/utils';
17+
import usePrevious from 'src/hooks/usePrevious';
1318
import { RPC_URL, BECH32_PREFIX, CHAIN_ID } from 'src/constants/config';
1419

1520
// TODO: interface for keplr and OfflineSigner
@@ -50,11 +55,37 @@ export function useSigningClient() {
5055
}
5156

5257
function SigningClientProvider({ children }: { children: React.ReactNode }) {
53-
const { defaultAccount } = useAppSelector((state) => state.pocket);
58+
const { defaultAccount, accounts } = useAppSelector((state) => state.pocket);
59+
const dispatch = useAppDispatch();
5460
const [signer, setSigner] = useState<SignerClientContextType['signer']>();
5561
const [signerReady, setSignerReady] = useState(false);
5662
const [signingClient, setSigningClient] =
5763
useState<SignerClientContextType['signingClient']>();
64+
const prevAccounts = usePrevious(accounts);
65+
66+
const selectAddress = useCallback(
67+
async (keplr: Keplr) => {
68+
if (!accounts || _.isEqual(prevAccounts, accounts)) {
69+
return;
70+
}
71+
const keyInfo = await keplr.getKey(CHAIN_ID);
72+
73+
const findAccount = Object.keys(accounts).find((key) => {
74+
if (accounts[key].cyber.bech32 === keyInfo.bech32Address) {
75+
return key;
76+
}
77+
78+
return undefined;
79+
});
80+
81+
if (findAccount) {
82+
dispatch(setDefaultAccount({ name: findAccount }));
83+
} else {
84+
dispatch(addAddressPocket(accountsKeplr(keyInfo)));
85+
}
86+
},
87+
[accounts, prevAccounts, dispatch]
88+
);
5889

5990
useEffect(() => {
6091
(async () => {
@@ -73,6 +104,8 @@ function SigningClientProvider({ children }: { children: React.ReactNode }) {
73104
const initSigner = useCallback(async () => {
74105
const windowKeplr = await getKeplr();
75106
if (windowKeplr && windowKeplr.experimentalSuggestChain) {
107+
selectAddress(windowKeplr);
108+
76109
windowKeplr.defaultOptions = {
77110
sign: {
78111
preferNoSetFee: true,
@@ -87,7 +120,7 @@ function SigningClientProvider({ children }: { children: React.ReactNode }) {
87120
setSigner(offlineSigner);
88121
setSigningClient(clientJs);
89122
}
90-
}, []);
123+
}, [selectAddress]);
91124

92125
useEffect(() => {
93126
(async () => {

src/hooks/useIsOnline.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { useState, useEffect } from 'react';
2+
3+
function useIsOnline() {
4+
const [isOnline, setIsOnline] = useState(navigator.onLine);
5+
6+
useEffect(() => {
7+
function handleOnline() {
8+
console.log('isOnline', true);
9+
setIsOnline(true);
10+
}
11+
12+
function handleOffline() {
13+
console.log('isOnline', false);
14+
setIsOnline(false);
15+
}
16+
17+
window.addEventListener('online', handleOnline);
18+
window.addEventListener('offline', handleOffline);
19+
20+
return () => {
21+
window.removeEventListener('online', handleOnline);
22+
window.removeEventListener('offline', handleOffline);
23+
};
24+
}, []);
25+
26+
return isOnline;
27+
}
28+
29+
export default useIsOnline;

src/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import DeviceProvider from './contexts/device';
4040
import IbcDenomProvider from './contexts/ibcDenom';
4141
import NetworksProvider from './contexts/networks';
4242
import BackendProvider from './contexts/backend/backend';
43+
4344
import AdviserProvider from './features/adviser/context';
4445
import HubProvider from './contexts/hub';
4546

0 commit comments

Comments
 (0)