Skip to content

Commit 6a72502

Browse files
committed
feat(neuron-ui): optimize updating descriptions
1. reduce diff between origin descriptions and local description 2. add updating state in the description field
1 parent f3e756e commit 6a72502

11 files changed

Lines changed: 150 additions & 111 deletions

File tree

packages/neuron-ui/src/components/Addresses/index.tsx

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { localNumberFormatter, shannonToCKBFormatter } from 'utils/formatters'
2020

2121
const Addresses = ({
2222
app: {
23-
loadings: { addressList: isLoading },
23+
loadings: { addressList: isLoading, updateDescription: isUpdatingDescription },
2424
},
2525
wallet: { addresses = [], id: walletID },
2626
settings: { showAddressBook = false },
@@ -34,19 +34,13 @@ const Addresses = ({
3434
}
3535
}, [showAddressBook, history])
3636

37-
const { localDescription, onDescriptionPress, onDescriptionFieldBlur, onDescriptionChange } = useLocalDescription(
38-
'address',
39-
walletID,
40-
useMemo(
41-
() =>
42-
addresses.map(({ address: key = '', description = '' }) => ({
43-
key,
44-
description,
45-
})),
46-
[addresses]
47-
),
48-
dispatch
49-
)
37+
const {
38+
localDescription,
39+
onDescriptionPress,
40+
onDescriptionFieldBlur,
41+
onDescriptionChange,
42+
onDescriptionFocus,
43+
} = useLocalDescription('address', walletID, dispatch)
5044

5145
const theme = getTheme()
5246
const { semanticColors } = theme
@@ -104,12 +98,15 @@ const Addresses = ({
10498
<TextField
10599
borderless
106100
title={item.description}
107-
value={
108-
(localDescription.find(local => local.key === item.address) || { description: '' }).description || ''
109-
}
110-
onKeyPress={onDescriptionPress(item.address)}
111-
onBlur={onDescriptionFieldBlur(item.address)}
101+
value={localDescription.key === item.address ? localDescription.description : item.description || ''}
102+
onBlur={onDescriptionFieldBlur(item.address, item.description)}
103+
onFocus={onDescriptionFocus}
104+
onKeyPress={onDescriptionPress(item.address, item.description)}
112105
onChange={onDescriptionChange(item.address)}
106+
disabled={localDescription.key === item.address && isUpdatingDescription}
107+
iconProps={{
108+
iconName: localDescription.key === item.address && isUpdatingDescription ? 'Updating' : '',
109+
}}
113110
styles={(props: ITextFieldStyleProps) => {
114111
return {
115112
root: {
@@ -160,7 +157,16 @@ const Addresses = ({
160157
},
161158
},
162159
],
163-
[onDescriptionChange, localDescription, onDescriptionFieldBlur, onDescriptionPress, t, semanticColors]
160+
[
161+
onDescriptionChange,
162+
localDescription,
163+
onDescriptionFieldBlur,
164+
onDescriptionPress,
165+
onDescriptionFocus,
166+
isUpdatingDescription,
167+
t,
168+
semanticColors,
169+
]
164170
)
165171

166172
return (

packages/neuron-ui/src/components/History/index.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { useSearch } from './hooks'
1313

1414
const History = ({
1515
app: {
16-
loadings: { transactionList: isLoading },
16+
loadings: { transactionList: isLoading, updateDescription: isUpdatingDescription },
1717
},
1818
wallet: { id },
1919
chain: {
@@ -45,7 +45,13 @@ const History = ({
4545
iconProps={{ iconName: 'Search', styles: { root: { height: '18px' } } }}
4646
/>
4747
</Stack>
48-
<TransactionList isLoading={isLoading} walletID={id} items={items} dispatch={dispatch} />
48+
<TransactionList
49+
isLoading={isLoading}
50+
isUpdatingDescription={isUpdatingDescription}
51+
walletID={id}
52+
items={items}
53+
dispatch={dispatch}
54+
/>
4955
<Pagination
5056
selectedPageIndex={pageNo - 1}
5157
pageCount={Math.ceil(totalCount / pageSize)}

packages/neuron-ui/src/components/TransactionList/index.tsx

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,26 @@ const onRenderHeader = ({ group }: any) => {
4646

4747
const TransactionList = ({
4848
isLoading = false,
49+
isUpdatingDescription = false,
4950
items = [],
5051
walletID,
5152
dispatch,
5253
}: {
53-
isLoading: boolean
54+
isLoading?: boolean
55+
isUpdatingDescription?: boolean
5456
walletID: string
5557
items: State.Transaction[]
5658
dispatch: StateDispatch
5759
}) => {
5860
const [t] = useTranslation()
5961

60-
const { localDescription, onDescriptionPress, onDescriptionFieldBlur, onDescriptionChange } = useLocalDescription(
61-
'transaction',
62-
walletID,
63-
useMemo(
64-
() =>
65-
items.map(({ hash: key, description = '' }) => ({
66-
key,
67-
description,
68-
})),
69-
[items]
70-
),
71-
dispatch
72-
)
62+
const {
63+
localDescription,
64+
onDescriptionPress,
65+
onDescriptionFieldBlur,
66+
onDescriptionChange,
67+
onDescriptionFocus,
68+
} = useLocalDescription('transaction', walletID, dispatch)
7369

7470
const transactionColumns: IColumn[] = useMemo(
7571
(): IColumn[] =>
@@ -113,12 +109,15 @@ const TransactionList = ({
113109
return item ? (
114110
<TextField
115111
title={item.description}
116-
value={
117-
(localDescription.find(local => local.key === item.hash) || { description: '' }).description || ''
118-
}
119-
onKeyPress={onDescriptionPress(item.hash)}
120-
onBlur={onDescriptionFieldBlur(item.hash)}
112+
value={localDescription.key === item.hash ? localDescription.description : item.description || ''}
113+
onFocus={onDescriptionFocus}
114+
onBlur={onDescriptionFieldBlur(item.hash, item.description)}
115+
onKeyPress={onDescriptionPress(item.hash, item.description)}
121116
onChange={onDescriptionChange(item.hash)}
117+
disabled={localDescription.key === item.hash && isUpdatingDescription}
118+
iconProps={{
119+
iconName: localDescription.key === item.hash && isUpdatingDescription ? 'Updating' : '',
120+
}}
122121
borderless
123122
styles={(props: ITextFieldStyleProps) => {
124123
return {
@@ -152,7 +151,15 @@ const TransactionList = ({
152151
].map(
153152
(col): IColumn => ({ fieldName: col.key, ariaLabel: col.name, isResizable: true, isCollapsable: false, ...col })
154153
),
155-
[localDescription, onDescriptionChange, onDescriptionFieldBlur, onDescriptionPress, t]
154+
[
155+
localDescription,
156+
onDescriptionChange,
157+
onDescriptionFieldBlur,
158+
onDescriptionPress,
159+
onDescriptionFocus,
160+
isUpdatingDescription,
161+
t,
162+
]
156163
)
157164

158165
const { groups, txs } = useMemo(() => {

packages/neuron-ui/src/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
Scan as ScanIcon,
2222
Search as SearchIcon,
2323
SubtractCircle as RemoveIcon,
24+
Update as UpdateIcon,
2425
} from 'grommet-icons'
2526

2627
import 'styles/index.scss'
@@ -76,6 +77,7 @@ registerIcons({
7677
Leave: <LeaveIcon />,
7778
Connected: <ConnectedIcon size="small" color="green" />,
7879
Disconnected: <AlertIcon size="small" color="red" />,
80+
Updating: <UpdateIcon size="16px" style={{ animation: 'rotate360 3s linear infinite' }} />,
7981
},
8082
})
8183

packages/neuron-ui/src/states/initStates/app.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const appState: State.App = {
3636
sending: false,
3737
addressList: false,
3838
transactionList: false,
39+
updateDescription: false,
3940
},
4041
}
4142

packages/neuron-ui/src/states/stateProvider/actionCreators/transactions.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,31 @@ export const updateTransaction = (params: { walletID: string; hash: string }) =>
2020
})
2121
}
2222
export const updateTransactionList = (params: GetTransactionListParams) => (dispatch: StateDispatch) => {
23+
getTransactionList(params).then(res => {
24+
if (res.status) {
25+
dispatch({
26+
type: NeuronWalletActions.UpdateTransactionList,
27+
payload: res.result,
28+
})
29+
} else {
30+
addNotification({ type: 'alert', content: res.message.title })(dispatch)
31+
}
32+
})
33+
}
34+
35+
export const updateTransactionDescription = (params: Controller.UpdateTransactionDescriptionParams) => (
36+
dispatch: StateDispatch
37+
) => {
2338
dispatch({
2439
type: AppActions.UpdateLoadings,
2540
payload: {
26-
transactionList: true,
41+
updateDescription: true,
2742
},
2843
})
29-
getTransactionList(params)
44+
updateRemoteTransactionDescription(params)
3045
.then(res => {
3146
if (res.status) {
32-
dispatch({
33-
type: NeuronWalletActions.UpdateTransactionList,
34-
payload: res.result,
35-
})
47+
dispatch({ type: AppActions.Ignore, payload: null })
3648
} else {
3749
addNotification({ type: 'alert', content: res.message.title })(dispatch)
3850
}
@@ -41,24 +53,12 @@ export const updateTransactionList = (params: GetTransactionListParams) => (disp
4153
dispatch({
4254
type: AppActions.UpdateLoadings,
4355
payload: {
44-
transactionList: false,
56+
updateDescription: false,
4557
},
4658
})
4759
})
4860
}
4961

50-
export const updateTransactionDescription = (params: Controller.UpdateTransactionDescriptionParams) => (
51-
dispatch: StateDispatch
52-
) => {
53-
updateRemoteTransactionDescription(params).then(res => {
54-
if (res.status) {
55-
dispatch({ type: AppActions.Ignore, payload: null })
56-
} else {
57-
addNotification({ type: 'alert', content: res.message.title })(dispatch)
58-
}
59-
})
60-
}
61-
6262
export default {
6363
updateTransaction,
6464
updateTransactionList,

packages/neuron-ui/src/states/stateProvider/actionCreators/wallets.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,31 @@ export const updateAddressList = (params: Controller.GetAddressesByWalletIDParam
156156
export const updateAddressDescription = (params: Controller.UpdateAddressDescriptionParams) => (
157157
dispatch: StateDispatch
158158
) => {
159-
updateRemoteAddressDescription(params).then(res => {
160-
if (res.status) {
159+
dispatch({
160+
type: AppActions.UpdateLoadings,
161+
payload: {
162+
updateDescription: true,
163+
},
164+
})
165+
updateRemoteAddressDescription(params)
166+
.then(res => {
167+
if (res.status) {
168+
dispatch({
169+
type: AppActions.Ignore,
170+
payload: null,
171+
})
172+
} else {
173+
addNotification({ type: 'alert', content: res.message.title })(dispatch)
174+
}
175+
})
176+
.finally(() => {
161177
dispatch({
162-
type: AppActions.Ignore,
163-
payload: null,
178+
type: AppActions.UpdateLoadings,
179+
payload: {
180+
updateDescription: false,
181+
},
164182
})
165-
} else {
166-
addNotification({ type: 'alert', content: res.message.title })(dispatch)
167-
}
168-
})
183+
})
169184
}
170185

171186
export const deleteWallet = (params: Controller.DeleteWalletParams) => (dispatch: StateDispatch) => {

packages/neuron-ui/src/stories/TransactionList.stories.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@ import transactions from './data/transactions'
55

66
const stories = storiesOf('TransactionList', module)
77
Object.entries(transactions).forEach(([title, list]) => {
8-
stories.add(title, () => <TransactionList isLoading={false} walletID="1" items={list} dispatch={() => {}} />)
8+
stories.add(title, () => (
9+
<TransactionList isLoading={false} isUpdatingDescription={false} walletID="1" items={list} dispatch={() => {}} />
10+
))
911
})
1012

1113
stories.add('Wtih empty pending list', () => (
1214
<TransactionList
1315
isLoading={false}
16+
isUpdatingDescription={false}
1417
walletID="1"
1518
items={transactions['Content List'].filter(item => item.status !== 'pending')}
1619
dispatch={() => {}}
1720
/>
1821
))
1922

2023
stories.add('Shimmered List', () => {
21-
return <TransactionList isLoading={false} walletID="1" items={[]} dispatch={() => {}} />
24+
return <TransactionList isLoading={false} isUpdatingDescription={false} walletID="1" items={[]} dispatch={() => {}} />
2225
})

packages/neuron-ui/src/styles/index.scss

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,13 @@ navbar {
6565
text-overflow: ellipsis
6666
}
6767
}
68+
69+
@keyframes rotate360 {
70+
from {
71+
transform: rotate(0)
72+
}
73+
74+
to {
75+
transform: rotate(360deg)
76+
}
77+
}

packages/neuron-ui/src/types/App/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ declare namespace State {
7979
sending: boolean
8080
addressList: boolean
8181
transactionList: boolean
82+
updateDescription: boolean
8283
}
8384
}
8485

0 commit comments

Comments
 (0)