clone functionality in collection

This commit is contained in:
Akshat Khosya 2023-12-11 15:57:28 +05:30
parent 8ddec6bf0c
commit fff3e6d88a
5 changed files with 244 additions and 54 deletions

84
package-lock.json generated
View File

@ -14297,6 +14297,7 @@
},
"node_modules/react-is": {
"version": "18.2.0",
"dev": true,
"license": "MIT"
},
"node_modules/react-pdf": {
@ -17670,7 +17671,7 @@
},
"packages/bruno-electron": {
"name": "bruno",
"version": "v1.3.0",
"version": "v1.4.0",
"dependencies": {
"@aws-sdk/credential-providers": "^3.425.0",
"@usebruno/js": "0.9.3",
@ -21404,8 +21405,7 @@
}
},
"@tabler/icons": {
"version": "1.119.0",
"requires": {}
"version": "1.119.0"
},
"@tippyjs/react": {
"version": "4.2.6",
@ -21957,8 +21957,7 @@
}
},
"@usebruno/schema": {
"version": "file:packages/bruno-schema",
"requires": {}
"version": "file:packages/bruno-schema"
},
"@usebruno/testbench": {
"version": "file:packages/bruno-testbench",
@ -22102,8 +22101,7 @@
},
"@webpack-cli/configtest": {
"version": "1.2.0",
"dev": true,
"requires": {}
"dev": true
},
"@webpack-cli/info": {
"version": "1.5.0",
@ -22114,8 +22112,7 @@
},
"@webpack-cli/serve": {
"version": "1.7.0",
"dev": true,
"requires": {}
"dev": true
},
"@xtuc/ieee754": {
"version": "1.2.0",
@ -22197,8 +22194,7 @@
},
"ajv-keywords": {
"version": "3.5.2",
"dev": true,
"requires": {}
"dev": true
},
"amdefine": {
"version": "0.0.8"
@ -23147,8 +23143,7 @@
"chai-string": {
"version": "1.5.0",
"resolved": "https://registry.npmjs.org/chai-string/-/chai-string-1.5.0.tgz",
"integrity": "sha512-sydDC3S3pNAQMYwJrs6dQX0oBQ6KfIPuOZ78n7rocW0eJJlsHPh2t3kwW7xfwYA/1Bf6/arGtSUo16rxR2JFlw==",
"requires": {}
"integrity": "sha512-sydDC3S3pNAQMYwJrs6dQX0oBQ6KfIPuOZ78n7rocW0eJJlsHPh2t3kwW7xfwYA/1Bf6/arGtSUo16rxR2JFlw=="
},
"chalk": {
"version": "4.1.2",
@ -23583,8 +23578,7 @@
},
"css-declaration-sorter": {
"version": "6.3.1",
"dev": true,
"requires": {}
"dev": true
},
"css-loader": {
"version": "6.7.3",
@ -23703,8 +23697,7 @@
},
"cssnano-utils": {
"version": "3.1.0",
"dev": true,
"requires": {}
"dev": true
},
"csso": {
"version": "4.2.0",
@ -24931,8 +24924,7 @@
}
},
"goober": {
"version": "2.1.11",
"requires": {}
"version": "2.1.11"
},
"gopd": {
"version": "1.0.1",
@ -25326,8 +25318,7 @@
},
"icss-utils": {
"version": "5.1.0",
"dev": true,
"requires": {}
"dev": true
},
"idb": {
"version": "7.1.1"
@ -25927,8 +25918,7 @@
},
"jest-pnp-resolver": {
"version": "1.2.3",
"dev": true,
"requires": {}
"dev": true
},
"jest-regex-util": {
"version": "29.2.0",
@ -26647,8 +26637,7 @@
"merge-refs": {
"version": "1.2.2",
"resolved": "https://registry.npmjs.org/merge-refs/-/merge-refs-1.2.2.tgz",
"integrity": "sha512-RwcT7GsQR3KbuLw1rRuodq4Nt547BKEBkliZ0qqsrpyNne9bGTFtsFIsIpx82huWhcl3kOlOlH4H0xkPk/DqVw==",
"requires": {}
"integrity": "sha512-RwcT7GsQR3KbuLw1rRuodq4Nt547BKEBkliZ0qqsrpyNne9bGTFtsFIsIpx82huWhcl3kOlOlH4H0xkPk/DqVw=="
},
"merge-stream": {
"version": "2.0.0",
@ -26658,8 +26647,7 @@
"version": "1.4.1"
},
"meros": {
"version": "1.2.1",
"requires": {}
"version": "1.2.1"
},
"methods": {
"version": "1.1.2"
@ -27536,23 +27524,19 @@
},
"postcss-discard-comments": {
"version": "5.1.2",
"dev": true,
"requires": {}
"dev": true
},
"postcss-discard-duplicates": {
"version": "5.1.0",
"dev": true,
"requires": {}
"dev": true
},
"postcss-discard-empty": {
"version": "5.1.1",
"dev": true,
"requires": {}
"dev": true
},
"postcss-discard-overridden": {
"version": "5.1.0",
"dev": true,
"requires": {}
"dev": true
},
"postcss-js": {
"version": "3.0.3",
@ -27634,8 +27618,7 @@
},
"postcss-modules-extract-imports": {
"version": "3.0.0",
"dev": true,
"requires": {}
"dev": true
},
"postcss-modules-local-by-default": {
"version": "4.0.0",
@ -27668,8 +27651,7 @@
},
"postcss-normalize-charset": {
"version": "5.1.0",
"dev": true,
"requires": {}
"dev": true
},
"postcss-normalize-display-values": {
"version": "5.1.0",
@ -28145,11 +28127,11 @@
}
},
"react-inspector": {
"version": "6.0.2",
"requires": {}
"version": "6.0.2"
},
"react-is": {
"version": "18.2.0"
"version": "18.2.0",
"dev": true
},
"react-pdf": {
"version": "7.5.1",
@ -28311,8 +28293,7 @@
}
},
"redux-thunk": {
"version": "2.4.2",
"requires": {}
"version": "2.4.2"
},
"regenerate": {
"version": "1.4.2",
@ -28549,8 +28530,7 @@
},
"rollup-plugin-peer-deps-external": {
"version": "2.2.4",
"dev": true,
"requires": {}
"dev": true
},
"rollup-plugin-postcss": {
"version": "4.0.2",
@ -29108,8 +29088,7 @@
},
"style-loader": {
"version": "3.3.1",
"dev": true,
"requires": {}
"dev": true
},
"styled-components": {
"version": "5.3.6",
@ -29138,8 +29117,7 @@
}
},
"styled-jsx": {
"version": "5.0.7",
"requires": {}
"version": "5.0.7"
},
"stylehacks": {
"version": "5.1.1",
@ -29812,8 +29790,7 @@
}
},
"use-sync-external-store": {
"version": "1.2.0",
"requires": {}
"version": "1.2.0"
},
"utf8-byte-length": {
"version": "1.0.4",
@ -29974,8 +29951,7 @@
},
"acorn-import-assertions": {
"version": "1.8.0",
"dev": true,
"requires": {}
"dev": true
},
"schema-utils": {
"version": "3.1.1",

View File

@ -0,0 +1,155 @@
import React, { useRef, useEffect } from 'react';
import { useDispatch } from 'react-redux';
import { useFormik } from 'formik';
import * as Yup from 'yup';
import { browseDirectory } from 'providers/ReduxStore/slices/collections/actions';
import { cloneCollection } from 'providers/ReduxStore/slices/collections/actions';
import toast from 'react-hot-toast';
import Tooltip from 'components/Tooltip';
import Modal from 'components/Modal';
const CloneCollection = ({ onClose, collection }) => {
const inputRef = useRef();
const dispatch = useDispatch();
const formik = useFormik({
enableReinitialize: true,
initialValues: {
collectionName: '',
collectionFolderName: '',
collectionLocation: ''
},
validationSchema: Yup.object({
collectionName: Yup.string()
.min(1, 'must be at least 1 character')
.max(50, 'must be 50 characters or less')
.required('collection name is required'),
collectionFolderName: Yup.string()
.min(1, 'must be at least 1 character')
.max(50, 'must be 50 characters or less')
.matches(/^[\w\-. ]+$/, 'Folder name contains invalid characters')
.required('folder name is required'),
collectionLocation: Yup.string().min(1, 'location is required').required('location is required')
}),
onSubmit: (values) => {
dispatch(
cloneCollection(
values.collectionName,
values.collectionFolderName,
values.collectionLocation,
collection.pathname
)
)
.then(() => {
toast.success('Collection created');
onClose();
})
.catch(() => toast.error('An error occurred while creating the collection'));
}
});
const browse = () => {
dispatch(browseDirectory())
.then((dirPath) => {
// When the user closes the diolog without selecting anything dirPath will be false
if (typeof dirPath === 'string') {
formik.setFieldValue('collectionLocation', dirPath);
}
})
.catch((error) => {
formik.setFieldValue('collectionLocation', '');
console.error(error);
});
};
useEffect(() => {
if (inputRef && inputRef.current) {
inputRef.current.focus();
}
}, [inputRef]);
const onSubmit = () => formik.handleSubmit();
return (
<Modal size="sm" title="Clone Collection" confirmText="Create" handleConfirm={onSubmit} handleCancel={onClose}>
<form className="bruno-form" onSubmit={formik.handleSubmit}>
<div>
<label htmlFor="collection-name" className="flex items-center font-semibold">
Name
</label>
<input
id="collection-name"
type="text"
name="collectionName"
ref={inputRef}
className="block textbox mt-2 w-full"
onChange={(e) => {
formik.handleChange(e);
if (formik.values.collectionName === formik.values.collectionFolderName) {
formik.setFieldValue('collectionFolderName', e.target.value);
}
}}
autoComplete="off"
autoCorrect="off"
autoCapitalize="off"
spellCheck="false"
value={formik.values.collectionName || ''}
/>
{formik.touched.collectionName && formik.errors.collectionName ? (
<div className="text-red-500">{formik.errors.collectionName}</div>
) : null}
<label htmlFor="collection-location" className="block font-semibold mt-3">
Location
</label>
<input
id="collection-location"
type="text"
name="collectionLocation"
readOnly={true}
className="block textbox mt-2 w-full cursor-pointer"
autoComplete="off"
autoCorrect="off"
autoCapitalize="off"
spellCheck="false"
value={formik.values.collectionLocation || ''}
onClick={browse}
/>
{formik.touched.collectionLocation && formik.errors.collectionLocation ? (
<div className="text-red-500">{formik.errors.collectionLocation}</div>
) : null}
<div className="mt-1">
<span className="text-link cursor-pointer hover:underline" onClick={browse}>
Browse
</span>
</div>
<label htmlFor="collection-folder-name" className="flex items-center mt-3">
<span className="font-semibold">Folder Name</span>
<Tooltip
text="This folder will be created under the selected location"
tooltipId="collection-folder-name-tooltip"
/>
</label>
<input
id="collection-folder-name"
type="text"
name="collectionFolderName"
className="block textbox mt-2 w-full"
onChange={formik.handleChange}
autoComplete="off"
autoCorrect="off"
autoCapitalize="off"
spellCheck="false"
value={formik.values.collectionFolderName || ''}
/>
{formik.touched.collectionFolderName && formik.errors.collectionFolderName ? (
<div className="text-red-500">{formik.errors.collectionFolderName}</div>
) : null}
</div>
</form>
</Modal>
);
};
export default CloneCollection;

View File

@ -20,11 +20,13 @@ import exportCollection from 'utils/collections/export';
import RenameCollection from './RenameCollection';
import StyledWrapper from './StyledWrapper';
import CloneCollection from './CloneCollection/index';
const Collection = ({ collection, searchText }) => {
const [showNewFolderModal, setShowNewFolderModal] = useState(false);
const [showNewRequestModal, setShowNewRequestModal] = useState(false);
const [showRenameCollectionModal, setShowRenameCollectionModal] = useState(false);
const [showCloneCollectionModalOpen, setShowCloneCollectionModalOpen] = useState(false);
const [showExportCollectionModal, setShowExportCollectionModal] = useState(false);
const [showRemoveCollectionModal, setShowRemoveCollectionModal] = useState(false);
const [collectionIsCollapsed, setCollectionIsCollapsed] = useState(collection.collapsed);
@ -133,6 +135,9 @@ const Collection = ({ collection, searchText }) => {
{showExportCollectionModal && (
<ExportCollection collection={collection} onClose={() => setShowExportCollectionModal(false)} />
)}
{showCloneCollectionModalOpen && (
<CloneCollection collection={collection} onClose={() => setShowCloneCollectionModalOpen(false)} />
)}
<div className="flex py-1 collection-name items-center" ref={drop}>
<div
className="flex flex-grow items-center overflow-hidden"
@ -169,6 +174,15 @@ const Collection = ({ collection, searchText }) => {
>
New Folder
</div>
<div
className="dropdown-item"
onClick={(e) => {
menuDropdownTippyRef.current.hide();
setShowCloneCollectionModalOpen(true);
}}
>
Clone
</div>
<div
className="dropdown-item"
onClick={(e) => {

View File

@ -939,7 +939,16 @@ export const createCollection = (collectionName, collectionFolderName, collectio
.catch(reject);
});
};
export const cloneCollection = (collectionName, collectionFolderName, collectionLocation, perviousPath) => () => {
const { ipcRenderer } = window;
return new Promise((resolve, reject) => {
ipcRenderer
.invoke('renderer:clone-collection', collectionName, collectionFolderName, collectionLocation, perviousPath)
.then(resolve)
.catch(reject);
});
};
export const openCollection = () => () => {
return new Promise((resolve, reject) => {
const { ipcRenderer } = window;

View File

@ -70,7 +70,43 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
}
}
);
// clone collection
ipcMain.handle(
'renderer:clone-collection',
async (event, collectionName, collectionFolderName, collectionLocation, perviousPath) => {
try {
const dirPath = path.join(collectionLocation, collectionFolderName);
if (fs.existsSync(dirPath)) {
throw new Error(`collection: ${dirPath} already exists`);
}
if (!isValidPathname(dirPath)) {
throw new Error(`collection: invalid pathname - ${dir}`);
}
await createDirectory(dirPath);
const uid = generateUidBasedOnHash(dirPath);
const brunoJsonFilePath = path.join(perviousPath, 'bruno.json');
const content = fs.readFileSync(brunoJsonFilePath, 'utf8');
let json = JSON.parse(content);
json.name = collectionName;
const cont = await stringifyJson(json);
await writeFile(path.join(dirPath, 'bruno.json'), cont);
const files = searchForBruFiles(perviousPath);
console.log(files);
for (const sourceFilePath of files) {
const fileName = path.basename(sourceFilePath);
const destinationFilePath = path.join(dirPath, fileName);
console.log(destinationFilePath);
fs.copyFileSync(sourceFilePath, destinationFilePath);
}
mainWindow.webContents.send('main:collection-opened', dirPath, uid, json);
ipcMain.emit('main:collection-opened', mainWindow, dirPath, uid);
} catch (error) {
return Promise.reject(error);
}
}
);
// rename collection
ipcMain.handle('renderer:rename-collection', async (event, newName, collectionPathname) => {
try {