import { observer } from "mobx-react"; import * as React from "react"; import { useDrop } from "react-dnd"; import { useTranslation } from "react-i18next"; import styled from "styled-components"; import Collection from "~/models/Collection"; import Document from "~/models/Document"; import DocumentsLoader from "~/components/DocumentsLoader"; import { ResizingHeightContainer } from "~/components/ResizingHeightContainer"; import usePolicy from "~/hooks/usePolicy"; import useStores from "~/hooks/useStores"; import useToasts from "~/hooks/useToasts"; import DocumentLink from "./DocumentLink"; import DropCursor from "./DropCursor"; import EmptyCollectionPlaceholder from "./EmptyCollectionPlaceholder"; import Folder from "./Folder"; import PlaceholderCollections from "./PlaceholderCollections"; import { DragObject } from "./SidebarLink"; import useCollectionDocuments from "./useCollectionDocuments"; type Props = { collection: Collection; expanded: boolean; prefetchDocument?: (documentId: string) => Promise; }; function CollectionLinkChildren({ collection, expanded, prefetchDocument, }: Props) { const can = usePolicy(collection); const { showToast } = useToasts(); const manualSort = collection.sort.field === "index"; const { documents } = useStores(); const { t } = useTranslation(); const childDocuments = useCollectionDocuments(collection, documents.active); // Drop to reorder document const [{ isOverReorder, isDraggingAnyDocument }, dropToReorder] = useDrop({ accept: "document", drop: (item: DragObject) => { if (!manualSort && item.collectionId === collection?.id) { showToast( t( "You can't reorder documents in an alphabetically sorted collection" ), { type: "info", timeout: 5000, } ); return; } if (!collection) { return; } void documents.move(item.id, collection.id, undefined, 0); }, collect: (monitor) => ({ isOverReorder: !!monitor.isOver(), isDraggingAnyDocument: !!monitor.canDrop(), }), }); return ( {isDraggingAnyDocument && can.createDocument && manualSort && ( )} {!childDocuments && ( )} {childDocuments?.map((node, index) => ( ))} {childDocuments?.length === 0 && } ); } const Loading = styled(PlaceholderCollections)` margin-left: 44px; min-height: 90px; `; export default observer(CollectionLinkChildren);