-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement WidgetProvider & useWidget for accessing specific wid…
…get data
- Loading branch information
1 parent
52fcd52
commit 65f1824
Showing
19 changed files
with
225 additions
and
149 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 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,18 @@ | ||
import { styled } from '@rango-dev/ui'; | ||
|
||
export const MainContainer = styled('div', { | ||
width: '100%', | ||
display: 'flex', | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
fontFamily: '$widget', | ||
boxSizing: 'border-box', | ||
'& *, *::before, *::after': { | ||
boxSizing: 'inherit', | ||
listStyleType: 'none', | ||
}, | ||
'& *:focus-visible': { | ||
outlineColor: '$info500', | ||
transition: 'none', | ||
}, | ||
}); |
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 @@ | ||
import type { WalletType } from '@rango-dev/wallets-shared'; | ||
|
||
import { I18nManager } from '@rango-dev/ui'; | ||
import React, { useContext, useEffect, useMemo, useState } from 'react'; | ||
|
||
import { AppRouter } from '../../components/AppRouter'; | ||
import { AppRoutes } from '../../components/AppRoutes'; | ||
import { WidgetEvents } from '../../components/WidgetEvents'; | ||
import { globalFont } from '../../globalStyles'; | ||
import { useLanguage } from '../../hooks/useLanguage'; | ||
import { useTheme } from '../../hooks/useTheme'; | ||
import { useAppStore } from '../../store/AppStore'; | ||
import { useNotificationStore } from '../../store/notification'; | ||
import { useSettingsStore } from '../../store/settings'; | ||
import { initConfig } from '../../utils/configs'; | ||
import { WidgetContext } from '../Wallets'; | ||
|
||
import { MainContainer } from './App.styles'; | ||
|
||
export function Main() { | ||
globalFont(); | ||
|
||
const { fetch: fetchMeta, config } = useAppStore(); | ||
const { activeTheme } = useTheme(config?.theme || {}); | ||
const { activeLanguage, changeLanguage } = useLanguage(); | ||
const [lastConnectedWalletWithNetwork, setLastConnectedWalletWithNetwork] = | ||
useState<string>(''); | ||
const [disconnectedWallet, setDisconnectedWallet] = useState<WalletType>(); | ||
const widgetContext = useContext(WidgetContext); | ||
|
||
useMemo(() => { | ||
if (config?.apiKey) { | ||
initConfig({ | ||
API_KEY: config?.apiKey, | ||
}); | ||
} | ||
}, [config]); | ||
useEffect(() => { | ||
void fetchMeta().catch(); | ||
void useSettingsStore.persist.rehydrate(); | ||
void useNotificationStore.persist.rehydrate(); | ||
widgetContext.onConnectWallet(setLastConnectedWalletWithNetwork); | ||
}, []); | ||
useEffect(() => { | ||
if (config?.language) { | ||
changeLanguage(config.language); | ||
} | ||
}, [config?.language]); | ||
|
||
return ( | ||
<I18nManager language={activeLanguage}> | ||
<MainContainer id="swap-container" className={activeTheme()}> | ||
<WidgetEvents /> | ||
<AppRouter | ||
lastConnectedWallet={lastConnectedWalletWithNetwork} | ||
disconnectedWallet={disconnectedWallet} | ||
clearDisconnectedWallet={() => { | ||
setDisconnectedWallet(undefined); | ||
}}> | ||
<AppRoutes /> | ||
</AppRouter> | ||
</MainContainer> | ||
</I18nManager> | ||
); | ||
} |
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 @@ | ||
export { Main } from './App'; |
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,12 @@ | ||
import type { WidgetConfig } from '../../types'; | ||
import type { EventHandler } from '@rango-dev/wallets-react'; | ||
|
||
export type OnConnectHandler = (key: string) => void; | ||
export interface WidgetContextInterface { | ||
onConnectWallet(handler: OnConnectHandler): void; | ||
} | ||
|
||
export interface PropTypes { | ||
onUpdateState?: EventHandler; | ||
config: WidgetConfig; | ||
} |
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 @@ | ||
export { WidgetWallets, WidgetContext } from './Wallets'; |
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,19 @@ | ||
import type { WidgetProps } from './Widget.types'; | ||
import type { PropsWithChildren } from 'react'; | ||
|
||
import React from 'react'; | ||
|
||
import { DEFAULT_CONFIG } from '../../store/slices/config'; | ||
import { Main } from '../App'; | ||
import { WidgetProvider } from '../WidgetProvider'; | ||
|
||
export function Widget(props: PropsWithChildren<WidgetProps>) { | ||
if (!props.config?.externalWallets) { | ||
return ( | ||
<WidgetProvider config={props.config ?? DEFAULT_CONFIG}> | ||
<Main /> | ||
</WidgetProvider> | ||
); | ||
} | ||
return <Main />; | ||
} |
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 @@ | ||
import type { WidgetConfig } from '../../types'; | ||
|
||
export type WidgetProps = { | ||
config?: WidgetConfig; | ||
}; |
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,2 @@ | ||
export { Widget } from './Widget'; | ||
export type { WidgetProps } from './Widget.types'; |
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,36 @@ | ||
import type { WidgetInfoContextInterface } from './WidgetInfo.types'; | ||
|
||
import { useManager } from '@rango-dev/queue-manager-react'; | ||
import React, { createContext, useContext, useMemo } from 'react'; | ||
|
||
import { getPendingSwaps } from '../../utils/queue'; | ||
|
||
export const WidgetInfoContext = createContext< | ||
WidgetInfoContextInterface | undefined | ||
>(undefined); | ||
|
||
export function WidgetInfo(props: React.PropsWithChildren) { | ||
const { manager } = useManager(); | ||
const swaps = getPendingSwaps(manager); | ||
const value = useMemo(() => { | ||
return { | ||
swaps, | ||
}; | ||
}, [manager, swaps]); | ||
|
||
return ( | ||
<WidgetInfoContext.Provider value={value}> | ||
{props.children} | ||
</WidgetInfoContext.Provider> | ||
); | ||
} | ||
|
||
export function useWidget(): WidgetInfoContextInterface { | ||
const context = useContext(WidgetInfoContext); | ||
if (!context) { | ||
throw new Error( | ||
'useWidget can only be used within the WidgetProvider component' | ||
); | ||
} | ||
return context; | ||
} |
5 changes: 5 additions & 0 deletions
5
widget/embedded/src/containers/WidgetInfo/WidgetInfo.types.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,5 @@ | ||
import type { PendingSwapWithQueueID } from '@rango-dev/queue-manager-rango-preset'; | ||
|
||
export interface WidgetInfoContextInterface { | ||
swaps?: PendingSwapWithQueueID[]; | ||
} |
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 @@ | ||
export { WidgetInfo, useWidget } from './WidgetInfo'; |
19 changes: 19 additions & 0 deletions
19
widget/embedded/src/containers/WidgetProvider/WidgetProvider.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,19 @@ | ||
import type { PropTypes } from './WidgetProvider.types'; | ||
import type { PropsWithChildren } from 'react'; | ||
|
||
import React from 'react'; | ||
|
||
import QueueManager from '../../QueueManager'; | ||
import { WidgetWallets } from '../Wallets'; | ||
import { WidgetInfo } from '../WidgetInfo'; | ||
|
||
export function WidgetProvider(props: PropsWithChildren<PropTypes>) { | ||
const { onUpdateState, config } = props; | ||
return ( | ||
<WidgetWallets config={config} onUpdateState={onUpdateState}> | ||
<QueueManager apiKey={config.apiKey}> | ||
<WidgetInfo>{props.children}</WidgetInfo> | ||
</QueueManager> | ||
</WidgetWallets> | ||
); | ||
} |
Oops, something went wrong.