From 795bf25486fa50f3e6c9a2d37ae1f3f01d272af8 Mon Sep 17 00:00:00 2001 From: ElRodrigote Date: Mon, 4 Nov 2024 20:09:48 +0100 Subject: [PATCH] chore: update Stackbox form context types for values coming from next-usequerystate --- packages/app/contexts/StackboxFormContext.tsx | 51 +++++++++++++++---- 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/packages/app/contexts/StackboxFormContext.tsx b/packages/app/contexts/StackboxFormContext.tsx index 623c3e33..106b3982 100644 --- a/packages/app/contexts/StackboxFormContext.tsx +++ b/packages/app/contexts/StackboxFormContext.tsx @@ -5,6 +5,7 @@ import { ReactNode, createContext, useContext, useMemo } from "react"; import { ChainId } from "@stackly/sdk"; import { createParser, + Options, parseAsString, parseAsStringEnum, parseAsTimestamp, @@ -50,14 +51,29 @@ const throwStackboxFormContextError = () => { throw new Error("No StackboxFormContext available"); }; +type StackboxFormStateInput = [ + T, + ( + value: T | ((old: T) => T | null) | null, + options?: Options | undefined + ) => Promise +]; + interface StackboxFormContextProps { resetFormValues: (newChainId: ChainId) => void; - stackboxFormState: any; + stackboxFormState: { + fromTokenState: StackboxFormStateInput; + toTokenState: StackboxFormStateInput; + tokenAmountState: StackboxFormStateInput; + frequencyState: StackboxFormStateInput; + startDateState: StackboxFormStateInput; + endDateState: StackboxFormStateInput; + }; } const StackboxFormContext = createContext({ resetFormValues: throwStackboxFormContextError, - stackboxFormState: null, + stackboxFormState: null as any, }); interface StackboxFormContextProviderProps { @@ -108,7 +124,6 @@ export const StackboxFormContextProvider = ({ "endDate", parseAsTimestamp.withDefault(new Date(endDateByFrequency[frequency])) ); - const stackboxFormContext = useMemo(() => { const resetFormValues = (newChainId: ChainId) => { const validChainId = checkIsValidChainId(newChainId) @@ -125,12 +140,30 @@ export const StackboxFormContextProvider = ({ }; const stackboxFormState = { - fromTokenState: [fromToken, setFromToken], - toTokenState: [toToken, setToToken], - tokenAmountState: [tokenAmount, setTokenAmount], - frequencyState: [frequency, setFrequency], - startDateState: [startDateTime, setStartDateTime], - endDateState: [endDateTime, setEndDateTime], + fromTokenState: [ + fromToken, + setFromToken, + ] as StackboxFormStateInput, + toTokenState: [ + toToken, + setToToken, + ] as StackboxFormStateInput, + tokenAmountState: [ + tokenAmount, + setTokenAmount, + ] as StackboxFormStateInput, + frequencyState: [ + frequency, + setFrequency, + ] as StackboxFormStateInput, + startDateState: [ + startDateTime, + setStartDateTime, + ] as StackboxFormStateInput, + endDateState: [ + endDateTime, + setEndDateTime, + ] as StackboxFormStateInput, }; return {