2022-12-19 15:59:14 -08:00
|
|
|
import { useState } from 'react';
|
|
|
|
|
import { Stack, Group, Checkbox } from '@mantine/core';
|
2022-12-19 16:12:57 -08:00
|
|
|
import { Button, PasswordInput, SegmentedControl, TextInput, toast } from '/@/renderer/components';
|
2022-12-19 15:59:14 -08:00
|
|
|
import { useForm } from '@mantine/form';
|
|
|
|
|
import { useFocusTrap } from '@mantine/hooks';
|
|
|
|
|
import { closeAllModals } from '@mantine/modals';
|
2023-06-13 17:52:51 +00:00
|
|
|
import isElectron from 'is-electron';
|
2022-12-19 15:59:14 -08:00
|
|
|
import { nanoid } from 'nanoid/non-secure';
|
2024-02-17 00:57:10 -08:00
|
|
|
import { AuthenticationResponse, ServerType } from '/@/renderer/api/types';
|
2023-10-30 19:22:45 -07:00
|
|
|
import { useAuthStoreActions } from '/@/renderer/store';
|
2023-05-09 05:48:11 -07:00
|
|
|
import { api } from '/@/renderer/api';
|
2023-10-30 19:22:45 -07:00
|
|
|
import { useTranslation } from 'react-i18next';
|
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
|
|
|
const SERVER_TYPES = [
|
2023-07-01 19:10:05 -07:00
|
|
|
{ label: 'Jellyfin', value: ServerType.JELLYFIN },
|
|
|
|
|
{ label: 'Navidrome', value: ServerType.NAVIDROME },
|
|
|
|
|
// { label: 'Subsonic', value: ServerType.SUBSONIC },
|
2022-12-19 15:59:14 -08:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
interface AddServerFormProps {
|
2023-07-01 19:10:05 -07:00
|
|
|
onCancel: () => void;
|
2022-12-19 15:59:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const AddServerForm = ({ onCancel }: AddServerFormProps) => {
|
2023-10-30 19:22:45 -07:00
|
|
|
const { t } = useTranslation();
|
2023-07-01 19:10:05 -07:00
|
|
|
const focusTrapRef = useFocusTrap(true);
|
|
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
|
const { addServer, setCurrentServer } = useAuthStoreActions();
|
|
|
|
|
|
|
|
|
|
const form = useForm({
|
|
|
|
|
initialValues: {
|
|
|
|
|
legacyAuth: false,
|
|
|
|
|
name: '',
|
|
|
|
|
password: '',
|
|
|
|
|
savePassword: false,
|
|
|
|
|
type: ServerType.JELLYFIN,
|
|
|
|
|
url: 'http://',
|
|
|
|
|
username: '',
|
2023-05-09 05:48:11 -07:00
|
|
|
},
|
2023-07-01 19:10:05 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const isSubmitDisabled = !form.values.name || !form.values.url || !form.values.username;
|
|
|
|
|
|
|
|
|
|
const handleSubmit = form.onSubmit(async (values) => {
|
|
|
|
|
const authFunction = api.controller.authenticate;
|
|
|
|
|
|
|
|
|
|
if (!authFunction) {
|
2023-10-30 19:22:45 -07:00
|
|
|
return toast.error({
|
|
|
|
|
message: t('error.invalidServer', { postProcess: 'sentenceCase' }),
|
|
|
|
|
});
|
2023-06-13 17:52:51 +00: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,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const serverItem = {
|
|
|
|
|
credential: data.credential,
|
|
|
|
|
id: nanoid(),
|
|
|
|
|
name: values.name,
|
|
|
|
|
ndCredential: data.ndCredential,
|
|
|
|
|
type: values.type,
|
|
|
|
|
url: values.url.replace(/\/$/, ''),
|
|
|
|
|
userId: data.userId,
|
|
|
|
|
username: data.username,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
addServer(serverItem);
|
|
|
|
|
setCurrentServer(serverItem);
|
|
|
|
|
closeAllModals();
|
|
|
|
|
|
2023-10-30 19:22:45 -07:00
|
|
|
toast.success({
|
|
|
|
|
message: t('form.addServer.success', { postProcess: 'sentenceCase' }),
|
|
|
|
|
});
|
2023-07-01 19:10:05 -07:00
|
|
|
|
|
|
|
|
if (localSettings && values.savePassword) {
|
|
|
|
|
const saved = await localSettings.passwordSet(values.password, serverItem.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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (err: any) {
|
|
|
|
|
setIsLoading(false);
|
|
|
|
|
return toast.error({ message: err?.message });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return setIsLoading(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
|
<Stack
|
|
|
|
|
ref={focusTrapRef}
|
|
|
|
|
m={5}
|
|
|
|
|
>
|
|
|
|
|
<SegmentedControl
|
|
|
|
|
data={SERVER_TYPES}
|
|
|
|
|
{...form.getInputProps('type')}
|
|
|
|
|
/>
|
|
|
|
|
<Group grow>
|
|
|
|
|
<TextInput
|
|
|
|
|
data-autofocus
|
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
|
|
|
{...form.getInputProps('name')}
|
|
|
|
|
/>
|
|
|
|
|
<TextInput
|
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
|
|
|
{...form.getInputProps('url')}
|
|
|
|
|
/>
|
|
|
|
|
</Group>
|
|
|
|
|
<TextInput
|
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
|
|
|
{...form.getInputProps('username')}
|
|
|
|
|
/>
|
|
|
|
|
<PasswordInput
|
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 && form.values.type === ServerType.NAVIDROME && (
|
|
|
|
|
<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',
|
|
|
|
|
})}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
{form.values.type === ServerType.SUBSONIC && (
|
|
|
|
|
<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
|
|
|
|
|
disabled={isSubmitDisabled}
|
|
|
|
|
loading={isLoading}
|
|
|
|
|
type="submit"
|
|
|
|
|
variant="filled"
|
|
|
|
|
>
|
2023-10-30 19:22:45 -07:00
|
|
|
{t('common.add', { postProcess: 'titleCase' })}
|
2023-07-01 19:10:05 -07:00
|
|
|
</Button>
|
|
|
|
|
</Group>
|
|
|
|
|
</Stack>
|
|
|
|
|
</form>
|
|
|
|
|
);
|
2022-12-19 15:59:14 -08:00
|
|
|
};
|