import { Input, Textarea, Button } from "@usememos/mui"; import { XIcon } from "lucide-react"; import React, { useState } from "react"; import { toast } from "react-hot-toast"; import { shortcutServiceClient } from "@/grpcweb"; import useCurrentUser from "@/hooks/useCurrentUser"; import useLoading from "@/hooks/useLoading"; import { userStore } from "@/store/v2"; import { Shortcut } from "@/types/proto/api/v1/shortcut_service"; import { useTranslate } from "@/utils/i18n"; import { generateDialog } from "./Dialog"; interface Props extends DialogProps { shortcut?: Shortcut; } const CreateShortcutDialog: React.FC = (props: Props) => { const { destroy } = props; const t = useTranslate(); const user = useCurrentUser(); const [shortcut, setShortcut] = useState({ name: props.shortcut?.name || "", title: props.shortcut?.title || "", filter: props.shortcut?.filter || "", }); const requestState = useLoading(false); const isCreating = !props.shortcut; const onShortcutTitleChange = (e: React.ChangeEvent) => { setShortcut({ ...shortcut, title: e.target.value }); }; const onShortcutFilterChange = (e: React.ChangeEvent) => { setShortcut({ ...shortcut, filter: e.target.value }); }; const handleConfirm = async () => { if (!shortcut.title || !shortcut.filter) { toast.error("Title and filter cannot be empty"); return; } try { if (isCreating) { await shortcutServiceClient.createShortcut({ parent: user.name, shortcut: { name: "", // Will be set by server title: shortcut.title, filter: shortcut.filter, }, }); toast.success("Create shortcut successfully"); } else { await shortcutServiceClient.updateShortcut({ shortcut: { ...shortcut, name: props.shortcut!.name, // Keep the original resource name }, updateMask: ["title", "filter"], }); toast.success("Update shortcut successfully"); } // Refresh shortcuts. await userStore.fetchShortcuts(); destroy(); } catch (error: any) { console.error(error); toast.error(error.details); } }; return (

{`${isCreating ? t("common.create") : t("common.edit")} ${t("common.shortcuts")}`}

{t("common.title")} {t("common.filter")}