import { RadioGroup } from '@headlessui/react'; import { Eye, EyeSlash, Info } from 'phosphor-react'; import { useState } from 'react'; import { useLibraryMutation, useLibraryQuery } from '@sd/client'; import { Button, Dialog, UseDialogProps, useDialog } from '@sd/ui'; import { Input, Switch, useZodForm, z } from '@sd/ui/src/forms'; import { showAlertDialog } from '~/util/dialog'; import { usePlatform } from '../../util/Platform'; import { Tooltip } from '../tooltip/Tooltip'; interface DecryptDialogProps extends UseDialogProps { location_id: number; path_id: number; } const schema = z.object({ type: z.union([z.literal('password'), z.literal('key')]), outputPath: z.string(), password: z.string(), saveToKeyManager: z.boolean() }); export const DecryptFileDialog = (props: DecryptDialogProps) => { const platform = usePlatform(); const dialog = useDialog(props); 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: () => { showAlertDialog({ title: 'Success', value: 'The decryption job has started successfully. You may track the progress in the job overview panel.' }); }, onError: () => { showAlertDialog({ title: 'Error', value: 'The decryption job failed to start.' }); } }); const [show, setShow] = useState({ password: false }); const PasswordCurrentEyeIcon = show.password ? EyeSlash : Eye; const form = useZodForm({ defaultValues: { type: hasMountedKeys ? 'key' : 'password', saveToKeyManager: true, outputPath: '', password: '' }, schema }); const onSubmit = form.handleSubmit((data) => decryptFile.mutateAsync({ location_id: props.location_id, path_id: props.path_id, output_path: data.outputPath !== '' ? data.outputPath : null, password: data.type === 'password' ? data.password : null, save_to_library: data.type === 'password' ? data.saveToKeyManager : null }) ); return ( form.setValue('type', e)} className="mt-2" > Key Type
{({ checked }) => ( )} {({ checked }) => ( )}
{form.watch('type') === 'password' && ( <>
Save to Key Manager
)}
Output file
); };