Skip to content
45 changes: 41 additions & 4 deletions packages/neuron-ui/src/components/GeneralSetting/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,45 @@
import React from 'react'
import { Stack } from 'office-ui-fabric-react'
import React, { useCallback, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { Stack, PrimaryButton, Spinner, Text } from 'office-ui-fabric-react'
import { StateWithDispatch } from 'states/stateProvider/reducer'
import { addPopup } from 'states/stateProvider/actionCreators'
import { clearCellCache } from 'services/remote'

const GeneralSetting = () => {
return <Stack tokens={{ childrenGap: 15 }} />
const GeneralSetting = ({ dispatch }: React.PropsWithoutRef<StateWithDispatch>) => {
const [t] = useTranslation()
const [clearing, setClearing] = useState(false)

const clearCache = useCallback(() => {
setClearing(true)
setTimeout(() => {
clearCellCache().finally(() => {
addPopup('clear-cache-successfully')(dispatch)
setClearing(false)
})
}, 100)
}, [dispatch])

return (
<Stack tokens={{ childrenGap: 15 }}>
<Stack horizontal horizontalAlign="start">
<PrimaryButton
onClick={clearCache}
disabled={clearing}
ariaDescription="Create new network configuration"
styles={{
root: {
minWidth: 150,
},
}}
>
{clearing ? <Spinner /> : t('settings.general.clear-cache')}
</PrimaryButton>
</Stack>
<Text as="p" variant="medium">
{t('settings.general.clear-cache-description')}
</Text>
</Stack>
)
}

GeneralSetting.displayName = 'GeneralSetting'
Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-ui/src/components/Settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { WalletWizardPath } from 'components/WalletWizard'
import { Routes } from 'utils/const'

const pivotItems = [
// { label: 'settings.setting-tabs.general', url: Routes.SettingsGeneral },
{ label: 'settings.setting-tabs.general', url: Routes.SettingsGeneral },
{ label: 'settings.setting-tabs.wallets', url: Routes.SettingsWallets },
{ label: 'settings.setting-tabs.network', url: Routes.SettingsNetworks },
]
Expand Down
44 changes: 17 additions & 27 deletions packages/neuron-ui/src/containers/Main/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useMemo } from 'react'
import { Route, RouteComponentProps, Switch, Redirect } from 'react-router-dom'
import { Route, RouteComponentProps } from 'react-router-dom'
import { useTranslation } from 'react-i18next'

import { useState } from 'states/stateProvider'
Expand Down Expand Up @@ -102,12 +102,12 @@ export const mainContents: CustomRouter.Route[] = [
exact: false,
comp: ImportKeystore,
},
// {
// name: `PasswordRequest`,
// path: '/',
// exact: false,
// comp: PasswordRequest,
// },
{
name: `PasswordRequest`,
path: '/',
exact: false,
comp: PasswordRequest,
},
{
name: `NervosDAO`,
path: Routes.NervosDAO,
Expand Down Expand Up @@ -158,26 +158,16 @@ const MainContent = ({

return (
<>
<Route
path="/"
key="PasswordRequest"
render={routerProps => {
return <PasswordRequest {...routerProps} {...neuronWalletState} dispatch={dispatch} />
}}
/>
<Switch>
<Redirect from={Routes.SettingsGeneral} to={Routes.SettingsWallets} />
{mainContents.map(container => (
<Route
exact={container.exact}
path={`${container.path}${container.params || ''}`}
key={container.name}
render={routerProps => {
return <container.comp {...routerProps} {...neuronWalletState} dispatch={dispatch} />
}}
/>
))}
</Switch>
{mainContents.map(container => (
<Route
exact={container.exact}
path={`${container.path}${container.params || ''}`}
key={container.name}
render={routerProps => {
return <container.comp {...routerProps} {...neuronWalletState} dispatch={dispatch} />
}}
/>
))}
</>
)
}
Expand Down
4 changes: 3 additions & 1 deletion packages/neuron-ui/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@
"network": "Network"
},
"general": {
"skip-data-and-type": "Skip the Cells which contain Data or Type Script",
"clear-cache": "Clear cache",
"clear-cache-description": "Clear cache if you encounter data sync or balance display problems. Neuron will rescan block data.",
"show": "Show",
"hide": "Hide"
},
Expand Down Expand Up @@ -257,6 +258,7 @@
"delete-wallet-successfully": "The wallet was deleted",
"create-network-successfully": "The network was created",
"update-network-successfully": "The network was updated",
"clear-cache-successfully": "The cache was cleared",
"addr-copied": "Address has been copied to the clipboard",
"qrcode-copied": "QR Code has been copied to the clipboard",
"view-the-run-node-doc": "View the guide in browser",
Expand Down
4 changes: 3 additions & 1 deletion packages/neuron-ui/src/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@
"network": "网络"
},
"general": {
"skip-data-and-type": "忽略包含 Data 或 Type Script 的 Cells",
"clear-cache": "清空缓存",
"clear-cache-description": "当数据同步或显示出现问题时,可以清空缓存,Neuron 会重新同步所有块数据。",
"show": "显示",
"hide": "隐藏"
},
Expand Down Expand Up @@ -257,6 +258,7 @@
"delete-wallet-successfully": "已删除钱包",
"create-network-successfully": "已添加新节点",
"update-network-successfully": "已更新节点信息",
"clear-cache-successfully": "已清空数据缓存",
"addr-copied": "已复制地址到剪贴板",
"qrcode-copied": "已复制二维码到剪贴板",
"view-the-run-node-doc": "打开浏览器查看文档",
Expand Down
3 changes: 3 additions & 0 deletions packages/neuron-ui/src/services/remote/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ export const getNeuronWalletState = apiMethodWrapper<void>(api => () => api.load
export const handleViewError = apiMethodWrapper<string>(api => errorMessage => api.handleViewError(errorMessage))
export const contextMenu = apiMethodWrapper<{ type: string; id: string }>(api => params => api.contextMenu(params))

export const clearCellCache = apiMethodWrapper<void>(api => () => api.clearCellCache())

export default {
getNeuronWalletState,
handleViewError,
contextMenu,
clearCellCache,
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const initAppState = () => (dispatch: StateDispatch, history: any) => {
})
}

// text: an i18n key under `messages`
Comment thread
Keith-CY marked this conversation as resolved.
export const addPopup = (text: string) => (dispatch: StateDispatch) => {
dispatch({
type: AppActions.PopIn,
Expand Down
16 changes: 15 additions & 1 deletion packages/neuron-ui/src/stories/GeneralSetting.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,23 @@ import React from 'react'
import { storiesOf } from '@storybook/react'
import { withKnobs } from '@storybook/addon-knobs'
import GeneralSetting from 'components/GeneralSetting'
import initStates from 'states/initStates'

const states: { [title: string]: boolean } = {
'Clear cell cache on': true,
'Clear cell cache off': false,
}

const stories = storiesOf('GeneralSettings', module)

Object.entries(states).forEach(([title]) => {
const props = { ...initStates, settings: { ...initStates.settings }, dispatch: () => {} }
stories.add(title, () => <GeneralSetting {...props} />)
})

stories.addDecorator(withKnobs).add('With knobs', () => {
return <GeneralSetting />
const props = {
...initStates,
}
return <GeneralSetting {...props} dispatch={() => {}} />
})
12 changes: 10 additions & 2 deletions packages/neuron-wallet/src/controllers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import env from 'env'
import i18n from 'utils/i18n'
import { popContextMenu } from './app/menu'
import { showWindow } from './app/show-window'
import { TransactionsController, WalletsController, SyncInfoController, NetworksController } from 'controllers'
import { TransactionsController, WalletsController, SyncController, NetworksController } from 'controllers'
import { NetworkType, NetworkID, Network } from 'types/network'
import NetworksService from 'services/networks'
import WalletsService from 'services/wallets'
Expand Down Expand Up @@ -40,7 +40,7 @@ export default class ApiController {
networksService.getCurrentID(),
networksService.getAll(),

SyncInfoController.currentBlockNumber()
SyncController.currentBlockNumber()
.then(res => {
if (res.status) {
return res.result.currentBlockNumber
Expand Down Expand Up @@ -297,4 +297,12 @@ export default class ApiController {
) {
return DaoController.getDaoCells(params)
}

// settings
@MapApiResponse
public static async clearCellCache() {
await SyncController.stopSyncing()
await SyncController.deleteData()
return SyncController.startSyncing()
}
}
4 changes: 2 additions & 2 deletions packages/neuron-wallet/src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import AppController from './app'
import NetworksController from './networks'
import WalletsController from './wallets'
import TransactionsController from './transactions'
import SyncInfoController from './sync-info'
import SyncController from './sync'
import UpdateController from './update'

import ApiController from './api'
Expand All @@ -12,7 +12,7 @@ export {
NetworksController,
WalletsController,
TransactionsController,
SyncInfoController,
SyncController,
UpdateController,
ApiController,
}
16 changes: 0 additions & 16 deletions packages/neuron-wallet/src/controllers/sync-info.ts

This file was deleted.

45 changes: 45 additions & 0 deletions packages/neuron-wallet/src/controllers/sync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import BlockNumber from 'services/sync/block-number'
import { createSyncBlockTask, killSyncBlockTask } from 'startup/sync-block-task/create'
import ChainCleaner from 'database/chain/cleaner'
import { ResponseCode } from 'utils/const'

export default class SyncController {
public static async startSyncing() {
createSyncBlockTask()

return {
status: ResponseCode.Success,
result: true
}
}

public static async stopSyncing() {
killSyncBlockTask()

return {
status: ResponseCode.Success,
result: true
}
}

public static async deleteData() {
ChainCleaner.clean()

return {
status: ResponseCode.Success,
result: true
}
}

public static async currentBlockNumber() {
const blockNumber = new BlockNumber()
const current: bigint = await blockNumber.getCurrent()

return {
status: ResponseCode.Success,
result: {
currentBlockNumber: current.toString(),
},
}
}
}
21 changes: 21 additions & 0 deletions packages/neuron-wallet/src/database/chain/cleaner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import fs from 'fs'
import path from 'path'
import env from 'env'

// Clean local sqlite storage
export default class ChainCleaner {
// Delete all sqlite files under cells folder.
// It doesn't handle error or throw exception when deleting fails.
public static clean() {
const folder = path.join(env.fileBasePath, 'cells')
fs.readdir(folder, (err, files) => {
if (err) {
return
}

for (const file of files.filter(f => { return path.extname(f) === '.sqlite' })) {
fs.unlink(path.join(folder, file), () => {})
}
})
}
}
6 changes: 3 additions & 3 deletions packages/neuron-wallet/src/database/chain/ormconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import { AddInputIndexToInput1573461100330 } from './migrations/1573461100330-Ad

export const CONNECTION_NOT_FOUND_NAME = 'ConnectionNotFoundError'

const dbPath = (networkName: string): string => {
const name = `cell-${networkName}.sqlite`
return path.join(env.fileBasePath, 'cells', name)
const dbPath = (name: string): string => {
const filename = `cell-${name}.sqlite`
return path.join(env.fileBasePath, 'cells', filename)
}

const connectOptions = async (genesisBlockHash: string): Promise<SqliteConnectionOptions> => {
Expand Down
4 changes: 2 additions & 2 deletions packages/neuron-wallet/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { app } from 'electron'

import AppController from 'controllers/app'
import SyncController from 'controllers/sync'
import WalletService from 'services/wallets'
import createSyncBlockTask from 'startup/sync-block-task/create'
import { changeLanguage } from 'utils/i18n'

const appController = new AppController()
Expand All @@ -11,7 +11,7 @@ app.on('ready', async () => {
changeLanguage(app.getLocale())

WalletService.getInstance().generateAddressesIfNecessary()
createSyncBlockTask()
SyncController.startSyncing()

appController.openWindow()
})
Expand Down
4 changes: 3 additions & 1 deletion packages/neuron-wallet/src/services/sync/renderer-params.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { remote } from 'electron'

export const { networkSwitchSubject, nodeService, addressChangeSubject, addressesUsedSubject } = remote.require(
export const {
networkSwitchSubject, nodeService, addressChangeSubject, addressesUsedSubject
} = remote.require(
'./startup/sync-block-task/params'
)
Loading