Skip to content

Commit

Permalink
feat: export meta and additional logics from useWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
samobasquiat authored and yeager-eren committed Dec 5, 2023
1 parent 47a9969 commit 5c8fbc5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
3 changes: 1 addition & 2 deletions widget/embedded/src/containers/App/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { MainContainer } from './App.styles';
export function Main() {
globalFont();

const { fetch: fetchMeta, config } = useAppStore();
const { config } = useAppStore();
const { activeTheme } = useTheme(config?.theme || {});
const { activeLanguage, changeLanguage } = useLanguage();
const [lastConnectedWalletWithNetwork, setLastConnectedWalletWithNetwork] =
Expand All @@ -27,7 +27,6 @@ export function Main() {
const widgetContext = useContext(WidgetContext);

useEffect(() => {
void fetchMeta().catch();
void useNotificationStore.persist.rehydrate();
widgetContext.onConnectWallet(setLastConnectedWalletWithNetwork);
}, []);
Expand Down
6 changes: 5 additions & 1 deletion widget/embedded/src/containers/Wallets/Wallets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const WidgetContext = createContext<WidgetContextInterface>({
});

function Main(props: PropsWithChildren<PropTypes>) {
const { updateConfig, updateSettings } = useAppStore();
const { updateConfig, updateSettings, fetch: fetchMeta } = useAppStore();
const blockchains = useAppStore().blockchains();
const tokens = useAppStore().tokens();
const walletOptions: ProvidersOptions = {
Expand All @@ -39,6 +39,10 @@ function Main(props: PropsWithChildren<PropTypes>) {
const onConnectWalletHandler = useRef<OnConnectHandler>();
useSyncStoresWithConfig();

useEffect(() => {
void fetchMeta().catch(console.log);
}, []);

useEffect(() => {
if (props.config) {
updateConfig(props.config);
Expand Down
21 changes: 20 additions & 1 deletion widget/embedded/src/containers/WidgetInfo/WidgetInfo.helpers.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import type { Meta } from '../../store/quote';
import type { Manager } from '@rango-dev/queue-manager-core';
import type { PendingSwap } from '@rango-dev/queue-manager-rango-preset';

import {
cancelSwap,
getCurrentBlockchainOfOrNull,
getCurrentStep,
getRelatedWalletOrNull,
} from '@rango-dev/queue-manager-rango-preset';

import { getPendingSwaps } from '../../utils/queue';

interface WidgetHistoryActions {
retrySwap: (pendingSwap: PendingSwap, meta: Meta) => void;
}

export class WidgetHistory {
private manager: Manager | undefined;
private actions: WidgetHistoryActions;

constructor(manager: Manager | undefined) {
constructor(manager: Manager | undefined, actions: WidgetHistoryActions) {
this.manager = manager;
this.actions = actions;
}

public getAllSwaps() {
Expand All @@ -32,6 +40,17 @@ export class WidgetHistory {
return this.getCurrentStepInfo(swap).network;
}

public retry(swap: PendingSwap, meta: Meta) {
return this.actions.retrySwap(swap, meta);
}

public cancel(id: string) {
const queue = this.manager?.get(id);
if (queue) {
cancelSwap(queue);
}
}

private getCurrentStepInfo(swap: PendingSwap) {
const currentStep = getCurrentStep(swap);
return {
Expand Down
16 changes: 15 additions & 1 deletion widget/embedded/src/containers/WidgetInfo/WidgetInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type { WidgetInfoContextInterface } from './WidgetInfo.types';
import { useManager } from '@rango-dev/queue-manager-react';
import React, { createContext, useContext } from 'react';

import { useAppStore } from '../../store/AppStore';
import { useQuoteStore } from '../../store/quote';
import { useWalletsStore } from '../../store/wallets';
import { calculateWalletUsdValue } from '../../utils/wallets';

Expand All @@ -14,11 +16,17 @@ export const WidgetInfoContext = createContext<

export function WidgetInfo(props: React.PropsWithChildren) {
const { manager } = useManager();
const history = new WidgetHistory(manager);
const retrySwap = useQuoteStore.use.retry();
const history = new WidgetHistory(manager, { retrySwap });
const details = useWalletsStore.use.connectedWallets();
const isLoading = useWalletsStore.use.loading();
const totalBalance = calculateWalletUsdValue(details);
const refetch = useWalletsStore.use.getWalletsDetails();
const blockchains = useAppStore().blockchains();
const tokens = useAppStore().tokens();
const swappers = useAppStore().swappers();
const loadingStatus = useAppStore().fetchStatus;

// eslint-disable-next-line react/jsx-no-constructed-context-values
const value = {
history,
Expand All @@ -28,6 +36,12 @@ export function WidgetInfo(props: React.PropsWithChildren) {
totalBalance,
refetch,
},
meta: {
blockchains,
tokens,
swappers,
loadingStatus,
},
};

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { WidgetHistory } from './WidgetInfo.helpers';
import type { FetchStatus } from '../../store/slices/data';
import type { ConnectedWallet } from '../../store/wallets';
import type { Wallet } from '../../types';
import type { Token } from 'rango-sdk';
import type { BlockchainMeta, SwapperMeta, Token } from 'rango-sdk';

export interface WidgetInfoContextInterface {
history: WidgetHistory;
Expand All @@ -11,4 +12,10 @@ export interface WidgetInfoContextInterface {
isLoading: boolean;
refetch: (accounts: Wallet[], tokens: Token[]) => void;
};
meta: {
blockchains: BlockchainMeta[];
tokens: Token[];
swappers: SwapperMeta[];
loadingStatus: FetchStatus;
};
}

0 comments on commit 5c8fbc5

Please sign in to comment.