Skip to content

Commit

Permalink
Use the aiFlags api to know when to enable the ai chat box.
Browse files Browse the repository at this point in the history
Signed-off-by: Hiram Chirino <[email protected]>
  • Loading branch information
chirino committed Oct 4, 2024
1 parent bc64c03 commit 0402875
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
6 changes: 3 additions & 3 deletions client/src/app/components/ai-assistant.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Input } from "@/components/ui/input";
import { ScrollArea } from "@/components/ui/scroll-area";
import { MessageCircle, Send, X } from "lucide-react";
import { ChatMessage, ChatState } from "../client";
import { useCompletionMutation } from "../queries/ai";
import { useCompletionMutation, useFetchAiFlagsQuery } from "../queries/ai";
import ReactMarkdown from "react-markdown";

interface Conversation {
Expand All @@ -35,8 +35,8 @@ export function AIAssistantProvider({
}: {
children: React.ReactNode;
}) {
// todo: querying the API to see if the feature is enabled.
const [isEnabled] = useState(true);
const aiFlags = useFetchAiFlagsQuery();
const isEnabled = aiFlags.data?.completions || false;

const [isOpen, setIsOpen] = useState(false);
const [chatState, setChatState] = useState({ messages: [] } as ChatState);
Expand Down
15 changes: 13 additions & 2 deletions client/src/app/queries/ai.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useMutation } from "@tanstack/react-query";
import { useMutation, useQuery } from "@tanstack/react-query";
import { AxiosError } from "axios";
import { client } from "@app/axios-config/apiInit";
import { ChatState, completions } from "@app/client";
import { aiFlags, ChatState, completions } from "@app/client";
import { dataOf } from "./dataOf";

export const useCompletionMutation = (
onError?: (err: AxiosError, next: ChatState) => void,
Expand All @@ -16,3 +17,13 @@ export const useCompletionMutation = (
onError,
});
};

export const useFetchAiFlagsQuery = () => {
return useQuery({
queryKey: ["ai/flags"],
queryFn: () => {
return dataOf(aiFlags({ client }));
},
refetchInterval: false,
});
};

0 comments on commit 0402875

Please sign in to comment.