import { useLocation } from "react-router"; import { useTranslation } from "react-i18next"; import React from "react"; import posthog from "posthog-js"; import { I18nKey } from "#/i18n/declaration"; import { organizeModelsAndProviders } from "#/utils/organize-models-and-providers"; import { DangerModal } from "../confirmation-modals/danger-modal"; import { extractSettings } from "#/utils/settings-utils"; import { ModalBackdrop } from "../modal-backdrop"; import { ModelSelector } from "./model-selector"; import { Settings } from "#/types/settings"; import { BrandButton } from "#/components/features/settings/brand-button"; import { KeyStatusIcon } from "#/components/features/settings/key-status-icon"; import { SettingsInput } from "#/components/features/settings/settings-input"; import { HelpLink } from "#/components/features/settings/help-link"; import { useSaveSettings } from "#/hooks/mutation/use-save-settings"; interface SettingsFormProps { settings: Settings; models: string[]; onClose: () => void; } export function SettingsForm({ settings, models, onClose }: SettingsFormProps) { const { mutate: saveUserSettings } = useSaveSettings(); const location = useLocation(); const { t } = useTranslation(); const formRef = React.useRef(null); const [confirmEndSessionModalOpen, setConfirmEndSessionModalOpen] = React.useState(false); const handleFormSubmission = async (formData: FormData) => { const newSettings = extractSettings(formData); await saveUserSettings(newSettings, { onSuccess: () => { onClose(); posthog.capture("settings_saved", { LLM_MODEL: newSettings.LLM_MODEL, LLM_API_KEY_SET: newSettings.LLM_API_KEY_SET ? "SET" : "UNSET", SEARCH_API_KEY_SET: newSettings.SEARCH_API_KEY ? "SET" : "UNSET", REMOTE_RUNTIME_RESOURCE_FACTOR: newSettings.REMOTE_RUNTIME_RESOURCE_FACTOR, }); }, }); }; const handleConfirmEndSession = () => { const formData = new FormData(formRef.current ?? undefined); handleFormSubmission(formData); }; const handleSubmit = (event: React.FormEvent) => { event.preventDefault(); const formData = new FormData(event.currentTarget); if (location.pathname.startsWith("/conversations/")) { setConfirmEndSessionModalOpen(true); } else { handleFormSubmission(formData); } }; const isLLMKeySet = settings.LLM_API_KEY_SET; return (
" : ""} startContent={isLLMKeySet && } />
{t(I18nKey.BUTTON$SAVE)}
{confirmEndSessionModalOpen && ( setConfirmEndSessionModalOpen(false), }, }} /> )}
); }