diff --git a/web/src/components/CreateShortcutDialog.tsx b/web/src/components/CreateShortcutDialog.tsx new file mode 100644 index 0000000000000..32a079b6965bb --- /dev/null +++ b/web/src/components/CreateShortcutDialog.tsx @@ -0,0 +1,135 @@ +import { Input, Textarea } from "@mui/joy"; +import { Button } from "@usememos/mui"; +import { XIcon } from "lucide-react"; +import React, { useState } from "react"; +import { toast } from "react-hot-toast"; +import { userServiceClient } from "@/grpcweb"; +import useCurrentUser from "@/hooks/useCurrentUser"; +import useLoading from "@/hooks/useLoading"; +import { useUserStore } from "@/store/v1"; +import { Shortcut } from "@/types/proto/api/v1/user_service"; +import { useTranslate } from "@/utils/i18n"; +import { generateUUID } from "@/utils/uuid"; +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 userStore = useUserStore(); + const [shortcut, setShortcut] = useState(Shortcut.fromPartial({ ...props.shortcut })); + 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 userServiceClient.createShortcut({ + parent: user.name, + shortcut: { + ...shortcut, + id: generateUUID(), + }, + }); + toast.success("Create shortcut successfully"); + } else { + await userServiceClient.updateShortcut({ parent: user.name, shortcut, 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 ? "Create" : "Edit"} Shortcut`}

+ +
+
+
+ Title + + Filter +