import { create } from "@bufbuild/protobuf"; import { useState } from "react"; import { toast } from "react-hot-toast"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { useInstance } from "@/contexts/InstanceContext"; import { buildInstanceSettingName } from "@/helpers/resource-names"; import { handleError } from "@/lib/error"; import { InstanceSetting_GeneralSetting_CustomProfile, InstanceSetting_GeneralSetting_CustomProfileSchema, InstanceSetting_Key, InstanceSettingSchema, } from "@/types/proto/api/v1/instance_service_pb"; import { useTranslate } from "@/utils/i18n"; interface Props { open: boolean; onOpenChange: (open: boolean) => void; onSuccess?: () => void; } function UpdateCustomizedProfileDialog({ open, onOpenChange, onSuccess }: Props) { const t = useTranslate(); const { generalSetting: instanceGeneralSetting, updateSetting } = useInstance(); const [customProfile, setCustomProfile] = useState( create(InstanceSetting_GeneralSetting_CustomProfileSchema, instanceGeneralSetting.customProfile || {}), ); const [isLoading, setIsLoading] = useState(false); const setPartialState = (partialState: Partial) => { setCustomProfile((state) => ({ ...state, ...partialState, })); }; const handleNameChanged = (e: React.ChangeEvent) => { setPartialState({ title: e.target.value as string, }); }; const handleLogoUrlChanged = (e: React.ChangeEvent) => { setPartialState({ logoUrl: e.target.value as string, }); }; const handleDescriptionChanged = (e: React.ChangeEvent) => { setPartialState({ description: e.target.value as string, }); }; const handleRestoreButtonClick = () => { setPartialState({ title: "阿年的备忘录", logoUrl: "/logo.webp", description: "", }); }; const handleCloseButtonClick = () => { onOpenChange(false); }; const handleSaveButtonClick = async () => { if (customProfile.title === "") { toast.error("Title cannot be empty."); return; } setIsLoading(true); try { await updateSetting( create(InstanceSettingSchema, { name: buildInstanceSettingName(InstanceSetting_Key.GENERAL), value: { case: "generalSetting", value: { ...instanceGeneralSetting, customProfile: customProfile, }, }, }), ); toast.success(t("message.update-succeed")); onSuccess?.(); onOpenChange(false); } catch (error) { handleError(error, toast.error, { context: "Update customized profile", fallbackMessage: "Failed to update profile", }); } finally { setIsLoading(false); } }; return ( {t("setting.system.customize-server.title")} Customize your instance appearance and settings.