import { observer } from "mobx-react"; import * as React from "react"; import { useTranslation } from "react-i18next"; import { useParams, Redirect, Switch, Route, useHistory, useRouteMatch, } from "react-router-dom"; import Collection from "~/models/Collection"; import Search from "~/scenes/Search"; import Badge from "~/components/Badge"; import CenteredContent from "~/components/CenteredContent"; import CollectionDescription from "~/components/CollectionDescription"; import CollectionIcon from "~/components/CollectionIcon"; import Heading from "~/components/Heading"; 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 useStores from "~/hooks/useStores"; import { collectionUrl, updateCollectionUrl } from "~/utils/routeHelpers"; import Actions from "./Collection/Actions"; import DropToImport from "./Collection/DropToImport"; import Empty from "./Collection/Empty"; function CollectionScene() { const params = useParams<{ id?: string }>(); const history = useHistory(); const match = useRouteMatch(); const { t } = useTranslation(); const { documents, pins, policies, collections, ui } = useStores(); const [isFetching, setFetching] = React.useState(false); const [error, setError] = React.useState(); const id = params.id || ""; const collection: Collection | null | undefined = collections.getByUrl(id) || collections.get(id); const can = policies.abilities(collection?.id || ""); React.useEffect(() => { if (collection) { const canonicalUrl = updateCollectionUrl(match.url, collection); if (match.url !== canonicalUrl) { history.replace(canonicalUrl); } } }, [collection, history, id, match.url]); React.useEffect(() => { if (collection) { ui.setActiveCollection(collection); } }, [ui, collection]); React.useEffect(() => { setError(undefined); if (collection) { pins.fetchPage({ collectionId: collection.id, }); } }, [pins, collection]); React.useEffect(() => { async function load() { if ((!can || !collection) && !error && !isFetching) { try { setError(undefined); setFetching(true); await collections.fetch(id); } catch (err) { setError(err); } finally { setFetching(false); } } } load(); }, [collections, isFetching, collection, error, id, can]); useCommandBarActions([editCollection]); if (!collection && error) { return ; } return collection ? (   {collection.name} } actions={} > {collection.isEmpty ? ( ) : ( <> {" "} {collection.name}{" "} {!collection.permission && ( {t("Private")} )} {t("Documents")} {t("Recently updated")} {t("Recently published")} {t("Least recently updated")} {t("A–Z")} )} ) : ( ); } export default observer(CollectionScene);