-
Notifications
You must be signed in to change notification settings - Fork 491
Expand file tree
/
Copy pathSendFlowTrackingContext.tsx
More file actions
75 lines (65 loc) · 2.11 KB
/
Copy pathSendFlowTrackingContext.tsx
File metadata and controls
75 lines (65 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import React, {
createContext,
type ReactNode,
useCallback,
useContext,
useMemo,
useState,
} from "react";
import type {
RecipientInputMethod,
RecipientResultType,
RecipientType,
} from "../utils/contactTracking";
type SendFlowTrackingState = Readonly<{
inputMethod: RecipientInputMethod;
resultType: RecipientResultType | null;
recipientType: RecipientType | null;
savedContactDuringFlow: boolean;
}>;
type SendFlowTrackingContextValue = SendFlowTrackingState &
Readonly<{
setInputMethod: (inputMethod: RecipientInputMethod) => void;
setRecipientResolution: (resultType: RecipientResultType, recipientType: RecipientType) => void;
markContactSaved: () => void;
}>;
const SendFlowTrackingContext = createContext<SendFlowTrackingContextValue | null>(null);
export function SendFlowTrackingProvider({ children }: Readonly<{ children: ReactNode }>) {
const [state, setState] = useState<SendFlowTrackingState>({
inputMethod: "manual",
resultType: null,
recipientType: null,
savedContactDuringFlow: false,
});
const setInputMethod = useCallback((inputMethod: RecipientInputMethod) => {
setState(previous => ({ ...previous, inputMethod }));
}, []);
const setRecipientResolution = useCallback(
(resultType: RecipientResultType, recipientType: RecipientType) => {
setState(previous => ({ ...previous, resultType, recipientType }));
},
[],
);
const markContactSaved = useCallback(() => {
setState(previous => ({ ...previous, savedContactDuringFlow: true }));
}, []);
const value = useMemo(
() => ({
...state,
setInputMethod,
setRecipientResolution,
markContactSaved,
}),
[markContactSaved, setInputMethod, setRecipientResolution, state],
);
return (
<SendFlowTrackingContext.Provider value={value}>{children}</SendFlowTrackingContext.Provider>
);
}
export function useSendFlowTracking(): SendFlowTrackingContextValue {
const context = useContext(SendFlowTrackingContext);
if (!context) {
throw new Error("useSendFlowTracking must be used within a SendFlowTrackingProvider");
}
return context;
}