Skip to content

Commit 6354dfd

Browse files
fix(qa): guard locale param and cancel inflight fetch
1 parent f44bf20 commit 6354dfd

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

frontend/components/q&a/useQaTabs.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ export function useQaTabs() {
3232
const searchParams = useSearchParams();
3333
const params = useParams();
3434

35-
const locale = params.locale as string;
35+
const locale =
36+
typeof params.locale === 'string' ? params.locale : params.locale?.[0] ?? '';
3637
const localeKey = resolveLocale(locale);
3738

3839
const rawPage = searchParams.get('page');
@@ -92,6 +93,8 @@ export function useQaTabs() {
9293
}, [searchQuery, active, updateUrl]);
9394

9495
useEffect(() => {
96+
const controller = new AbortController();
97+
9598
async function load() {
9699
setIsLoading(true);
97100

@@ -101,7 +104,8 @@ export function useQaTabs() {
101104
: '';
102105

103106
const res = await fetch(
104-
`/api/questions/${active}?page=${currentPage}&limit=10&locale=${localeKey}${searchParam}`
107+
`/api/questions/${active}?page=${currentPage}&limit=10&locale=${localeKey}${searchParam}`,
108+
{ signal: controller.signal }
105109
);
106110

107111
if (!res.ok) {
@@ -129,6 +133,9 @@ export function useQaTabs() {
129133
}
130134

131135
load();
136+
return () => {
137+
controller.abort();
138+
};
132139
}, [active, currentPage, debouncedSearch, localeKey]);
133140

134141
const handleCategoryChange = useCallback(

0 commit comments

Comments
 (0)