Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ai-help): handle invalid chat ids correctly #11678

Merged
merged 6 commits into from
Sep 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions client/src/plus/ai-help/use-ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,20 @@ export function useAiChat({
});
}, [setSearchParams, chatId]);

const removeChatIdFromUrl = useCallback(() => {
setSearchParams(
(prev) => {
prev.delete("c");
return prev;
},
{ replace: true }
);
}, [setSearchParams]);

useEffect(() => {
if (!isHistoryEnabled) {
// If we got a chat id passed in without history enabled, clear parameters from URL
removeChatIdFromUrl();
return;
}
let timeoutID;
Expand Down Expand Up @@ -451,12 +463,19 @@ export function useAiChat({
}
} catch (e) {
setPreviousChatId(undefined);
setChatId(convId);
setPath([]);
dispatchState({
type: "reset",
});
handleError(e);
// If we got a 404 from the API, reset and remove the parameter from the URL,
// do not show an error.
if (e instanceof Error && e.message.startsWith("404")) {
setChatId(undefined);
removeChatIdFromUrl();
} else {
setChatId(convId);
handleError(e);
}
}
setIsHistoryLoading(false);
};
Expand All @@ -466,7 +485,14 @@ export function useAiChat({
if (r) {
reset();
}
}, [isHistoryEnabled, searchParams, chatId, reset, handleError]);
}, [
isHistoryEnabled,
removeChatIdFromUrl,
searchParams,
chatId,
reset,
handleError,
]);

useEffect(() => {
if (remoteQuota !== undefined) {
Expand Down