import { useForm } from 'react-hook-form'; import { useEffect, useState } from 'react'; import { PreferencesSchema } from '~common/schemas'; import zodResolver from '~renderer/utils/zodResolver'; import { api } from '~renderer/utils/api'; import { useT } from '~renderer/i18n'; import TextButton from './styled/TextButton'; import FilePickerInput from './form/FilePickerInput'; import CheckboxInput from './form/CheckboxInput'; import CloseButton from './styled/CloseButton'; type Props = { close: () => void }; const ClientDirDialog = ({ close }: Props) => { const t = useT(); const { data: pref } = api.preferences.get.useQuery(); const setPref = api.preferences.set.useMutation(); const isValidClientDir = api.preferences.isValidClientDir.useQuery( pref?.clientDir, { enabled: !!pref?.isPortable } ); const verify = api.updater.verify.useMutation(); const { register, handleSubmit, watch, formState, setValue, setError, reset } = useForm({ defaultValues: { clientDir: pref?.clientDir ?? '' }, resolver: zodResolver(PreferencesSchema.pick({ clientDir: true })) }); const chosen = watch('clientDir'); const [acceptEmpty, setAcceptEmpty] = useState(false); const chosenIsClient = api.preferences.isValidClientDir.useQuery(chosen, { enabled: !!chosen && !pref?.isPortable }); const needsEmptyConfirm = !!chosen && chosenIsClient.isFetched && chosenIsClient.data === false; useEffect(() => { setAcceptEmpty(false); }, [chosen]); useEffect(() => { pref && reset(pref); }, [reset, pref]); if (pref?.isPortable) { return (

{t('prefs.installLocationTitle')}

{t('prefs.portableInfo')}

{!isValidClientDir.isLoading && !isValidClientDir.data && (

{t('prefs.errorLabel')} {t('prefs.wowExeNotFound', { exe: 'WoW.exe' })}

)} ); } return (
{ if (needsEmptyConfirm && !acceptEmpty) return; try { await setPref.mutateAsync({ clientDir }); verify.mutate(); close(); } catch (e) { setError('clientDir', { message: e instanceof Error ? e.message : JSON.stringify(e) }); } })} > { reset(); close(); }} />

{t('prefs.installLocationTitle')}


{t('prefs.selectDirectory')}

{t('prefs.upgradeExisting')}

setValue('clientDir', v, { shouldTouch: true, shouldDirty: true, shouldValidate: true }) } options={{ properties: ['openDirectory', 'createDirectory'] }} />
{formState.errors.clientDir && (

{formState.errors.clientDir.message}

)} {needsEmptyConfirm && ( <>

{t('prefs.noClientHere', { exe: 'WoW.exe' })}

)} {t('prefs.confirm')} ); }; export default ClientDirDialog;