2024-01-05 08:46:03 +00:00
|
|
|
import {
|
|
|
|
useItemsAsEphemeralPaths,
|
|
|
|
useItemsAsFilePaths,
|
|
|
|
useLibraryMutation,
|
|
|
|
useSelector
|
|
|
|
} from '@sd/client';
|
2023-10-24 07:51:58 +00:00
|
|
|
import { toast } from '@sd/ui';
|
|
|
|
import { useExplorerContext } from '~/app/$libraryId/Explorer/Context';
|
2024-01-05 08:46:03 +00:00
|
|
|
import { explorerStore } from '~/app/$libraryId/Explorer/store';
|
2023-10-24 07:51:58 +00:00
|
|
|
import { useExplorerSearchParams } from '~/app/$libraryId/Explorer/util';
|
2023-10-15 10:47:03 +00:00
|
|
|
|
2024-02-07 13:47:55 +00:00
|
|
|
import { useLocale } from './useLocale';
|
2023-11-06 10:37:24 +00:00
|
|
|
import { useShortcut } from './useShortcut';
|
2023-10-15 10:47:03 +00:00
|
|
|
|
2023-10-16 15:29:18 +00:00
|
|
|
export const useKeyCopyCutPaste = () => {
|
2024-02-07 13:47:55 +00:00
|
|
|
const { t } = useLocale();
|
|
|
|
|
2024-01-05 08:46:03 +00:00
|
|
|
const cutCopyState = useSelector(explorerStore, (s) => s.cutCopyState);
|
2023-10-15 10:47:03 +00:00
|
|
|
const [{ path }] = useExplorerSearchParams();
|
|
|
|
|
|
|
|
const copyFiles = useLibraryMutation('files.copyFiles');
|
2023-11-03 17:06:34 +00:00
|
|
|
const copyEphemeralFiles = useLibraryMutation('ephemeralFiles.copyFiles');
|
2023-10-15 10:47:03 +00:00
|
|
|
const cutFiles = useLibraryMutation('files.cutFiles');
|
2023-11-03 17:06:34 +00:00
|
|
|
const cutEphemeralFiles = useLibraryMutation('ephemeralFiles.cutFiles');
|
2023-10-15 10:47:03 +00:00
|
|
|
const explorer = useExplorerContext();
|
2023-11-03 17:06:34 +00:00
|
|
|
|
|
|
|
const { parent } = explorer;
|
|
|
|
|
2023-10-15 10:47:03 +00:00
|
|
|
const selectedFilePaths = useItemsAsFilePaths(Array.from(explorer.selectedItems));
|
2023-11-03 17:06:34 +00:00
|
|
|
const selectedEphemeralPaths = useItemsAsEphemeralPaths(Array.from(explorer.selectedItems));
|
|
|
|
|
|
|
|
const indexedArgs =
|
2023-11-06 10:37:24 +00:00
|
|
|
parent?.type === 'Location'
|
2023-11-03 17:06:34 +00:00
|
|
|
? {
|
|
|
|
sourceLocationId: parent.location.id,
|
|
|
|
sourcePathIds: selectedFilePaths.map((p) => p.id)
|
|
|
|
}
|
|
|
|
: undefined;
|
|
|
|
|
|
|
|
const ephemeralArgs =
|
2023-11-06 10:37:24 +00:00
|
|
|
parent?.type === 'Ephemeral'
|
2023-11-03 17:06:34 +00:00
|
|
|
? { sourcePaths: selectedEphemeralPaths.map((p) => p.path) }
|
|
|
|
: undefined;
|
2023-10-15 10:47:03 +00:00
|
|
|
|
2023-11-06 10:37:24 +00:00
|
|
|
useShortcut('copyObject', (e) => {
|
2023-10-15 10:47:03 +00:00
|
|
|
e.stopPropagation();
|
2023-10-24 07:51:58 +00:00
|
|
|
if (explorer.parent?.type === 'Location') {
|
2024-01-05 08:46:03 +00:00
|
|
|
explorerStore.cutCopyState = {
|
2023-10-15 10:47:03 +00:00
|
|
|
sourceParentPath: path ?? '/',
|
2023-11-06 10:37:24 +00:00
|
|
|
type: 'Copy',
|
2023-11-03 17:06:34 +00:00
|
|
|
indexedArgs,
|
2023-11-25 01:49:52 +00:00
|
|
|
ephemeralArgs
|
2023-10-15 10:47:03 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-11-06 10:37:24 +00:00
|
|
|
useShortcut('cutObject', (e) => {
|
2023-10-15 10:47:03 +00:00
|
|
|
e.stopPropagation();
|
|
|
|
if (explorer.parent?.type === 'Location') {
|
2024-01-05 08:46:03 +00:00
|
|
|
explorerStore.cutCopyState = {
|
2023-10-15 10:47:03 +00:00
|
|
|
sourceParentPath: path ?? '/',
|
2023-11-06 10:37:24 +00:00
|
|
|
type: 'Cut',
|
2023-11-03 17:06:34 +00:00
|
|
|
indexedArgs,
|
2023-11-25 01:49:52 +00:00
|
|
|
ephemeralArgs
|
2023-10-15 10:47:03 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-11-06 10:37:24 +00:00
|
|
|
useShortcut('duplicateObject', async (e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
if (parent?.type === 'Location') {
|
|
|
|
try {
|
|
|
|
await copyFiles.mutateAsync({
|
|
|
|
source_location_id: parent.location.id,
|
|
|
|
sources_file_path_ids: selectedFilePaths.map((p) => p.id),
|
|
|
|
target_location_id: parent.location.id,
|
|
|
|
target_location_relative_directory_path: path ?? '/'
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
toast.error({
|
2024-02-07 13:47:55 +00:00
|
|
|
title: t('failed_to_duplicate_file'),
|
2023-11-06 10:37:24 +00:00
|
|
|
body: `Error: ${error}.`
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
useShortcut('pasteObject', async (e) => {
|
2023-10-15 10:47:03 +00:00
|
|
|
e.stopPropagation();
|
|
|
|
const parent = explorer.parent;
|
2023-11-03 17:06:34 +00:00
|
|
|
if (
|
|
|
|
(parent?.type === 'Location' || parent?.type === 'Ephemeral') &&
|
|
|
|
cutCopyState.type !== 'Idle' &&
|
|
|
|
path
|
|
|
|
) {
|
|
|
|
const { type, sourceParentPath, indexedArgs, ephemeralArgs } = cutCopyState;
|
2023-10-15 10:47:03 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (type == 'Copy') {
|
2023-11-03 17:06:34 +00:00
|
|
|
if (parent?.type === 'Location' && indexedArgs != undefined) {
|
|
|
|
await copyFiles.mutateAsync({
|
|
|
|
source_location_id: indexedArgs.sourceLocationId,
|
|
|
|
sources_file_path_ids: [...indexedArgs.sourcePathIds],
|
|
|
|
target_location_id: parent.location.id,
|
|
|
|
target_location_relative_directory_path: path
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parent?.type === 'Ephemeral' && ephemeralArgs != undefined) {
|
|
|
|
await copyEphemeralFiles.mutateAsync({
|
|
|
|
sources: [...ephemeralArgs.sourcePaths],
|
|
|
|
target_dir: path
|
|
|
|
});
|
|
|
|
}
|
2023-10-15 10:47:03 +00:00
|
|
|
} else {
|
2023-11-03 17:06:34 +00:00
|
|
|
if (parent?.type === 'Location' && indexedArgs != undefined) {
|
|
|
|
if (
|
|
|
|
indexedArgs.sourceLocationId === parent.location.id &&
|
|
|
|
sourceParentPath === path
|
|
|
|
) {
|
2024-02-07 13:47:55 +00:00
|
|
|
toast.error(t('file_already_exist_in_this_location'));
|
2023-11-03 17:06:34 +00:00
|
|
|
}
|
|
|
|
await cutFiles.mutateAsync({
|
|
|
|
source_location_id: indexedArgs.sourceLocationId,
|
|
|
|
sources_file_path_ids: [...indexedArgs.sourcePathIds],
|
|
|
|
target_location_id: parent.location.id,
|
|
|
|
target_location_relative_directory_path: path
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parent?.type === 'Ephemeral' && ephemeralArgs != undefined) {
|
|
|
|
if (sourceParentPath !== path) {
|
|
|
|
await cutEphemeralFiles.mutateAsync({
|
|
|
|
sources: [...ephemeralArgs.sourcePaths],
|
|
|
|
target_dir: path
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2023-10-15 10:47:03 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2024-02-07 13:47:55 +00:00
|
|
|
if (type === 'Copy') {
|
|
|
|
toast.error({
|
|
|
|
title: t('failed_to_copy_file'),
|
|
|
|
body: `Error: ${error}.`
|
|
|
|
});
|
|
|
|
} else if (type === 'Cut') {
|
|
|
|
toast.error({
|
|
|
|
title: t('failed_to_cut_file'),
|
|
|
|
body: `Error: ${error}.`
|
|
|
|
});
|
|
|
|
}
|
2023-10-15 10:47:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2023-10-24 07:51:58 +00:00
|
|
|
};
|