import { Tooltip } from "@mui/joy"; import copy from "copy-to-clipboard"; import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import toast from "react-hot-toast"; import { shortcutService } from "../services"; import useFaviconStore from "../stores/v1/favicon"; import useViewStore from "../stores/v1/view"; import useUserStore from "../stores/v1/user"; import { absolutifyLink } from "../helpers/utils"; import { showCommonDialog } from "./Alert"; import Icon from "./Icon"; import Dropdown from "./common/Dropdown"; import VisibilityIcon from "./VisibilityIcon"; import GenerateQRCodeDialog from "./GenerateQRCodeDialog"; import AnalyticsDialog from "./AnalyticsDialog"; interface Props { shortcut: Shortcut; handleEdit: () => void; } const ShortcutView = (props: Props) => { const { shortcut, handleEdit } = props; const { t } = useTranslation(); const currentUser = useUserStore().getCurrentUser(); const viewStore = useViewStore(); const faviconStore = useFaviconStore(); const [favicon, setFavicon] = useState(undefined); const [showQRCodeDialog, setShowQRCodeDialog] = useState(false); const [showAnalyticsDialog, setShowAnalyticsDialog] = useState(false); const havePermission = currentUser.role === "ADMIN" || shortcut.creatorId === currentUser.id; const shortifyLink = absolutifyLink(`/s/${shortcut.name}`); useEffect(() => { faviconStore.getOrFetchUrlFavicon(shortcut.link).then((url) => { if (url) { setFavicon(url); } }); }, [shortcut.link]); const handleCopyButtonClick = () => { copy(shortifyLink); toast.success("Shortcut link copied to clipboard."); }; const handleDeleteShortcutButtonClick = (shortcut: Shortcut) => { showCommonDialog({ title: "Delete Shortcut", content: `Are you sure to delete shortcut \`${shortcut.name}\`? You cannot undo this action.`, style: "danger", onConfirm: async () => { await shortcutService.deleteShortcutById(shortcut.id); }, }); }; return ( <>
{favicon ? ( ) : ( )}
s/ {shortcut.name}
{havePermission && ( } > )}
{shortcut.description &&

{shortcut.description}

} {shortcut.tags.length > 0 && (
{shortcut.tags.map((tag) => { return ( viewStore.setFilter({ tag: tag })} > #{tag} ); })}
)}
{shortcut.creator.nickname}
viewStore.setFilter({ visibility: shortcut.visibility })} > {t(`shortcut.visibility.${shortcut.visibility.toLowerCase()}.self`)}
setShowAnalyticsDialog(true)} > {shortcut.view} visits
{showQRCodeDialog && setShowQRCodeDialog(false)} />} {showAnalyticsDialog && setShowAnalyticsDialog(false)} />} ); }; export default ShortcutView;