import { observer } from "mobx-react"; import * as React from "react"; import { useTranslation } from "react-i18next"; import { useParams, Redirect, Switch, Route, useHistory, useRouteMatch, useLocation, } from "react-router-dom"; import styled from "styled-components"; import breakpoint from "styled-components-breakpoint"; import { s } from "@shared/styles"; import Collection from "~/models/Collection"; import Search from "~/scenes/Search"; import { Action } from "~/components/Actions"; import Badge from "~/components/Badge"; import CenteredContent from "~/components/CenteredContent"; 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 Tooltip from "~/components/Tooltip"; import { editCollection } from "~/actions/definitions/collections"; import useCommandBarActions from "~/hooks/useCommandBarActions"; import useLastVisitedPath from "~/hooks/useLastVisitedPath"; import usePolicy from "~/hooks/usePolicy"; import useStores from "~/hooks/useStores"; import { Feature, FeatureFlags } from "~/utils/FeatureFlags"; import { collectionPath, updateCollectionPath } from "~/utils/routeHelpers"; import Actions from "./components/Actions"; import DropToImport from "./components/DropToImport"; import Empty from "./components/Empty"; import MembershipPreview from "./components/MembershipPreview"; import ShareButton from "./components/ShareButton"; function CollectionScene() { const params = useParams<{ id?: string }>(); const history = useHistory(); const match = useRouteMatch(); const location = useLocation(); const { t } = useTranslation(); const { documents, pins, collections, ui } = useStores(); const [isFetching, setFetching] = React.useState(false); const [error, setError] = React.useState(); const currentPath = location.pathname; const [, setLastVisitedPath] = useLastVisitedPath(); const id = params.id || ""; const collection: Collection | null | undefined = collections.getByUrl(id) || collections.get(id); const can = usePolicy(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(() => { setError(undefined); if (collection) { void pins.fetchPage({ collectionId: collection.id, }); } }, [pins, 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 ? [ui.activeCollectionId] : undefined ); if (!collection && error) { return ; } return collection ? ( ) } title={ <>  {collection.name} } actions={ <> {FeatureFlags.isEnabled(Feature.newCollectionSharing) && can.update && } } > {collection.isEmpty ? ( ) : ( <> {collection.name} {collection.isPrivate && !FeatureFlags.isEnabled(Feature.newCollectionSharing) && ( {t("Private")} )} {t("Documents")} {t("Recently updated")} {t("Recently published")} {t("Least recently updated")} {t("A–Z")} )} ) : ( ); } const Documents = styled.div` position: relative; background: ${s("background")}; `; const HeadingWithIcon = styled(Heading)` display: flex; align-items: center; ${breakpoint("tablet")` margin-left: -40px; `}; `; const HeadingIcon = styled(CollectionIcon)` align-self: flex-start; flex-shrink: 0; `; export default observer(CollectionScene);