5
5
6
6
import DoneIcon from '@mui/icons-material/Done' ;
7
7
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' ;
9
9
10
- import { InputWithLabel , MenuItem , VaadinIcon } from '../../../components' ;
10
+ import { AccountContext , InputWithLabel , MenuItem , VaadinIcon } from '../../../components' ;
11
+ import { getStorage , setStorage } from '../../../components/Loading' ;
11
12
import { useInfo , useIsExtensionPopup , useProfiles , useTranslation } from '../../../hooks' ;
13
+ import { PROFILE_TAGS } from '../../../hooks/useProfileAccounts' ;
12
14
import { updateMeta } from '../../../messaging' ;
13
15
14
16
interface Props {
@@ -186,13 +188,21 @@ function ProfileMenu ({ address, closeParentMenu }: Props): React.ReactElement<P
186
188
const isExtensionMode = useIsExtensionPopup ( ) ;
187
189
188
190
const { account } = useInfo ( address ) ;
191
+ const { accounts } = useContext ( AccountContext ) ;
189
192
190
193
const [ anchorEl , setAnchorEl ] = useState < HTMLButtonElement | HTMLDivElement | null > ( ) ;
191
194
const [ status , setStatus ] = useState < STATUS > ( ) ;
192
195
const [ showName , setShowName ] = useState < boolean > ( ) ;
196
+ const [ currentProfile , setCurrentProfile ] = useState < string > ( ) ;
193
197
194
198
const profileNames = account ?. profile ? account . profile . split ( ',' ) : undefined ;
195
199
200
+ useEffect ( ( ) => {
201
+ getStorage ( 'profile' ) . then ( ( res ) => {
202
+ setCurrentProfile ( res as string ) ;
203
+ } ) . catch ( console . error ) ;
204
+ } , [ ] ) ;
205
+
196
206
const handleClose = useCallback ( ( ) => {
197
207
setAnchorEl ( null ) ;
198
208
setShowName ( false ) ;
@@ -210,22 +220,26 @@ function ProfileMenu ({ address, closeParentMenu }: Props): React.ReactElement<P
210
220
} , [ ] ) ;
211
221
212
222
const onRemove = useCallback ( ( profileToBeRemoved : string ) => {
213
- if ( ! account ?. profile ) {
223
+ if ( ! profileNames || ! accounts ) {
214
224
return ;
215
225
}
216
226
217
- const accountProfiles = account . profile . split ( ',' ) ;
218
- const indexToBeRemoved = accountProfiles . findIndex ( ( item ) => item === profileToBeRemoved ) ;
227
+ const accountsInAProfileTag = accounts . filter ( ( { profile } ) => profile && profile === currentProfile ) ;
219
228
220
- accountProfiles . splice ( indexToBeRemoved , 1 ) ;
229
+ const newProfiles = profileNames . filter ( ( item ) => item !== profileToBeRemoved ) ;
221
230
222
- const metaData = JSON . stringify ( { profile : accountProfiles ?. length ? accountProfiles . join ( ',' ) : null } ) ;
231
+ const metaData = JSON . stringify ( { profile : newProfiles ?. length ? newProfiles . join ( ',' ) : null } ) ;
223
232
224
233
updateMeta ( String ( address ) , metaData )
225
234
. 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
+ }
227
241
} ) . catch ( console . error ) ;
228
- } , [ account ?. profile , address , handleClose ] ) ;
242
+ } , [ accounts , address , currentProfile , handleClose , profileNames ] ) ;
229
243
230
244
const open = Boolean ( anchorEl ) ;
231
245
const id = open ? 'simple-popover 2' : undefined ;
0 commit comments