summaryrefslogtreecommitdiff
path: root/packages/anastasis-webui/src/pages/home/AddingProviderScreen/state.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/anastasis-webui/src/pages/home/AddingProviderScreen/state.ts')
-rw-r--r--packages/anastasis-webui/src/pages/home/AddingProviderScreen/state.ts63
1 files changed, 40 insertions, 23 deletions
diff --git a/packages/anastasis-webui/src/pages/home/AddingProviderScreen/state.ts b/packages/anastasis-webui/src/pages/home/AddingProviderScreen/state.ts
index 009ab20a2..30e4d750d 100644
--- a/packages/anastasis-webui/src/pages/home/AddingProviderScreen/state.ts
+++ b/packages/anastasis-webui/src/pages/home/AddingProviderScreen/state.ts
@@ -25,7 +25,11 @@ interface Props {
notifications?: Notification[];
}
-export default function useComponentState({ providerType, onCancel, notifications = [] }: Props): State {
+export default function useComponentState({
+ providerType,
+ onCancel,
+ notifications = [],
+}: Props): State {
const reducer = useAnastasisContext();
const [providerURL, setProviderURL] = useState("");
@@ -39,9 +43,9 @@ export default function useComponentState({ providerType, onCancel, notification
const allAuthProviders =
!reducer ||
- !reducer.currentReducerState ||
- reducer.currentReducerState.reducer_type === "error" ||
- !reducer.currentReducerState.authentication_providers
+ !reducer.currentReducerState ||
+ reducer.currentReducerState.reducer_type === "error" ||
+ !reducer.currentReducerState.authentication_providers
? {}
: reducer.currentReducerState.authentication_providers;
@@ -58,7 +62,12 @@ export default function useComponentState({ providerType, onCancel, notification
prev[p.status].push({ ...p, url });
return prev;
},
- { "not-contacted": [], disabled: [], error: [], ok: [] } as AuthProvByStatusMap,
+ {
+ "not-contacted": [],
+ disabled: [],
+ error: [],
+ ok: [],
+ } as AuthProvByStatusMap,
);
const authProviders = authProvidersByStatus["ok"].map((p) => p.url);
@@ -67,14 +76,23 @@ export default function useComponentState({ providerType, onCancel, notification
useEffect(() => {
if (timeout.current) clearTimeout(timeout.current);
timeout.current = setTimeout(async () => {
- const url = providerURL.endsWith("/") ? providerURL : providerURL + "/";
- if (!providerURL || authProviders.includes(url)) return;
+ let url = providerURL;
+ if (!url || authProviders.includes(url)) return;
+ if (url && !url.match(/^(https?:)\/\/.+\/(?:config)?$/iu))
+ return setError(
+ "Malformed URL: Must be an HTTP(S) URL ending with a /",
+ );
+ if (url.endsWith("/config")) url = url.substring(0, url.length - 6);
try {
setTesting(true);
await testProvider(url, providerType);
setError("");
} catch (e) {
if (e instanceof Error) setError(e.message);
+ else
+ throw new Error(
+ `Unexpected Error Type: ${typeof e} - Cannot handle. Error: ${e}`,
+ );
}
setTesting(false);
}, 200);
@@ -98,19 +116,20 @@ export default function useComponentState({ providerType, onCancel, notification
const addProvider = async (provider_url: string): Promise<void> => {
await reducer.transition("add_provider", { provider_url });
onCancel();
- }
+ };
const deleteProvider = async (provider_url: string): Promise<void> => {
reducer.transition("delete_provider", { provider_url });
- }
+ };
let errors = !providerURL ? "Add provider URL" : undefined;
let url: string | undefined;
- try {
- url = new URL("", providerURL).href;
- } catch {
- errors = "Check the URL";
- }
- const _url = url
+ // We'll validate it in testProvider & via a regex above - there's no need in this :)
+ // try {
+ // url = new URL("", providerURL).href;
+ // } catch {
+ // errors = "Check the URL";
+ // }
+ const _url = url;
if (!!error && !errors) {
errors = error;
@@ -130,21 +149,19 @@ export default function useComponentState({ providerType, onCancel, notification
setProviderURL: async (s: string) => setProviderURL(s),
errors,
error,
- notifications
- }
+ notifications,
+ };
if (!providerLabel) {
return {
status: "without-type",
- ...commonState
- }
+ ...commonState,
+ };
} else {
return {
status: "with-type",
providerLabel,
- ...commonState
- }
+ ...commonState,
+ };
}
-
}
-