2017-09-18 06:45:40 +00:00
|
|
|
// @flow
|
2021-03-19 00:27:33 +00:00
|
|
|
import fractionalIndex from "fractional-index";
|
|
|
|
import { observer } from "mobx-react";
|
2021-08-23 07:37:28 +00:00
|
|
|
import { PlusIcon, CollapsedIcon } from "outline-icons";
|
2020-08-09 05:53:59 +00:00
|
|
|
import * as React from "react";
|
2021-03-19 00:27:33 +00:00
|
|
|
import { useDrop } from "react-dnd";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2021-08-23 07:37:28 +00:00
|
|
|
import styled from "styled-components";
|
2020-08-09 05:53:59 +00:00
|
|
|
import Fade from "components/Fade";
|
|
|
|
import Flex from "components/Flex";
|
2021-03-19 00:27:33 +00:00
|
|
|
import useStores from "../../../hooks/useStores";
|
2020-08-09 05:53:59 +00:00
|
|
|
import CollectionLink from "./CollectionLink";
|
2021-03-19 00:27:33 +00:00
|
|
|
import DropCursor from "./DropCursor";
|
2021-07-15 19:26:34 +00:00
|
|
|
import PlaceholderCollections from "./PlaceholderCollections";
|
2020-08-09 05:53:59 +00:00
|
|
|
import SidebarLink from "./SidebarLink";
|
2021-04-12 02:39:17 +00:00
|
|
|
import useCurrentTeam from "hooks/useCurrentTeam";
|
2021-07-20 09:06:10 +00:00
|
|
|
import useToasts from "hooks/useToasts";
|
|
|
|
|
2017-09-18 06:45:40 +00:00
|
|
|
type Props = {
|
2017-10-15 18:42:03 +00:00
|
|
|
onCreateCollection: () => void,
|
2017-09-18 06:45:40 +00:00
|
|
|
};
|
|
|
|
|
2021-03-19 00:27:33 +00:00
|
|
|
function Collections({ onCreateCollection }: Props) {
|
2021-04-06 02:05:27 +00:00
|
|
|
const [isFetching, setFetching] = React.useState(false);
|
2021-06-05 16:52:49 +00:00
|
|
|
const [fetchError, setFetchError] = React.useState();
|
2021-03-19 00:27:33 +00:00
|
|
|
const { ui, policies, documents, collections } = useStores();
|
2021-07-20 09:06:10 +00:00
|
|
|
const { showToast } = useToasts();
|
2021-08-23 07:37:28 +00:00
|
|
|
const [expanded, setExpanded] = React.useState(true);
|
2021-03-19 00:27:33 +00:00
|
|
|
const isPreloaded: boolean = !!collections.orderedData.length;
|
|
|
|
const { t } = useTranslation();
|
2021-04-12 02:39:17 +00:00
|
|
|
const team = useCurrentTeam();
|
2021-03-19 00:27:33 +00:00
|
|
|
const orderedCollections = collections.orderedData;
|
2021-04-12 02:39:17 +00:00
|
|
|
const can = policies.abilities(team.id);
|
2021-03-19 00:27:33 +00:00
|
|
|
const [isDraggingAnyCollection, setIsDraggingAnyCollection] = React.useState(
|
|
|
|
false
|
|
|
|
);
|
2019-08-21 05:13:36 +00:00
|
|
|
|
2021-03-19 00:27:33 +00:00
|
|
|
React.useEffect(() => {
|
2021-04-06 02:05:27 +00:00
|
|
|
async function load() {
|
2021-06-05 16:52:49 +00:00
|
|
|
if (!collections.isLoaded && !isFetching && !fetchError) {
|
2021-04-06 02:05:27 +00:00
|
|
|
try {
|
|
|
|
setFetching(true);
|
|
|
|
await collections.fetchPage({ limit: 100 });
|
2021-06-05 16:52:49 +00:00
|
|
|
} catch (error) {
|
2021-07-20 09:06:10 +00:00
|
|
|
showToast(
|
2021-06-05 16:52:49 +00:00
|
|
|
t("Collections could not be loaded, please reload the app"),
|
|
|
|
{
|
|
|
|
type: "error",
|
|
|
|
}
|
|
|
|
);
|
|
|
|
setFetchError(error);
|
2021-04-06 02:05:27 +00:00
|
|
|
} finally {
|
|
|
|
setFetching(false);
|
|
|
|
}
|
|
|
|
}
|
2019-08-21 05:13:36 +00:00
|
|
|
}
|
2021-04-06 02:05:27 +00:00
|
|
|
load();
|
2021-07-20 09:06:10 +00:00
|
|
|
}, [collections, isFetching, showToast, fetchError, t]);
|
2019-03-10 04:00:45 +00:00
|
|
|
|
2021-03-19 00:27:33 +00:00
|
|
|
const [{ isCollectionDropping }, dropToReorderCollection] = useDrop({
|
|
|
|
accept: "collection",
|
|
|
|
drop: async (item, monitor) => {
|
|
|
|
collections.move(
|
|
|
|
item.id,
|
|
|
|
fractionalIndex(null, orderedCollections[0].index)
|
|
|
|
);
|
|
|
|
},
|
|
|
|
canDrop: (item, monitor) => {
|
|
|
|
return item.id !== orderedCollections[0].id;
|
|
|
|
},
|
|
|
|
collect: (monitor) => ({
|
|
|
|
isCollectionDropping: monitor.isOver(),
|
|
|
|
}),
|
|
|
|
});
|
2019-10-06 01:42:03 +00:00
|
|
|
|
2021-03-19 00:27:33 +00:00
|
|
|
const content = (
|
|
|
|
<>
|
|
|
|
<DropCursor
|
|
|
|
isActiveDrop={isCollectionDropping}
|
|
|
|
innerRef={dropToReorderCollection}
|
|
|
|
from="collections"
|
|
|
|
/>
|
|
|
|
{orderedCollections.map((collection, index) => (
|
|
|
|
<CollectionLink
|
|
|
|
key={collection.id}
|
|
|
|
collection={collection}
|
|
|
|
activeDocument={documents.active}
|
|
|
|
prefetchDocument={documents.prefetchDocument}
|
|
|
|
canUpdate={policies.abilities(collection.id).update}
|
|
|
|
ui={ui}
|
|
|
|
isDraggingAnyCollection={isDraggingAnyCollection}
|
|
|
|
onChangeDragging={setIsDraggingAnyCollection}
|
|
|
|
belowCollection={orderedCollections[index + 1]}
|
2019-01-07 00:12:07 +00:00
|
|
|
/>
|
2021-03-19 00:27:33 +00:00
|
|
|
))}
|
2021-04-12 02:39:17 +00:00
|
|
|
{can.createCollection && (
|
|
|
|
<SidebarLink
|
|
|
|
to="/collections"
|
|
|
|
onClick={onCreateCollection}
|
|
|
|
icon={<PlusIcon color="currentColor" />}
|
|
|
|
label={`${t("New collection")}…`}
|
|
|
|
exact
|
2021-08-23 07:37:28 +00:00
|
|
|
depth={0.5}
|
2021-04-12 02:39:17 +00:00
|
|
|
/>
|
|
|
|
)}
|
2021-03-19 00:27:33 +00:00
|
|
|
</>
|
|
|
|
);
|
2017-09-18 06:45:40 +00:00
|
|
|
|
2021-06-05 16:52:49 +00:00
|
|
|
if (!collections.isLoaded || fetchError) {
|
2017-09-18 06:45:40 +00:00
|
|
|
return (
|
2020-05-30 19:50:08 +00:00
|
|
|
<Flex column>
|
2021-08-23 07:37:28 +00:00
|
|
|
<SidebarLink
|
|
|
|
label={t("Collections")}
|
|
|
|
icon={<Disclosure expanded={expanded} color="currentColor" />}
|
|
|
|
disabled
|
|
|
|
/>
|
2021-07-15 19:26:34 +00:00
|
|
|
<PlaceholderCollections />
|
2020-05-30 19:50:08 +00:00
|
|
|
</Flex>
|
2017-09-18 06:45:40 +00:00
|
|
|
);
|
|
|
|
}
|
2021-03-19 00:27:33 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Flex column>
|
2021-08-23 07:37:28 +00:00
|
|
|
<SidebarLink
|
|
|
|
onClick={() => setExpanded((prev) => !prev)}
|
|
|
|
label={t("Collections")}
|
|
|
|
icon={<Disclosure expanded={expanded} color="currentColor" />}
|
|
|
|
/>
|
|
|
|
{expanded && (isPreloaded ? content : <Fade>{content}</Fade>)}
|
2021-03-19 00:27:33 +00:00
|
|
|
</Flex>
|
|
|
|
);
|
2017-09-18 06:45:40 +00:00
|
|
|
}
|
|
|
|
|
2021-08-23 07:37:28 +00:00
|
|
|
const Disclosure = styled(CollapsedIcon)`
|
|
|
|
transition: transform 100ms ease, fill 50ms !important;
|
|
|
|
${({ expanded }) => !expanded && "transform: rotate(-90deg);"};
|
|
|
|
`;
|
|
|
|
|
2021-03-19 00:27:33 +00:00
|
|
|
export default observer(Collections);
|