import { useEffect, useState } from "react"; import { shortcutService } from "../services"; import useLoading from "../hooks/useLoading"; import Icon from "./Icon"; import { generateDialog } from "./Dialog"; import toastHelper from "./Toast"; interface Props extends DialogProps { workspaceId: WorkspaceId; shortcutId?: ShortcutId; } const CreateShortcutDialog: React.FC = (props: Props) => { const { destroy, workspaceId, shortcutId } = props; const [name, setName] = useState(""); const [link, setLink] = useState(""); const requestState = useLoading(false); useEffect(() => { if (shortcutId) { const shortcutTemp = shortcutService.getShortcutById(shortcutId); if (shortcutTemp) { setName(shortcutTemp.name); setLink(shortcutTemp.link); } } }, [shortcutId]); const handleNameInputChange = (e: React.ChangeEvent) => { const text = e.target.value as string; setName(text); }; const handleLinkInputChange = (e: React.ChangeEvent) => { const text = e.target.value as string; setLink(text); }; const handleSaveBtnClick = async () => { if (!name) { toastHelper.error("Name is required"); return; } try { if (shortcutId) { await shortcutService.patchShortcut({ id: shortcutId, name, link, }); } else { await shortcutService.createShortcut({ workspaceId, name, link, visibility: "PRIVATE", }); } } catch (error: any) { console.error(error); toastHelper.error(error.response.data.message); } destroy(); }; return ( <>

{shortcutId ? "Edit Shortcut" : "Create Shortcut"}

); }; export default function showCreateShortcutDialog(workspaceId: WorkspaceId, shortcutId?: ShortcutId): void { generateDialog( { className: "px-2 sm:px-0", }, CreateShortcutDialog, { workspaceId, shortcutId, } ); }