import { observer } from "mobx-react"; import * as React from "react"; import { useTranslation } from "react-i18next"; import { useParams, Switch, Route, useHistory, useRouteMatch, useLocation, Redirect, } from "react-router-dom"; import styled from "styled-components"; import breakpoint from "styled-components-breakpoint"; import Icon, { IconTitleWrapper } from "@shared/components/Icon"; import { s } from "@shared/styles"; import { StatusFilter } from "@shared/types"; import { colorPalette } from "@shared/utils/collections"; import Collection from "~/models/Collection"; import { Action } from "~/components/Actions"; import CenteredContent from "~/components/CenteredContent"; import { CollectionBreadcrumb } from "~/components/CollectionBreadcrumb"; import CollectionDescription from "~/components/CollectionDescription"; import Heading from "~/components/Heading"; import CollectionIcon from "~/components/Icons/CollectionIcon"; import InputSearchPage from "~/components/InputSearchPage"; import PlaceholderList from "~/components/List/Placeholder"; import PaginatedDocumentList from "~/components/PaginatedDocumentList"; import PinnedDocuments from "~/components/PinnedDocuments"; import PlaceholderText from "~/components/PlaceholderText"; import Scene from "~/components/Scene"; import Tab from "~/components/Tab"; import Tabs from "~/components/Tabs"; import { editCollection } from "~/actions/definitions/collections"; import useCommandBarActions from "~/hooks/useCommandBarActions"; import { useLastVisitedPath } from "~/hooks/useLastVisitedPath"; import { useLocationSidebarContext } from "~/hooks/useLocationSidebarContext"; import usePersistedState from "~/hooks/usePersistedState"; import { usePinnedDocuments } from "~/hooks/usePinnedDocuments"; import usePolicy from "~/hooks/usePolicy"; import useStores from "~/hooks/useStores"; import { collectionPath, updateCollectionPath } from "~/utils/routeHelpers"; import Error404 from "../Errors/Error404"; import Actions from "./components/Actions"; import DropToImport from "./components/DropToImport"; import Empty from "./components/Empty"; import MembershipPreview from "./components/MembershipPreview"; import Notices from "./components/Notices"; import ShareButton from "./components/ShareButton"; const IconPicker = React.lazy(() => import("~/components/IconPicker")); enum CollectionPath { Overview = "overview", Recent = "recent", Updated = "updated", Published = "published", Old = "old", Alphabetical = "alphabetical", } const CollectionScene = observer(function _CollectionScene() { const params = useParams<{ id?: string }>(); const history = useHistory(); const match = useRouteMatch(); const location = useLocation(); const { t } = useTranslation(); const { documents, collections, ui } = useStores(); const [isFetching, setFetching] = React.useState(false); const [error, setError] = React.useState(); const currentPath = location.pathname; const [, setLastVisitedPath] = useLastVisitedPath(); const sidebarContext = useLocationSidebarContext(); const id = params.id || ""; const urlId = id.split("-").pop() ?? ""; const collection: Collection | null | undefined = collections.getByUrl(id) || collections.get(id); const can = usePolicy(collection); const { pins, count } = usePinnedDocuments(urlId, collection?.id); const [collectionTab, setCollectionTab] = usePersistedState( `collection-tab:${collection?.id}`, collection?.hasDescription ? CollectionPath.Overview : CollectionPath.Recent, { listen: false, } ); const handleIconChange = React.useCallback( (icon: string | null, color: string | null) => collection?.save({ icon, color }), [collection] ); React.useEffect(() => { setLastVisitedPath(currentPath); }, [currentPath, setLastVisitedPath]); React.useEffect(() => { if (collection?.name) { const canonicalUrl = updateCollectionPath(match.url, collection); if (match.url !== canonicalUrl) { history.replace(canonicalUrl, history.location.state); } } }, [collection, collection?.name, history, id, match.url]); React.useEffect(() => { if (collection) { ui.setActiveCollection(collection.id); } return () => ui.setActiveCollection(undefined); }, [ui, collection]); React.useEffect(() => { async function fetchData() { if ((!can || !collection) && !error && !isFetching) { try { setError(undefined); setFetching(true); await collections.fetch(id); } catch (err) { setError(err); } finally { setFetching(false); } } } void fetchData(); }, [collections, isFetching, collection, error, id, can]); useCommandBarActions([editCollection], [ui.activeCollectionId ?? "none"]); if (!collection && error) { return ; } const hasOverview = can.update || collection?.hasDescription; const fallbackIcon = collection ? ( ) : null; const tabProps = (path: CollectionPath) => ({ exact: true, onClick: () => setCollectionTab(path), to: { pathname: collectionPath(collection!.path, path), state: { sidebarContext }, }, }); return collection ? ( ) : collection.isEmpty ? undefined : ( ) } title={ <>  {collection.name} } actions={ <> {can.update && } } > {can.update ? ( ) : ( fallbackIcon )} {collection.name} {hasOverview && ( {t("Overview")} )} {t("Documents")} {!collection.isArchived && ( <> {t("Recently updated")} {t("Recently published")} {t("Least recently updated")} {t("A–Z")} )} {hasOverview ? ( ) : ( )} {collection.isEmpty ? ( ) : !collection.isArchived ? ( <> ) : ( )} ) : ( ); }); const KeyedCollection = () => { const params = useParams<{ id?: string }>(); // Forced mount prevents animation of pinned documents when navigating // _between_ collections, speeds up perceived performance. return ; }; const Documents = styled.div` position: relative; background: ${s("background")}; `; const CollectionHeading = styled(Heading)` display: flex; align-items: center; position: relative; margin-left: 40px; ${breakpoint("tablet")` margin-left: 0; `} `; export default KeyedCollection;