Skip to content

Commit bb4c6ce

Browse files
authored
feat: Optimize data migration (nervosnetwork#3321)
* feat: Optimize data migration * feat: getCkbNodeDataNeedSize * fix: comments * fix: text * fix: text * fix * fix: ui * fix: lang
1 parent 1ccb2d7 commit bb4c6ce

File tree

24 files changed

+653
-174
lines changed

24 files changed

+653
-174
lines changed

packages/neuron-ui/src/components/DataSetting/dataSetting.module.scss

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,16 @@
5050

5151
.itemBtn {
5252
color: var(--secondary-text-color);
53-
padding: 0 16px;
54-
flex-shrink: 0;
53+
padding: 0 8px;
5554
border-radius: 8px;
5655
height: 100%;
5756
background: var(--input-disabled-color);
5857
border: 1px solid var(--divide-line-color);
5958
&:hover:not([disabled]) {
60-
color: var(--primary-color);
59+
g,
60+
path {
61+
stroke: var(--primary-color);
62+
}
6163
}
6264
&[disabled] {
6365
cursor: not-allowed;
@@ -160,3 +162,29 @@
160162
word-break: normal;
161163
white-space: normal;
162164
}
165+
166+
.moreTooltip {
167+
height: 100%;
168+
}
169+
170+
.moreTip {
171+
margin-top: 4px;
172+
margin-left: 60px;
173+
}
174+
175+
.actions {
176+
button {
177+
background: none;
178+
font-size: 14px;
179+
line-height: 30px;
180+
color: var(--main-text-color);
181+
border: none;
182+
text-align: left;
183+
display: flex;
184+
align-items: center;
185+
cursor: pointer;
186+
&:hover {
187+
color: var(--primary-color);
188+
}
189+
}
190+
}

packages/neuron-ui/src/components/DataSetting/hooks.ts

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,62 @@ export const useDataPath = (network?: State.Network) => {
1818
}
1919
})
2020
}, [network?.id])
21-
const onSetting = useCallback(() => {
22-
invokeShowOpenDialog({
23-
buttonLabel: t('settings.data.set', { lng: navigator.language }),
24-
properties: ['openDirectory', 'createDirectory', 'promptToCreate', 'treatPackageAsDirectory'],
25-
}).then(res => {
26-
if (isSuccessResponse(res) && !res.result?.canceled && res.result?.filePaths?.length) {
27-
setCurrentPath(res.result?.filePaths?.[0])
28-
stopProcessMonitor(type).then(stopRes => {
29-
if (isSuccessResponse(stopRes)) {
30-
setIsDialogOpen(true)
31-
}
32-
})
33-
}
34-
})
35-
}, [t, type])
21+
22+
const onSetting = useCallback(
23+
(onSuccess?: (path: string) => void) => {
24+
invokeShowOpenDialog({
25+
buttonLabel: t('settings.data.set', { lng: navigator.language }),
26+
properties: ['openDirectory', 'createDirectory', 'promptToCreate', 'treatPackageAsDirectory'],
27+
}).then(res => {
28+
if (isSuccessResponse(res) && !res.result?.canceled && res.result?.filePaths?.length) {
29+
const path = res.result?.filePaths?.[0]
30+
setCurrentPath(path)
31+
stopProcessMonitor(type).then(stopRes => {
32+
if (isSuccessResponse(stopRes)) {
33+
onSuccess?.(path)
34+
}
35+
})
36+
}
37+
})
38+
},
39+
[t, type]
40+
)
41+
3642
const onCancel = useCallback(() => {
3743
startProcessMonitor(type).then(res => {
3844
if (isSuccessResponse(res)) {
3945
setIsDialogOpen(false)
4046
}
4147
})
4248
}, [setIsDialogOpen, type])
49+
50+
const onResync = useCallback(async () => {
51+
await stopProcessMonitor(type)
52+
return startProcessMonitor(type)
53+
}, [type])
54+
4355
const onConfirm = useCallback(
4456
(dataPath: string) => {
4557
setPrevPath(dataPath)
4658
setIsDialogOpen(false)
4759
},
4860
[setIsDialogOpen, setPrevPath]
4961
)
62+
63+
const openDialog = useCallback(() => {
64+
setCurrentPath('')
65+
setIsDialogOpen(true)
66+
}, [setIsDialogOpen, setCurrentPath])
67+
5068
return {
5169
prevPath,
5270
currentPath,
5371
onSetting,
5472
onCancel,
5573
onConfirm,
5674
isDialogOpen,
75+
openDialog,
76+
onResync,
5777
}
5878
}
5979

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

Lines changed: 80 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import React, { useCallback, useMemo } from 'react'
1+
import React, { useCallback, useMemo, useState } from 'react'
22
import { useTranslation } from 'react-i18next'
33
import ClearCache from 'components/ClearCache'
44
import { useDispatch, useState as useGlobalState } from 'states'
55
import { shell } from 'electron'
66
import Tooltip from 'widgets/Tooltip'
77
import { NetworkType } from 'utils/const'
8-
import { Attention } from 'widgets/Icons/icon'
9-
import MigrateCkbDataDialog from 'widgets/MigrateCkbDataDialog'
8+
import { Attention, More } from 'widgets/Icons/icon'
9+
import Toast from 'widgets/Toast'
10+
import ModifyPathDialog from 'components/ModifyPathDialog'
11+
import AlertDialog from 'widgets/AlertDialog'
12+
import { isSuccessResponse } from 'utils'
1013
import { useDataPath } from './hooks'
1114

1215
import styles from './dataSetting.module.scss'
@@ -28,9 +31,33 @@ const PathItem = ({
2831
<button className={styles.itemPath} type="button" onClick={openPath}>
2932
{path}
3033
</button>
31-
<button className={styles.itemBtn} type="button" onClick={handleClick} disabled={disabled}>
32-
{t('settings.data.set-path')}
33-
</button>
34+
<Tooltip
35+
className={styles.moreTooltip}
36+
tipClassName={styles.moreTip}
37+
tip={
38+
<div className={styles.actions}>
39+
{[
40+
{
41+
label: 'settings.data.browse-local-files',
42+
onClick: openPath,
43+
},
44+
{
45+
label: 'settings.data.modify-path',
46+
onClick: handleClick,
47+
},
48+
].map(({ label, onClick }) => (
49+
<button type="button" key={label} onClick={onClick}>
50+
<span>{t(label)}</span>
51+
</button>
52+
))}
53+
</div>
54+
}
55+
trigger="click"
56+
>
57+
<button className={styles.itemBtn} type="button" disabled={disabled}>
58+
<More />
59+
</button>
60+
</Tooltip>
3461
</div>
3562
)
3663
}
@@ -42,16 +69,31 @@ const DataSetting = () => {
4269
chain: { networkID },
4370
settings: { networks = [] },
4471
} = useGlobalState()
72+
const [notice, setNotice] = useState('')
73+
const [showLostDialog, setShowLostDialog] = useState(false)
4574
const network = useMemo(() => networks.find(n => n.id === networkID), [networkID, networks])
46-
const { onSetting, prevPath, currentPath, isDialogOpen, onCancel, onConfirm } = useDataPath(network)
75+
const { isDialogOpen, openDialog, onSetting, prevPath, currentPath, onCancel, onConfirm, onResync } =
76+
useDataPath(network)
4777

4878
const openPath = useCallback(() => {
49-
if (prevPath) {
50-
shell.openPath(prevPath!)
51-
}
52-
}, [prevPath])
79+
shell.openPath(prevPath).then(res => {
80+
if (res) {
81+
setShowLostDialog(true)
82+
}
83+
})
84+
}, [prevPath, onResync])
5385
const isLightClient = network?.type === NetworkType.Light
5486
const hiddenDataPath = isLightClient || !network?.readonly
87+
88+
const handleResync = useCallback(() => {
89+
setShowLostDialog(false)
90+
onResync().then(res => {
91+
if (isSuccessResponse(res)) {
92+
openPath()
93+
}
94+
})
95+
}, [openPath])
96+
5597
return (
5698
<>
5799
<div className={styles.root}>
@@ -82,17 +124,36 @@ const DataSetting = () => {
82124
</div>
83125
</div>
84126
<div className={styles.rightContainer}>
85-
{hiddenDataPath ? null : <PathItem path={prevPath} openPath={openPath} handleClick={onSetting} />}
127+
{hiddenDataPath ? null : <PathItem path={prevPath} openPath={openPath} handleClick={openDialog} />}
86128
<ClearCache className={styles.item} btnClassName={styles.itemBtn} dispatch={dispatch} />
87129
</div>
88130
</div>
89-
<MigrateCkbDataDialog
90-
show={isDialogOpen}
91-
prevPath={prevPath}
92-
currentPath={currentPath}
93-
onCancel={onCancel}
94-
onConfirm={onConfirm}
95-
/>
131+
132+
{isDialogOpen && (
133+
<ModifyPathDialog
134+
prevPath={prevPath}
135+
currentPath={currentPath}
136+
onCancel={onCancel}
137+
onConfirm={onConfirm}
138+
onSetting={onSetting}
139+
setNotice={setNotice}
140+
/>
141+
)}
142+
143+
{showLostDialog && (
144+
<AlertDialog
145+
show
146+
title={t('settings.data.sync-file-lost')}
147+
message={t('settings.data.sync-file-lost-notice')}
148+
type="warning"
149+
cancelText={t('settings.data.resync')}
150+
onCancel={handleResync}
151+
okText={t('settings.data.retry')}
152+
onOk={() => setShowLostDialog(false)}
153+
/>
154+
)}
155+
156+
<Toast content={notice} onDismiss={() => setNotice('')} />
96157
</>
97158
)
98159
}

0 commit comments

Comments
 (0)