Skip to content

Commit df09dad

Browse files
authored
Fix useEffect dependencies in ChefAuthWrapper (#440)
Making the two following fixes to ChefAuthWrapper to ensure the dependencies for `useEffect` are correct: - Adding `convex` to the dependencies array, which should be OK since `useConvex` is expected to always return the same object - Changing `hasAltertedAboutOptIns` from `useEffect` to `useRef`, which avoid the need for an additional useEffect dependency and an extra rerender - Modifying the code getting and setting the session ID in local storage to avoid an extra rerender (+ extra runs of `useEffect`)
1 parent ce26a22 commit df09dad

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

app/components/chat/ChefAuthWrapper.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useConvex } from 'convex/react';
22

33
import { useConvexAuth } from 'convex/react';
4-
import { createContext, useContext, useEffect, useState } from 'react';
4+
import { createContext, useContext, useEffect, useRef } from 'react';
55

66
import { sessionIdStore } from '~/lib/stores/sessionId';
77

@@ -60,7 +60,7 @@ export const ChefAuthProvider = ({
6060
SESSION_ID_KEY,
6161
null,
6262
);
63-
const [hasAlertedAboutOptIns, setHasAlertedAboutOptIns] = useState(false);
63+
const hasAlertedAboutOptIns = useRef(false);
6464
const { getAccessTokenSilently } = useAuth0();
6565

6666
useEffect(() => {
@@ -113,11 +113,11 @@ export const ChefAuthProvider = ({
113113
if (optIns.kind === 'loaded' && optIns.optIns.length === 0) {
114114
setSessionId(sessionIdFromLocalStorage as Id<'sessions'>);
115115
}
116-
if (!hasAlertedAboutOptIns && optIns.kind === 'loaded' && optIns.optIns.length > 0) {
116+
if (!hasAlertedAboutOptIns.current && optIns.kind === 'loaded' && optIns.optIns.length > 0) {
117117
toast.info('Please accept the Convex Terms of Service to continue');
118-
setHasAlertedAboutOptIns(true);
118+
hasAlertedAboutOptIns.current = true;
119119
}
120-
if (hasAlertedAboutOptIns && optIns.kind === 'error') {
120+
if (hasAlertedAboutOptIns.current && optIns.kind === 'error') {
121121
toast.error('Unexpected error setting up your account.');
122122
}
123123
} else {
@@ -139,6 +139,7 @@ export const ChefAuthProvider = ({
139139
}
140140
void verifySession();
141141
}, [
142+
convex,
142143
sessionId,
143144
isAuthenticated,
144145
isConvexAuthLoading,

0 commit comments

Comments
 (0)