-
Notifications
You must be signed in to change notification settings - Fork 3
574 send nft wrapper #3611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
574 send nft wrapper #3611
Changes from all commits
8831c9e
fe604f5
30488e4
16f07b8
b944d84
5233390
327461e
7f1dcd7
ab1448f
dad759f
91ad1b9
507126b
0148ae9
31df23f
beb9343
55efd0c
28531c0
2a8569c
91670c9
ed1dad7
367a2b8
684b164
7c1eb0d
9680a36
db804c1
a8978c1
870e019
d8dede5
69b0de0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,6 @@ | ||||||||||||||||||||||||||||||||||||||||||||||
| import React, { PropsWithChildren, useCallback, useContext, useMemo, useState } from "react" | ||||||||||||||||||||||||||||||||||||||||||||||
| import { EntryAnimationsValues, ExitAnimationsValues, LayoutAnimation, useSharedValue } from "react-native-reanimated" | ||||||||||||||||||||||||||||||||||||||||||||||
| import { FungibleTokenWithBalance } from "~Model" | ||||||||||||||||||||||||||||||||||||||||||||||
| import { FungibleTokenWithBalance, NonFungibleToken } from "~Model" | ||||||||||||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||||||||||||
| EnteringFromLeftAnimation, | ||||||||||||||||||||||||||||||||||||||||||||||
| EnteringFromRightAnimation, | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -9,7 +9,8 @@ import { ExitingToLeftAnimation, ExitingToRightAnimation } from "~Screens/Flows/ | |||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| export type SendFlowStep = "insertAddress" | "selectAmount" | "summary" | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| type SendFlowState = { | ||||||||||||||||||||||||||||||||||||||||||||||
| export type SendFlowState = { | ||||||||||||||||||||||||||||||||||||||||||||||
| type: "token" | ||||||||||||||||||||||||||||||||||||||||||||||
| token?: FungibleTokenWithBalance | ||||||||||||||||||||||||||||||||||||||||||||||
| amount?: string | ||||||||||||||||||||||||||||||||||||||||||||||
| fiatAmount?: string | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -22,17 +23,23 @@ type SendFlowState = { | |||||||||||||||||||||||||||||||||||||||||||||
| amountInFiat?: boolean | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| type SendContextType = { | ||||||||||||||||||||||||||||||||||||||||||||||
| flowState: SendFlowState | ||||||||||||||||||||||||||||||||||||||||||||||
| setFlowState: React.Dispatch<React.SetStateAction<SendFlowState>> | ||||||||||||||||||||||||||||||||||||||||||||||
| export type SendNFTFlowState = { | ||||||||||||||||||||||||||||||||||||||||||||||
| type: "nft" | ||||||||||||||||||||||||||||||||||||||||||||||
| nft: NonFungibleToken | ||||||||||||||||||||||||||||||||||||||||||||||
| address?: string | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| export type SendContextType<T = SendFlowState | SendNFTFlowState> = { | ||||||||||||||||||||||||||||||||||||||||||||||
| flowState: T | ||||||||||||||||||||||||||||||||||||||||||||||
| setFlowState: React.Dispatch<React.SetStateAction<T>> | ||||||||||||||||||||||||||||||||||||||||||||||
| step: SendFlowStep | ||||||||||||||||||||||||||||||||||||||||||||||
| goToNext: () => void | ||||||||||||||||||||||||||||||||||||||||||||||
| goToPrevious: () => void | ||||||||||||||||||||||||||||||||||||||||||||||
| EnteringAnimation: (values: EntryAnimationsValues) => LayoutAnimation | ||||||||||||||||||||||||||||||||||||||||||||||
| ExitingAnimation: (values: ExitAnimationsValues) => LayoutAnimation | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| const SendContext = React.createContext<SendContextType | undefined>(undefined) | ||||||||||||||||||||||||||||||||||||||||||||||
| export const SendContext = React.createContext<SendContextType<any> | undefined>(undefined) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| type SendContextProviderProps = PropsWithChildren<{ | ||||||||||||||||||||||||||||||||||||||||||||||
| initialToken?: FungibleTokenWithBalance | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -43,6 +50,7 @@ const ORDER: SendFlowStep[] = ["selectAmount", "insertAddress", "summary"] | |||||||||||||||||||||||||||||||||||||||||||||
| export const SendContextProvider = ({ children, initialToken }: SendContextProviderProps) => { | ||||||||||||||||||||||||||||||||||||||||||||||
| const [step, setStep] = useState<SendFlowStep>("selectAmount") | ||||||||||||||||||||||||||||||||||||||||||||||
| const [flowState, setFlowState] = useState<SendFlowState>({ | ||||||||||||||||||||||||||||||||||||||||||||||
| type: "token", | ||||||||||||||||||||||||||||||||||||||||||||||
| token: initialToken, | ||||||||||||||||||||||||||||||||||||||||||||||
| amount: "0", | ||||||||||||||||||||||||||||||||||||||||||||||
| fiatAmount: "", | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -111,7 +119,7 @@ export const SendContextProvider = ({ children, initialToken }: SendContextProvi | |||||||||||||||||||||||||||||||||||||||||||||
| [nextStep.value, previousStep.value], | ||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| const contextValue: SendContextType = useMemo( | ||||||||||||||||||||||||||||||||||||||||||||||
| const contextValue: SendContextType<SendFlowState> = useMemo( | ||||||||||||||||||||||||||||||||||||||||||||||
| () => ({ | ||||||||||||||||||||||||||||||||||||||||||||||
| flowState, | ||||||||||||||||||||||||||||||||||||||||||||||
| setFlowState, | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -132,5 +140,13 @@ export const useSendContext = () => { | |||||||||||||||||||||||||||||||||||||||||||||
| if (!context) { | ||||||||||||||||||||||||||||||||||||||||||||||
| throw new Error("useSendContext must be used within a SendContextProvider") | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| return context | ||||||||||||||||||||||||||||||||||||||||||||||
| return context as SendContextType<SendFlowState | SendNFTFlowState> | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| export const useTokenSendContext = () => { | ||||||||||||||||||||||||||||||||||||||||||||||
| return useSendContext() as SendContextType<SendFlowState> | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| export const useNFTSendContext = () => { | ||||||||||||||||||||||||||||||||||||||||||||||
| return useSendContext() as SendContextType<SendNFTFlowState> | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+146
to
152
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unsafe type assertions in specialized hooks may cause runtime errors.
Consider adding runtime checks: export const useTokenSendContext = () => {
- return useSendContext() as SendContextType<SendFlowState>
+ const context = useSendContext()
+ if (context.flowState.type !== "token") {
+ throw new Error("useTokenSendContext must be used within a token Send flow")
+ }
+ return context as SendContextType<SendFlowState>
}
export const useNFTSendContext = () => {
- return useSendContext() as SendContextType<SendNFTFlowState>
+ const context = useSendContext()
+ if (context.flowState.type !== "nft") {
+ throw new Error("useNFTSendContext must be used within an NFT Send flow")
+ }
+ return context as SendContextType<SendNFTFlowState>
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add explicit type assertion to prevent false positives.
The conditional wrapper allows the test to pass silently if
flowState.type !== "token", creating a false positive where important assertions are skipped.Apply this diff to add an explicit type assertion:
+ expect(result.current.flowState.type).toBe("token") if (result.current.flowState.type === "token") { expect(result.current.flowState.token).not.toBeUndefined() expect(result.current.flowState.token?.symbol).toEqual(VETWithBalance.symbol) expect(result.current.flowState.amount).toBe("1") expect(result.current.flowState.address).toBe("0x1234567890123456789012345678901234567890") }📝 Committable suggestion
🤖 Prompt for AI Agents