Skip to content

Commit

Permalink
feat: export store for jotai
Browse files Browse the repository at this point in the history
  • Loading branch information
originalix committed Oct 20, 2023
1 parent 46eada0 commit 099bef2
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 15 deletions.
47 changes: 46 additions & 1 deletion packages/kit/src/components/WebView/mock/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {},
},
Expand Down
6 changes: 4 additions & 2 deletions packages/kit/src/store/jotai/createJotaiContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ export { atom };

export function createJotaiContext() {
const Context = createContext<ReturnType<typeof createStore> | null>(null);
const store = createStore();
function Provider({ children }: { children?: ReactNode | undefined }) {
const store = useMemo(() => createStore(), []);
return <Context.Provider value={store}>{children}</Context.Provider>;
const innerStore = useMemo(() => store, []);
return <Context.Provider value={innerStore}>{children}</Context.Provider>;
}
function useContextAtom<Value, Args extends any[], Result>(
atomInstance: WritableAtom<Value, Args, Result>,
Expand All @@ -36,5 +37,6 @@ export function createJotaiContext() {
Provider,
withProvider,
useContextAtom,
store,
};
}
28 changes: 18 additions & 10 deletions packages/kit/src/views/Discover/Explorer/Context/contextWebTabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@ export const homeResettingFlags: Record<string, number> = {};
// 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<string, WebTab> = {};
const keys: string[] = [];
Expand All @@ -70,7 +63,10 @@ export const atomWebTabsMap = atom<Record<string, WebTab>>({
});
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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
};
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -48,7 +48,7 @@ function ExplorerHeaderCmp() {
<Stack mt={`${top ? top + 10 : 0}px`} bg={tabBarBgColor} zIndex={5}>
<HandleRebuildTabBarData />
<TabBarDesktop />
{/* <ControllerBarDesktop /> */}
<ControllerBarDesktop />
</Stack>
);
}
Expand Down
9 changes: 9 additions & 0 deletions packages/kit/src/views/Discover/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export type DAppItemType = {
_id: string;
name: string;
url: string;
logoURL: string;
subtitle: string;
networkIds: string[];
_subtitle?: string;
};

0 comments on commit 099bef2

Please sign in to comment.