import { BackupStates, RecoveryStates, ReducerStateBackup, ReducerStateRecovery } from "anastasis-core"; import { ComponentChildren, Fragment, FunctionalComponent, h, VNode } from "preact"; import { useErrorBoundary, useLayoutEffect, useRef } from "preact/hooks"; import { Menu } from "../../components/menu"; import { AnastasisProvider, useAnastasisContext } from "../../context/anastasis"; import { AnastasisReducerApi, useAnastasisReducer } from "../../hooks/use-anastasis-reducer"; import { AttributeEntryScreen } from "./AttributeEntryScreen"; import { AuthenticationEditorScreen } from "./AuthenticationEditorScreen"; import { BackupFinishedScreen } from "./BackupFinishedScreen"; import { ChallengeOverviewScreen } from "./ChallengeOverviewScreen"; import { ContinentSelectionScreen } from "./ContinentSelectionScreen"; import { CountrySelectionScreen } from "./CountrySelectionScreen"; import { PoliciesPayingScreen } from "./PoliciesPayingScreen"; import { RecoveryFinishedScreen } from "./RecoveryFinishedScreen"; import { ReviewPoliciesScreen } from "./ReviewPoliciesScreen"; import { SecretEditorScreen } from "./SecretEditorScreen"; import { SecretSelectionScreen } from "./SecretSelectionScreen"; import { SolveScreen } from "./SolveScreen"; import { StartScreen } from "./StartScreen"; import { TruthsPayingScreen } from "./TruthsPayingScreen"; function isBackup(reducer: AnastasisReducerApi): boolean { return !!reducer.currentReducerState?.backup_state; } export function withProcessLabel( reducer: AnastasisReducerApi, text: string, ): string { if (isBackup(reducer)) { return `Backup: ${text}`; } return `Recovery: ${text}`; } interface AnastasisClientFrameProps { onNext?(): void; title: string; children: ComponentChildren; /** * Should back/next buttons be provided? */ hideNav?: boolean; /** * Hide only the "next" button. */ hideNext?: boolean; } function ErrorBoundary(props: { reducer: AnastasisReducerApi; children: ComponentChildren; }): VNode { const [error, resetError] = useErrorBoundary((error) => console.log("got error", error), ); if (error) { return (

Error:

{error.stack}

); } return
{props.children}
; } export function AnastasisClientFrame(props: AnastasisClientFrameProps): VNode { const reducer = useAnastasisContext(); if (!reducer) { return

Fatal: Reducer must be in context.

; } const next = (): void => { if (props.onNext) { props.onNext(); } else { reducer.transition("next", {}); } }; const handleKeyPress = ( e: h.JSX.TargetedKeyboardEvent, ): void => { console.log("Got key press", e.key); // FIXME: By default, "next" action should be executed here }; return (
handleKeyPress(e)}>

{props.title}

{props.children} {!props.hideNav ? (
{!props.hideNext ? : null}
) : null}
); } const AnastasisClient: FunctionalComponent = () => { const reducer = useAnastasisReducer(); return ( ); }; const AnastasisClientImpl: FunctionalComponent = () => { const reducer = useAnastasisContext() if (!reducer) { return

Fatal: Reducer must be in context.

; } const state = reducer.currentReducerState; if (!state) { return ; } console.log("state", reducer.currentReducerState); if ( state.backup_state === BackupStates.ContinentSelecting || state.recovery_state === RecoveryStates.ContinentSelecting ) { return ( ); } if ( state.backup_state === BackupStates.CountrySelecting || state.recovery_state === RecoveryStates.CountrySelecting ) { return ( ); } if ( state.backup_state === BackupStates.UserAttributesCollecting || state.recovery_state === RecoveryStates.UserAttributesCollecting ) { return ( ); } if (state.backup_state === BackupStates.AuthenticationsEditing) { return ( ); } if (state.backup_state === BackupStates.PoliciesReviewing) { return ( ); } if (state.backup_state === BackupStates.SecretEditing) { return ; } if (state.backup_state === BackupStates.BackupFinished) { return ; } if (state.backup_state === BackupStates.TruthsPaying) { return ; } if (state.backup_state === BackupStates.PoliciesPaying) { return ; } if (state.recovery_state === RecoveryStates.SecretSelecting) { return ( ); } if (state.recovery_state === RecoveryStates.ChallengeSelecting) { return ( ); } if (state.recovery_state === RecoveryStates.ChallengeSolving) { return ; } if (state.recovery_state === RecoveryStates.RecoveryFinished) { return ( ); } console.log("unknown state", reducer.currentReducerState); return (

Bug: Unknown state.

); }; interface LabeledInputProps { label: string; grabFocus?: boolean; bind: [string, (x: string) => void]; } export function LabeledInput(props: LabeledInputProps): VNode { const inputRef = useRef(null); useLayoutEffect(() => { if (props.grabFocus) { inputRef.current?.focus(); } }, [props.grabFocus]); return ( ); } /** * Show a dismissible error banner if there is a current error. */ function ErrorBanner(): VNode | null { const reducer = useAnastasisContext(); if (!reducer || !reducer.currentError) return null; return (

Error: {JSON.stringify(reducer.currentError)}

); } export default AnastasisClient;