From f25cef4d452fff06a7134811fccb997c0792d6a5 Mon Sep 17 00:00:00 2001 From: Alexey Vasyukov Date: Sun, 4 Dec 2022 22:44:00 +0300 Subject: [PATCH] feat(#27): add option to include directories from another collection --- .../forms/collection.form copy.tsx | 174 +++++++++++++++ .../collections/forms/collection.form.tsx | 95 +------- .../include-directories/directory.node.tsx | 55 +++++ .../include-directories.field.tsx | 210 ++++++++++++++++++ .../forms/fields/include-directories/index.ts | 1 + .../pages/collections/forms/fields/index.ts | 1 + .../modals/create-collection.modal.tsx | 2 +- .../modals/update-collection.modal.tsx | 1 + .../pages/side-bar/nodes/collection.node.tsx | 3 +- src/app/pages/side-bar/side-bar.tsx | 1 + .../collection-types/grpc/tls/tls.form.tsx | 2 +- 11 files changed, 451 insertions(+), 94 deletions(-) create mode 100644 src/app/pages/collections/forms/collection.form copy.tsx create mode 100644 src/app/pages/collections/forms/fields/include-directories/directory.node.tsx create mode 100644 src/app/pages/collections/forms/fields/include-directories/include-directories.field.tsx create mode 100644 src/app/pages/collections/forms/fields/include-directories/index.ts create mode 100644 src/app/pages/collections/forms/fields/index.ts diff --git a/src/app/pages/collections/forms/collection.form copy.tsx b/src/app/pages/collections/forms/collection.form copy.tsx new file mode 100644 index 0000000..056a85d --- /dev/null +++ b/src/app/pages/collections/forms/collection.form copy.tsx @@ -0,0 +1,174 @@ +import { faPlus, faTrash } from '@fortawesome/free-solid-svg-icons'; +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; +import { Button, Container, Input, Spacer, styled, Table, Text } from '@nextui-org/react'; +import { nanoid } from 'nanoid'; +import React from 'react'; +import { Controller, useForm } from 'react-hook-form'; + +import { EzyButton, FileInput } from '@components'; +import { Collection, CollectionType } from '@storage'; + +export interface CollectionFormProps { + id?: string; + + defaultValues?: Partial>; + + onSubmit: (payload: Collection) => void; +} + +const PathComponent = styled(Text, { + overflowX: 'auto', + overflowY: 'hidden', + flex: 1, +}); + +type IncludeDirectoriesContainerProps = { + value?: string[]; + + onChange: (value: string[]) => void; +}; + +const IncludeDirectoriesContainer = React.forwardRef< + HTMLDivElement, + IncludeDirectoriesContainerProps +>(({ onChange, value = [] }, ref) => { + const [directories, setDirectories] = React.useState(value); + const handleAddPathButtonClick = async () => { + const paths = await window.electronDialog.open({ properties: ['openDirectory'] }); + const newDirectories = [...directories, ...paths]; + + setDirectories(newDirectories); + onChange(newDirectories); + }; + + const handleDeletePathButtonClick = (path: string) => () => { + const newDirectories = directories.filter((item) => item !== path); + + setDirectories(newDirectories); + onChange(newDirectories); + }; + + return ( +
+
+ + Include directories + + + } + css={{ minWidth: 10, color: '$ezy', borderColor: '$accents3' }} + onClick={handleAddPathButtonClick} + /> +
+ + + + Path + + + {directories.map((directory) => ( + + + {directory} + +
+
+ ); +}); + +export const CollectionForm: React.FC = ({ + onSubmit = () => {}, + id, + defaultValues, +}) => { + const { + control, + register, + reset, + handleSubmit, + formState: { errors }, + } = useForm>({ defaultValues }); + + React.useEffect(() => { + reset(); + }, [defaultValues]); + + return ( +
+ + + + ( + + )} + /> + + } + /> + +
+ ); +}; diff --git a/src/app/pages/collections/forms/collection.form.tsx b/src/app/pages/collections/forms/collection.form.tsx index 056a85d..7188ed0 100644 --- a/src/app/pages/collections/forms/collection.form.tsx +++ b/src/app/pages/collections/forms/collection.form.tsx @@ -1,13 +1,12 @@ -import { faPlus, faTrash } from '@fortawesome/free-solid-svg-icons'; -import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; -import { Button, Container, Input, Spacer, styled, Table, Text } from '@nextui-org/react'; -import { nanoid } from 'nanoid'; +import { Container, Input, Spacer } from '@nextui-org/react'; import React from 'react'; import { Controller, useForm } from 'react-hook-form'; -import { EzyButton, FileInput } from '@components'; +import { FileInput } from '@components'; import { Collection, CollectionType } from '@storage'; +import { IncludeDirectoriesField } from './fields'; + export interface CollectionFormProps { id?: string; @@ -16,90 +15,6 @@ export interface CollectionFormProps { onSubmit: (payload: Collection) => void; } -const PathComponent = styled(Text, { - overflowX: 'auto', - overflowY: 'hidden', - flex: 1, -}); - -type IncludeDirectoriesContainerProps = { - value?: string[]; - - onChange: (value: string[]) => void; -}; - -const IncludeDirectoriesContainer = React.forwardRef< - HTMLDivElement, - IncludeDirectoriesContainerProps ->(({ onChange, value = [] }, ref) => { - const [directories, setDirectories] = React.useState(value); - const handleAddPathButtonClick = async () => { - const paths = await window.electronDialog.open({ properties: ['openDirectory'] }); - const newDirectories = [...directories, ...paths]; - - setDirectories(newDirectories); - onChange(newDirectories); - }; - - const handleDeletePathButtonClick = (path: string) => () => { - const newDirectories = directories.filter((item) => item !== path); - - setDirectories(newDirectories); - onChange(newDirectories); - }; - - return ( -
-
- - Include directories - - - } - css={{ minWidth: 10, color: '$ezy', borderColor: '$accents3' }} - onClick={handleAddPathButtonClick} - /> -
- - - - Path - - - {directories.map((directory) => ( - - - {directory} - -
-
- ); -}); - export const CollectionForm: React.FC = ({ onSubmit = () => {}, id, @@ -166,7 +81,7 @@ export const CollectionForm: React.FC = ({ } + render={({ field }) => } /> diff --git a/src/app/pages/collections/forms/fields/include-directories/directory.node.tsx b/src/app/pages/collections/forms/fields/include-directories/directory.node.tsx new file mode 100644 index 0000000..9e3eb3b --- /dev/null +++ b/src/app/pages/collections/forms/fields/include-directories/directory.node.tsx @@ -0,0 +1,55 @@ +import { faTrash } from '@fortawesome/free-solid-svg-icons'; +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; +import { Button, Container, Row, Text } from '@nextui-org/react'; +import React from 'react'; + +import { TreeData, TreeNode, TreeNodeRendererProps } from '@components'; + +export type DirectoryNodeData = TreeData & { + value: string; +}; + +export const DirectoryNode: React.FC< + TreeNodeRendererProps & { onDirectoryRemove: (id: string) => void } +> = ({ data, onDirectoryRemove }) => { + const content = ( + + + {data.value} + + + ); + + const handleRemoveButtonClick = () => { + onDirectoryRemove(data.id); + }; + + const commandsContent = ( +