2022-12-19 15:59:14 -08:00
|
|
|
import { useState } from 'react';
|
2023-06-13 17:52:51 +00:00
|
|
|
import { Stack, Group } from '@mantine/core';
|
|
|
|
|
import { Button, Checkbox, PasswordInput, TextInput, toast, Tooltip } from '/@/renderer/components';
|
2022-12-19 15:59:14 -08:00
|
|
|
import { useForm } from '@mantine/form';
|
2023-05-09 02:40:49 -07:00
|
|
|
import { useFocusTrap } from '@mantine/hooks';
|
2022-12-19 15:59:14 -08:00
|
|
|
import { closeAllModals } from '@mantine/modals';
|
2023-06-13 17:52:51 +00:00
|
|
|
import isElectron from 'is-electron';
|
2023-10-30 19:22:45 -07:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2022-12-19 15:59:14 -08:00
|
|
|
import { RiInformationLine } from 'react-icons/ri';
|
|
|
|
|
import { AuthenticationResponse } from '/@/renderer/api/types';
|
|
|
|
|
import { useAuthStoreActions } from '/@/renderer/store';
|
|
|
|
|
import { ServerListItem, ServerType } from '/@/renderer/types';
|
2023-05-09 05:48:11 -07:00
|
|
|
import { api } from '/@/renderer/api';
|
2023-10-30 19:22:45 -07:00
|
|
|
import i18n from '/@/i18n/i18n';
|
2022-12-19 15:59:14 -08:00
|
|
|
|
2023-06-13 17:52:51 +00:00
|
|
|
const localSettings = isElectron() ? window.electron.localSettings : null;
|
|
|
|
|
|
2022-12-19 15:59:14 -08:00
|
|
|
interface EditServerFormProps {
|
2023-07-01 19:10:05 -07:00
|
|
|
isUpdate?: boolean;
|
|
|
|
|
onCancel: () => void;
|
2023-07-23 12:23:18 +00:00
|
|
|
password: string | null;
|
2023-07-01 19:10:05 -07:00
|
|
|
server: ServerListItem;
|
2022-12-19 15:59:14 -08:00
|
|
|
}
|
|
|
|
|
|
2023-05-09 02:40:49 -07:00
|
|
|
const ModifiedFieldIndicator = () => {
|
2023-07-01 19:10:05 -07:00
|
|
|
return (
|
2023-10-30 19:22:45 -07:00
|
|
|
<Tooltip label={i18n.t('common.modified', { postProcess: 'titleCase' }) as string}>
|
2023-07-01 19:10:05 -07:00
|
|
|
<span>
|
|
|
|
|
<RiInformationLine color="red" />
|
|
|
|
|
</span>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
);
|
2023-05-09 02:40:49 -07:00
|
|
|
};
|
|
|
|
|
|
2023-06-13 17:52:51 +00:00
|
|
|
export const EditServerForm = ({ isUpdate, password, server, onCancel }: EditServerFormProps) => {
|
2023-10-30 19:22:45 -07:00
|
|
|
const { t } = useTranslation();
|
2023-07-01 19:10:05 -07:00
|
|
|
const { updateServer } = useAuthStoreActions();
|
|
|
|
|
const focusTrapRef = useFocusTrap();
|
|
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
2022-12-19 15:59:14 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const form = useForm({
|
|
|
|
|
initialValues: {
|
|
|
|
|
legacyAuth: false,
|
|
|
|
|
name: server?.name,
|
|
|
|
|
password: password || '',
|
|
|
|
|
savePassword: server.savePassword || false,
|
|
|
|
|
type: server?.type,
|
|
|
|
|
url: server?.url,
|
|
|
|
|
username: server?.username,
|
|
|
|
|
},
|
|
|
|
|
});
|
2022-12-19 15:59:14 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const isSubsonic = form.values.type === ServerType.SUBSONIC;
|
|
|
|
|
const isNavidrome = form.values.type === ServerType.NAVIDROME;
|
2022-12-19 15:59:14 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const handleSubmit = form.onSubmit(async (values) => {
|
|
|
|
|
const authFunction = api.controller.authenticate;
|
2022-12-19 15:59:14 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
if (!authFunction) {
|
2023-10-30 19:22:45 -07:00
|
|
|
return toast.error({
|
|
|
|
|
message: t('error.invalidServer', { postProcess: 'sentenceCase' }),
|
|
|
|
|
});
|
2023-07-01 19:10:05 -07:00
|
|
|
}
|
2022-12-19 15:59:14 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
try {
|
|
|
|
|
setIsLoading(true);
|
|
|
|
|
const data: AuthenticationResponse | undefined = await authFunction(
|
|
|
|
|
values.url,
|
|
|
|
|
{
|
|
|
|
|
legacy: values.legacyAuth,
|
|
|
|
|
password: values.password,
|
|
|
|
|
username: values.username,
|
|
|
|
|
},
|
|
|
|
|
values.type,
|
|
|
|
|
);
|
2023-05-09 05:48:11 -07:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
if (!data) {
|
2023-10-30 19:22:45 -07:00
|
|
|
return toast.error({
|
|
|
|
|
message: t('error.authenticationFailed', { postProcess: 'sentenceCase' }),
|
|
|
|
|
});
|
2023-07-01 19:10:05 -07:00
|
|
|
}
|
2022-12-19 15:59:14 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const serverItem = {
|
|
|
|
|
credential: data.credential,
|
|
|
|
|
name: values.name,
|
|
|
|
|
ndCredential: data.ndCredential,
|
|
|
|
|
savePassword: values.savePassword,
|
|
|
|
|
type: values.type,
|
|
|
|
|
url: values.url,
|
|
|
|
|
userId: data.userId,
|
|
|
|
|
username: data.username,
|
|
|
|
|
};
|
2022-12-19 15:59:14 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
updateServer(server.id, serverItem);
|
2023-10-30 19:22:45 -07:00
|
|
|
toast.success({
|
|
|
|
|
message: t('form.updateServer.title', { postProcess: 'sentenceCase' }),
|
|
|
|
|
});
|
2023-06-13 17:52:51 +00:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
if (localSettings) {
|
|
|
|
|
if (values.savePassword) {
|
|
|
|
|
const saved = await localSettings.passwordSet(values.password, server.id);
|
|
|
|
|
if (!saved) {
|
2023-10-30 19:22:45 -07:00
|
|
|
toast.error({
|
|
|
|
|
message: t('form.addServer.error', {
|
|
|
|
|
context: 'savePassword',
|
|
|
|
|
postProcess: 'sentenceCase',
|
|
|
|
|
}),
|
|
|
|
|
});
|
2023-07-01 19:10:05 -07:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
localSettings.passwordRemove(server.id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (err: any) {
|
|
|
|
|
setIsLoading(false);
|
|
|
|
|
return toast.error({ message: err?.message });
|
2023-06-13 17:52:51 +00:00
|
|
|
}
|
2022-12-19 15:59:14 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
if (isUpdate) closeAllModals();
|
|
|
|
|
return setIsLoading(false);
|
|
|
|
|
});
|
2022-12-19 15:59:14 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
return (
|
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
|
<Stack ref={focusTrapRef}>
|
|
|
|
|
<TextInput
|
|
|
|
|
required
|
2023-10-30 19:22:45 -07:00
|
|
|
label={t('form.addServer.input', {
|
|
|
|
|
context: 'name',
|
|
|
|
|
postProcess: 'titleCase',
|
|
|
|
|
})}
|
2023-07-01 19:10:05 -07:00
|
|
|
rightSection={form.isDirty('name') && <ModifiedFieldIndicator />}
|
|
|
|
|
{...form.getInputProps('name')}
|
|
|
|
|
/>
|
|
|
|
|
<TextInput
|
|
|
|
|
required
|
2023-10-30 19:22:45 -07:00
|
|
|
label={t('form.addServer.input', {
|
|
|
|
|
context: 'url',
|
|
|
|
|
postProcess: 'titleCase',
|
|
|
|
|
})}
|
2023-07-01 19:10:05 -07:00
|
|
|
rightSection={form.isDirty('url') && <ModifiedFieldIndicator />}
|
|
|
|
|
{...form.getInputProps('url')}
|
|
|
|
|
/>
|
|
|
|
|
<TextInput
|
|
|
|
|
required
|
2023-10-30 19:22:45 -07:00
|
|
|
label={t('form.addServer.input', {
|
|
|
|
|
context: 'username',
|
|
|
|
|
postProcess: 'titleCase',
|
|
|
|
|
})}
|
2023-07-01 19:10:05 -07:00
|
|
|
rightSection={form.isDirty('username') && <ModifiedFieldIndicator />}
|
|
|
|
|
{...form.getInputProps('username')}
|
|
|
|
|
/>
|
|
|
|
|
<PasswordInput
|
|
|
|
|
data-autofocus
|
|
|
|
|
required
|
2023-10-30 19:22:45 -07:00
|
|
|
label={t('form.addServer.input', {
|
|
|
|
|
context: 'password',
|
|
|
|
|
postProcess: 'titleCase',
|
|
|
|
|
})}
|
2023-07-01 19:10:05 -07:00
|
|
|
{...form.getInputProps('password')}
|
|
|
|
|
/>
|
|
|
|
|
{localSettings && isNavidrome && (
|
|
|
|
|
<Checkbox
|
2023-10-30 19:22:45 -07:00
|
|
|
label={t('form.addServer.input', {
|
|
|
|
|
context: 'savePassword',
|
|
|
|
|
postProcess: 'titleCase',
|
|
|
|
|
})}
|
2023-07-01 19:10:05 -07:00
|
|
|
{...form.getInputProps('savePassword', {
|
|
|
|
|
type: 'checkbox',
|
|
|
|
|
})}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
{isSubsonic && (
|
|
|
|
|
<Checkbox
|
2023-10-30 19:22:45 -07:00
|
|
|
label={t('form.addServer.input', {
|
|
|
|
|
context: 'legacyAuthentication',
|
|
|
|
|
postProcess: 'titleCase',
|
|
|
|
|
})}
|
2023-07-01 19:10:05 -07:00
|
|
|
{...form.getInputProps('legacyAuth', {
|
|
|
|
|
type: 'checkbox',
|
|
|
|
|
})}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
<Group position="right">
|
|
|
|
|
<Button
|
|
|
|
|
variant="subtle"
|
|
|
|
|
onClick={onCancel}
|
|
|
|
|
>
|
2023-10-30 19:22:45 -07:00
|
|
|
{t('common.cancel', { postProcess: 'titleCase' })}
|
2023-07-01 19:10:05 -07:00
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
loading={isLoading}
|
|
|
|
|
type="submit"
|
|
|
|
|
variant="filled"
|
|
|
|
|
>
|
2023-10-30 19:22:45 -07:00
|
|
|
{t('common.save', { postProcess: 'titleCase' })}
|
2023-07-01 19:10:05 -07:00
|
|
|
</Button>
|
|
|
|
|
</Group>
|
|
|
|
|
</Stack>
|
|
|
|
|
</form>
|
|
|
|
|
);
|
2022-12-19 15:59:14 -08:00
|
|
|
};
|