Skip to content

Commit 05ba9de

Browse files
committed
feat: add cancelled var to avoid race conditions in fetch records
1 parent 25903b0 commit 05ba9de

1 file changed

Lines changed: 7 additions & 13 deletions

File tree

apps/web/src/hooks/useInstrumentVisualization.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,7 @@ export function useInstrumentVisualization({ params }: UseInstrumentVisualizatio
6363
}
6464
});
6565

66-
// Create a new sessionsUsernameQuery which uses the useFindSessionQuery hook
67-
// have use a different return type with
68-
69-
const userInfo = async (userId: string): Promise<null | User> => {
70-
try {
71-
const response = await axios.get(`/v1/users/${userId}`);
72-
return response.data ? (response.data as User) : null;
73-
} catch (error) {
74-
console.error('Error fetching user:', error);
75-
return null; // ensures a resolved value instead of `void`
76-
}
77-
};
66+
7867

7968
const dl = (option: 'CSV' | 'CSV Long' | 'Excel' | 'Excel Long' | 'JSON' | 'TSV' | 'TSV Long') => {
8069
if (!instrument) {
@@ -250,7 +239,9 @@ export function useInstrumentVisualization({ params }: UseInstrumentVisualizatio
250239
};
251240
});
252241

253-
setRecords(records);
242+
if (!cancelled) {
243+
setRecords(records);
244+
}
254245
}
255246
} catch (error) {
256247
console.error('Error occurred: ', error);
@@ -264,6 +255,9 @@ export function useInstrumentVisualization({ params }: UseInstrumentVisualizatio
264255
}
265256
};
266257
void fetchRecords();
258+
return () => {
259+
cancelled = true;
260+
};
267261
}, [recordsQuery.data]);
268262

269263
const instrumentOptions: { [key: string]: string } = useMemo(() => {

0 commit comments

Comments
 (0)