From 099bef255adc911711fd5ad71fde1cde69b0b8c8 Mon Sep 17 00:00:00 2001 From: Leon Date: Fri, 20 Oct 2023 16:33:58 +0800 Subject: [PATCH] feat: export store for jotai --- .../kit/src/components/WebView/mock/index.ts | 47 ++++++++++++++++++- .../src/store/jotai/createJotaiContext.tsx | 6 ++- .../Explorer/Context/contextWebTabs.ts | 28 +++++++---- .../Explorer/Desktop/ExplorerDesktop.tsx | 4 +- packages/kit/src/views/Discover/types.ts | 9 ++++ 5 files changed, 79 insertions(+), 15 deletions(-) create mode 100644 packages/kit/src/views/Discover/types.ts diff --git a/packages/kit/src/components/WebView/mock/index.ts b/packages/kit/src/components/WebView/mock/index.ts index 616fe0e2a93..a263be54a6d 100644 --- a/packages/kit/src/components/WebView/mock/index.ts +++ b/packages/kit/src/components/WebView/mock/index.ts @@ -9,7 +9,52 @@ export const simpleDb = { discoverWebTabs: { getRawData: () => Promise.resolve({ - tabs: [], + tabs: [ + { + 'id': 'home', + 'url': 'about:blank', + 'title': 'OneKey', + 'isCurrent': false, + 'canGoBack': false, + 'loading': false, + }, + { + 'id': 'BMgUCWustX5tI6fmqQsoT', + 'url': 'about:blank', + 'title': 'OneKey', + 'isCurrent': false, + 'canGoBack': false, + 'loading': false, + 'timestamp': 1697790363161, + }, + { + 'id': 'CBPTaj9jYQ72FPmAkczYn', + 'url': 'about:blank', + 'title': 'OneKey', + 'isCurrent': true, + 'canGoBack': false, + 'loading': false, + 'timestamp': 1697790364880, + }, + { + 'id': 'KA2RHowvl_WxG6pURtwBC', + 'url': 'about:blank', + 'title': 'OneKey', + 'isCurrent': false, + 'canGoBack': false, + 'loading': false, + 'timestamp': 1697790365626, + }, + { + 'id': '_3BCqo5_PFJiKy2zoVB9c', + 'url': 'about:blank', + 'title': 'OneKey', + 'isCurrent': false, + 'canGoBack': false, + 'loading': false, + 'timestamp': 1697790436363, + }, + ], }), setRawData: (_: any) => {}, }, diff --git a/packages/kit/src/store/jotai/createJotaiContext.tsx b/packages/kit/src/store/jotai/createJotaiContext.tsx index f86d52873ff..e38c6b5b526 100644 --- a/packages/kit/src/store/jotai/createJotaiContext.tsx +++ b/packages/kit/src/store/jotai/createJotaiContext.tsx @@ -9,9 +9,10 @@ export { atom }; export function createJotaiContext() { const Context = createContext | null>(null); + const store = createStore(); function Provider({ children }: { children?: ReactNode | undefined }) { - const store = useMemo(() => createStore(), []); - return {children}; + const innerStore = useMemo(() => store, []); + return {children}; } function useContextAtom( atomInstance: WritableAtom, @@ -36,5 +37,6 @@ export function createJotaiContext() { Provider, withProvider, useContextAtom, + store, }; } diff --git a/packages/kit/src/views/Discover/Explorer/Context/contextWebTabs.ts b/packages/kit/src/views/Discover/Explorer/Context/contextWebTabs.ts index 931f0ec7ad4..9f320b8e354 100644 --- a/packages/kit/src/views/Discover/Explorer/Context/contextWebTabs.ts +++ b/packages/kit/src/views/Discover/Explorer/Context/contextWebTabs.ts @@ -38,13 +38,6 @@ export const homeResettingFlags: Record = {}; // eslint-disable-next-line @typescript-eslint/naming-convention let _currentTabId = ''; -export const getCurrentTabId = () => - // TODO: Fix this - // if (!_currentTabId) { - // _currentTabId = webTabsObs.peek().find((t) => t.isCurrent)?.id || ''; - // } - _currentTabId; - export function buildWebTabData(tabs: WebTab[]) { const map: Record = {}; const keys: string[] = []; @@ -70,7 +63,10 @@ export const atomWebTabsMap = atom>({ }); export const setWebTabsWriteAtom = atom(null, (get, set, payload: WebTab[]) => { let newTabs = payload; - if (!newTabs) { + if (!Array.isArray(payload)) { + throw new Error('setWebTabsWriteAtom: payload must be an array'); + } + if (!newTabs || !newTabs.length) { newTabs = [{ ...homeTab }]; } const result = buildWebTabData(newTabs); @@ -174,6 +170,7 @@ export const closeAllWebTabsAtomWithWriteOnly = atom(null, (_, set) => { export const setCurrentWebTabAtomWithWriteOnly = atom( null, (get, set, tabId: string) => { + // eslint-disable-next-line @typescript-eslint/no-use-before-define const currentTabId = getCurrentTabId(); if (currentTabId !== tabId) { // pauseDappInteraction(currentTabId); @@ -209,7 +206,18 @@ export const setCurrentWebTabAtomWithWriteOnly = atom( export const incomingUrlAtom = atom(''); -const { withProvider: withProviderWebTabs, useContextAtom: useAtomWebTabs } = - createJotaiContext(); +const { + withProvider: withProviderWebTabs, + useContextAtom: useAtomWebTabs, + store: webTabsStore, +} = createJotaiContext(); export { withProviderWebTabs, useAtomWebTabs }; + +export const getCurrentTabId = () => { + if (!_currentTabId) { + const { tabs } = webTabsStore.get(atomWebTabs); + _currentTabId = tabs.find((t) => t.isCurrent)?.id || ''; + } + return _currentTabId; +}; diff --git a/packages/kit/src/views/Discover/Explorer/Desktop/ExplorerDesktop.tsx b/packages/kit/src/views/Discover/Explorer/Desktop/ExplorerDesktop.tsx index 5840d6d3172..64742dfcc42 100644 --- a/packages/kit/src/views/Discover/Explorer/Desktop/ExplorerDesktop.tsx +++ b/packages/kit/src/views/Discover/Explorer/Desktop/ExplorerDesktop.tsx @@ -13,7 +13,7 @@ import { } from '../Context/contextWebTabs'; import { webHandler } from '../explorerUtils'; -// import ControllerBarDesktop from './ControllerBarDesktop'; +import ControllerBarDesktop from './ControllerBarDesktop'; import TabBarDesktop from './TabBarDesktop'; const showExplorerBar = webHandler !== 'browser'; @@ -48,7 +48,7 @@ function ExplorerHeaderCmp() { - {/* */} + ); } diff --git a/packages/kit/src/views/Discover/types.ts b/packages/kit/src/views/Discover/types.ts new file mode 100644 index 00000000000..5e43ec96bf1 --- /dev/null +++ b/packages/kit/src/views/Discover/types.ts @@ -0,0 +1,9 @@ +export type DAppItemType = { + _id: string; + name: string; + url: string; + logoURL: string; + subtitle: string; + networkIds: string[]; + _subtitle?: string; +};