import clsx from 'clsx';
import {
ArrowBendUpRight,
Copy,
FileX,
LockSimple,
LockSimpleOpen,
Package,
Plus,
Scissors,
Share,
TagSimple,
Trash,
TrashSimple
} from 'phosphor-react';
import { PropsWithChildren } from 'react';
import {
ExplorerItem,
isObject,
useLibraryContext,
useLibraryMutation,
useLibraryQuery
} from '@sd/client';
import { ContextMenu, dialogManager } from '@sd/ui';
import { showAlertDialog } from '~/components';
import { getExplorerStore, useExplorerStore, useOperatingSystem } from '~/hooks';
import { usePlatform } from '~/util/Platform';
import AssignTagMenuItems from '../AssignTagMenuItems';
import { OpenInNativeExplorer } from '../ContextMenu';
import { getItemFilePath, useExplorerSearchParams } from '../util';
import OpenWith from './ContextMenu/OpenWith';
import DecryptDialog from './DecryptDialog';
import DeleteDialog from './DeleteDialog';
import EncryptDialog from './EncryptDialog';
import EraseDialog from './EraseDialog';
interface Props extends PropsWithChildren {
data: ExplorerItem;
className?: string;
}
export default ({ data, className, ...props }: Props) => {
const store = useExplorerStore();
const [params] = useExplorerSearchParams();
const objectData = data ? (isObject(data) ? data.item : data.item.object) : null;
const keyManagerUnlocked = useLibraryQuery(['keys.isUnlocked']).data ?? false;
const mountedKeys = useLibraryQuery(['keys.listMounted']);
const hasMountedKeys = mountedKeys.data?.length ?? 0 > 0;
const copyFiles = useLibraryMutation('files.copyFiles');
const removeFromRecents = useLibraryMutation('files.removeAccessTime');
const generateThumbnails = useLibraryMutation('jobs.generateThumbsForLocation');
const fullRescan = useLibraryMutation('locations.fullRescan');
return (
e.stopPropagation()} className={clsx('flex', className)}>
{!store.showInspector && (
<>
(getExplorerStore().showInspector = true)}
/>
>
)}
(getExplorerStore().isRenaming = true)}
/>
{data.type == 'Path' && data.item.object && data.item.object.date_accessed && (
data.item.object_id && removeFromRecents.mutate(data.item.object_id)
}
/>
)}
{
if (params.path === undefined) return;
getExplorerStore().cutCopyState = {
sourcePath: params.path,
sourceLocationId: store.locationId!,
sourcePathId: data.item.id,
actionType: 'Cut',
active: true
};
}}
icon={Scissors}
/>
{
if (params.path === undefined) return;
getExplorerStore().cutCopyState = {
sourcePath: params.path,
sourceLocationId: store.locationId!,
sourcePathId: data.item.id,
actionType: 'Copy',
active: true
};
}}
icon={Copy}
/>
{
if (params.path === undefined) return;
copyFiles.mutate({
source_location_id: store.locationId!,
source_path_id: data.item.id,
target_location_id: store.locationId!,
target_path: params.path,
target_file_name_suffix: ' copy'
});
}}
/>
{
getExplorerStore().cutCopyState = {
...store.cutCopyState,
active: false
};
}}
icon={FileX}
/>
{
e.preventDefault();
navigator.share?.({
title: 'Spacedrive',
text: 'Check out this cool app',
url: 'https://spacedrive.com'
});
}}
/>
{objectData && (
)}
{
if (keyManagerUnlocked && hasMountedKeys) {
dialogManager.create((dp) => (
));
} else if (!keyManagerUnlocked) {
showAlertDialog({
title: 'Key manager locked',
value: 'The key manager is currently locked. Please unlock it and try again.'
});
} else if (!hasMountedKeys) {
showAlertDialog({
title: 'No mounted keys',
value: 'No mounted keys were found. Please mount a key and try again.'
});
}
}}
/>
{/* should only be shown if the file is a valid spacedrive-encrypted file (preferably going from the magic bytes) */}
{
if (keyManagerUnlocked) {
dialogManager.create((dp) => (
));
} else {
showAlertDialog({
title: 'Key manager locked',
value: 'The key manager is currently locked. Please unlock it and try again.'
});
}
}}
/>
{
fullRescan.mutate(getExplorerStore().locationId!);
}}
label="Rescan Directory"
icon={Package}
/>
{
generateThumbnails.mutate({
id: getExplorerStore().locationId!,
path: '/'
});
}}
label="Regen Thumbnails"
icon={Package}
/>
{
dialogManager.create((dp) => (
));
}}
/>
{
dialogManager.create((dp) => (
));
}}
/>
);
};
const OpenOrDownloadOptions = (props: { data: ExplorerItem }) => {
const os = useOperatingSystem();
const { openFilePath } = usePlatform();
const updateAccessTime = useLibraryMutation('files.updateAccessTime');
const filePath = getItemFilePath(props.data);
const { library } = useLibraryContext();
if (os === 'browser') return ;
else
return (
<>
{filePath && (
<>
{openFilePath && (
{
props.data.type === 'Path' &&
props.data.item.object_id &&
updateAccessTime.mutate(props.data.item.object_id);
openFilePath(library.uuid, filePath.id);
}}
/>
)}
>
)}
(getExplorerStore().quickViewObject = props.data)}
/>
>
);
};