mirror of
https://github.com/hex248/sprint.git
synced 2026-02-07 18:23:03 +00:00
useQuery instead of mutation
This commit is contained in:
@@ -16,7 +16,6 @@ export function Chat({ setHighlighted }: { setHighlighted: (ids: number[]) => vo
|
|||||||
const selectedOrganisation = useSelectedOrganisation();
|
const selectedOrganisation = useSelectedOrganisation();
|
||||||
const selectedProject = useSelectedProject();
|
const selectedProject = useSelectedProject();
|
||||||
const chat = useChat();
|
const chat = useChat();
|
||||||
const models = useModels();
|
|
||||||
|
|
||||||
const [isOpen, setIsOpen] = useState(false);
|
const [isOpen, setIsOpen] = useState(false);
|
||||||
const [message, setMessage] = useState("");
|
const [message, setMessage] = useState("");
|
||||||
@@ -25,11 +24,7 @@ export function Chat({ setHighlighted }: { setHighlighted: (ids: number[]) => vo
|
|||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const [selectedModel, setSelectedModel] = useState<string>("");
|
const [selectedModel, setSelectedModel] = useState<string>("");
|
||||||
|
|
||||||
useEffect(() => {
|
const models = useModels(isOpen);
|
||||||
if (isOpen && !models.data) {
|
|
||||||
models.mutate();
|
|
||||||
}
|
|
||||||
}, [isOpen, models]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (models.data && models.data.length > 0 && !selectedModel) {
|
if (models.data && models.data.length > 0 && !selectedModel) {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import type { ChatRequest, ChatResponse, ModelsResponse } from "@sprint/shared";
|
import type { ChatRequest, ChatResponse, ModelsResponse } from "@sprint/shared";
|
||||||
import { useMutation } from "@tanstack/react-query";
|
import { useMutation, useQuery } from "@tanstack/react-query";
|
||||||
import { apiClient } from "@/lib/server";
|
import { apiClient } from "@/lib/server";
|
||||||
|
|
||||||
export function useChat() {
|
export function useChat() {
|
||||||
@@ -14,14 +14,17 @@ export function useChat() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useModels() {
|
export function useModels(enabled: boolean) {
|
||||||
return useMutation<ModelsResponse, Error>({
|
return useQuery<ModelsResponse, Error>({
|
||||||
mutationKey: ["ai", "models"],
|
queryKey: ["ai", "models"],
|
||||||
mutationFn: async () => {
|
queryFn: async () => {
|
||||||
const { data, error } = await apiClient.aiModels();
|
const { data, error } = await apiClient.aiModels();
|
||||||
if (error) throw new Error(error);
|
if (error) throw new Error(error);
|
||||||
if (!data) throw new Error("failed to get models");
|
if (!data) throw new Error("failed to get models");
|
||||||
return data as ModelsResponse;
|
return data as ModelsResponse;
|
||||||
},
|
},
|
||||||
|
enabled,
|
||||||
|
retry: false,
|
||||||
|
staleTime: 5 * 60 * 1000,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user