Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
anderson-oki committed Nov 25, 2024
1 parent 3ea4ec5 commit f6b520b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
30 changes: 23 additions & 7 deletions frontend/src/pages/Settings/Providers/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,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 +151,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 @@ -173,12 +194,7 @@ const ProviderTool: FunctionComponent<ProviderToolProps> = ({
hooks: {},
},
validate: {
settings: {
"settings-opensubtitlescom-username": (value) =>
/^.\S+@\S+$/.test(value)
? "Invalid Username. Do not use your e-mail."
: null,
},
settings: validation!,
},
});

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 @@ -375,6 +379,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

0 comments on commit f6b520b

Please sign in to comment.