Skip to content

Commit

Permalink
create transfer provider
Browse files Browse the repository at this point in the history
  • Loading branch information
JayJay1024 committed Oct 11, 2023
1 parent 4f169ff commit 5cffc19
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
4 changes: 2 additions & 2 deletions packages/apps/src/components/transfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import SwitchCross from "./switch-cross";
import { from, Subscription } from "rxjs";
import { useToggle } from "@/hooks/use-toggle";
import TransferModal from "./transfer-modal";
import { useApp } from "@/hooks/use-app";
import TransferAction from "./transfer-action";
import DisclaimerModal from "./disclaimer-modal";
import { Token } from "@/types/token";
import Faucet from "./faucet";
import { isProduction } from "@/utils/env";
import { useTransfer } from "@/hooks/use-transfer";

const {
defaultSourceOptions,
Expand All @@ -34,7 +34,7 @@ const {
} = getParsedCrossChain();

export default function Transfer() {
const { transferValue, setTransferValue } = useApp();
const { transferValue, setTransferValue } = useTransfer();
const deferredTransferValue = useDeferredValue(transferValue);

const [isOpen, _, setIsOpenTrue, setIsOpenFalse] = useToggle(false);
Expand Down
4 changes: 4 additions & 0 deletions packages/apps/src/hooks/use-transfer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { TransferContext } from "@/providers/transfer-provider";
import { useContext } from "react";

export const useTransfer = () => useContext(TransferContext);
18 changes: 1 addition & 17 deletions packages/apps/src/providers/app-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,21 @@
"use client";

import { TransferValue } from "@/components/transfer-input";
import { Dispatch, PropsWithChildren, SetStateAction, createContext, useState } from "react";

interface AppCtx {
recordsSearch: string;
transferValue: TransferValue;

setRecordsSearch: Dispatch<SetStateAction<string>>;
setTransferValue: Dispatch<SetStateAction<TransferValue>>;
}

const defaultValue: AppCtx = {
recordsSearch: "",
transferValue: { value: "", formatted: 0n },

setRecordsSearch: () => undefined,
setTransferValue: () => undefined,
};

export const AppContext = createContext(defaultValue);

export default function AppProvider({ children }: PropsWithChildren<unknown>) {
const [recordsSearch, setRecordsSearch] = useState(defaultValue.recordsSearch);

/**
* Why we define here: https://react.dev/reference/react/useDeferredValue#caveats
*/
const [transferValue, setTransferValue] = useState(defaultValue.transferValue);

return (
<AppContext.Provider value={{ recordsSearch, transferValue, setRecordsSearch, setTransferValue }}>
{children}
</AppContext.Provider>
);
return <AppContext.Provider value={{ recordsSearch, setRecordsSearch }}>{children}</AppContext.Provider>;
}
25 changes: 25 additions & 0 deletions packages/apps/src/providers/transfer-provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use client";

import { TransferValue } from "@/components/transfer-input";
import { Dispatch, PropsWithChildren, SetStateAction, createContext, useState } from "react";

interface TransferCtx {
transferValue: TransferValue;
setTransferValue: Dispatch<SetStateAction<TransferValue>>;
}

const defaultValue: TransferCtx = {
transferValue: { value: "", formatted: 0n },
setTransferValue: () => undefined,
};

export const TransferContext = createContext(defaultValue);

export default function TransferProvider({ children }: PropsWithChildren<unknown>) {
/**
* Why we define here: https://react.dev/reference/react/useDeferredValue#caveats
*/
const [transferValue, setTransferValue] = useState(defaultValue.transferValue);

return <TransferContext.Provider value={{ transferValue, setTransferValue }}>{children}</TransferContext.Provider>;
}

0 comments on commit 5cffc19

Please sign in to comment.