Skip to content

Commit e78e65f

Browse files
fix(app): sync acc in tabs (#1122)
1 parent dfd1583 commit e78e65f

File tree

7 files changed

+39
-62
lines changed

7 files changed

+39
-62
lines changed

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ import useOnClickOutside from 'src/hooks/useOnClickOutside';
88
import { routes } from 'src/routes';
99
import usePassportByAddress from 'src/features/passport/hooks/usePassportByAddress';
1010

11-
import { useAppDispatch, useAppSelector } from 'src/redux/hooks';
11+
import { useAppSelector } from 'src/redux/hooks';
1212
import Pill from 'src/components/Pill/Pill';
1313
import { useSigningClient } from 'src/contexts/signerClient';
14+
import BroadcastChannelSender from 'src/services/backend/channels/BroadcastChannelSender';
15+
import { useBackend } from 'src/contexts/backend/backend';
1416
import { AvataImgIpfs } from '../../../portal/components/avataIpfs';
1517
import styles from './SwitchAccount.module.scss';
1618
import networkStyles from '../SwitchNetwork/SwitchNetwork.module.scss';
1719
import useMediaQuery from '../../../../hooks/useMediaQuery';
1820
import robot from '../../../../image/temple/robot.png';
1921
import Karma from '../../Karma/Karma';
20-
import { setDefaultAccount } from '../../../../redux/features/pocket';
21-
import { useBackend } from 'src/contexts/backend/backend';
2222

2323
// should be refactored
2424
function AccountItem({
@@ -96,7 +96,6 @@ function SwitchAccount() {
9696
const [controlledVisible, setControlledVisible] = React.useState(false);
9797

9898
const { defaultAccount, accounts } = useAppSelector((state) => state.pocket);
99-
const dispatch = useAppDispatch();
10099

101100
const useGetAddress = defaultAccount?.account?.cyber?.bech32 || null;
102101

@@ -121,11 +120,8 @@ function SwitchAccount() {
121120
const isReadOnly = defaultAccount.account?.cyber.keys === 'read-only';
122121

123122
const onClickChangeActiveAcc = async (key: string) => {
124-
dispatch(
125-
setDefaultAccount({
126-
name: key,
127-
})
128-
);
123+
const broadcastChannel = new BroadcastChannelSender();
124+
broadcastChannel.postSetDefaultAccount(key);
129125
setControlledVisible(false);
130126
};
131127

src/contexts/signerClient.tsx

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,12 @@ import React, {
55
useMemo,
66
useState,
77
} from 'react';
8-
import _ from 'lodash';
98
import { SigningCyberClient } from '@cybercongress/cyber-js';
109
import { CYBER } from 'src/utils/config';
1110
import configKeplr, { getKeplr } from 'src/utils/keplrUtils';
1211
import { OfflineSigner } from '@cybercongress/cyber-js/build/signingcyberclient';
1312
import { Option } from 'src/types';
14-
import { useAppDispatch, useAppSelector } from 'src/redux/hooks';
15-
import { Keplr } from '@keplr-wallet/types';
16-
import { addAddressPocket, setDefaultAccount } from 'src/redux/features/pocket';
17-
import { accountsKeplr } from 'src/utils/utils';
18-
import usePrevious from 'src/hooks/usePrevious';
13+
import { useAppSelector } from 'src/redux/hooks';
1914

2015
// TODO: interface for keplr and OfflineSigner
2116
// type SignerType = OfflineSigner & {
@@ -56,37 +51,11 @@ export function useSigningClient() {
5651
}
5752

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

9160
useEffect(() => {
9261
(async () => {
@@ -105,8 +74,6 @@ function SigningClientProvider({ children }: { children: React.ReactNode }) {
10574
const initSigner = useCallback(async () => {
10675
const windowKeplr = await getKeplr();
10776
if (windowKeplr && windowKeplr.experimentalSuggestChain) {
108-
selectAddress(windowKeplr);
109-
11077
windowKeplr.defaultOptions = {
11178
sign: {
11279
preferNoSetFee: true,
@@ -125,7 +92,7 @@ function SigningClientProvider({ children }: { children: React.ReactNode }) {
12592
setSigner(offlineSigner);
12693
setSigningClient(clientJs);
12794
}
128-
}, [selectAddress]);
95+
}, []);
12996

13097
useEffect(() => {
13198
(async () => {

src/pages/Keys/ActionBar/actionBar.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import { useEffect, useState } from 'react';
22
import { Pane, ActionBar as ActionBarGravity } from '@cybercongress/gravity';
33
import { useSigningClient } from 'src/contexts/signerClient';
4-
import ActionBarKeplr from './actionBarKeplr';
5-
import ActionBarUser from './actionBarUser';
6-
import ActionBarConnect from './actionBarConnect';
74
import waitForWeb3 from 'components/web3/waitForWeb3';
85
import { NETWORKSIDS } from 'src/utils/config';
96

10-
import imgLedger from 'src/image/ledger.svg';
117
import imgKeplr from 'src/image/keplr-icon.svg';
128
import imgRead from 'src/image/duplicate-outline.svg';
139
import Button from 'src/components/btnGrd';
1410
import { useDispatch, useSelector } from 'react-redux';
1511
import { RootState } from 'src/redux/store';
16-
import { deleteAddress, setDefaultAccount } from 'src/redux/features/pocket';
12+
import { deleteAddress } from 'src/redux/features/pocket';
13+
import BroadcastChannelSender from 'src/services/backend/channels/BroadcastChannelSender';
14+
import ActionBarConnect from './actionBarConnect';
15+
import ActionBarUser from './actionBarUser';
16+
import ActionBarKeplr from './actionBarKeplr';
1717

1818
const STAGE_INIT = 1;
1919
const STAGE_CONNECT = 2;
@@ -161,7 +161,8 @@ function ActionBar({
161161
)?.[0];
162162

163163
if (accountName) {
164-
dispatch(setDefaultAccount({ name: accountName }));
164+
const broadcastChannel = new BroadcastChannelSender();
165+
broadcastChannel.postSetDefaultAccount(accountName);
165166
}
166167
}
167168

src/services/backend/channels/BroadcastChannelSender.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { updateSenseList } from 'src/features/sense/redux/sense.redux';
2+
import { setDefaultAccount } from 'src/redux/features/pocket';
3+
import { Account } from 'src/types/defaultAccount';
24
import { SenseListItem } from '../types/sense';
35
import {
46
BroadcastChannelMessage,
@@ -39,6 +41,15 @@ class BroadcastChannelSender {
3941
}
4042
}
4143

44+
public postSetDefaultAccount(name: string, account?: Account) {
45+
this.channel.postMessage(
46+
setDefaultAccount({
47+
name,
48+
account,
49+
})
50+
);
51+
}
52+
4253
post(msg: BroadcastChannelMessage) {
4354
this.channel.postMessage(msg);
4455
}

src/services/backend/channels/RxBroadcastChannelListener.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { bufferTime, filter } from 'rxjs/operators';
44

55
import {
66
BC_MSG_LOAD_COMMUNITY,
7+
BC_MSG_SET_DEFAULT_ACCOUNT,
78
BroadcastChannelMessage,
89
getBroadcastChannemMessageKey,
910
} from '../types/services';
@@ -24,7 +25,10 @@ class RxBroadcastChannelListener {
2425
const channel = new BroadcastChannel(CYB_BROADCAST_CHANNEL);
2526

2627
channel.onmessage = (msg: MessageEvent<BroadcastChannelMessage>) => {
27-
if (msg.data.type === BC_MSG_LOAD_COMMUNITY) {
28+
if (
29+
msg.data.type === BC_MSG_LOAD_COMMUNITY ||
30+
msg.data.type === BC_MSG_SET_DEFAULT_ACCOUNT
31+
) {
2832
dispatch(msg.data);
2933
return;
3034
}

src/services/backend/types/services.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { SyncCommunityResult } from 'src/services/community/community';
2+
import { setDefaultAccount } from 'src/redux/features/pocket';
23
import { IndexedDbWriteMessage } from '../../CozoDb/types/types';
34

45
export type SyncEntryName =
@@ -71,6 +72,10 @@ export type LoadCommunityMessage = {
7172
value: SyncCommunityResult;
7273
};
7374

75+
export const BC_MSG_SET_DEFAULT_ACCOUNT = setDefaultAccount.type;
76+
77+
export type SetDefaultAccountMessage = typeof setDefaultAccount;
78+
7479
// export type SenseListUpdate = {
7580
// type: 'sense_list_update';
7681
// list: SenseListItem[];
@@ -81,7 +86,8 @@ export type BroadcastChannelMessage =
8186
| SyncEntryMessage
8287
| IndexedDbWriteMessage
8388
| ServiceStatusMessage
84-
| LoadCommunityMessage;
89+
| LoadCommunityMessage
90+
| SetDefaultAccountMessage;
8591
// | SenseListUpdate
8692
// | SenseListRemove;
8793

src/utils/config.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,8 @@ const COSMOS = {
7575
BECH32_PREFIX_ACC_ADDR_COSMOS: 'cosmos',
7676
};
7777

78-
const LOCALSTORAGE_CHAIN_ID = localStorage.getItem('chainId');
78+
const LOCALSTORAGE_CHAIN_ID = Networks.BOSTROM;
7979

80-
const CHAIN_PARAMS_LOCALSTORAGE = localStorage.getItem('CHAIN_PARAMS');
8180
let CHAIN_PARAMS = {
8281
CHAIN_ID: process.env.CHAIN_ID || Networks.BOSTROM,
8382
DENOM_CYBER: 'boot',
@@ -119,13 +118,6 @@ if (LOCALSTORAGE_CHAIN_ID === 'space-pussy') {
119118
};
120119
}
121120

122-
if (CHAIN_PARAMS_LOCALSTORAGE !== null && LOCALSTORAGE_CHAIN_ID !== null) {
123-
const CHAIN_PARAMS_LOCALSTORAGE_DATA = JSON.parse(CHAIN_PARAMS_LOCALSTORAGE);
124-
if (CHAIN_PARAMS_LOCALSTORAGE_DATA[LOCALSTORAGE_CHAIN_ID]) {
125-
CHAIN_PARAMS = { ...CHAIN_PARAMS_LOCALSTORAGE_DATA[LOCALSTORAGE_CHAIN_ID] };
126-
}
127-
}
128-
129121
const CYBER = {
130122
CYBER_CONGRESS_ADDRESS: 'bostrom1xszmhkfjs3s00z2nvtn7evqxw3dtus6yr8e4pw',
131123
DIVISOR_CYBER_G: 10 ** 9,

0 commit comments

Comments
 (0)