This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(context): move StoreServiceProvider inside walletkit-ui (#31)
* feature(context): move StoreServiceProvider inside walletkit-ui * feature(context): update storeservice provider
- Loading branch information
1 parent
4b88b6a
commit a860074
Showing
4 changed files
with
150 additions
and
1 deletion.
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
114 changes: 114 additions & 0 deletions
114
packages/walletkit-ui/src/contexts/StoreServiceProvider.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,114 @@ | ||
import { | ||
EnvironmentNetwork, | ||
getDefaultDefiChainURL, | ||
} from "@waveshq/walletkit-core"; | ||
import React, { | ||
createContext, | ||
PropsWithChildren, | ||
useContext, | ||
useEffect, | ||
useMemo, | ||
useState, | ||
} from "react"; | ||
|
||
import { BaseLogger } from "./logger"; | ||
import { useNetworkContext } from "./NetworkContext"; | ||
|
||
interface ServiceProviderContextProps { | ||
api: { | ||
get: () => Promise<string | undefined>; | ||
set: (url: NonNullable<string>) => Promise<void>; | ||
}; | ||
logger: BaseLogger; | ||
} | ||
|
||
interface ServiceProviderURLProps extends ServiceProviderContextProps { | ||
network: EnvironmentNetwork; | ||
} | ||
|
||
interface ServiceProviderLoader { | ||
isUrlLoaded: boolean; | ||
url: NonNullable<string>; | ||
} | ||
|
||
function useServiceProviderUrl({ | ||
api, | ||
network, | ||
logger, | ||
}: ServiceProviderURLProps): ServiceProviderLoader { | ||
const [isUrlLoaded, setIsUrlLoaded] = useState<boolean>(false); | ||
const defaultDefichainURL = getDefaultDefiChainURL(network); | ||
const [url, setUrl] = useState<NonNullable<string>>(defaultDefichainURL); | ||
|
||
useEffect(() => { | ||
api | ||
.get() | ||
.then((val) => { | ||
setUrl(val !== undefined ? val : defaultDefichainURL); | ||
}) | ||
.catch((err) => logger.error(err)) | ||
.finally(() => setIsUrlLoaded(true)); | ||
}, [url, network]); | ||
|
||
return { | ||
isUrlLoaded, | ||
url, | ||
}; | ||
} | ||
|
||
interface ServiceProviderContextI { | ||
url: NonNullable<string>; | ||
defaultUrl: string; | ||
isCustomUrl: boolean; | ||
setUrl: (val: NonNullable<string>) => Promise<void>; | ||
} | ||
|
||
const ServiceProviderContext = createContext<ServiceProviderContextI>( | ||
undefined as any | ||
); | ||
|
||
export function useServiceProviderContext(): ServiceProviderContextI { | ||
return useContext(ServiceProviderContext); | ||
} | ||
|
||
export function StoreServiceProvider( | ||
props: ServiceProviderContextProps & PropsWithChildren<any> | ||
): JSX.Element | null { | ||
const { api, children, logger } = props; | ||
const { network } = useNetworkContext(); | ||
const { url } = useServiceProviderUrl({ | ||
api, | ||
network, | ||
logger, | ||
}); | ||
const defaultUrl = getDefaultDefiChainURL(network); | ||
const [currentUrl, setCurrentUrl] = useState<string>(url); | ||
|
||
useEffect(() => { | ||
setCurrentUrl(url); | ||
}, [url]); | ||
|
||
const isCustomUrl = useMemo( | ||
() => currentUrl !== defaultUrl, | ||
[currentUrl, defaultUrl] | ||
); | ||
|
||
const setUrl = async (newUrl: string): Promise<void> => { | ||
setCurrentUrl(newUrl); | ||
await api.set(newUrl); | ||
}; | ||
|
||
// eslint-disable-next-line react/jsx-no-constructed-context-values | ||
const context: ServiceProviderContextI = { | ||
url: currentUrl === undefined ? defaultUrl : currentUrl, | ||
isCustomUrl, | ||
defaultUrl, | ||
setUrl, | ||
}; | ||
|
||
return ( | ||
<ServiceProviderContext.Provider value={context}> | ||
{children} | ||
</ServiceProviderContext.Provider> | ||
); | ||
} |
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 "./NetworkContext"; | ||
export * from "./StoreServiceProvider"; | ||
export * from "./ThemeProvider"; | ||
export * from "./WhaleContext"; |