Files
spacedrive/interface/hooks/useKeyCopyCutPaste.ts
Vítor Vasconcellos f7277d602f Frontend misc fixes and deps update (#1650)
Dependencies overhaul
 - Update dependencies for all projects (except Mobile-only deps)
 - Remove unused dependencies from all projects (except Mobile-only deps)
 - Fix Storybook failing to import sd/ui style
 - Add Node 21 as not supported due to sass-loader not working on it yet
 - Add work-around for new rook version requiring webpack specific global property
 - Fix landing dev not working due to missing default env value on dev
 - Fix some incorrect uses of phosphor-icons non server side icons on server components on landing
 - Fix some incorrect uses of phosphor-icons server side icon on client components on landing
 - Fix landing fail to build on dev due to always required a Github Token to get the latest release
 - Fix new Next.js version not suporting Response.redirect due to immutable Headers
 - Add Gitlab as social link for teams page
 - Update Vítor's team photo
 - Add Vítor's twitter link
 - Fix some warning due to missing useEffect dependencies
 - Remove test-files dir
 - Fix QuickPreview unblurred buttons in Linux
 - Formatting
2023-10-24 07:51:58 +00:00

80 lines
2.5 KiB
TypeScript

import { useKeys } from 'rooks';
import { useItemsAsFilePaths, useLibraryMutation } from '@sd/client';
import { toast } from '@sd/ui';
import { useExplorerContext } from '~/app/$libraryId/Explorer/Context';
import { getExplorerStore, useExplorerStore } from '~/app/$libraryId/Explorer/store';
import { useExplorerSearchParams } from '~/app/$libraryId/Explorer/util';
import { useKeyMatcher } from './useKeyMatcher';
export const useKeyCopyCutPaste = () => {
const { cutCopyState } = useExplorerStore();
const [{ path }] = useExplorerSearchParams();
const metaCtrlKey = useKeyMatcher('Meta').key;
const copyFiles = useLibraryMutation('files.copyFiles');
const cutFiles = useLibraryMutation('files.cutFiles');
const explorer = useExplorerContext();
const selectedFilePaths = useItemsAsFilePaths(Array.from(explorer.selectedItems));
useKeys([metaCtrlKey, 'KeyC'], (e) => {
e.stopPropagation();
if (explorer.parent?.type === 'Location') {
getExplorerStore().cutCopyState = {
sourceParentPath: path ?? '/',
sourceLocationId: explorer.parent.location.id,
sourcePathIds: selectedFilePaths.map((p) => p.id),
type: 'Copy'
};
}
});
useKeys([metaCtrlKey, 'KeyX'], (e) => {
e.stopPropagation();
if (explorer.parent?.type === 'Location') {
getExplorerStore().cutCopyState = {
sourceParentPath: path ?? '/',
sourceLocationId: explorer.parent.location.id,
sourcePathIds: selectedFilePaths.map((p) => p.id),
type: 'Cut'
};
}
});
useKeys([metaCtrlKey, 'KeyV'], async (e) => {
e.stopPropagation();
const parent = explorer.parent;
if (parent?.type === 'Location' && cutCopyState.type !== 'Idle' && path) {
const { type, sourcePathIds, sourceParentPath, sourceLocationId } = cutCopyState;
const sameLocation =
sourceLocationId === parent.location.id && sourceParentPath === path;
try {
if (type == 'Copy') {
await copyFiles.mutateAsync({
source_location_id: sourceLocationId,
sources_file_path_ids: [...sourcePathIds],
target_location_id: parent.location.id,
target_location_relative_directory_path: path
});
} else if (sameLocation) {
toast.error('File already exists in this location');
} else {
await cutFiles.mutateAsync({
source_location_id: sourceLocationId,
sources_file_path_ids: [...sourcePathIds],
target_location_id: parent.location.id,
target_location_relative_directory_path: path
});
}
} catch (error) {
toast.error({
title: `Failed to ${type.toLowerCase()} file`,
body: `Error: ${error}.`
});
}
}
});
};