Skip to content

Commit

Permalink
fix(ai-help): handle invalid chat ids correctly (#11678)
Browse files Browse the repository at this point in the history
* first rough attempt at a fix

* a bit of refactoring, handle history disabled case

* use reacty way for modifying url

* use setSearchParams

* lint

* use useCallback

---------

Co-authored-by: Florian Dieminger <me@fiji-flo.de>
  • Loading branch information
argl and fiji-flo authored Sep 10, 2024
1 parent 6dc8934 commit 225fe24
Showing 1 changed file with 29 additions and 3 deletions.
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 @@ -408,8 +408,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 @@ -452,12 +464,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 @@ -467,7 +486,14 @@ export function useAiChat({
if (r) {
reset();
}
}, [isHistoryEnabled, searchParams, chatId, reset, handleError]);
}, [
isHistoryEnabled,
removeChatIdFromUrl,
searchParams,
chatId,
reset,
handleError,
]);

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

0 comments on commit 225fe24

Please sign in to comment.