diff --git a/packages/neuron-ui/src/components/Addresses/index.tsx b/packages/neuron-ui/src/components/Addresses/index.tsx index 2a8064ae4e..f321ec1aa4 100644 --- a/packages/neuron-ui/src/components/Addresses/index.tsx +++ b/packages/neuron-ui/src/components/Addresses/index.tsx @@ -12,8 +12,7 @@ import { } from 'office-ui-fabric-react' import { StateWithDispatch } from 'states/stateProvider/reducer' - -import { appCalls } from 'services/UILayer' +import { contextMenu } from 'services/remote' import { useLocalDescription } from 'utils/hooks' import { MIN_CELL_WIDTH, Routes } from 'utils/const' @@ -162,7 +161,7 @@ const Addresses = ({ columns={addressColumns.map(col => ({ ...col, name: t(col.name) }))} items={addresses} onItemContextMenu={item => { - appCalls.contextMenu({ type: 'addressList', id: item.identifier }) + contextMenu({ type: 'addressList', id: item.identifier }) }} styles={{ contentWrapper: { diff --git a/packages/neuron-ui/src/components/ErrorBoundary/index.tsx b/packages/neuron-ui/src/components/ErrorBoundary/index.tsx index 499867d979..e2e169e7ff 100644 --- a/packages/neuron-ui/src/components/ErrorBoundary/index.tsx +++ b/packages/neuron-ui/src/components/ErrorBoundary/index.tsx @@ -1,9 +1,9 @@ import React, { Component } from 'react' -import { appCalls } from 'services/UILayer' import { Stack, Spinner } from 'office-ui-fabric-react' +import { handleViewError } from 'services/remote' const handleError = (error: Error) => { - appCalls.handleViewError(error.toString()) + handleViewError(error.toString()) setTimeout(() => { window.location.reload() }, 0) @@ -16,7 +16,6 @@ class ErrorBoundary extends Component<{ children: React.ReactChild }, { hasError } static getDerivedStateFromError(error: Error) { - window.alert(error.stack) return handleError(error) } diff --git a/packages/neuron-ui/src/components/GeneralSetting/index.tsx b/packages/neuron-ui/src/components/GeneralSetting/index.tsx index ccf99e36dd..b4ff6155d5 100644 --- a/packages/neuron-ui/src/components/GeneralSetting/index.tsx +++ b/packages/neuron-ui/src/components/GeneralSetting/index.tsx @@ -3,12 +3,12 @@ import { Stack, Toggle } from 'office-ui-fabric-react' import { useTranslation } from 'react-i18next' import { StateWithDispatch } from 'states/stateProvider/reducer' -import actionCreators from 'states/stateProvider/actionCreators' +import { toggleAddressBook } from 'states/stateProvider/actionCreators' const GeneralSetting = ({ settings: { showAddressBook }, dispatch }: React.PropsWithoutRef) => { const [t] = useTranslation() const onToggle = useCallback(() => { - dispatch(actionCreators.toggleAddressBook()) + dispatch(toggleAddressBook()) }, [dispatch]) return ( diff --git a/packages/neuron-ui/src/components/History/hooks.ts b/packages/neuron-ui/src/components/History/hooks.ts index d63a323c00..2733e4f59f 100644 --- a/packages/neuron-ui/src/components/History/hooks.ts +++ b/packages/neuron-ui/src/components/History/hooks.ts @@ -1,7 +1,7 @@ import { useState, useEffect } from 'react' -import { NeuronWalletActions, AppActions } from 'states/stateProvider/reducer' +import { AppActions } from 'states/stateProvider/reducer' +import { updateTransactionList } from 'states/stateProvider/actionCreators/transactions' import { queryParsers } from 'utils/parser' -import { getTransactionList } from 'services/remote' const backToTop = () => { const container = document.querySelector('main') as HTMLElement @@ -31,16 +31,7 @@ export const useSearch = (search: string = '', walletID: string = '', dispatch: type: AppActions.CleanTransactions, payload: null, }) - getTransactionList({ ...params, keywords: params.keywords, walletID }).then(res => { - if (res.status) { - dispatch({ - type: NeuronWalletActions.UpdateTransactionList, - payload: res.result, - }) - } else { - // TODO: notification - } - }) + updateTransactionList({ ...params, keywords: params.keywords, walletID })(dispatch) }, [search, walletID, dispatch]) return { keywords, onKeywordsChange, setKeywords } } diff --git a/packages/neuron-ui/src/components/NetworkEditor/hooks.ts b/packages/neuron-ui/src/components/NetworkEditor/hooks.ts index e6e6d84b2e..9128ad6d59 100644 --- a/packages/neuron-ui/src/components/NetworkEditor/hooks.ts +++ b/packages/neuron-ui/src/components/NetworkEditor/hooks.ts @@ -1,8 +1,8 @@ import { useState, useEffect, useMemo, useCallback } from 'react' import { AppActions, StateDispatch } from 'states/stateProvider/reducer' -import { Message, MAX_NETWORK_NAME_LENGTH, Routes } from 'utils/const' import { createNetwork, updateNetwork } from 'services/remote' +import { Message, MAX_NETWORK_NAME_LENGTH, Routes } from 'utils/const' import i18n from 'utils/i18n' diff --git a/packages/neuron-ui/src/components/NetworkSetting/index.tsx b/packages/neuron-ui/src/components/NetworkSetting/index.tsx index ea5ef2dea6..32a07c7f40 100644 --- a/packages/neuron-ui/src/components/NetworkSetting/index.tsx +++ b/packages/neuron-ui/src/components/NetworkSetting/index.tsx @@ -5,13 +5,12 @@ import { Stack, PrimaryButton, ChoiceGroup, IChoiceGroupOption } from 'office-ui import { StateWithDispatch } from 'states/stateProvider/reducer' import chainState from 'states/initStates/chain' -import { appCalls } from 'services/UILayer' -import { setCurrentNetowrk } from 'services/remote' +import { setCurrentNetowrk, contextMenu } from 'services/remote' import { Routes } from 'utils/const' const onContextMenu = (id: string = '') => () => { - appCalls.contextMenu({ type: 'networkList', id }) + contextMenu({ type: 'networkList', id }) } const NetworkSetting = ({ diff --git a/packages/neuron-ui/src/components/Overview/index.tsx b/packages/neuron-ui/src/components/Overview/index.tsx index d80b3ab076..a8f55fe6f6 100644 --- a/packages/neuron-ui/src/components/Overview/index.tsx +++ b/packages/neuron-ui/src/components/Overview/index.tsx @@ -21,9 +21,10 @@ import { MessageBarType, } from 'office-ui-fabric-react' -import { StateWithDispatch, NeuronWalletActions } from 'states/stateProvider/reducer' +import { StateWithDispatch } from 'states/stateProvider/reducer' +import { updateTransactionList } from 'states/stateProvider/actionCreators' -import { showErrorMessage, getTransactionList } from 'services/remote' +import { showErrorMessage } from 'services/remote' import { localNumberFormatter, shannonToCKBFormatter, uniformTimeFormatter as timeFormatter } from 'utils/formatters' import { PAGE_SIZE, MIN_CELL_WIDTH } from 'utils/const' @@ -99,18 +100,12 @@ const Overview = ({ const minerInfoRef = useRef(null) useEffect(() => { - getTransactionList({ + updateTransactionList({ pageNo: 1, pageSize: PAGE_SIZE, keywords: '', walletID: id, - }).then(res => { - if (res.status) { - dispatch({ type: NeuronWalletActions.UpdateTransactionList, payload: res.result }) - } else { - // TODO: notification - } - }) + })(dispatch) }, [id, dispatch]) const onTransactionRowRender = useCallback((props?: IDetailsRowProps) => { diff --git a/packages/neuron-ui/src/components/PasswordRequest/index.tsx b/packages/neuron-ui/src/components/PasswordRequest/index.tsx index c2db0839ac..08dfb7bb78 100644 --- a/packages/neuron-ui/src/components/PasswordRequest/index.tsx +++ b/packages/neuron-ui/src/components/PasswordRequest/index.tsx @@ -23,20 +23,9 @@ const PasswordRequest = ({ }, [dispatch]) const onConfirm = useCallback(() => { - const params = { id: walletID, password } switch (actionType) { - case 'delete': { - dispatch(actionCreators.deleteWallet(params)) - break - } - case 'backup': { - dispatch(actionCreators.backupWallet(params)) - break - } case 'send': { - dispatch( - actionCreators.submitTransaction(txID, walletID, outputs, description, password, priceToFee(price, cycles)) - ) + submitTransaction(txID, walletID, outputs, description, password, priceToFee(price, cycles))(dispatch) break } default: { diff --git a/packages/neuron-ui/src/components/Transaction/index.tsx b/packages/neuron-ui/src/components/Transaction/index.tsx index b85507feee..be364f19e4 100644 --- a/packages/neuron-ui/src/components/Transaction/index.tsx +++ b/packages/neuron-ui/src/components/Transaction/index.tsx @@ -3,11 +3,11 @@ import { RouteComponentProps } from 'react-router-dom' import { useTranslation } from 'react-i18next' import { Stack, DetailsList, Text, DetailsListLayoutMode, CheckboxVisibility, IColumn } from 'office-ui-fabric-react' -import { AppActions, StateWithDispatch, NeuronWalletActions } from 'states/stateProvider/reducer' +import { AppActions, StateWithDispatch } from 'states/stateProvider/reducer' +import { updateTransaction } from 'states/stateProvider/actionCreators' import chainState from 'states/initStates/chain' import { localNumberFormatter, uniformTimeFormatter } from 'utils/formatters' -import { getTransaction } from 'services/remote' const MIN_CELL_WIDTH = 70 @@ -105,16 +105,7 @@ const Transaction = ({ type: AppActions.CleanTransaction, payload: null, }) - getTransaction({ walletID, hash: match.params.hash }).then(res => { - if (res.status) { - dispatch({ - type: NeuronWalletActions.UpdateTransaction, - payload: res.result, - }) - } else { - // TODO: notification - } - }) + updateTransaction({ walletID, hash: match.params.hash })(dispatch) }, [match.params.hash, dispatch, walletID]) const basicInfoItems = useMemo( diff --git a/packages/neuron-ui/src/components/TransactionList/index.tsx b/packages/neuron-ui/src/components/TransactionList/index.tsx index 74de95abfa..f8a70618e5 100644 --- a/packages/neuron-ui/src/components/TransactionList/index.tsx +++ b/packages/neuron-ui/src/components/TransactionList/index.tsx @@ -14,8 +14,8 @@ import { import { FormUp as ExpandIcon } from 'grommet-icons' import { StateDispatch } from 'states/stateProvider/reducer' +import { contextMenu } from 'services/remote' -import { appCalls } from 'services/UILayer' import { useLocalDescription } from 'utils/hooks' import { shannonToCKBFormatter, uniformTimeFormatter as timeFormatter, uniformTimeFormatter } from 'utils/formatters' import { registerIcons } from 'utils/icons' @@ -190,7 +190,7 @@ const TransactionList = ({ items = [] }: { walletID: string; items: State.Transa checkboxVisibility={CheckboxVisibility.hidden} onItemContextMenu={item => { if (item) { - appCalls.contextMenu({ type: 'transactionList', id: item.hash }) + contextMenu({ type: 'transactionList', id: item.hash }) } }} styles={{ diff --git a/packages/neuron-ui/src/components/WalletEditor/hooks.ts b/packages/neuron-ui/src/components/WalletEditor/hooks.ts index 0afb93d271..e2ef2415c2 100644 --- a/packages/neuron-ui/src/components/WalletEditor/hooks.ts +++ b/packages/neuron-ui/src/components/WalletEditor/hooks.ts @@ -1,5 +1,5 @@ import { useState, useMemo, useCallback } from 'react' -import actionCreators from 'states/stateProvider/actionCreators' +import { updateWallet } from 'states/stateProvider/actionCreators' import { StateDispatch } from 'states/stateProvider/reducer' import i18n from 'utils/i18n' @@ -37,12 +37,10 @@ export const useInputs = ({ name }: ReturnType) => { export const useOnConfirm = (name: string = '', id: string = '', dispatch: StateDispatch) => { return useCallback(() => { - dispatch( - actionCreators.updateWallet({ - id, - name, - }) - ) + updateWallet({ + id, + name, + })(dispatch) }, [name, id, dispatch]) } diff --git a/packages/neuron-ui/src/components/WalletSetting/index.tsx b/packages/neuron-ui/src/components/WalletSetting/index.tsx index fe2c74a9e4..7ab9046727 100644 --- a/packages/neuron-ui/src/components/WalletSetting/index.tsx +++ b/packages/neuron-ui/src/components/WalletSetting/index.tsx @@ -4,11 +4,11 @@ import { useTranslation } from 'react-i18next' import { Stack, PrimaryButton, ChoiceGroup, IChoiceGroupOption } from 'office-ui-fabric-react' import { StateWithDispatch } from 'states/stateProvider/reducer' -import actionCreators from 'states/stateProvider/actionCreators' +import { activateWallet } from 'states/stateProvider/actionCreators' import { WalletWizardPath } from 'components/WalletWizard' -import { appCalls } from 'services/UILayer' +import { contextMenu } from 'services/remote' import { Routes, MnemonicAction } from 'utils/const' const buttons = [ @@ -32,14 +32,14 @@ const WalletSetting = ({ const onChange = useCallback( (_e, option) => { if (option) { - dispatch(actionCreators.activateWallet(option.key)) + activateWallet(option.key)(dispatch) } }, [dispatch] ) const onContextMenu = useCallback( (id: string = '') => () => { - appCalls.contextMenu({ type: 'walletList', id }) + contextMenu({ type: 'walletList', id }) }, [] ) diff --git a/packages/neuron-ui/src/containers/Main/hooks.ts b/packages/neuron-ui/src/containers/Main/hooks.ts index 7d05d96c12..6d6898d2f4 100644 --- a/packages/neuron-ui/src/containers/Main/hooks.ts +++ b/packages/neuron-ui/src/containers/Main/hooks.ts @@ -2,10 +2,10 @@ import { useEffect } from 'react' import { WalletWizardPath } from 'components/WalletWizard' import { NeuronWalletActions, StateDispatch, AppActions } from 'states/stateProvider/reducer' -import { actionCreators } from 'states/stateProvider/actionCreators' +import { toggleAddressBook, updateTransactionList, updateTransaction } from 'states/stateProvider/actionCreators' -import UILayer, { AppMethod, WalletsMethod, walletsCall } from 'services/UILayer' -import { initWindow, getTransactionList, getTransaction } from 'services/remote' +import UILayer, { WalletsMethod, walletsCall } from 'services/UILayer' +import { initWindow, getWinID } from 'services/remote' import { SystemScript as SystemScriptSubject, DataUpdate as DataUpdateSubject, @@ -13,6 +13,7 @@ import { CurrentNetworkID as CurrentNetworkIDSubject, ConnectionStatus as ConnectionStatusSubject, SyncedBlockNumber as SyncedBlockNumberSubject, + Command as CommandSubject, } from 'services/subjects' import { ckbCore, getTipBlockNumber, getBlockchainInfo } from 'services/chain' import { Routes, Channel, ConnectionStatus } from 'utils/const' @@ -44,24 +45,6 @@ export const useChannelListeners = ({ i18n: any }) => useEffect(() => { - UILayer.on(Channel.App, (_e: Event, method: AppMethod, args: ChannelResponse) => { - if (args && args.status) { - switch (method) { - case AppMethod.NavTo: { - history.push(args.result) - break - } - case AppMethod.ToggleAddressBook: { - dispatch(actionCreators.toggleAddressBook()) - break - } - default: { - break - } - } - } - }) - UILayer.on(Channel.Wallets, (_e: Event, method: WalletsMethod, args: ChannelResponse) => { if (args.status) { switch (method) { @@ -236,19 +219,12 @@ export const useOnCurrentWalletChange = ({ useEffect(() => { if (walletID) { walletsCall.getAllAddresses(walletID) - getTransactionList({ + updateTransactionList({ walletID, keywords: '', pageNo, pageSize, - }).then(res => { - if (res.status) { - dispatch({ - type: NeuronWalletActions.UpdateTransactionList, - payload: res.result, - }) - } - }) + })(dispatch) } else { initWindow() .then((initializedState: any) => { @@ -270,10 +246,12 @@ export const useOnCurrentWalletChange = ({ export const useSubscription = ({ walletID, chain, + history, dispatch, }: { walletID: string chain: State.Chain + history: any dispatch: StateDispatch }) => { const { pageNo, pageSize, keywords } = chain.transactions @@ -296,19 +274,13 @@ export const useSubscription = ({ break } case 'transaction': { - getTransactionList({ + updateTransactionList({ walletID, keywords, pageNo, pageSize, - }).then(res => { - if (res.status) { - dispatch({ type: NeuronWalletActions.UpdateTransactionList, payload: res.result }) - } else { - // TODO: notification - } - }) - getTransaction({ walletID, hash: txHash }) + })(dispatch) + updateTransaction({ walletID, hash: txHash }) break } case 'wallet': { @@ -348,6 +320,25 @@ export const useSubscription = ({ payload: syncedBlockNumber, }) }) + const commandSubscription = CommandSubject.subscribe( + ({ winID, type, payload }: { winID: number; type: 'nav' | 'toggleAddressBook'; payload: string | null }) => { + if (getWinID() === winID) { + switch (type) { + case 'nav': { + history.push(payload) + break + } + case 'toggleAddressBook': { + dispatch(toggleAddressBook()) + break + } + default: { + break + } + } + } + } + ) return () => { systemScriptSubscription.unsubscribe() dataUpdateSubscription.unsubscribe() @@ -355,8 +346,9 @@ export const useSubscription = ({ currentNetworkIDSubscription.unsubscribe() connectionStatusSubscription.unsubscribe() syncedBlockNumberSubscription.unsubscribe() + commandSubscription.unsubscribe() } - }, [walletID, pageNo, pageSize, keywords, txHash, dispatch]) + }, [walletID, pageNo, pageSize, keywords, txHash, history, dispatch]) } export default { diff --git a/packages/neuron-ui/src/containers/Main/index.tsx b/packages/neuron-ui/src/containers/Main/index.tsx index da25f5667d..2a3b512126 100644 --- a/packages/neuron-ui/src/containers/Main/index.tsx +++ b/packages/neuron-ui/src/containers/Main/index.tsx @@ -125,6 +125,7 @@ const MainContent = ({ useSubscription({ walletID, chain, + history, dispatch, }) diff --git a/packages/neuron-ui/src/services/UILayer.ts b/packages/neuron-ui/src/services/UILayer.ts index bbbbcfb110..9b740180e2 100644 --- a/packages/neuron-ui/src/services/UILayer.ts +++ b/packages/neuron-ui/src/services/UILayer.ts @@ -2,14 +2,6 @@ import { Channel } from 'utils/const' import SyntheticEventEmitter from 'utils/SyntheticEventEmitter' import instantiateMethodCall from 'utils/instantiateMethodCall' -export enum AppMethod { - ToggleAddressBook = 'toggleAddressBook', - ContextMenu = 'contextMenu', - NavTo = 'navTo', - SetUILocale = 'setUILocale', - HandleViewError = 'handleViewError', -} - export enum WalletsMethod { GetAll = 'getAll', Get = 'get', @@ -30,10 +22,6 @@ export enum WalletsMethod { GetAllAddresses = 'getAllAddresses', } -export enum HelpersMethod { - GenerateMnemonic = 'generateMnemonic', -} - const UILayer = (() => { if (window.bridge) { return new SyntheticEventEmitter(window.bridge.ipcRenderer) @@ -58,15 +46,6 @@ const UILayer = (() => { } })() -export const app = (method: AppMethod, ...params: any) => { - UILayer.send(Channel.App, method, ...params) -} - -export const appCalls = instantiateMethodCall(app) as { - contextMenu: ({ type, id }: { type: string; id: string }) => void - handleViewError: (errorMessage: string) => void -} - export const wallets = ( method: WalletsMethod, params: @@ -116,21 +95,4 @@ export const walletsCall = instantiateMethodCall(wallets) as { updateAddressDescription: (params: { walletID: string; address: string; description: string }) => void } -export const helpers = (method: HelpersMethod, ...params: any) => { - return new Promise((res, rej) => { - UILayer.send(Channel.Helpers, method, ...params) - UILayer.once(Channel.Helpers, (_e: Event, _method: HelpersMethod, args: ChannelResponse) => { - if (args.status) { - res(args.result) - } else { - rej(args.msg) - } - }) - }) -} - -export const helpersCall = instantiateMethodCall(helpers) as { - generateMnemonic: () => Promise -} - export default UILayer diff --git a/packages/neuron-ui/src/services/remote/app.ts b/packages/neuron-ui/src/services/remote/app.ts new file mode 100644 index 0000000000..1293269df3 --- /dev/null +++ b/packages/neuron-ui/src/services/remote/app.ts @@ -0,0 +1,15 @@ +import { controllerMethodWrapper } from './controllerMethodWrapper' + +const CONTROLLER_NAME = 'app' + +export const handleViewError = controllerMethodWrapper(CONTROLLER_NAME)(controller => (errorMessage: string) => + controller.handleViewError(errorMessage) +) +export const contextMenu = controllerMethodWrapper(CONTROLLER_NAME)( + controller => (params: { type: string; id: string }) => controller.contextMenu(params) +) + +export default { + handleViewError, + contextMenu, +} diff --git a/packages/neuron-ui/src/services/remote/controllerMethodWrapper.ts b/packages/neuron-ui/src/services/remote/controllerMethodWrapper.ts index a2063e99f1..04f6dc66ba 100644 --- a/packages/neuron-ui/src/services/remote/controllerMethodWrapper.ts +++ b/packages/neuron-ui/src/services/remote/controllerMethodWrapper.ts @@ -37,10 +37,16 @@ export const controllerMethodWrapper = (controllerName: string) => ( return controllerNotLoaded(controllerName) } const res = await callControllerMethod(controller)(realParams) + if (!res) { + return { + status: 1, + result: null, + } + } if (res.status) { return { status: 1, - result: res.result || true, + result: res.result || null, } } return { diff --git a/packages/neuron-ui/src/services/remote/index.ts b/packages/neuron-ui/src/services/remote/index.ts index 14a00e8f1b..27c82be78b 100644 --- a/packages/neuron-ui/src/services/remote/index.ts +++ b/packages/neuron-ui/src/services/remote/index.ts @@ -1,5 +1,14 @@ +export * from './app' export * from './networks' export * from './transactions' + +export const getWinID = () => { + if (!window.remote) { + console.warn('remote is not supported') + return -1 + } + return window.remote.getCurrentWindow().id +} export const initWindow = () => { if (!window.remote) { console.warn('remote is not supported') @@ -51,4 +60,5 @@ export default { generateMnemonic, showMessage, showErrorMessage, + getWinID, } diff --git a/packages/neuron-ui/src/services/subjects.ts b/packages/neuron-ui/src/services/subjects.ts index fc3169ec5b..1dfe162cf7 100644 --- a/packages/neuron-ui/src/services/subjects.ts +++ b/packages/neuron-ui/src/services/subjects.ts @@ -46,6 +46,14 @@ export const SyncedBlockNumber = window.remote ? (window.remote.require(`${SUBJECT_PATH}/node`).SyncedBlockNumberSubject as NeuronWalletSubject) : FallbackSubject +export const Command = window.remote + ? (window.remote.require(`${SUBJECT_PATH}/command`).default as NeuronWalletSubject<{ + winID: number + type: 'nav' | 'toggleAddressBook' + payload: string | null + }>) + : FallbackSubject + export default { SystemScript, DataUpdate, @@ -53,4 +61,5 @@ export default { CurrentNetworkID, ConnectionStatus, SyncedBlockNumber, + Command, } diff --git a/packages/neuron-ui/src/states/stateProvider/actionCreators/index.ts b/packages/neuron-ui/src/states/stateProvider/actionCreators/index.ts index 8bf9984b6c..b9b5e8842b 100644 --- a/packages/neuron-ui/src/states/stateProvider/actionCreators/index.ts +++ b/packages/neuron-ui/src/states/stateProvider/actionCreators/index.ts @@ -3,6 +3,10 @@ import send from './send' import transactions from './transactions' import settings from './settings' +export * from './wallets' +export * from './send' +export * from './transactions' +export * from './settings' export const actionCreators = { ...wallets, ...send, diff --git a/packages/neuron-ui/src/states/stateProvider/actionCreators/send.ts b/packages/neuron-ui/src/states/stateProvider/actionCreators/send.ts index 848e10f36b..1c6bdb72d5 100644 --- a/packages/neuron-ui/src/states/stateProvider/actionCreators/send.ts +++ b/packages/neuron-ui/src/states/stateProvider/actionCreators/send.ts @@ -3,31 +3,33 @@ import { walletsCall } from 'services/UILayer' import { CKBToShannonFormatter } from 'utils/formatters' import { TransactionOutput } from 'components/Send' -import { AppActions } from '../reducer' +import { AppActions, StateDispatch } from '../reducer' + +export const submitTransaction = ( + id: string = '', + walletID: string = '', + items: TransactionOutput[] = [], + description: string = '', + password: string = '', + fee: string = '0' +) => (dispatch: StateDispatch) => { + walletsCall.sendCapacity({ + id, + walletID, + items: items.map(item => ({ + address: item.address, + capacity: CKBToShannonFormatter(item.amount, item.unit), + })), + password, + fee, + description, + }) + dispatch({ + type: AppActions.DismissPasswordRequest, + payload: null, + }) +} export default { - submitTransaction: ( - id: string = '', - walletID: string = '', - items: TransactionOutput[] = [], - description: string = '', - password: string = '', - fee: string = '0' - ) => { - walletsCall.sendCapacity({ - id, - walletID, - items: items.map(item => ({ - address: item.address, - capacity: CKBToShannonFormatter(item.amount, item.unit), - })), - password, - fee, - description, - }) - return { - type: AppActions.DismissPasswordRequest, - payload: null, - } - }, + submitTransaction, } diff --git a/packages/neuron-ui/src/states/stateProvider/actionCreators/settings.ts b/packages/neuron-ui/src/states/stateProvider/actionCreators/settings.ts index 88309b407b..2a723673c8 100644 --- a/packages/neuron-ui/src/states/stateProvider/actionCreators/settings.ts +++ b/packages/neuron-ui/src/states/stateProvider/actionCreators/settings.ts @@ -1,14 +1,16 @@ import { addressBook } from 'utils/localCache' import { NeuronWalletActions } from '../reducer' +export const toggleAddressBook = () => { + addressBook.toggleVisibility() + return { + type: NeuronWalletActions.Settings, + payload: { + toggleAddressBook: true, + }, + } +} + export default { - toggleAddressBook: () => { - addressBook.toggleVisibility() - return { - type: NeuronWalletActions.Settings, - payload: { - toggleAddressBook: true, - }, - } - }, + toggleAddressBook, } diff --git a/packages/neuron-ui/src/states/stateProvider/actionCreators/transactions.ts b/packages/neuron-ui/src/states/stateProvider/actionCreators/transactions.ts index 4832377655..028b072ba9 100644 --- a/packages/neuron-ui/src/states/stateProvider/actionCreators/transactions.ts +++ b/packages/neuron-ui/src/states/stateProvider/actionCreators/transactions.ts @@ -1,38 +1,28 @@ -import { walletsCall } from 'services/UILayer' -import { AppActions } from '../reducer' +import { NeuronWalletActions, StateDispatch } from 'states/stateProvider/reducer' +import { GetTransactionListParams, getTransaction, getTransactionList } from 'services/remote' -export default { - updateDescription: ({ - type, - walletID, - key, - description, - }: { - type: 'address' | 'transaction' - walletID: string - key: string - description: string - }) => { - if (type === 'address') { - walletsCall.updateAddressDescription({ - walletID, - address: key, - description, +export const updateTransaction = (params: { walletID: string; hash: string }) => (dispatch: StateDispatch) => { + getTransaction(params).then(res => { + if (res.status) { + dispatch({ + type: NeuronWalletActions.UpdateTransaction, + payload: res.result, }) - return { - type: AppActions.Ignore, - payload: null, - } - } - if (type === 'transaction') { - return { - type: AppActions.Ignore, - payload: null, - } } - return { - type: AppActions.Ignore, - payload: null, + }) +} +export const updateTransactionList = (params: GetTransactionListParams) => (dispatch: StateDispatch) => { + getTransactionList(params).then(res => { + if (res.status) { + dispatch({ + type: NeuronWalletActions.UpdateTransactionList, + payload: res.result, + }) } - }, + }) +} + +export default { + updateTransaction, + updateTransactionList, } diff --git a/packages/neuron-ui/src/states/stateProvider/actionCreators/wallets.ts b/packages/neuron-ui/src/states/stateProvider/actionCreators/wallets.ts index 938147ffaf..da848b5e42 100644 --- a/packages/neuron-ui/src/states/stateProvider/actionCreators/wallets.ts +++ b/packages/neuron-ui/src/states/stateProvider/actionCreators/wallets.ts @@ -1,44 +1,26 @@ -import { AppActions } from 'states/stateProvider/reducer' +import { AppActions, StateDispatch } from 'states/stateProvider/reducer' import { walletsCall } from 'services/UILayer' import { NeuronWalletActions } from '../reducer' +export const activateWallet = (id: string) => (dispatch: StateDispatch) => { + walletsCall.activate(id) + dispatch({ + type: NeuronWalletActions.Wallet, + payload: id, + }) +} + +export const updateWallet = (params: { id: string; password?: string; newPassword?: string; name?: string }) => ( + dispatch: StateDispatch +) => { + walletsCall.update(params) + dispatch({ + type: AppActions.Ignore, + payload: null, + }) +} + export default { - getAll: () => { - walletsCall.getAll() - return { - type: NeuronWalletActions.Wallet, - } - }, - activateWallet: (id: string) => { - walletsCall.activate(id) - return { - type: NeuronWalletActions.Wallet, - payload: id, - } - }, - updateWallet: (params: { id: string; password?: string; newPassword?: string; name?: string }) => { - walletsCall.update(params) - return { - type: AppActions.Ignore, - payload: null, - } - }, - deleteWallet: (params: { id: string; password: string }) => { - setTimeout(() => { - walletsCall.delete(params) - }, 100) - return { - type: AppActions.DismissPasswordRequest, - payload: null, - } - }, - backupWallet: (params: { id: string; password: string }) => { - setTimeout(() => { - walletsCall.backup(params) - }, 100) - return { - type: AppActions.DismissPasswordRequest, - payload: null, - } - }, + updateWallet, + activateWallet, } diff --git a/packages/neuron-ui/src/utils/const.ts b/packages/neuron-ui/src/utils/const.ts index 25e539a6f3..0b1b42736b 100644 --- a/packages/neuron-ui/src/utils/const.ts +++ b/packages/neuron-ui/src/utils/const.ts @@ -14,12 +14,7 @@ export enum ConnectionStatus { } export enum Channel { - NavTo = 'navTo', - App = 'app', - Networks = 'networks', - Transactions = 'transactions', Wallets = 'wallets', - Helpers = 'helpers', } export enum Routes { diff --git a/packages/neuron-wallet/src/controllers/app/index.ts b/packages/neuron-wallet/src/controllers/app/index.ts index 282dc25552..12a4d99643 100644 --- a/packages/neuron-wallet/src/controllers/app/index.ts +++ b/packages/neuron-wallet/src/controllers/app/index.ts @@ -12,10 +12,11 @@ import WalletsController from '../wallets' import SyncInfoController from '../sync-info' import { Controller as ControllerDecorator } from '../../decorators' -import { Channel, ResponseCode } from '../../utils/const' +import { Channel } from '../../utils/const' import WindowManager from '../../models/window-manager' import i18n from '../../utils/i18n' import env from '../../env' +import CommandSubject from '../../models/subjects/command' const nodeService = NodeService.getInstance() @@ -100,23 +101,19 @@ export default class AppController { } public static toggleAddressBook() { - WindowManager.broadcast(Channel.App, 'toggleAddressBook', { - status: ResponseCode.Success, - }) + if (WindowManager.mainWindow) { + CommandSubject.next({ + winID: WindowManager.mainWindow.id, + type: 'toggleAddressBook', + payload: null, + }) + } } public static navTo(url: string) { - WindowManager.sendToMainWindow(Channel.App, 'navTo', { - status: ResponseCode.Success, - result: url, - }) - } - - public static setUILocale(locale: string) { - WindowManager.broadcast(Channel.App, 'setUILocale', { - status: ResponseCode.Success, - result: locale, - }) + if (WindowManager.mainWindow) { + CommandSubject.next({ winID: WindowManager.mainWindow.id, type: 'nav', payload: url }) + } } public static openExternal(url: string) { diff --git a/packages/neuron-wallet/src/models/subjects/command.ts b/packages/neuron-wallet/src/models/subjects/command.ts new file mode 100644 index 0000000000..8a7d4d4e0d --- /dev/null +++ b/packages/neuron-wallet/src/models/subjects/command.ts @@ -0,0 +1,5 @@ +import { Subject } from 'rxjs' + +const CommandSubject = new Subject<{ winID: number; type: 'nav' | 'toggleAddressBook'; payload: string | null }>() + +export default CommandSubject diff --git a/packages/neuron-wallet/src/models/window-manager.ts b/packages/neuron-wallet/src/models/window-manager.ts index 96aba90daa..be14689a9c 100644 --- a/packages/neuron-wallet/src/models/window-manager.ts +++ b/packages/neuron-wallet/src/models/window-manager.ts @@ -5,15 +5,11 @@ import logger from '../utils/logger' const error = { level: 'error', message: 'Electron is not loaded' } interface SendMessage { - (channel: Channel.App, method: Controller.AppMethod, params: any): void ( channel: Channel.Wallets, method: Controller.WalletsMethod | 'allAddresses' | 'sendingStatus' | 'requestPassword', params: any ): void - (channel: Channel.Transactions, method: Controller.TransactionsMethod | 'transactionUpdated', params: any): void - (channel: Channel.Networks, method: Controller.NetworksMethod, params: any): void - (channel: Channel.Helpers, method: Controller.HelpersMethod, params: any): void } export default class WindowManager { diff --git a/packages/neuron-wallet/src/services/networks.ts b/packages/neuron-wallet/src/services/networks.ts index ca18304b28..7796ab6555 100644 --- a/packages/neuron-wallet/src/services/networks.ts +++ b/packages/neuron-wallet/src/services/networks.ts @@ -55,6 +55,11 @@ export default class NetworksService extends Store { this.getCurrentID().then(currentNetworkID => { if (currentNetworkID) { CurrentNetworkIDSubject.next({ currentNetworkID }) + this.get(currentNetworkID).then(network => { + if (network) { + networkSwitchSubject.next(network) + } + }) } }) diff --git a/packages/neuron-wallet/src/utils/const.ts b/packages/neuron-wallet/src/utils/const.ts index 12ab0c9766..db587ce125 100644 --- a/packages/neuron-wallet/src/utils/const.ts +++ b/packages/neuron-wallet/src/utils/const.ts @@ -6,8 +6,6 @@ export enum Channel { Networks = 'networks', Wallets = 'wallets', Transactions = 'transactions', - Helpers = 'helpers', - DataUpdate = 'dataUpdate', } export enum ResponseCode {