-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: chain order for multiple widget instances
- Loading branch information
Showing
6 changed files
with
124 additions
and
69 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { createContext, useContext, useRef } from 'react'; | ||
import type { StoreApi, UseBoundStore } from 'zustand'; | ||
import type { PersistStoreProviderProps } from '../types'; | ||
import { createChainOrderStore } from './createChainOrderStore'; | ||
import type { ChainOrderState } from './types'; | ||
|
||
export type ChainOrderStore = UseBoundStore<StoreApi<ChainOrderState>>; | ||
|
||
export const ChainOrderStoreContext = createContext<ChainOrderStore | null>( | ||
null, | ||
); | ||
|
||
export function ChainOrderStoreProvider({ | ||
children, | ||
...props | ||
}: PersistStoreProviderProps) { | ||
const storeRef = useRef<ChainOrderStore>(); | ||
if (!storeRef.current) { | ||
storeRef.current = createChainOrderStore(props); | ||
} | ||
return ( | ||
<ChainOrderStoreContext.Provider value={storeRef.current}> | ||
{children} | ||
</ChainOrderStoreContext.Provider> | ||
); | ||
} | ||
|
||
export function useChainOrderStore<T>( | ||
selector: (state: ChainOrderState) => T, | ||
equalityFn?: (left: T, right: T) => boolean, | ||
): T { | ||
const useStore = useContext(ChainOrderStoreContext); | ||
if (!useStore) { | ||
throw new Error( | ||
`You forgot to wrap your component in <${ChainOrderStoreProvider.name}>.`, | ||
); | ||
} | ||
return useStore(selector, equalityFn); | ||
} | ||
|
||
export function useChainOrderStoreContext() { | ||
const useStore = useContext(ChainOrderStoreContext); | ||
if (!useStore) { | ||
throw new Error( | ||
`You forgot to wrap your component in <${ChainOrderStoreProvider.name}>.`, | ||
); | ||
} | ||
return useStore; | ||
} |
66 changes: 66 additions & 0 deletions
66
packages/widget/src/stores/chains/createChainOrderStore.ts
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,66 @@ | ||
import type { StateCreator } from 'zustand'; | ||
import { create } from 'zustand'; | ||
import { persist } from 'zustand/middleware'; | ||
import type { PersistStoreProps } from '../types'; | ||
import type { ChainOrderState } from './types'; | ||
|
||
export const maxChainToOrder = 9; | ||
|
||
export const createChainOrderStore = ({ namePrefix }: PersistStoreProps) => | ||
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: `${namePrefix || '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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
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 was deleted.
Oops, something went wrong.