MCPcopy Index your code
hub / github.com/Iterable/react-native-sdk / IterableAppProvider

Function IterableAppProvider

example/src/hooks/useIterableApp.tsx:96–308  ·  view source on GitHub ↗
({ children })

Source from the content-addressed store, hash-verified

94export const IterableAppProvider: FunctionComponent<
95 React.PropsWithChildren<unknown>
96> = ({ children }) => {
97 const [returnToInboxTrigger, setReturnToInboxTrigger] =
98 useState<boolean>(false);
99 const [isInboxTab, setIsInboxTab] = useState<boolean>(false);
100 const [itblConfig, setItblConfig] = useState<IterableConfig | null>(null);
101 const [isLoggedIn, setIsLoggedIn] = useState<boolean>(false);
102 const [isInitialized, setIsInitialized] = useState<boolean>(false);
103 const [apiKey, setApiKey] = useState<string | undefined>(
104 process.env.ITBL_API_KEY
105 );
106 const [userId, setUserId] = useState<string | null>(
107 process.env.ITBL_ID ?? null
108 );
109 const [loginInProgress, setLoginInProgress] = useState<boolean>(false);
110
111 const getUserId = useCallback(() => userId ?? process.env.ITBL_ID, [userId]);
112
113 const getJwtToken = useCallback(async () => {
114 const id = userId ?? process.env.ITBL_ID;
115 const idType = getIsEmail(id as string) ? 'email' : 'userId';
116 const secret = process.env.ITBL_JWT_SECRET ?? '';
117 const duration = 1000 * 60 * 60 * 24; // 1 day in milliseconds
118 const jwtToken = await NativeJwtTokenModule.generateJwtToken(
119 secret,
120 duration,
121 idType === 'email' ? (id as string) : null, // Email (can be null if userId is provided)
122 idType === 'userId' ? (id as string) : null // UserId (can be null if email is provided)
123 );
124
125 return jwtToken;
126 }, [userId]);
127
128 const login = useCallback(async () => {
129 const id = userId ?? process.env.ITBL_ID;
130
131 if (!id) return Promise.reject('No User ID or Email set');
132
133 setLoginInProgress(true);
134
135 const fn = getIsEmail(id) ? Iterable.setEmail : Iterable.setUserId;
136
137 let token;
138
139 if (process.env.ITBL_IS_JWT_ENABLED === 'true' && process.env.ITBL_JWT_SECRET) {
140 token = await getJwtToken();
141 }
142
143 fn(id, token);
144 setIsLoggedIn(true);
145 setLoginInProgress(false);
146
147 return Promise.resolve(true);
148 }, [getJwtToken, userId]);
149
150 const initialize = useCallback(
151 (navigation: Navigation) => {
152 logout();
153

Callers

nothing calls this directly

Calls 8

getIsEmailFunction · 0.85
logMethod · 0.80
errorMethod · 0.80
initializeMethod · 0.80
logoutMethod · 0.80
generateJwtTokenMethod · 0.65
setEmailMethod · 0.65
setUserIdMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…