diff --git a/app/components/DocumentPathList.js b/app/components/DocumentPathList.js new file mode 100644 index 0000000000..c3561f2be9 --- /dev/null +++ b/app/components/DocumentPathList.js @@ -0,0 +1,200 @@ +// @flow +import { Search } from "js-search"; +import { last } from "lodash"; +import { useMemo, useState, useCallback } from "react"; +import * as React from "react"; +import { useTranslation } from "react-i18next"; +import AutoSizer from "react-virtualized-auto-sizer"; +import { FixedSizeList as List } from "react-window"; +import styled from "styled-components"; +import { type DocumentPath } from "stores/CollectionsStore"; +import Document from "models/Document"; +import Button from "components/Button"; +import Flex from "components/Flex"; +import InputSearch from "components/InputSearch"; +import PathToDocument from "components/PathToDocument"; +import useStores from "hooks/useStores"; + +type Props = {| + document: Document, + handleSelect: (?DocumentPath) => Promise, + selectName: string, +|}; + +const DocumentPathList = ({ document, handleSelect, selectName }: Props) => { + const [searchTerm, setSearchTerm] = useState(); + const { collections, documents, policies } = useStores(); + const [selectedPath, setSelectedPath] = useState(); + const { t } = useTranslation(); + + const searchIndex = useMemo(() => { + let paths = collections.pathsToDocuments; + + paths = paths.filter((path) => { + if ( + (path.type === "collection" && policies.abilities(path.id).update) || + (path.type === "document" && + policies.abilities(path.collectionId).update) + ) + return true; + + return false; + }); + + const index = new Search("id"); + index.addIndex("title"); + + // Build index + const indexeableDocuments = []; + paths.forEach((path) => { + const doc = documents.get(path.id); + if (!doc || !doc.isTemplate) { + indexeableDocuments.push(path); + } + }); + index.addDocuments(indexeableDocuments); + + return index; + }, [collections.pathsToDocuments, policies, documents]); + + const handleChange = (ev: SyntheticInputEvent<*>) => { + setSearchTerm(ev.target.value); + }; + + const handleKeyDown = React.useCallback((event) => { + if (event.currentTarget.value && event.key === "Escape") { + event.preventDefault(); + event.stopPropagation(); + setSearchTerm(""); + } + }, []); + + const selected = useCallback( + (result) => { + if (!selectedPath) return; + + if (selectedPath.type === "collection" && selectedPath.id === result.id) { + return true; + } + if ( + selectedPath.type === "document" && + selectedPath.id === result.id && + selectedPath.collectionId === result.collectionId + ) { + return true; + } + return false; + }, + [selectedPath] + ); + + const results: DocumentPath[] = useMemo(() => { + const onlyShowCollections = document.isTemplate; + let results = []; + if (collections.isLoaded) { + if (searchTerm) { + results = searchIndex.search(searchTerm); + } else { + results = searchIndex._documents; + } + } + + if (onlyShowCollections) { + results = results.filter((result) => result.type === "collection"); + } else { + // Exclude root from search results if document is already at the root + if (!document.parentDocumentId) { + results = results.filter( + (result) => result.id !== document.collectionId + ); + } + + // Exclude document if on the path to result, or the same result + results = results.filter( + (result) => + !result.path.map((doc) => doc.id).includes(document.id) && + last(result.path.map((doc) => doc.id)) !== document.parentDocumentId + ); + } + + return results; + }, [ + document.isTemplate, + document.parentDocumentId, + document.collectionId, + document.id, + collections.isLoaded, + searchTerm, + searchIndex, + ]); + + const row = ({ index, data, style }) => { + const result = data[index]; + + return ( + + ); + }; + + const data = results; + + return ( + <> + + + + + + {({ width, height }) => ( + + data[index].id} + > + {row} + + + )} + + + + + + + ); +}; + +const Results = styled.div` + padding: 8px 0; + height: calc(100% - 80px); +`; + +const ButtonWrapper = styled(Flex)` + margin: 10px 0; +`; + +export default DocumentPathList; diff --git a/app/scenes/Document/components/PublishDialog.js b/app/scenes/Document/components/PublishDialog.js index 7f3b4a0699..00b85e59d0 100644 --- a/app/scenes/Document/components/PublishDialog.js +++ b/app/scenes/Document/components/PublishDialog.js @@ -1,21 +1,11 @@ // @flow -import { Search } from "js-search"; -import { last } from "lodash"; import * as React from "react"; import { useTranslation } from "react-i18next"; -import AutoSizer from "react-virtualized-auto-sizer"; -import { FixedSizeList as List } from "react-window"; import { Dialog, DialogBackdrop, type DialogStateReturn } from "reakit"; import styled from "styled-components"; import breakpoint from "styled-components-breakpoint"; -import { type DocumentPath } from "stores/CollectionsStore"; import Document from "models/Document"; -import Button from "components/Button"; -import Divider from "components/Divider"; -import Flex from "components/Flex"; -import InputSearch from "components/InputSearch"; -import PathToDocument from "components/PathToDocument"; -import useStores from "hooks/useStores"; +import DocumentPathList from "../../../components/DocumentPathList"; import useToasts from "hooks/useToasts"; type Props = {| @@ -30,28 +20,13 @@ type Props = {| const PublishDialog = ({ dialog, document, onSave }: Props) => { const { t } = useTranslation(); - const [searchTerm, setSearchTerm] = React.useState(); - const [selectedPath, setSelectedPath] = React.useState(); - const { collections, documents, policies } = useStores(); const { showToast } = useToasts(); - React.useEffect(() => { - if (!dialog.visible) { - setSelectedPath(undefined); - } - }, [dialog.visible]); - - const handleChange = React.useCallback((event) => { - setSearchTerm(event.target.value); - }, []); - - const handleKeyDown = React.useCallback((event) => { - if (event.currentTarget.value && event.key === "Escape") { - event.preventDefault(); - event.stopPropagation(); - setSearchTerm(""); - } - }, []); + // React.useEffect(() => { + // if (!dialog.visible) { + // setSelectedPath(undefined); + // } + // }, [dialog.visible]); const handlePublishFromModal = React.useCallback( async (selectedPath) => { @@ -80,104 +55,6 @@ const PublishDialog = ({ dialog, document, onSave }: Props) => { [dialog, document, onSave, showToast, t] ); - const selected = React.useCallback( - (result) => { - if (!selectedPath) return; - - if (selectedPath.type === "collection" && selectedPath.id === result.id) { - return true; - } - if ( - selectedPath.type === "document" && - selectedPath.id === result.id && - selectedPath.collectionId === result.collectionId - ) { - return true; - } - return false; - }, - [selectedPath] - ); - - const searchIndex = React.useMemo(() => { - let paths = collections.pathsToDocuments; - - paths = paths.filter((path) => { - if ( - (path.type === "collection" && policies.abilities(path.id).update) || - (path.type === "document" && - policies.abilities(path.collectionId).update) - ) - return true; - - return false; - }); - - const index = new Search("id"); - index.addIndex("title"); - - // Build index - const indexeableDocuments = []; - paths.forEach((path) => { - const doc = documents.get(path.id); - if (!doc || !doc.isTemplate) { - indexeableDocuments.push(path); - } - }); - index.addDocuments(indexeableDocuments); - - return index; - }, [collections.pathsToDocuments, policies, documents]); - - const results: DocumentPath[] = React.useMemo(() => { - const onlyShowCollections = document.isTemplate; - let results = []; - if (collections.isLoaded) { - if (searchTerm) { - results = searchIndex.search(searchTerm); - } else { - results = searchIndex._documents; - } - } - - if (onlyShowCollections) { - results = results.filter((result) => result.type === "collection"); - } else { - // Exclude root from search results if document is already at the root - if (!document.parentDocumentId) { - results = results.filter( - (result) => result.id !== document.collectionId - ); - } - - // Exclude document if on the path to result, or the same result - results = results.filter( - (result) => - !result.path.map((doc) => doc.id).includes(document.id) && - last(result.path.map((doc) => doc.id)) !== document.parentDocumentId - ); - } - - return results; - }, [document, collections, searchTerm, searchIndex]); - - const row = ({ index, data, style }) => { - const result = data[index]; - return ( - - ); - }; - - const data = results; - return ( @@ -189,45 +66,11 @@ const PublishDialog = ({ dialog, document, onSave }: Props) => { > - - - - - - {({ width, height }) => { - return ( - - data[index].id} - > - {row} - - - ); - }} - - - - - - + @@ -256,11 +99,6 @@ const Position = styled.div` `}; `; -const Results = styled.div` - padding: 8px 0; - height: calc(93% - 52px); -`; - const Content = styled.div` background: ${(props) => props.theme.background}; width: 70vw; @@ -277,8 +115,4 @@ const Content = styled.div` `}; `; -const ButtonWrapper = styled(Flex)` - margin: 10px 0; -`; - export default PublishDialog; diff --git a/app/scenes/DocumentMove.js b/app/scenes/DocumentMove.js index cb4e74fc4f..c9895e7e7f 100644 --- a/app/scenes/DocumentMove.js +++ b/app/scenes/DocumentMove.js @@ -1,18 +1,11 @@ // @flow -import { Search } from "js-search"; -import { last } from "lodash"; import { observer } from "mobx-react"; import * as React from "react"; -import { useMemo, useState, useCallback } from "react"; -import { useTranslation, Trans } from "react-i18next"; -import AutoSizer from "react-virtualized-auto-sizer"; -import { FixedSizeList as List } from "react-window"; +import { useTranslation } from "react-i18next"; import styled from "styled-components"; -import { type DocumentPath } from "stores/CollectionsStore"; import Document from "models/Document"; -import Button from "components/Button"; +import DocumentPathList from "components/DocumentPathList"; import Flex from "components/Flex"; -import { Outline } from "components/Input"; import Labeled from "components/Labeled"; import PathToDocument from "components/PathToDocument"; import useStores from "hooks/useStores"; @@ -24,94 +17,11 @@ type Props = {| |}; function DocumentMove({ document, onRequestClose }: Props) { - const [searchTerm, setSearchTerm] = useState(); - const { collections, documents, policies } = useStores(); - const [selectedPath, setSelectedPath] = useState(); + const { collections } = useStores(); const { showToast } = useToasts(); const { t } = useTranslation(); - const selected = useCallback( - (result) => { - if (!selectedPath) return; - - if (selectedPath.type === "collection" && selectedPath.id === result.id) { - return true; - } - if ( - selectedPath.type === "document" && - selectedPath.id === result.id && - selectedPath.collectionId === result.collectionId - ) { - return true; - } - return false; - }, - [selectedPath] - ); - - const searchIndex = useMemo(() => { - let paths = collections.pathsToDocuments; - - paths = paths.filter((path) => { - if ( - (path.type === "collection" && policies.abilities(path.id).update) || - (path.type === "document" && - policies.abilities(path.collectionId).update) - ) - return true; - - return false; - }); - - const index = new Search("id"); - index.addIndex("title"); - - // Build index - const indexeableDocuments = []; - paths.forEach((path) => { - const doc = documents.get(path.id); - if (!doc || !doc.isTemplate) { - indexeableDocuments.push(path); - } - }); - index.addDocuments(indexeableDocuments); - - return index; - }, [collections.pathsToDocuments, policies, documents]); - - const results: DocumentPath[] = useMemo(() => { - const onlyShowCollections = document.isTemplate; - let results = []; - if (collections.isLoaded) { - if (searchTerm) { - results = searchIndex.search(searchTerm); - } else { - results = searchIndex._documents; - } - } - - if (onlyShowCollections) { - results = results.filter((result) => result.type === "collection"); - } else { - // Exclude root from search results if document is already at the root - if (!document.parentDocumentId) { - results = results.filter( - (result) => result.id !== document.collectionId - ); - } - - // Exclude document if on the path to result, or the same result - results = results.filter( - (result) => - !result.path.map((doc) => doc.id).includes(document.id) && - last(result.path.map((doc) => doc.id)) !== document.parentDocumentId - ); - } - - return results; - }, [document, collections, searchTerm, searchIndex]); - - const handleMove = async () => { + const handleMove = async (selectedPath) => { if (!document) return; if (!selectedPath) { @@ -129,10 +39,6 @@ function DocumentMove({ document, onRequestClose }: Props) { onRequestClose(); }; - const handleFilter = (ev: SyntheticInputEvent<*>) => { - setSearchTerm(ev.target.value); - }; - const renderPathToCurrentDocument = () => { const result = collections.getPathForDocument(document.id); @@ -146,24 +52,6 @@ function DocumentMove({ document, onRequestClose }: Props) { } }; - const row = ({ index, data, style }) => { - const result = data[index]; - - return ( - - ); - }; - - const data = results; - if (!document || !collections.isLoaded) { return null; } @@ -175,79 +63,26 @@ function DocumentMove({ document, onRequestClose }: Props) { {renderPathToCurrentDocument()} -
- - - - - - {({ width, height }) => ( - - data[index].id} - > - {row} - - - )} - - + - - -
); } -const InputWrapper = styled("div")` - padding: 8px; - width: 100%; -`; - -const Input = styled("input")` - width: 100%; - outline: none; - background: none; - border-radius: 4px; - height: 30px; - border: 0; - color: ${(props) => props.theme.text}; - - &::placeholder { - color: ${(props) => props.theme.placeholder}; - } -`; - -const NewLocation = styled(Outline)` +const NewLocation = styled(Flex)` display: block; flex: initial; height: 40vh; `; -const Results = styled.div` - padding: 8px 0; - height: calc(100% - 46px); -`; - const Section = styled(Flex)` margin-bottom: 24px; `;