import { Button, DialogActions, DialogContent, DialogTitle, Divider, Drawer, Input, ModalClose, 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 useShortcutStore, { getShortcutUpdateMask } from "@/stores/v1/shortcut"; import useWorkspaceStore from "@/stores/v1/workspace"; import { Visibility } from "@/types/proto/api/v1/common"; import { Shortcut } from "@/types/proto/api/v1/shortcut_service"; import { convertVisibilityFromPb } from "@/utils/visibility"; import useLoading from "../hooks/useLoading"; import Icon from "./Icon"; interface Props { shortcutId?: number; initialShortcut?: Partial; onClose: () => void; onConfirm?: () => void; } interface State { shortcutCreate: Shortcut; } const CreateShortcutDrawer: React.FC = (props: Props) => { const { onClose, onConfirm, shortcutId, initialShortcut } = props; const { t } = useTranslation(); const [state, setState] = useState({ shortcutCreate: Shortcut.fromPartial({ visibility: Visibility.PUBLIC, ogMetadata: { title: "", description: "", image: "", }, ...initialShortcut, }), }); const shortcutStore = useShortcutStore(); const workspaceStore = useWorkspaceStore(); const [showOpenGraphMetadata, setShowOpenGraphMetadata] = useState(false); const shortcutList = shortcutStore.getShortcutList(); const [tag, setTag] = useState(""); const tagSuggestions = uniq(shortcutList.map((shortcut) => shortcut.tags).flat()); const isCreating = isUndefined(shortcutId); const loadingState = useLoading(!isCreating); const requestState = useLoading(false); const setPartialState = (partialState: Partial) => { setState({ ...state, ...partialState, }); }; useEffect(() => { if (workspaceStore.setting.defaultVisibility !== Visibility.VISIBILITY_UNSPECIFIED) { setPartialState({ shortcutCreate: Object.assign(state.shortcutCreate, { visibility: workspaceStore.setting.defaultVisibility, }), }); } }, []); useEffect(() => { if (shortcutId) { const shortcut = shortcutStore.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, ogMetadata: shortcut.ogMetadata, }), }); setTag(shortcut.tags.join(" ")); loadingState.setFinish(); } } }, [shortcutId]); if (loadingState.isLoading) { return null; } 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: Number(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, { ogMetadata: { ...state.shortcutCreate.ogMetadata, image: e.target.value, }, }), }); }; const handleOpenGraphMetadataTitleChange = (e: React.ChangeEvent) => { setPartialState({ shortcutCreate: Object.assign(state.shortcutCreate, { ogMetadata: { ...state.shortcutCreate.ogMetadata, title: e.target.value, }, }), }); }; const handleOpenGraphMetadataDescriptionChange = (e: React.ChangeEvent) => { setPartialState({ shortcutCreate: Object.assign(state.shortcutCreate, { ogMetadata: { ...state.shortcutCreate.ogMetadata, 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 { const tags = tag.split(" ").filter(Boolean); if (shortcutId) { const originShortcut = shortcutStore.getShortcutById(shortcutId); const updatingShortcut = { ...state.shortcutCreate, id: shortcutId, tags, }; await shortcutStore.updateShortcut(updatingShortcut, getShortcutUpdateMask(originShortcut, updatingShortcut)); } else { await shortcutStore.createShortcut({ ...state.shortcutCreate, tags, }); } if (onConfirm) { onConfirm(); } else { onClose(); } } catch (error: any) { console.error(error); toast.error(error.details); } }; return ( {isCreating ? "Create Shortcut" : "Edit Shortcut"}
Name *
Link *
Title
Description
Tags {tagSuggestions.length > 0 && (
{tagSuggestions.map((tag) => ( handleTagSuggestionsClick(tag)} > {tag} ))}
)}
Visibility

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

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