Files
spacedrive/packages/interface/src/components/dialog/DecryptFileDialog.tsx
jake d15577d22a [ENG-315] Key viewer/general improvements (#483)
* add encryption+hashing algorithm to key viewer dialog

* abstract `save()` and `open()` with `Platform`

* add automount to `StoredKey` and offer option to enable/disable it for keys

* fix missing statement exec

* general tidy up

* add sync to library button and cleanup default setting code

* use alert dialogs for unsupported `open`/`save` operations (web)

* tweak alert message
2022-12-13 09:48:14 +08:00

102 lines
2.7 KiB
TypeScript

import { useLibraryMutation } from '@sd/client';
import { Button, Dialog } from '@sd/ui';
import { useState } from 'react';
import { usePlatform } from '../../util/Platform';
import { GenericAlertDialogProps } from './AlertDialog';
interface DecryptDialogProps {
open: boolean;
setOpen: (isShowing: boolean) => void;
location_id: number | null;
object_id: number | null;
setAlertDialogData: (data: GenericAlertDialogProps) => void;
}
export const DecryptFileDialog = (props: DecryptDialogProps) => {
const platform = usePlatform();
const { location_id, object_id } = props;
const decryptFile = useLibraryMutation('files.decryptFiles');
const [outputPath, setOutputpath] = useState('');
return (
<>
<Dialog
open={props.open}
setOpen={props.setOpen}
title="Decrypt a file"
description="Leave the output file blank for the default."
loading={decryptFile.isLoading}
ctaLabel="Decrypt"
ctaAction={() => {
const output = outputPath !== '' ? outputPath : null;
props.setOpen(false);
location_id &&
object_id &&
decryptFile.mutate(
{
location_id,
object_id,
output_path: output
},
{
onSuccess: () => {
props.setAlertDialogData({
open: true,
title: 'Info',
value:
'The decryption job has started successfully. You may track the progress in the job overview panel.',
inputBox: false,
description: ''
});
},
onError: () => {
props.setAlertDialogData({
open: true,
title: 'Error',
value: 'The decryption job failed to start.',
inputBox: false,
description: ''
});
}
}
);
}}
>
<div className="grid w-full grid-cols-2 gap-4 mt-4 mb-3">
<div className="flex flex-col">
<span className="text-xs font-bold">Output file</span>
<Button
size="sm"
variant={outputPath !== '' ? 'accent' : 'gray'}
className="h-[23px] text-xs leading-3 mt-2"
type="button"
onClick={() => {
// if we allow the user to encrypt multiple files simultaneously, this should become a directory instead
if (!platform.saveFilePickerDialog) {
// TODO: Support opening locations on web
props.setAlertDialogData({
open: true,
title: 'Error',
description: '',
value: "System dialogs aren't supported on this platform.",
inputBox: false
});
return;
}
platform.saveFilePickerDialog().then((result) => {
if (result) setOutputpath(result as string);
});
}}
>
Select
</Button>
</div>
</div>
</Dialog>
</>
);
};