Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added opensubtitlescom provider validation #2770

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 77 additions & 30 deletions frontend/src/pages/Settings/Providers/components.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Fragment,
FunctionComponent,
useCallback,
useMemo,
Expand Down Expand Up @@ -42,7 +43,7 @@ import {
} from "@/pages/Settings/utilities/SettingsProvider";
import { BuildKey, useSelectorOptions } from "@/utilities";
import { ASSERT } from "@/utilities/console";
import { ProviderInfo } from "./list";
import { ProviderInfo, ProviderList } from "./list";

type SettingsKey =
| "settings-general-enabled_providers"
Expand Down Expand Up @@ -151,6 +152,27 @@ const SelectItem: AutocompleteProps["renderOption"] = ({ option }) => {
);
};

const validation = ProviderList.map((provider) => {
return provider.inputs
?.map((input) => {
if (input.validation === undefined) {
return null;
}

return {
[`settings-${provider.key}-${input.key}`]: input.validation?.rule,
};
})
.filter((input) => input && Object.keys(input).length > 0)
.reduce((acc, curr) => {
return { ...acc, ...curr };
}, {});
})
.filter((provider) => provider && Object.keys(provider).length > 0)
.reduce((acc, item) => {
return { ...acc, ...item };
}, {});

const ProviderTool: FunctionComponent<ProviderToolProps> = ({
payload,
enabledProviders,
Expand All @@ -172,6 +194,9 @@ const ProviderTool: FunctionComponent<ProviderToolProps> = ({
settings: staged,
hooks: {},
},
validate: {
settings: validation!,
},
});

const deletePayload = useCallback(() => {
Expand All @@ -188,6 +213,12 @@ const ProviderTool: FunctionComponent<ProviderToolProps> = ({

const submit = useCallback(
(values: FormValues) => {
const result = form.validate();

if (result.hasErrors) {
return;
}

if (info && enabledProviders) {
const changes = { ...values.settings };
const hooks = values.hooks;
Expand All @@ -204,7 +235,7 @@ const ProviderTool: FunctionComponent<ProviderToolProps> = ({
modals.closeAll();
}
},
[info, enabledProviders, modals, settingsKey],
[info, enabledProviders, modals, settingsKey, form],
);

const canSave = info !== null;
Expand Down Expand Up @@ -249,43 +280,57 @@ const ProviderTool: FunctionComponent<ProviderToolProps> = ({
const label = value.name ?? capitalize(value.key);
const options = value.options ?? [];

const error = form.errors[`settings.settings-${itemKey}-${key}`] ? (
<MantineText c="red" component="span" size="xs">
{form.errors[`settings.settings-${itemKey}-${key}`]}
</MantineText>
) : null;

switch (value.type) {
case "text":
elements.push(
<Text
key={BuildKey(itemKey, key)}
label={label}
settingKey={`settings-${itemKey}-${key}`}
></Text>,
<Fragment key={BuildKey(itemKey, key)}>
<Text
label={label}
settingKey={`settings-${itemKey}-${key}`}
></Text>
{error}
</Fragment>,
);
return;
case "password":
elements.push(
<Password
key={BuildKey(itemKey, key)}
label={label}
settingKey={`settings-${itemKey}-${key}`}
></Password>,
<Fragment key={BuildKey(itemKey, key)}>
<Password
label={label}
settingKey={`settings-${itemKey}-${key}`}
></Password>
{error}
</Fragment>,
);
return;
case "switch":
elements.push(
<Check
key={key}
inline
label={label}
settingKey={`settings-${itemKey}-${key}`}
></Check>,
<Fragment key={BuildKey(itemKey, key)}>
<Check
inline
label={label}
settingKey={`settings-${itemKey}-${key}`}
></Check>
{error}
</Fragment>,
);
return;
case "select":
elements.push(
<GlobalSelector
key={key}
label={label}
settingKey={`settings-${itemKey}-${key}`}
options={options}
></GlobalSelector>,
<Fragment key={BuildKey(itemKey, key)}>
<GlobalSelector
label={label}
settingKey={`settings-${itemKey}-${key}`}
options={options}
></GlobalSelector>
{error}
</Fragment>,
);
return;
case "testbutton":
Expand All @@ -295,11 +340,13 @@ const ProviderTool: FunctionComponent<ProviderToolProps> = ({
return;
case "chips":
elements.push(
<Chips
key={key}
label={label}
settingKey={`settings-${itemKey}-${key}`}
></Chips>,
<Fragment key={BuildKey(itemKey, key)}>
<Chips
label={label}
settingKey={`settings-${itemKey}-${key}`}
></Chips>
{error}
</Fragment>,
);
return;
default:
Expand All @@ -308,7 +355,7 @@ const ProviderTool: FunctionComponent<ProviderToolProps> = ({
});

return <Stack gap="xs">{elements}</Stack>;
}, [info]);
}, [info, form]);

return (
<SettingsProvider value={settings}>
Expand Down
16 changes: 13 additions & 3 deletions frontend/src/pages/Settings/Providers/list.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
import { ReactText } from "react";
import { SelectorOption } from "@/components";

type Text = string | number;

type Input<T, N> = {
type: N;
key: string;
defaultValue?: T;
name?: string;
description?: string;
options?: SelectorOption<string>[];
validation?: {
rule: (value: string) => string | null;
};
};

type AvailableInput =
| Input<ReactText, "text">
| Input<Text, "text">
| Input<string, "password">
| Input<boolean, "switch">
| Input<string, "select">
| Input<string, "testbutton">
| Input<ReactText[], "chips">;
| Input<Text[], "chips">;

export interface ProviderInfo {
key: string;
Expand Down Expand Up @@ -390,6 +394,12 @@ export const ProviderList: Readonly<ProviderInfo[]> = [
{
type: "text",
key: "username",
validation: {
rule: (value: string) =>
/^.\S+@\S+$/.test(value)
? "Invalid Username. Do not use your e-mail."
: null,
},
},
{
type: "password",
Expand Down