import clsx from 'clsx'; import { CaretRight, Spinner } from 'phosphor-react'; import { useState } from 'react'; import { HASHING_ALGOS, hashingAlgoSlugSchema, useLibraryMutation, useLibraryQuery } from '@sd/client'; import { Button, SelectOption, forms } from '@sd/ui'; import { Form } from '~/../packages/ui/src/forms'; const { z, useZodForm, PasswordInput, Select } = forms; const schema = z .object({ password: z.string(), passwordValidate: z.string(), algorithm: z.enum(['XChaCha20Poly1305', 'Aes256Gcm']), hashingAlgorithm: hashingAlgoSlugSchema }) .superRefine((data, ctx) => { if (!data.password || !data.passwordValidate) { ctx.addIssue({ code: 'custom', path: ['password', 'passwordValidate'], message: 'Password is required' }); } if (data.password && data.password !== data.passwordValidate) { ctx.addIssue({ code: 'custom', path: ['passwordValidate'], message: 'Passwords do not match' }); } }); export default () => { const isSetup = useLibraryQuery(['keys.isSetup']); const setupKeyManager = useLibraryMutation('keys.setup'); const [showAdvancedOptions, setShowAdvancedOptions] = useState(false); const form = useZodForm({ schema: schema, defaultValues: { password: '', passwordValidate: '', algorithm: 'XChaCha20Poly1305', hashingAlgorithm: 'Argon2id-s' } }); const onSubmit = form.handleSubmit((data) => setupKeyManager .mutateAsync({ password: data.password, algorithm: data.algorithm, hashing_algorithm: HASHING_ALGOS[data.hashingAlgorithm] }) .then(() => isSetup.refetch()) ); const isSettingUp = setupKeyManager.isLoading || form.formState.isSubmitting; return (
); };