"use client"; import { useObjectState } from "@/hooks/use-object-state"; import { UserPreferences } from "app-types/user"; import { authClient } from "auth/client"; import { fetcher } from "lib/utils"; import { AlertCircle, ArrowLeft, LinkIcon, Loader, Share2, Trash2, } from "lucide-react"; import { useTranslations } from "next-intl"; import { useMemo, useState } from "react"; import { toast } from "sonner"; import useSWR from "swr"; import { safe } from "ts-safe"; import { Button } from "ui/button"; import { ExamplePlaceholder } from "ui/example-placeholder"; import { Input } from "ui/input"; import { Label } from "ui/label"; import { Skeleton } from "ui/skeleton"; import { Textarea } from "ui/textarea"; import { McpServerCustomizationContent } from "./mcp-customization-popup"; import { MCPServerInfo } from "app-types/mcp"; import { useMcpList } from "@/hooks/queries/use-mcp-list"; import { ChatExportSummary } from "app-types/chat-export"; import { formatDistanceToNow } from "date-fns"; import { notify } from "lib/notify"; export function UserInstructionsContent() { const t = useTranslations(); const responseStyleExamples = useMemo( () => [ t("Chat.ChatPreferences.responseStyleExample1"), t("Chat.ChatPreferences.responseStyleExample2"), t("Chat.ChatPreferences.responseStyleExample3"), t("Chat.ChatPreferences.responseStyleExample4"), ], [], ); const professionExamples = useMemo( () => [ t("Chat.ChatPreferences.professionExample1"), t("Chat.ChatPreferences.professionExample2"), t("Chat.ChatPreferences.professionExample3"), t("Chat.ChatPreferences.professionExample4"), t("Chat.ChatPreferences.professionExample5"), ], [], ); const { data: session } = authClient.useSession(); const [preferences, setPreferences] = useObjectState({ displayName: "", responseStyleExample: "", profession: "", botName: "", }); const { data, mutate: fetchPreferences, isLoading, isValidating, } = useSWR("/api/user/preferences", fetcher, { fallback: {}, dedupingInterval: 0, onSuccess: (data) => { setPreferences(data); }, }); const [isSaving, setIsSaving] = useState(false); const savePreferences = async () => { safe(() => setIsSaving(true)) .ifOk(() => fetch("/api/user/preferences", { method: "PUT", body: JSON.stringify(preferences), }), ) .ifOk(() => fetchPreferences()) .watch((result) => { if (result.isOk) toast.success(t("Chat.ChatPreferences.preferencesSaved")); else toast.error(t("Chat.ChatPreferences.failedToSavePreferences")); }) .watch(() => setIsSaving(false)); }; const isDiff = useMemo(() => { if ((data?.displayName || "") !== (preferences.displayName || "")) return true; if ((data?.profession || "") !== (preferences.profession || "")) return true; if ( (data?.responseStyleExample || "") !== (preferences.responseStyleExample || "") ) return true; if ((data?.botName || "") !== (preferences.botName || "")) return true; return false; }, [preferences, data]); return (

{t("Chat.ChatPreferences.userInstructions")}

{t("Chat.ChatPreferences.userInstructionsDescription")}

{isLoading ? ( ) : ( { setPreferences({ displayName: e.target.value, }); }} /> )}
{isLoading ? ( ) : ( { setPreferences({ botName: e.target.value, }); }} /> )}
{isLoading ? ( ) : ( <> { setPreferences({ profession: e.target.value, }); }} /> {(preferences.profession?.length ?? 0) === 0 && (
)} )}
{isLoading ? ( ) : ( <>