/* eslint-disable @typescript-eslint/camelcase */ import { AuthMethod, ReducerStateBackup } from "anastasis-core"; import { h, VNode } from "preact"; import { useState } from "preact/hooks"; import { useAnastasisContext } from "../../context/anastasis"; import { AnastasisReducerApi } from "../../hooks/use-anastasis-reducer"; import { AuthMethodEmailSetup } from "./AuthMethodEmailSetup"; import { AuthMethodPostSetup } from "./AuthMethodPostSetup"; import { AuthMethodQuestionSetup } from "./AuthMethodQuestionSetup"; import { AuthMethodSmsSetup } from "./AuthMethodSmsSetup"; import { AnastasisClientFrame } from "./index"; export function AuthenticationEditorScreen(): VNode { const [selectedMethod, setSelectedMethod] = useState( undefined ); const reducer = useAnastasisContext() if (!reducer) { return
no reducer in context
} if (!reducer.currentReducerState || reducer.currentReducerState.backup_state === undefined) { return
invalid state
} const providers = reducer.currentReducerState.authentication_providers!; const authAvailableSet = new Set(); for (const provKey of Object.keys(providers)) { const p = providers[provKey]; if ("http_status" in p && (!("error_code" in p)) && p.methods) { for (const meth of p.methods) { authAvailableSet.add(meth.type); } } } if (selectedMethod) { const cancel = (): void => setSelectedMethod(undefined); const addMethod = (args: any): void => { reducer.transition("add_authentication", args); setSelectedMethod(undefined); }; const methodMap: Record< string, (props: AuthMethodSetupProps) => h.JSX.Element > = { sms: AuthMethodSmsSetup, question: AuthMethodQuestionSetup, email: AuthMethodEmailSetup, post: AuthMethodPostSetup, }; const AuthSetup = methodMap[selectedMethod] ?? AuthMethodNotImplemented; return ( ); } function MethodButton(props: { method: string; label: string }): VNode { return ( ); } const configuredAuthMethods: AuthMethod[] = reducer.currentReducerState.authentication_methods ?? []; const haveMethodsConfigured = configuredAuthMethods.length; return (

Configured authentication methods

{haveMethodsConfigured ? ( configuredAuthMethods.map((x, i) => { return (

{x.type} ({x.instructions}){" "}

); }) ) : (

No authentication methods configured yet.

)}
); } export interface AuthMethodSetupProps { method: string; addAuthMethod: (x: any) => void; cancel: () => void; } function AuthMethodNotImplemented(props: AuthMethodSetupProps): VNode { return (

This auth method is not implemented yet, please choose another one.

); } interface AuthenticationEditorProps { reducer: AnastasisReducerApi; backupState: ReducerStateBackup; }