import { RadioGroup } from '@headlessui/react'; import { useLibraryMutation, useLibraryQuery } from '@sd/client'; import { Button, Dialog, Input, Switch } from '@sd/ui'; import { Eye, EyeSlash, Info } from 'phosphor-react'; import { useState } from 'react'; import { useForm } from 'react-hook-form'; import { usePlatform } from '../../util/Platform'; import { Tooltip } from '../tooltip/Tooltip'; import { GenericAlertDialogProps } from './AlertDialog'; interface DecryptDialogProps { open: boolean; setOpen: (isShowing: boolean) => void; location_id: number | null; path_id: number | undefined; setAlertDialogData: (data: GenericAlertDialogProps) => void; } type FormValues = { type: 'password' | 'key'; outputPath: string; password: string; saveToKeyManager: boolean; }; export const DecryptFileDialog = (props: DecryptDialogProps) => { const platform = usePlatform(); const mountedUuids = useLibraryQuery(['keys.listMounted'], { onSuccess: (data) => { hasMountedKeys = data.length > 0 ? true : false; if (!hasMountedKeys) { form.setValue('type', 'password'); } else { form.setValue('type', 'key'); } } }); let hasMountedKeys = mountedUuids.data !== undefined && mountedUuids.data.length > 0 ? true : false; const decryptFile = useLibraryMutation('files.decryptFiles', { 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: '' }); } }); const [show, setShow] = useState({ password: false }); const PasswordCurrentEyeIcon = show.password ? EyeSlash : Eye; const form = useForm({ defaultValues: { type: hasMountedKeys ? 'key' : 'password', outputPath: '', password: '', saveToKeyManager: true } }); const onSubmit = form.handleSubmit((data) => { const output = data.outputPath !== '' ? data.outputPath : null; const pw = data.type === 'password' ? data.password : null; const save = data.type === 'password' ? data.saveToKeyManager : null; props.setOpen(false); props.location_id && props.path_id && decryptFile.mutate({ location_id: props.location_id, path_id: props.path_id, output_path: output, password: pw, save_to_library: save }); form.reset(); }); return (
form.setValue('type', e)} className="mt-2" > Key Type
{({ checked }) => ( )} {({ checked }) => ( )}
{form.watch('type') === 'password' && ( <>
form.setValue('saveToKeyManager', e)} />
Save to Key Manager
)}
Output file
); };