import { Button, Input, Modal, ModalDialog, Radio, RadioGroup } from "@mui/joy"; import { isUndefined } from "lodash-es"; import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { toast } from "react-hot-toast"; import { shortcutService } from "../services"; import useLoading from "../hooks/useLoading"; import Icon from "./Icon"; interface Props { shortcutId?: ShortcutId; onClose: () => void; onConfirm?: () => void; } interface State { shortcutCreate: ShortcutCreate; } const visibilities: Visibility[] = ["PRIVATE", "WORKSPACE", "PUBLIC"]; const CreateShortcutDialog: React.FC = (props: Props) => { const { onClose, onConfirm, shortcutId } = props; const { t } = useTranslation(); const [state, setState] = useState({ shortcutCreate: { name: "", link: "", description: "", visibility: "PRIVATE", tags: [], }, }); const [tag, setTag] = useState(""); const requestState = useLoading(false); const isCreating = isUndefined(shortcutId); useEffect(() => { if (shortcutId) { const shortcutTemp = shortcutService.getShortcutById(shortcutId); if (shortcutTemp) { setState({ ...state, shortcutCreate: Object.assign(state.shortcutCreate, { name: shortcutTemp.name, link: shortcutTemp.link, description: shortcutTemp.description, visibility: shortcutTemp.visibility, }), }); setTag(shortcutTemp.tags.join(" ")); } } }, [shortcutId]); const setPartialState = (partialState: Partial) => { setState({ ...state, ...partialState, }); }; const handleNameInputChange = (e: React.ChangeEvent) => { setPartialState({ shortcutCreate: Object.assign(state.shortcutCreate, { name: e.target.value.replace(/\s+/g, "-").toLowerCase(), }), }); }; const handleLinkInputChange = (e: React.ChangeEvent) => { setPartialState({ shortcutCreate: Object.assign(state.shortcutCreate, { link: e.target.value, }), }); }; const handleDescriptionInputChange = (e: React.ChangeEvent) => { setPartialState({ shortcutCreate: Object.assign(state.shortcutCreate, { description: e.target.value, }), }); }; const handleTagsInputChange = (e: React.ChangeEvent) => { const text = e.target.value as string; setTag(text); }; const handleVisibilityInputChange = (e: React.ChangeEvent) => { setPartialState({ shortcutCreate: Object.assign(state.shortcutCreate, { visibility: e.target.value, }), }); }; const handleSaveBtnClick = async () => { if (!state.shortcutCreate.name) { toast.error("Name is required"); return; } try { if (shortcutId) { await shortcutService.patchShortcut({ id: shortcutId, name: state.shortcutCreate.name, link: state.shortcutCreate.link, description: state.shortcutCreate.description, visibility: state.shortcutCreate.visibility, tags: tag.split(" "), }); } else { await shortcutService.createShortcut({ ...state.shortcutCreate, tags: tag.split(" "), }); } if (onConfirm) { onConfirm(); } else { onClose(); } } catch (error: any) { console.error(error); toast.error(error.response.data.message); } }; return (
{isCreating ? "Create Shortcut" : "Edit Shortcut"}
Name *
Link *
Description
Tags
Visibility *
{visibilities.map((visibility) => ( ))}

{t(`shortcut.visibility.${state.shortcutCreate.visibility.toLowerCase()}.description`)}

); }; export default CreateShortcutDialog;