mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-07-12 22:42:13 +02:00
refactor: migrate setup-llm-step to jotai + tanstack query pattern
- Replace imperative useLLMConfigs hook with jotai atoms - Use createLLMConfigMutationAtom for config creation - Use deleteLLMConfigMutationAtom for config deletion - Use llmConfigsAtom for fetching configs - Update types from CreateLLMConfig to CreateLLMConfigRequest - Add proper error handling with try-catch - Fix TypeScript issues with is_global property check - Maintain backward compatibility with existing callbacks
This commit is contained in:
parent
5b7e5770be
commit
63b0bb2472
1 changed files with 12 additions and 9 deletions
|
|
@ -44,9 +44,7 @@ import { LANGUAGES } from "@/contracts/enums/languages";
|
||||||
import { getModelsByProvider } from "@/contracts/enums/llm-models";
|
import { getModelsByProvider } from "@/contracts/enums/llm-models";
|
||||||
import { LLM_PROVIDERS } from "@/contracts/enums/llm-providers";
|
import { LLM_PROVIDERS } from "@/contracts/enums/llm-providers";
|
||||||
import {
|
import {
|
||||||
type CreateLLMConfig,
|
|
||||||
useGlobalLLMConfigs,
|
useGlobalLLMConfigs,
|
||||||
useLLMConfigs,
|
|
||||||
useLLMPreferences,
|
useLLMPreferences,
|
||||||
} from "@/hooks/use-llm-configs";
|
} from "@/hooks/use-llm-configs";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
|
|
@ -54,7 +52,9 @@ import { cn } from "@/lib/utils";
|
||||||
import InferenceParamsEditor from "../inference-params-editor";
|
import InferenceParamsEditor from "../inference-params-editor";
|
||||||
import { useAtomValue } from "jotai";
|
import { useAtomValue } from "jotai";
|
||||||
import { createLLMConfigMutationAtom } from "@/atoms/llm-config/llm-config-mutation.atoms";
|
import { createLLMConfigMutationAtom } from "@/atoms/llm-config/llm-config-mutation.atoms";
|
||||||
import { CreateLLMConfigRequest } from "@/contracts/types/llm-config.types";
|
import { deleteLLMConfigMutationAtom } from "@/atoms/llm-config/llm-config-mutation.atoms";
|
||||||
|
import { llmConfigsAtom } from "@/atoms/llm-config/llm-config-query.atoms";
|
||||||
|
import { CreateLLMConfigRequest, LLMConfig } from "@/contracts/types/llm-config.types";
|
||||||
|
|
||||||
interface SetupLLMStepProps {
|
interface SetupLLMStepProps {
|
||||||
searchSpaceId: number;
|
searchSpaceId: number;
|
||||||
|
|
@ -99,9 +99,10 @@ export function SetupLLMStep({
|
||||||
onConfigDeleted,
|
onConfigDeleted,
|
||||||
onPreferencesUpdated,
|
onPreferencesUpdated,
|
||||||
}: SetupLLMStepProps) {
|
}: SetupLLMStepProps) {
|
||||||
|
const { mutateAsync : createLLMConfig, isPending : isCreatingLlmConfig } = useAtomValue(createLLMConfigMutationAtom);
|
||||||
const t = useTranslations("onboard");
|
const t = useTranslations("onboard");
|
||||||
const { llmConfigs, deleteLLMConfig } = useLLMConfigs(searchSpaceId);
|
const { mutateAsync : deleteLLMConfig, isPending : isDeletingLlmConfig } = useAtomValue(deleteLLMConfigMutationAtom);
|
||||||
const { mutateAsync : createLLMConfig, isPending : isCreatingLlmConfig } = useAtomValue(createLLMConfigMutationAtom)
|
const { data : llmConfigs = [], isFetching : isFetchingLlmConfigs, refetch : refreshConfigs } = useAtomValue(llmConfigsAtom);
|
||||||
const { globalConfigs } = useGlobalLLMConfigs();
|
const { globalConfigs } = useGlobalLLMConfigs();
|
||||||
const { preferences, updatePreferences } = useLLMPreferences(searchSpaceId);
|
const { preferences, updatePreferences } = useLLMPreferences(searchSpaceId);
|
||||||
|
|
||||||
|
|
@ -138,7 +139,7 @@ export function SetupLLMStep({
|
||||||
});
|
});
|
||||||
}, [preferences]);
|
}, [preferences]);
|
||||||
|
|
||||||
const handleInputChange = (field: keyof CreateLLMConfig, value: string) => {
|
const handleInputChange = (field: keyof CreateLLMConfigRequest, value: string) => {
|
||||||
setFormData((prev) => ({ ...prev, [field]: value }));
|
setFormData((prev) => ({ ...prev, [field]: value }));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -323,9 +324,11 @@ export function SetupLLMStep({
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
size="sm"
|
size="sm"
|
||||||
onClick={async () => {
|
onClick={async () => {
|
||||||
const success = await deleteLLMConfig(config.id);
|
try {
|
||||||
if (success) {
|
await deleteLLMConfig({ id: config.id });
|
||||||
onConfigDeleted?.();
|
onConfigDeleted?.();
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to delete config:', error);
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
className="text-destructive hover:text-destructive"
|
className="text-destructive hover:text-destructive"
|
||||||
|
|
@ -731,7 +734,7 @@ export function SetupLLMStep({
|
||||||
<div className="flex items-center gap-2 text-sm">
|
<div className="flex items-center gap-2 text-sm">
|
||||||
<Bot className="w-4 h-4" />
|
<Bot className="w-4 h-4" />
|
||||||
<span className="font-medium">{t("assigned")}:</span>
|
<span className="font-medium">{t("assigned")}:</span>
|
||||||
{assignedConfig.is_global && (
|
{"is_global" in assignedConfig && assignedConfig.is_global && (
|
||||||
<Badge variant="secondary" className="text-xs">
|
<Badge variant="secondary" className="text-xs">
|
||||||
🌐 Global
|
🌐 Global
|
||||||
</Badge>
|
</Badge>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue