import React, { useRef, useEffect, forwardRef } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import { useFormik } from 'formik'; import * as Yup from 'yup'; import path from 'path'; import { browseDirectory, createCollection } from 'providers/ReduxStore/slices/collections/actions'; import toast from 'react-hot-toast'; import Portal from 'components/Portal'; import Modal from 'components/Modal'; import { sanitizeName, validateName, validateNameError } from 'utils/common/regex'; import PathDisplay from 'components/PathDisplay/index'; import { useState } from 'react'; import { IconArrowBackUp, IconEdit, IconCaretDown } from '@tabler/icons'; import Help from 'components/Help'; import { multiLineMsg } from 'utils/common'; import { formatIpcError } from 'utils/common/error'; import { toggleSidebarCollapse } from 'providers/ReduxStore/slices/app'; import Dropdown from 'components/Dropdown'; import StyledWrapper from './StyledWrapper'; import get from 'lodash/get'; const CreateCollection = ({ onClose, defaultLocation: propDefaultLocation }) => { const inputRef = useRef(); const dispatch = useDispatch(); const workspaces = useSelector((state) => state.workspaces?.workspaces || []); const workspaceUid = useSelector((state) => state.workspaces?.activeWorkspaceUid); const [isEditing, toggleEditing] = useState(false); const preferences = useSelector((state) => state.app.preferences); const [showExternalLocation, setShowExternalLocation] = useState(false); const [showAdvanced, setShowAdvanced] = useState(true); const dropdownTippyRef = useRef(); const onDropdownCreate = (ref) => (dropdownTippyRef.current = ref); const activeWorkspace = workspaces.find((w) => w.uid === workspaceUid); const isDefaultWorkspace = activeWorkspace?.type === 'default'; const hideLocationInput = activeWorkspace && activeWorkspace.type !== 'default' && !!activeWorkspace?.pathname; const defaultLocation = isDefaultWorkspace ? get(preferences, 'general.defaultCollectionLocation', '') : (activeWorkspace?.pathname ? `${activeWorkspace.pathname}/collections` : ''); const shouldShowAccordion = workspaceUid && hideLocationInput && !isDefaultWorkspace; const actuallyHideLocationInput = hideLocationInput && !showExternalLocation && !isDefaultWorkspace; const formik = useFormik({ enableReinitialize: true, initialValues: { collectionName: '', collectionFolderName: '', collectionLocation: defaultLocation || '', format: 'bru' }, validationSchema: Yup.object({ collectionName: Yup.string() .min(1, 'must be at least 1 character') .max(255, 'must be 255 characters or less') .required('collection name is required'), collectionFolderName: Yup.string() .min(1, 'must be at least 1 character') .max(255, 'must be 255 characters or less') .test('is-valid-collection-name', function (value) { const isValid = validateName(value); return isValid ? true : this.createError({ message: validateNameError(value) }); }) .required('folder name is required'), collectionLocation: actuallyHideLocationInput ? Yup.string() // Optional for workspaces when not using external location : Yup.string().min(1, 'location is required').required('location is required'), format: Yup.string().oneOf(['bru', 'yml'], 'invalid format').required('format is required') }), onSubmit: async (values) => { try { const currentWorkspace = workspaces.find((w) => w.uid === workspaceUid); const useExternalLocation = workspaceUid && showExternalLocation && values.collectionLocation; let collectionLocation = values.collectionLocation; if (workspaceUid && !useExternalLocation && currentWorkspace && currentWorkspace.type !== 'default') { collectionLocation = path.join(currentWorkspace.pathname, 'collections'); } await dispatch(createCollection(values.collectionName, values.collectionFolderName, collectionLocation, { format: values.format })); if (useExternalLocation && currentWorkspace) { const { ipcRenderer } = window; const collectionPath = path.join(values.collectionLocation, values.collectionFolderName); const workspaceCollection = { name: values.collectionName, path: collectionPath }; await ipcRenderer.invoke('renderer:add-collection-to-workspace', currentWorkspace.pathname, workspaceCollection); } toast.success('Collection created!'); onClose(); } catch (e) { toast.error(multiLineMsg('An error occurred while creating the collection', formatIpcError(e))); } } }); const browse = () => { dispatch(browseDirectory()) .then((dirPath) => { if (typeof dirPath === 'string') { formik.setFieldValue('collectionLocation', dirPath); } }) .catch(() => { formik.setFieldValue('collectionLocation', ''); }); }; useEffect(() => { if (inputRef && inputRef.current) { inputRef.current.focus(); } }, [inputRef]); const AdvancedOptions = forwardRef((props, ref) => { return (