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 3 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
29 changes: 26 additions & 3 deletions client/src/plus/ai-help/use-ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// License: Apache 2.0 - https://github.com/supabase/supabase/blob/0f1254252f6b066e088a40617f239744e3a1e22b/LICENSE
import type { OpenAI } from "openai";
import { useCallback, useEffect, useReducer, useRef, useState } from "react";
import { useSearchParams } from "react-router-dom";
import { useNavigate, useSearchParams } from "react-router-dom";

import { SSE } from "sse.js";
import useSWR, { mutate } from "swr";
Expand Down Expand Up @@ -341,6 +341,7 @@
export function useAiChat({
messageTemplate = (message) => message,
}: UseAiChatOptions = {}) {
const navigate = useNavigate();
const gleanClick = useGleanClick();
const eventSourceRef = useRef<SSE>();

Expand Down Expand Up @@ -407,8 +408,23 @@
});
}, [setSearchParams, chatId]);

const removeChatIdFromUrl = () => {
const searchParams = new URLSearchParams(window.location.search);

searchParams.delete("c");

navigate(
{
search: searchParams.toString(),
},
{ replace: true }
);
};

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 +467,19 @@
}
} 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 +489,7 @@
if (r) {
reset();
}
}, [isHistoryEnabled, searchParams, chatId, reset, handleError]);

Check warning on line 492 in client/src/plus/ai-help/use-ai.ts

View workflow job for this annotation

GitHub Actions / build

React Hook useEffect has a missing dependency: 'removeChatIdFromUrl'. Either include it or remove the dependency array

Check warning on line 492 in client/src/plus/ai-help/use-ai.ts

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has a missing dependency: 'removeChatIdFromUrl'. Either include it or remove the dependency array

Check warning on line 492 in client/src/plus/ai-help/use-ai.ts

View workflow job for this annotation

GitHub Actions / build

React Hook useEffect has a missing dependency: 'removeChatIdFromUrl'. Either include it or remove the dependency array

Check warning on line 492 in client/src/plus/ai-help/use-ai.ts

View workflow job for this annotation

GitHub Actions / lighthouse

React Hook useEffect has a missing dependency: 'removeChatIdFromUrl'. Either include it or remove the dependency array

Check warning on line 492 in client/src/plus/ai-help/use-ai.ts

View workflow job for this annotation

GitHub Actions / test

React Hook useEffect has a missing dependency: 'removeChatIdFromUrl'. Either include it or remove the dependency array

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