import { create } from "@bufbuild/protobuf"; import { FieldMaskSchema } from "@bufbuild/protobuf/wkt"; import { useEffect, useState } from "react"; import { toast } from "react-hot-toast"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, 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 { shortcutServiceClient } from "@/connect"; import { useAuth } from "@/contexts/AuthContext"; import useCurrentUser from "@/hooks/useCurrentUser"; import useLoading from "@/hooks/useLoading"; import { handleError } from "@/lib/error"; import { Shortcut, ShortcutSchema } from "@/types/proto/api/v1/shortcut_service_pb"; import { useTranslate } from "@/utils/i18n"; interface Props { open: boolean; onOpenChange: (open: boolean) => void; shortcut?: Shortcut; onSuccess?: () => void; } function CreateShortcutDialog({ open, onOpenChange, shortcut: initialShortcut, onSuccess }: Props) { const t = useTranslate(); const user = useCurrentUser(); const { refetchSettings } = useAuth(); const [shortcut, setShortcut] = useState( create(ShortcutSchema, { name: initialShortcut?.name || "", title: initialShortcut?.title || "", filter: initialShortcut?.filter || "", }), ); const requestState = useLoading(false); const isCreating = shortcut.name === ""; useEffect(() => { if (shortcut.name) { setShortcut(shortcut); } }, [shortcut.name, shortcut.title, shortcut.filter]); const onShortcutTitleChange = (e: React.ChangeEvent) => { setPartialState({ title: e.target.value, }); }; const onShortcutFilterChange = (e: React.ChangeEvent) => { setPartialState({ filter: e.target.value, }); }; const setPartialState = (partialState: Partial) => { setShortcut({ ...shortcut, ...partialState, }); }; const handleSaveBtnClick = async () => { if (!shortcut.title || !shortcut.filter) { toast.error("Title and filter cannot be empty"); return; } try { requestState.setLoading(); if (isCreating) { await shortcutServiceClient.createShortcut({ parent: user?.name, shortcut: { name: "", title: shortcut.title, filter: shortcut.filter, }, }); toast.success("Create shortcut successfully"); } else { await shortcutServiceClient.updateShortcut({ shortcut: { ...shortcut, name: initialShortcut!.name, }, updateMask: create(FieldMaskSchema, { paths: ["title", "filter"] }), }); toast.success("Update shortcut successfully"); } await refetchSettings(); requestState.setFinish(); onSuccess?.(); onOpenChange(false); } catch (error: unknown) { await handleError(error, toast.error, { context: isCreating ? "Create shortcut" : "Update shortcut", onError: () => requestState.setError(), }); } }; return ( {`${isCreating ? t("common.create") : t("common.edit")} ${t("common.shortcuts")}`}