3
3
4
4
/* eslint-disable react/jsx-max-props-per-line */
5
5
6
+ import type { AccountJson } from '@polkadot/extension-base/background/types' ;
7
+
6
8
import { Grid , type SxProps , type Theme , Typography , useTheme } from '@mui/material' ;
7
- import React , { useCallback , useContext , useMemo } from 'react' ;
9
+ import React , { useCallback , useContext , useMemo , useRef } from 'react' ;
8
10
9
11
import { openOrFocusTab } from '../fullscreen/accountDetails/components/CommonTasks' ;
10
12
import { useTranslation } from '../hooks' ;
@@ -22,23 +24,48 @@ interface Props {
22
24
accountTypeFilter ?: AccountTypeFilterType ;
23
25
selectedAccounts : string [ ] ;
24
26
setSelectedAccounts : React . Dispatch < React . SetStateAction < string [ ] > > ;
27
+ manageConnectedAccounts ?: boolean ;
25
28
}
26
29
27
- export default function AccountsTable ( { accountTypeFilter, areAllCheck, label, maxHeight = '112px' , selectedAccounts, setSelectedAccounts, style } : Props ) : React . ReactElement < Props > {
30
+ const sortAccounts = ( accountA : AccountJson , accountB : AccountJson , selectedList : string [ ] ) : number => {
31
+ const isASelected = selectedList . includes ( accountA . address ) ;
32
+ const isBSelected = selectedList . includes ( accountB . address ) ;
33
+
34
+ if ( ! isASelected && isBSelected ) {
35
+ return - 1 ;
36
+ } else if ( isASelected && ! isBSelected ) {
37
+ return 1 ;
38
+ }
39
+
40
+ return 0 ;
41
+ } ;
42
+
43
+ function AccountsTable ( { accountTypeFilter, areAllCheck, label, manageConnectedAccounts, maxHeight = '112px' , selectedAccounts, setSelectedAccounts, style } : Props ) : React . ReactElement < Props > {
28
44
const { t } = useTranslation ( ) ;
29
45
const theme = useTheme ( ) ;
30
46
const { accounts } = useContext ( AccountContext ) ;
31
47
48
+ // Sort only on the first render, store result in a ref
49
+ const sortedAccountsRef = useRef < AccountJson [ ] | null > ( null ) ;
50
+
32
51
const accountsToShow = useMemo ( ( ) => {
33
- const filtered = accounts . filter ( ( { isExternal, isHardware, isHidden, isQR } ) =>
52
+ const filtered = [ ... accounts ] . filter ( ( { isExternal, isHardware, isHidden, isQR } ) =>
34
53
( accountTypeFilter ?. includes ( 'Watch-Only' ) && ! isExternal ) ||
35
54
( accountTypeFilter ?. includes ( 'Hardware' ) && ! isHardware ) ||
36
55
( accountTypeFilter ?. includes ( 'QR' ) && ! isQR ) ||
37
56
! isHidden
38
57
) ;
39
58
40
- return filtered ;
41
- } , [ accountTypeFilter , accounts ] ) ;
59
+ // Only sort accounts when:
60
+ // 1. We're in manage authorized accounts mode (manageConnectedAccounts is true)
61
+ // 2. The accounts haven't been sorted yet (sortedAccountsRef.current is null)
62
+ // 3. There are some selected accounts (selectedAccounts.length !== 0)
63
+ if ( manageConnectedAccounts && ! sortedAccountsRef . current && selectedAccounts . length !== 0 ) {
64
+ sortedAccountsRef . current = [ ...filtered ] . sort ( ( a , b ) => sortAccounts ( a , b , selectedAccounts ) ) ;
65
+ }
66
+
67
+ return filtered ; // .sort((a, b) => sortAccounts(a, b, selectedAccounts))
68
+ } , [ accountTypeFilter , accounts , manageConnectedAccounts , selectedAccounts ] ) ;
42
69
43
70
const onCheck = useCallback ( ( address : string ) => {
44
71
const isAlreadySelected = selectedAccounts . includes ( address ) ;
@@ -106,7 +133,7 @@ export default function AccountsTable ({ accountTypeFilter, areAllCheck, label,
106
133
/>
107
134
</ Grid >
108
135
}
109
- { accountsToShow . map ( ( { address } , index ) => (
136
+ { ( sortedAccountsRef . current ?? accountsToShow ) . map ( ( { address } , index ) => (
110
137
< Grid container item key = { index } sx = { { '> div:not(:last-child)' : { borderRight : '1px solid' , borderRightColor : 'secondary.light' } , height : '37px' , textAlign : 'center' } } xs = { 12 } >
111
138
< Grid alignItems = 'center' container item justifyContent = 'left' pl = '15px' xs = { 8 } >
112
139
< Identity address = { address } identiconSize = { 25 } showShortAddress showSocial = { false } style = { { fontSize : '14px' } } subIdOnly />
@@ -130,3 +157,5 @@ export default function AccountsTable ({ accountTypeFilter, areAllCheck, label,
130
157
</ Grid >
131
158
) ;
132
159
}
160
+
161
+ export default React . memo ( AccountsTable ) ;
0 commit comments