import { Button, Divider, Input, Modal, ModalDialog, Radio, RadioGroup, Textarea } from "@mui/joy"; import classnames from "classnames"; import { isUndefined, uniq } from "lodash-es"; import { useEffect, useState } from "react"; import { toast } from "react-hot-toast"; import { useTranslation } from "react-i18next"; import { useAppSelector } from "@/stores"; import useLoading from "../hooks/useLoading"; import { shortcutService } from "../services"; import Icon from "./Icon"; interface Props { shortcutId?: ShortcutId; initialShortcut?: Partial; onClose: () => void; onConfirm?: () => void; } interface State { shortcutCreate: ShortcutCreate; } const visibilities: Visibility[] = ["PRIVATE", "WORKSPACE", "PUBLIC"]; const CreateShortcutDialog: React.FC = (props: Props) => { const { onClose, onConfirm, shortcutId, initialShortcut } = props; const { t } = useTranslation(); const { shortcutList } = useAppSelector((state) => state.shortcut); const [state, setState] = useState({ shortcutCreate: { name: "", link: "", title: "", description: "", visibility: "PRIVATE", tags: [], openGraphMetadata: { title: "", description: "", image: "", }, ...initialShortcut, }, }); const [showAdditionalFields, setShowAdditionalFields] = useState(false); const [showOpenGraphMetadata, setShowOpenGraphMetadata] = useState(false); const [tag, setTag] = useState(""); const tagSuggestions = uniq(shortcutList.map((shortcut) => shortcut.tags).flat()); const requestState = useLoading(false); const isCreating = isUndefined(shortcutId); useEffect(() => { if (shortcutId) { const shortcut = shortcutService.getShortcutById(shortcutId); if (shortcut) { setState({ ...state, shortcutCreate: Object.assign(state.shortcutCreate, { name: shortcut.name, link: shortcut.link, title: shortcut.title, description: shortcut.description, visibility: shortcut.visibility, openGraphMetadata: shortcut.openGraphMetadata, }), }); setTag(shortcut.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, "-"), }), }); }; const handleLinkInputChange = (e: React.ChangeEvent) => { setPartialState({ shortcutCreate: Object.assign(state.shortcutCreate, { link: e.target.value, }), }); }; const handleTitleInputChange = (e: React.ChangeEvent) => { setPartialState({ shortcutCreate: Object.assign(state.shortcutCreate, { title: e.target.value, }), }); }; const handleVisibilityInputChange = (e: React.ChangeEvent) => { setPartialState({ shortcutCreate: Object.assign(state.shortcutCreate, { visibility: 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 handleOpenGraphMetadataImageChange = (e: React.ChangeEvent) => { setPartialState({ shortcutCreate: Object.assign(state.shortcutCreate, { openGraphMetadata: { ...state.shortcutCreate.openGraphMetadata, image: e.target.value, }, }), }); }; const handleOpenGraphMetadataTitleChange = (e: React.ChangeEvent) => { setPartialState({ shortcutCreate: Object.assign(state.shortcutCreate, { openGraphMetadata: { ...state.shortcutCreate.openGraphMetadata, title: e.target.value, }, }), }); }; const handleOpenGraphMetadataDescriptionChange = (e: React.ChangeEvent) => { setPartialState({ shortcutCreate: Object.assign(state.shortcutCreate, { openGraphMetadata: { ...state.shortcutCreate.openGraphMetadata, description: e.target.value, }, }), }); }; const handleTagSuggestionsClick = (suggestion: string) => { if (tag === "") { setTag(suggestion); } else { setTag(`${tag} ${suggestion}`); } }; const handleSaveBtnClick = async () => { if (!state.shortcutCreate.name || !state.shortcutCreate.link) { toast.error("Please fill in required fields."); return; } try { if (shortcutId) { await shortcutService.patchShortcut({ id: shortcutId, name: state.shortcutCreate.name, link: state.shortcutCreate.link, title: state.shortcutCreate.title, description: state.shortcutCreate.description, visibility: state.shortcutCreate.visibility, tags: tag.split(" ").filter(Boolean), openGraphMetadata: state.shortcutCreate.openGraphMetadata, }); } else { await shortcutService.createShortcut({ ...state.shortcutCreate, tags: tag.split(" ").filter(Boolean), }); } if (onConfirm) { onConfirm(); } else { onClose(); } } catch (error: any) { console.error(error); toast.error(error.response.data.message); } }; return (
{isCreating ? "Create Shortcut" : "Edit Shortcut"}
Name *
Destination URL *
Tags {tagSuggestions.length > 0 && (
{tagSuggestions.map((tag) => ( handleTagSuggestionsClick(tag)} > {tag} ))}
)}
Visibility
{visibilities.map((visibility) => ( ))}

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

More
setShowAdditionalFields(!showAdditionalFields)} > Additional fields
{showAdditionalFields && (
Title
Description
)}
setShowOpenGraphMetadata(!showOpenGraphMetadata)} > Social media metadata
{showOpenGraphMetadata && (
Image URL
Title
Description