-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '474-refactor-the-token-map-hook-to-enhance-efficiency' …
…into 'dev' Resolve "Refactor the token map hook to enhance efficiency" Closes #474 See merge request ergo/rosen-bridge/ui!395
- Loading branch information
Showing
4 changed files
with
64 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@rosen-bridge/rosen-app': patch | ||
--- | ||
|
||
Refactor the token map hook to enhance efficiency |
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.
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,55 @@ | ||
import { | ||
ReactNode, | ||
createContext, | ||
useContext, | ||
useEffect, | ||
useState, | ||
useTransition, | ||
} from 'react'; | ||
|
||
import { TokenMap } from '@rosen-bridge/tokens'; | ||
|
||
import { getTokenMap } from '@/_tokenMap/getClientTokenMap'; | ||
|
||
/** | ||
* return TokenMap instance | ||
*/ | ||
export const useTokenMap = () => { | ||
const context = useContext(TokenMapContext); | ||
|
||
if (!context) { | ||
throw new Error('useTokenMap must be used within TokenMapProvider'); | ||
} | ||
|
||
return context; | ||
}; | ||
|
||
export type TokenMapContextType = TokenMap; | ||
|
||
export const TokenMapContext = createContext<TokenMap | null>(null); | ||
|
||
export const TokenMapProvider = ({ children }: { children: ReactNode }) => { | ||
const [tokenMap, setTokenMap] = useState( | ||
new TokenMap({ idKeys: {}, tokens: [] }), | ||
); | ||
|
||
const [, startTransition] = useTransition(); | ||
|
||
useEffect(() => { | ||
startTransition(async () => { | ||
try { | ||
setTokenMap(await getTokenMap()); | ||
} catch (error) { | ||
throw new Error('Failed to fetch TokenMap, reload the page.', { | ||
cause: error, | ||
}); | ||
} | ||
}); | ||
}, []); | ||
|
||
return ( | ||
<TokenMapContext.Provider value={tokenMap}> | ||
{children} | ||
</TokenMapContext.Provider> | ||
); | ||
}; |