Skip to content

Commit 99b217e

Browse files
committed
fix: set profile to default on removing the last member of a profile
1 parent e723838 commit 99b217e

File tree

1 file changed

+23
-9
lines changed
  • packages/extension-polkagate/src/fullscreen/homeFullScreen/partials

1 file changed

+23
-9
lines changed

packages/extension-polkagate/src/fullscreen/homeFullScreen/partials/ProfileMenu.tsx

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55

66
import DoneIcon from '@mui/icons-material/Done';
77
import { Divider, Grid, IconButton, Popover, useTheme } from '@mui/material';
8-
import React, { useCallback, useState } from 'react';
8+
import React, { useCallback, useContext, useEffect, useState } from 'react';
99

10-
import { InputWithLabel, MenuItem, VaadinIcon } from '../../../components';
10+
import { AccountContext, InputWithLabel, MenuItem, VaadinIcon } from '../../../components';
11+
import { getStorage, setStorage } from '../../../components/Loading';
1112
import { useInfo, useIsExtensionPopup, useProfiles, useTranslation } from '../../../hooks';
13+
import { PROFILE_TAGS } from '../../../hooks/useProfileAccounts';
1214
import { updateMeta } from '../../../messaging';
1315

1416
interface Props {
@@ -186,13 +188,21 @@ function ProfileMenu ({ address, closeParentMenu }: Props): React.ReactElement<P
186188
const isExtensionMode = useIsExtensionPopup();
187189

188190
const { account } = useInfo(address);
191+
const { accounts } = useContext(AccountContext);
189192

190193
const [anchorEl, setAnchorEl] = useState<HTMLButtonElement | HTMLDivElement | null>();
191194
const [status, setStatus] = useState<STATUS>();
192195
const [showName, setShowName] = useState<boolean>();
196+
const [currentProfile, setCurrentProfile] = useState<string>();
193197

194198
const profileNames = account?.profile ? account.profile.split(',') : undefined;
195199

200+
useEffect(() => {
201+
getStorage('profile').then((res) => {
202+
setCurrentProfile(res as string);
203+
}).catch(console.error);
204+
}, []);
205+
196206
const handleClose = useCallback(() => {
197207
setAnchorEl(null);
198208
setShowName(false);
@@ -210,22 +220,26 @@ function ProfileMenu ({ address, closeParentMenu }: Props): React.ReactElement<P
210220
}, []);
211221

212222
const onRemove = useCallback((profileToBeRemoved: string) => {
213-
if (!account?.profile) {
223+
if (!profileNames || !accounts) {
214224
return;
215225
}
216226

217-
const accountProfiles = account.profile.split(',');
218-
const indexToBeRemoved = accountProfiles.findIndex((item) => item === profileToBeRemoved);
227+
const accountsInAProfileTag = accounts.filter(({ profile }) => profile && profile === currentProfile);
219228

220-
accountProfiles.splice(indexToBeRemoved, 1);
229+
const newProfiles = profileNames.filter((item) => item !== profileToBeRemoved);
221230

222-
const metaData = JSON.stringify({ profile: accountProfiles?.length ? accountProfiles.join(',') : null });
231+
const metaData = JSON.stringify({ profile: newProfiles?.length ? newProfiles.join(',') : null });
223232

224233
updateMeta(String(address), metaData)
225234
.then(() => {
226-
handleClose();
235+
if (accountsInAProfileTag.length === 1 && currentProfile === profileToBeRemoved) {
236+
// set profile tab to ALL, since this account was the last account with such a tag and the current profile is the same as account's profile
237+
setStorage('profile', PROFILE_TAGS.ALL).then(handleClose).catch(console.error);
238+
} else {
239+
handleClose();
240+
}
227241
}).catch(console.error);
228-
}, [account?.profile, address, handleClose]);
242+
}, [accounts, address, currentProfile, handleClose, profileNames]);
229243

230244
const open = Boolean(anchorEl);
231245
const id = open ? 'simple-popover 2' : undefined;

0 commit comments

Comments
 (0)