import { 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 Document from "~/models/Document"; import DocumentBreadcrumb from "~/components/DocumentBreadcrumb"; import DocumentTasks from "~/components/DocumentTasks"; import Flex from "~/components/Flex"; import Time from "~/components/Time"; import useCurrentUser from "~/hooks/useCurrentUser"; import useStores from "~/hooks/useStores"; type Props = { showCollection?: boolean; showPublished?: boolean; showLastViewed?: boolean; showParentDocuments?: boolean; document: Document; replace?: boolean; to?: LocationDescriptor; }; const DocumentMeta: React.FC = ({ showPublished, showCollection, showLastViewed, showParentDocuments, document, children, replace, to, ...rest }) => { const { t } = useTranslation(); const { collections } = useStores(); const user = useCurrentUser(); const { modifiedSinceViewed, updatedAt, updatedBy, createdAt, publishedAt, archivedAt, deletedAt, isDraft, lastViewedAt, isTasks, isTemplate, } = 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 = collections.get(document.collectionId); const lastUpdatedByCurrentUser = user.id === updatedBy.id; const userName = updatedBy.name; let content; if (deletedAt) { content = ( {lastUpdatedByCurrentUser ? t("You deleted") : t("{{ userName }} deleted", { userName })}{" "} ); } else if (archivedAt) { content = ( {lastUpdatedByCurrentUser ? t("You archived") : t("{{ userName }} archived", { userName })}{" "} ); } 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 if (isDraft) { content = ( {lastUpdatedByCurrentUser ? t("You saved") : t("{{ userName }} saved", { userName })}{" "} ); } else { content = ( {lastUpdatedByCurrentUser ? t("You updated") : t("{{ userName }} updated", { userName })}{" "} ); } const nestedDocumentsCount = collection ? collection.getDocumentChildren(document.id).length : 0; const canShowProgressBar = isTasks && !isTemplate; const timeSinceNow = () => { if (isDraft || !showLastViewed) { return null; } if (!lastViewedAt) { if (lastUpdatedByCurrentUser) { return null; } return ( • {t("Never viewed")} ); } return ( • {t("Viewed")} ); }; return ( {to ? ( {content} ) : ( content )} {showCollection && collection && (  {t("in")}  )} {showParentDocuments && nestedDocumentsCount > 0 && (  • {nestedDocumentsCount}{" "} {t("nested document", { count: nestedDocumentsCount, })} )}  {timeSinceNow()} {canShowProgressBar && ( <>  •  )} {children} ); }; const Container = styled(Flex)<{ rtl?: boolean }>` justify-content: ${(props) => (props.rtl ? "flex-end" : "flex-start")}; color: ${(props) => props.theme.textTertiary}; font-size: 13px; white-space: nowrap; overflow: hidden; min-width: 0; `; const Viewed = styled.span` text-overflow: ellipsis; overflow: hidden; `; const Modified = styled.span<{ highlight?: boolean }>` font-weight: ${(props) => (props.highlight ? "600" : "400")}; `; export default observer(DocumentMeta);