import { useSortable } from "@dnd-kit/sortable"; import { CSS } from "@dnd-kit/utilities"; import { subDays } from "date-fns"; import { m } from "framer-motion"; import { observer } from "mobx-react"; import { CloseIcon, ClockIcon } from "outline-icons"; import { useRef, useCallback, Suspense } from "react"; import { useTranslation } from "react-i18next"; import { Link } from "react-router-dom"; import styled, { useTheme } from "styled-components"; import Icon from "@shared/components/Icon"; import Squircle from "@shared/components/Squircle"; import { s, hover, ellipsis, borderRadius } from "@shared/styles"; import { IconType } from "@shared/types"; import { determineIconType } from "@shared/utils/icon"; import type Document from "~/models/Document"; import type Pin from "~/models/Pin"; import Flex from "~/components/Flex"; import NudeButton from "~/components/NudeButton"; import Time from "~/components/Time"; import usePolicy from "~/hooks/usePolicy"; import useStores from "~/hooks/useStores"; import { DocumentContextMenu } from "~/menus/DocumentContextMenu"; import { DocumentIcon } from "~/components/Icons/DocumentIcon"; import CollectionIcon from "./Icons/CollectionIcon"; import Text from "./Text"; import Tooltip from "./Tooltip"; import lazyWithRetry from "@shared/utils/lazyWithRetry"; const ReadingTime = lazyWithRetry(() => import("./ReadingTime")); type Props = { /** The pin record */ pin: Pin | undefined; /** The document related to the pin */ document: Document; /** Whether this pin can be reordered by dragging */ isDraggable?: boolean; }; function DocumentCard(props: Props) { const { t } = useTranslation(); const { collections } = useStores(); const theme = useTheme(); const { document, pin, isDraggable } = props; const canPin = usePolicy(pin); const canDocument = usePolicy(document); const pinnedToHome = useRef(!pin?.collectionId).current; // Pins in a collection are governed by the document policy, pins on home by // the policy of the pin itself. const canReorder = pin?.collectionId ? canDocument.pin : canPin.update; const canUnpin = pin?.collectionId ? canDocument.unpin : canPin.delete; const collection = document.collectionId ? collections.get(document.collectionId) : undefined; const { attributes, listeners, setNodeRef, transform, transition, isDragging, } = useSortable({ id: props.document.id, disabled: !isDraggable || !canReorder, }); const hasEmojiInTitle = determineIconType(document.icon) === IconType.Emoji; const style = { transform: CSS.Transform.toString(transform), transition, }; const handleUnpin = useCallback( async (ev: React.MouseEvent) => { ev.preventDefault(); ev.stopPropagation(); await pin?.delete(); }, [pin] ); // Keep the drag sensor from claiming the pointer, otherwise holding the // button for longer than the activation delay starts a drag const handleActionsPointerDown = useCallback((ev: React.PointerEvent) => { ev.stopPropagation(); }, []); // If the document was updated within the last 7 days, show a timestamp instead of reading time const isRecentlyUpdated = new Date(document.updatedAt) > subDays(new Date(), 7); const updatedAt = ( <>