-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add provider for recommended route store
- Loading branch information
Showing
17 changed files
with
296 additions
and
374 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,16 @@ | ||
import type { PropsWithChildren } from 'react'; | ||
import { ChainOrderStoreProvider } from './chains'; | ||
import { RouteExecutionStoreProvider } from './routes'; | ||
import { SettingsStoreProvider } from './settings'; | ||
import { | ||
RecommendedRouteStoreProvider, | ||
RouteExecutionStoreProvider, | ||
} from './routes'; | ||
import type { PersistStoreProviderProps } from './types'; | ||
|
||
export const StoreProvider: React.FC< | ||
PropsWithChildren<PersistStoreProviderProps> | ||
> = ({ children, namePrefix }) => { | ||
return ( | ||
<RouteExecutionStoreProvider namePrefix={namePrefix}> | ||
{/* We don't want separate settings in each widget instance for now. */} | ||
<SettingsStoreProvider | ||
// namePrefix={namePrefix} | ||
> | ||
<ChainOrderStoreProvider | ||
// namePrefix={namePrefix} | ||
> | ||
{children} | ||
</ChainOrderStoreProvider> | ||
</SettingsStoreProvider> | ||
<RecommendedRouteStoreProvider>{children}</RecommendedRouteStoreProvider> | ||
</RouteExecutionStoreProvider> | ||
); | ||
}; |
This file was deleted.
Oops, something went wrong.
67 changes: 0 additions & 67 deletions
67
packages/widget/src/stores/chains/createChainOrderStore.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export * from './ChainOrderStore'; | ||
export * from './createChainOrderStore'; | ||
export * from './types'; | ||
export * from './useChainOrder'; | ||
export * from './useChainOrderStore'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* eslint-disable no-underscore-dangle */ | ||
import type { StateCreator } from 'zustand'; | ||
import { create } from 'zustand'; | ||
import { persist } from 'zustand/middleware'; | ||
import type { ChainOrderState } from './types'; | ||
|
||
export const maxChainToOrder = 9; | ||
|
||
export const useChainOrderStore = create<ChainOrderState>( | ||
persist( | ||
(set, get) => ({ | ||
chainOrder: [], | ||
availableChains: [], | ||
initializeChains: (chainIds: number[]) => { | ||
set((state: ChainOrderState) => { | ||
const chainOrder = state.chainOrder.filter((chainId) => | ||
chainIds.includes(chainId), | ||
); | ||
const chainsToAdd = chainIds.filter( | ||
(chainId) => !chainOrder.includes(chainId), | ||
); | ||
if (chainOrder.length === maxChainToOrder || !chainsToAdd.length) { | ||
return { | ||
availableChains: chainIds, | ||
chainOrder, | ||
}; | ||
} | ||
const chainsToAddLength = maxChainToOrder - chainOrder.length; | ||
for (let index = 0; index < chainsToAddLength; index++) { | ||
chainOrder.push(chainsToAdd[index]); | ||
} | ||
return { | ||
availableChains: chainIds, | ||
chainOrder, | ||
}; | ||
}); | ||
return get().chainOrder; | ||
}, | ||
setChain: (chainId: number) => { | ||
const state = get(); | ||
if ( | ||
state.chainOrder.includes(chainId) || | ||
!state.availableChains.includes(chainId) | ||
) { | ||
return; | ||
} | ||
set((state: ChainOrderState) => { | ||
const chainOrder = state.chainOrder.slice(); | ||
chainOrder.unshift(chainId); | ||
if (chainOrder.length > maxChainToOrder) { | ||
chainOrder.pop(); | ||
} | ||
return { | ||
chainOrder, | ||
}; | ||
}); | ||
}, | ||
}), | ||
{ | ||
name: `li.fi-widget-chains-order`, | ||
version: 0, | ||
partialize: (state) => ({ chainOrder: state.chainOrder }), | ||
}, | ||
) as StateCreator<ChainOrderState, [], [], ChainOrderState>, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
packages/widget/src/stores/routes/useRecommendedRouteStore.ts
This file was deleted.
Oops, something went wrong.
60 changes: 60 additions & 0 deletions
60
packages/widget/src/stores/routes/useRecommendedRouteStore.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import type { Route } from '@lifi/sdk'; | ||
import { createContext, useContext, useRef } from 'react'; | ||
import type { StoreApi, UseBoundStore } from 'zustand'; | ||
import { create } from 'zustand'; | ||
import type { PersistStoreProviderProps } from '../types'; | ||
import type { RecommendedRouteState } from './types'; | ||
|
||
export const createRecommendedRouteStore = () => | ||
create<RecommendedRouteState>((set) => ({ | ||
setRecommendedRoute: (recommendedRoute?: Route) => { | ||
set(() => ({ | ||
recommendedRoute, | ||
})); | ||
}, | ||
})); | ||
|
||
export type RecommendedRouteStore = UseBoundStore< | ||
StoreApi<RecommendedRouteState> | ||
>; | ||
|
||
export const RecommendedRouteStoreContext = | ||
createContext<RecommendedRouteStore | null>(null); | ||
|
||
export function RecommendedRouteStoreProvider({ | ||
children, | ||
...props | ||
}: PersistStoreProviderProps) { | ||
const storeRef = useRef<RecommendedRouteStore>(); | ||
if (!storeRef.current) { | ||
storeRef.current = createRecommendedRouteStore(); | ||
} | ||
return ( | ||
<RecommendedRouteStoreContext.Provider value={storeRef.current}> | ||
{children} | ||
</RecommendedRouteStoreContext.Provider> | ||
); | ||
} | ||
|
||
export function useRecommendedRouteStore<T>( | ||
selector: (state: RecommendedRouteState) => T, | ||
equalityFn?: (left: T, right: T) => boolean, | ||
): T { | ||
const useStore = useContext(RecommendedRouteStoreContext); | ||
if (!useStore) { | ||
throw new Error( | ||
`You forgot to wrap your component in <${RecommendedRouteStoreProvider.name}>.`, | ||
); | ||
} | ||
return useStore(selector, equalityFn); | ||
} | ||
|
||
export function useRecommendedRouteStoreContext() { | ||
const useStore = useContext(RecommendedRouteStoreContext); | ||
if (!useStore) { | ||
throw new Error( | ||
`You forgot to wrap your component in <${RecommendedRouteStoreProvider.name}>.`, | ||
); | ||
} | ||
return useStore; | ||
} |
Oops, something went wrong.