Skip to content

Commit

Permalink
Merge branch 'develop' into fix-288
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith-CY authored Dec 26, 2023
2 parents 9a6a3de + 4af44ea commit 7628599
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 183 deletions.
69 changes: 5 additions & 64 deletions packages/neuron-ui/src/components/ClearCache/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useCallback, useState } from 'react'
import React, { useCallback, useState } from 'react'
import { useTranslation } from 'react-i18next'
import Spinner from 'widgets/Spinner'
import Dialog from 'widgets/Dialog'
Expand All @@ -9,59 +9,23 @@ import { cacheClearDate } from 'services/localCache'
import { isSuccessResponse, uniformTimeFormatter } from 'utils'
import styles from './clearCache.module.scss'

const I18N_PATH = 'settings.clear-cache'
const IDs = {
submitClearCache: 'submit-clear-cache',
refreshCacheOption: 'refresh-cache-option',
rebuildCacheOption: 'rebuild-cache-option',
}

const ClearCacheDialog = ({
dispatch,
className,
btnClassName,
hideRebuild,
}: {
dispatch: StateDispatch
className?: string
btnClassName?: string
hideRebuild?: boolean
}) => {
const [t] = useTranslation()
const [clearedDate, setClearedDate] = useState(cacheClearDate.load())
const [isClearing, setIsClearing] = useState(false)
const [isDialogOpen, setIsDialogOpen] = useState(false)
const [isRebuild, setIsRebuild] = useState(false)
const [notice, setNotice] = useState('')

const showDialog = useCallback(() => {
setIsDialogOpen(true)
}, [setIsDialogOpen])

const dismissDialog = useCallback(() => {
setIsDialogOpen(false)
}, [setIsDialogOpen])

const toggleIsRebuild = useCallback(
(e: React.ChangeEvent<HTMLInputElement>) => {
setIsRebuild(e.target.checked)
},
[setIsRebuild]
)

useEffect(() => {
if (isDialogOpen) {
/* eslint-disable-next-line no-unused-expressions */
document.querySelector<HTMLButtonElement>(`#${IDs.submitClearCache}`)?.focus()
} else {
setIsRebuild(false)
}
}, [isDialogOpen, setIsRebuild])

const handleSubmit = useCallback(() => {
setIsDialogOpen(false)
setIsClearing(true)
clearCellCache({ resetIndexerData: isRebuild })
clearCellCache()
.then(res => {
if (isSuccessResponse(res) && res.result) {
addPopup('clear-cache-successfully')(dispatch)
Expand All @@ -74,7 +38,7 @@ const ClearCacheDialog = ({
.finally(() => {
setIsClearing(false)
})
}, [dispatch, setClearedDate, setIsDialogOpen, isRebuild])
}, [dispatch, setClearedDate])

return (
<>
Expand All @@ -83,10 +47,10 @@ const ClearCacheDialog = ({
<button
type="button"
className={`${btnClassName} ${styles.clearBtn}`}
onClick={showDialog}
onClick={handleSubmit}
disabled={isClearing}
>
{t('settings.data.clear-cache')}
{t('settings.data.refresh')}
</button>
</div>

Expand All @@ -96,29 +60,6 @@ const ClearCacheDialog = ({
<p>{t('settings.data.clearing-cache')}</p>
</div>
</Dialog>

<Dialog
show={isDialogOpen}
title={t(`${I18N_PATH}.title`)}
onConfirm={handleSubmit}
onCancel={dismissDialog}
confirmText={t('settings.data.confirm-clear')}
confirmProps={{ id: IDs.submitClearCache }}
>
<div className={styles.options}>
<label htmlFor={IDs.refreshCacheOption}>
<input type="checkbox" id={IDs.refreshCacheOption} checked disabled />
<span className={styles.highlight}>{t(`${I18N_PATH}.options.refresh.label`)}</span>
</label>
{hideRebuild ? null : (
<label htmlFor={IDs.rebuildCacheOption}>
<input type="checkbox" id={IDs.rebuildCacheOption} checked={isRebuild} onChange={toggleIsRebuild} />
<span>{t(`${I18N_PATH}.options.rebuild.label`)}</span>
</label>
)}
</div>
</Dialog>

<Toast content={notice} onDismiss={() => setNotice('')} />
</>
)
Expand Down
7 changes: 1 addition & 6 deletions packages/neuron-ui/src/components/DataSetting/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,7 @@ const DataSetting = () => {
</div>
<div className={styles.rightContainer}>
{hiddenDataPath ? null : <PathItem path={prevPath} openPath={openPath} handleClick={onSetting} />}
<ClearCache
className={styles.item}
btnClassName={styles.itemBtn}
dispatch={dispatch}
hideRebuild={isLightClient}
/>
<ClearCache className={styles.item} btnClassName={styles.itemBtn} dispatch={dispatch} />
</div>
</div>

Expand Down
19 changes: 2 additions & 17 deletions packages/neuron-ui/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -418,28 +418,13 @@
"zh": "中文(简体)",
"zh-TW": "中文(繁體)"
},
"clear-cache": {
"title": "Clear Cache",
"options": {
"refresh": {
"label": "Refresh"
},
"rebuild": {
"label": "Fully rebuild index (cost extra 30 ~ 50 min)"
}
},
"buttons": {
"cancel": "Cancel",
"ok": "OK"
}
},
"data": {
"ckb-node-data": "CKB Node Config & Storage",
"set": "Set",
"cache": "Cache",
"clear-cache-description": "Clear cache if you encounter data sync or balance display problems. Neuron will rescan block data.",
"cache-cleared-on": "Cache cleared on {{date}}",
"clear-cache": "Clear Cache",
"cache-cleared-on": "Refreshed cache on {{date}}",
"refresh": "Refresh",
"clearing-cache": "Clearing Cache, please wait a moment",
"clear-success": "Cache Cleared",
"set-path": "Set Path",
Expand Down
19 changes: 2 additions & 17 deletions packages/neuron-ui/src/locales/zh-tw.json
Original file line number Diff line number Diff line change
Expand Up @@ -412,28 +412,13 @@
"zh": "中文(简体)",
"zh-TW": "中文(繁體)"
},
"clear-cache": {
"title": "清理緩存",
"options": {
"refresh": {
"label": "刷新緩存"
},
"rebuild": {
"label": "重建索引 (需要 30 ~ 50 分鐘)"
}
},
"buttons": {
"cancel": "取消",
"ok": "確認"
}
},
"data": {
"ckb-node-data": "節點配置和數據",
"set": "設置",
"cache": "緩存",
"clear-cache-description": "當資料同步或餘額顯示出現問題時,可以清理緩存,Neuron 會重新同步所有塊數據。",
"cache-cleared-on": "上次清理緩存時間 {{date}}",
"clear-cache": "清理緩存",
"cache-cleared-on": "上次刷新緩存時間 {{date}}",
"refresh": "刷新",
"clearing-cache": "正在清除緩存中,請稍候",
"clear-success": "已清除緩存",
"set-path": "設置路徑",
Expand Down
19 changes: 2 additions & 17 deletions packages/neuron-ui/src/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -411,28 +411,13 @@
"zh": "中文(简体)",
"zh-TW": "中文(繁體)"
},
"clear-cache": {
"title": "清除缓存",
"options": {
"refresh": {
"label": "刷新缓存"
},
"rebuild": {
"label": "重建索引 (需要 30 ~ 50 分钟)"
}
},
"buttons": {
"cancel": "取消",
"ok": "确认"
}
},
"data": {
"ckb-node-data": "节点配置和数据",
"set": "设置",
"cache": "缓存",
"clear-cache-description": "当数据同步或余额显示出现问题时,可以清空缓存,Neuron 会重新同步所有块数据。",
"cache-cleared-on": "上次清空缓存时间 {{date}}",
"clear-cache": "清空缓存",
"cache-cleared-on": "上次刷新缓存时间 {{date}}",
"refresh": "刷新",
"clearing-cache": "正在清除缓存中,请稍候",
"clear-success": "已清除缓存",
"set-path": "设置路径",
Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-ui/src/services/remote/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export type VerifyExternalCkbNodeRes =
| undefined
export const verifyExternalCkbNode = remoteApi<void, VerifyExternalCkbNodeRes>('verify-external-ckb-node')

export const clearCellCache = remoteApi<Controller.ClearCache.Params>('clear-cache')
export const clearCellCache = remoteApi<void, boolean>('clear-cache')

export const invokeShowErrorMessage = remoteApi<{ title: string; content: string }>('show-error-message')
export const invokeShowOpenDialog = remoteApi<OpenDialogOptions, OpenDialogReturnValue>('show-open-dialog')
Expand Down
8 changes: 0 additions & 8 deletions packages/neuron-ui/src/types/Controller/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,6 @@ declare namespace Controller {
}
type Response = TokenInfo[]
}

namespace ClearCache {
interface Params {
resetIndexerData: boolean
}
type Response = boolean
}

namespace GetSUDTTokenInfo {
interface Params {
tokenID: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
line-height: 28px;
margin: 0;
color: var(--dialog-secondary-text-color);
word-break: break-word;
white-space: pre-wrap;
word-wrap: break-word;
overflow-wrap: break-word;
}

.actions {
Expand Down
4 changes: 2 additions & 2 deletions packages/neuron-wallet/src/controllers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,8 @@ export default class ApiController {
new UpdateController(false).quitAndInstall()
})

handle('clear-cache', async (_, params: { resetIndexerData: boolean } | null) => {
await IndexerService.clearCache(params?.resetIndexerData)
handle('clear-cache', async () => {
await IndexerService.clearCache()
return { status: ResponseCode.Success, result: true }
})

Expand Down
12 changes: 1 addition & 11 deletions packages/neuron-wallet/src/services/indexer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import path from 'path'
import fs from 'fs'
import logger from '../utils/logger'
import SyncedBlockNumber from '../models/synced-block-number'
import { clean as cleanChain } from '../database/chain'
import SettingsService from './settings'
import startMonitor, { stopMonitor } from './monitor'
import { resetSyncTaskQueue } from '../block-sync-renderer'
import NetworksService from './networks'

export default class IndexerService {
private constructor() {}
Expand All @@ -19,15 +16,8 @@ export default class IndexerService {
return IndexerService.instance
}

static clearCache = async (clearIndexerFolder = false) => {
static clearCache = async () => {
await cleanChain()

if (NetworksService.getInstance().getCurrent().readonly && clearIndexerFolder) {
await stopMonitor('ckb')
IndexerService.getInstance().clearData()
await new SyncedBlockNumber().setNextBlock(BigInt(0))
await startMonitor('ckb', true)
}
resetSyncTaskQueue.asyncPush(true)
}

Expand Down
40 changes: 1 addition & 39 deletions packages/neuron-wallet/tests/services/indexer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import IndexerService from '../../src/services/indexer'

const existsSyncMock = jest.fn()
const rmSyncMock = jest.fn()
const stopMonitorMock = jest.fn()
const getCurrentWalletMock = jest.fn()

jest.mock('fs', () => {
return {
Expand Down Expand Up @@ -48,20 +46,6 @@ jest.mock('../../src/database/chain', () => ({
clean: () => jest.fn(),
}))

jest.mock('../../src/services/monitor', () => {
function mockMonitor() {}
mockMonitor.stopMonitor = () => stopMonitorMock()
return mockMonitor
})

jest.mock('../../src/services/networks', () => ({
getInstance() {
return {
getCurrent: getCurrentWalletMock,
}
},
}))

const resetSyncTaskQueueAsyncPushMock = jest.fn()
jest.mock('../../src/block-sync-renderer', () => ({
resetSyncTaskQueue: {
Expand All @@ -75,8 +59,6 @@ describe('test IndexerService', () => {
rmSyncMock.mockReset()
setIndexerDataPathMock.mockReset()
getIndexerDataPathMock.mockReset()
getCurrentWalletMock.mockReset()
stopMonitorMock.mockReset()
})
describe('test remove old indexer data', () => {
it('old indexer data path exist', () => {
Expand All @@ -103,28 +85,8 @@ describe('test IndexerService', () => {
beforeEach(() => {
resetSyncTaskQueueAsyncPushMock.mockReset()
})
it('is external ckb node', async () => {
getCurrentWalletMock.mockReturnValue({ readonly: false })
it('clear cache success', async () => {
await IndexerService.clearCache()
expect(stopMonitorMock).toBeCalledTimes(0)
expect(resetSyncTaskQueueAsyncPushMock).toBeCalledTimes(1)
})
it('is internal ckb node', async () => {
getCurrentWalletMock.mockReturnValue({ readonly: true })
await IndexerService.clearCache()
expect(stopMonitorMock).toBeCalledTimes(0)
expect(resetSyncTaskQueueAsyncPushMock).toBeCalledTimes(1)
})
it('clear indexer data with internal ckb node', async () => {
getCurrentWalletMock.mockReturnValue({ readonly: true })
await IndexerService.clearCache(true)
expect(stopMonitorMock).toBeCalledTimes(1)
expect(resetSyncTaskQueueAsyncPushMock).toBeCalledTimes(1)
})
it('clear indexer data with external ckb node', async () => {
getCurrentWalletMock.mockReturnValue({ readonly: false })
await IndexerService.clearCache(true)
expect(stopMonitorMock).toBeCalledTimes(0)
expect(resetSyncTaskQueueAsyncPushMock).toBeCalledTimes(1)
})
})
Expand Down

1 comment on commit 7628599

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Packaging for test is done in 7328856430

Please sign in to comment.