import { subYears } from "date-fns"; import type { LocationDescriptor } from "history"; import { observer } from "mobx-react"; import * as React from "react"; import { useTranslation } from "react-i18next"; import { Link } from "react-router-dom"; import styled from "styled-components"; import { ellipsis } from "@shared/styles"; import type Document from "~/models/Document"; import type Revision from "~/models/Revision"; import DocumentBreadcrumb from "~/components/DocumentBreadcrumb"; import DocumentTasks from "~/components/DocumentTasks"; import Flex from "~/components/Flex"; import { MetaButton, Separator, metaStyles } from "~/components/HeaderMeta"; import Time from "~/components/Time"; import useClickIntent from "~/hooks/useClickIntent"; import useCurrentUser from "~/hooks/useCurrentUser"; import useStores from "~/hooks/useStores"; type Props = { /** Additional content appended to the end of the meta. */ children?: React.ReactNode; /** Show the collection that the document belongs to. */ showCollection?: boolean; /** Show the published time, even when the document has since been updated. */ showPublished?: boolean; /** Show when the current user last viewed the document. */ showLastViewed?: boolean; /** Show the number of documents nested under this one. */ showParentDocuments?: boolean; /** The document to display meta information for. */ document: Document; /** A revision of the document, when displaying meta for a point in history. */ revision?: Revision; /** Replace the current history entry instead of pushing a new one when `to` is set. */ replace?: boolean; /** Destination to link the meta content to. */ to?: LocationDescriptor; /** Called when the meta content is clicked, renders it as a button. Takes precedence over `to`. */ onClick?: () => void; /** Called when the user is likely to click the meta content, used to preload. */ onClickIntent?: () => void; }; const DocumentMeta: React.FC = ({ showPublished, showCollection, showLastViewed, showParentDocuments, document, revision, children, replace, to, onClick, onClickIntent, ...rest }: Props) => { const { t } = useTranslation(); const { ref: intentRef, handleMouseEnter, handleMouseLeave, } = useClickIntent(onClickIntent, { predict: !!onClickIntent }); const { collections } = useStores(); const user = useCurrentUser(); const { modifiedSinceViewed, updatedAt, updatedBy, createdAt, publishedAt, archivedAt, deletedAt, isDraft, lastViewedAt, isTasks, } = document; // Prevent meta information from displaying if updatedBy is not available. // Currently the situation where this is true is rendering share links. if (!updatedBy) { return null; } const collection = document.collectionId ? collections.get(document.collectionId) : undefined; const lastUpdatedByCurrentUser = user.id === updatedBy.id; const userName = updatedBy.name; let content; if (revision) { content = ( {revision.createdBy?.id === user.id ? t("You updated") : t("{{ userName }} updated", { userName: revision.createdBy?.name ?? t("Unknown"), })}{" "} ); } else if (deletedAt) { content = ( {document.deletedBy?.id === user.id ? t("You deleted") : t("{{ userName }} deleted", { userName: document.deletedBy?.name ?? t("Unknown"), })}{" "} ); } else if (archivedAt) { content = ( {lastUpdatedByCurrentUser ? t("You archived") : t("{{ userName }} archived", { userName })}{" "} ); } else if ( document.sourceMetadata && document.sourceMetadata?.importedAt && document.sourceMetadata.importedAt >= updatedAt ) { content = ( {document.sourceMetadata.createdByName ? t("{{ userName }} updated", { userName: document.sourceMetadata.createdByName, }) : t("Imported")}{" "} ); } else if (createdAt === updatedAt) { content = ( {lastUpdatedByCurrentUser ? t("You created") : t("{{ userName }} created", { userName })}{" "} ); } else if (publishedAt && (publishedAt === updatedAt || showPublished)) { content = ( {lastUpdatedByCurrentUser ? t("You published") : t("{{ userName }} published", { userName })}{" "} ); } else { content = ( {lastUpdatedByCurrentUser ? t("You updated") : t("{{ userName }} updated", { userName })}{" "} ); } const nestedDocumentsCount = collection ? collection.getChildrenForDocument(document.id).length : 0; const canShowProgressBar = isTasks; const timeSinceNow = () => { if (isDraft || !showLastViewed) { return null; } if (!lastViewedAt) { if (lastUpdatedByCurrentUser) { return null; } return ( {t("Never viewed")} ); } // Hide the section entirely once the last view is over a year old. if (new Date(lastViewedAt) < subYears(new Date(), 1)) { return null; } return ( {t("Viewed")} ); }; return ( {onClick ? ( {content} ) : to ? ( {content} ) : ( content )} {showCollection && collection && (  {t("in")}  )} {showParentDocuments && nestedDocumentsCount > 0 && ( {nestedDocumentsCount}{" "} {t("nested document", { count: nestedDocumentsCount, })} )} {timeSinceNow()} {canShowProgressBar && ( <> )} {children} ); }; const Strong = styled.strong` font-weight: 550; `; const Container = styled(Flex)<{ $rtl?: boolean }>` ${metaStyles} `; const Viewed = styled.span` ${ellipsis()} `; const Modified = styled.span<{ highlight?: boolean }>` font-weight: ${(props) => (props.highlight ? "600" : "400")}; `; export default observer(DocumentMeta);