Skip to content

Commit

Permalink
feat: add some other logics to useWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
samobasquiat committed Dec 3, 2023
1 parent 4fdc2f1 commit 082db78
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
20 changes: 19 additions & 1 deletion widget/embedded/src/containers/WidgetInfo/WidgetInfo.helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
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,
Expand All @@ -11,9 +13,14 @@ import { getPendingSwaps } from '../../utils/queue';

export class WidgetHistory {
private manager: Manager | undefined;
private retrySwap: (pendingSwap: PendingSwap, meta: Meta) => void;

constructor(manager: Manager | undefined) {
constructor(
manager: Manager | undefined,
retrySwap: (pendingSwap: PendingSwap, meta: Meta) => void
) {
this.manager = manager;
this.retrySwap = retrySwap;
}

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

public retry(swap: PendingSwap, meta: Meta) {
return this.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
5 changes: 4 additions & 1 deletion widget/embedded/src/containers/WidgetInfo/WidgetInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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 @@ -15,7 +16,8 @@ export const WidgetInfoContext = createContext<

export function WidgetInfo(props: React.PropsWithChildren) {
const { manager } = useManager();
const history = new WidgetHistory(manager);
const retry = useQuoteStore.use.retry();
const history = new WidgetHistory(manager, retry);
const details = useWalletsStore.use.connectedWallets();
const isLoading = useWalletsStore.use.loading();
const totalBalance = calculateWalletUsdValue(details);
Expand All @@ -24,6 +26,7 @@ export function WidgetInfo(props: React.PropsWithChildren) {
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 Down

0 comments on commit 082db78

Please sign in to comment.