From 2ec2161a7e1c0787b5e9ea14311f19c6b6a52d63 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Fri, 22 Oct 2021 01:31:46 -0300 Subject: some storybook exmaples --- packages/anastasis-webui/src/pages/home/index.tsx | 162 +++++++++------------- 1 file changed, 67 insertions(+), 95 deletions(-) (limited to 'packages/anastasis-webui/src/pages/home/index.tsx') diff --git a/packages/anastasis-webui/src/pages/home/index.tsx b/packages/anastasis-webui/src/pages/home/index.tsx index 5001d1ee4..4cec47ec8 100644 --- a/packages/anastasis-webui/src/pages/home/index.tsx +++ b/packages/anastasis-webui/src/pages/home/index.tsx @@ -1,28 +1,25 @@ import { - Component, - ComponentChildren, - createContext, - Fragment, + BackupStates, + RecoveryStates, + ReducerStateBackup, + ReducerStateRecovery +} from "anastasis-core"; +import { + ComponentChildren, Fragment, FunctionalComponent, h, - VNode, + VNode } from "preact"; import { - useContext, useErrorBoundary, useLayoutEffect, - useRef, + useRef } from "preact/hooks"; import { Menu } from "../../components/menu"; -import { - BackupStates, - RecoveryStates, - ReducerStateBackup, - ReducerStateRecovery, -} from "anastasis-core"; +import { AnastasisProvider, useAnastasisContext } from "../../context/anastasis"; import { AnastasisReducerApi, - useAnastasisReducer, + useAnastasisReducer } from "../../hooks/use-anastasis-reducer"; import { AttributeEntryScreen } from "./AttributeEntryScreen"; import { AuthenticationEditorScreen } from "./AuthenticationEditorScreen"; @@ -38,19 +35,11 @@ import { SecretSelectionScreen } from "./SecretSelectionScreen"; import { SolveScreen } from "./SolveScreen"; import { StartScreen } from "./StartScreen"; import { TruthsPayingScreen } from "./TruthsPayingScreen"; -import "./../home/style"; - -const WithReducer = createContext(undefined); function isBackup(reducer: AnastasisReducerApi): boolean { return !!reducer.currentReducerState?.backup_state; } -export interface CommonReducerProps { - reducer: AnastasisReducerApi; - reducerState: ReducerStateBackup | ReducerStateRecovery; -} - export function withProcessLabel( reducer: AnastasisReducerApi, text: string, @@ -61,16 +50,6 @@ export function withProcessLabel( return `Recovery: ${text}`; } -export interface BackupReducerProps { - reducer: AnastasisReducerApi; - backupState: ReducerStateBackup; -} - -export interface RecoveryReducerProps { - reducer: AnastasisReducerApi; - recoveryState: ReducerStateRecovery; -} - interface AnastasisClientFrameProps { onNext?(): void; title: string; @@ -88,7 +67,7 @@ interface AnastasisClientFrameProps { function ErrorBoundary(props: { reducer: AnastasisReducerApi; children: ComponentChildren; -}) { +}): VNode { const [error, resetError] = useErrorBoundary((error) => console.log("got error", error), ); @@ -113,7 +92,7 @@ function ErrorBoundary(props: { } export function AnastasisClientFrame(props: AnastasisClientFrameProps): VNode { - const reducer = useContext(WithReducer); + const reducer = useAnastasisContext(); if (!reducer) { return

Fatal: Reducer must be in context.

; } @@ -135,9 +114,8 @@ export function AnastasisClientFrame(props: AnastasisClientFrameProps): VNode {
handleKeyPress(e)}> -

{props.title}

- + {props.children} {!props.hideNav ? (
@@ -154,96 +132,94 @@ export function AnastasisClientFrame(props: AnastasisClientFrameProps): VNode { const AnastasisClient: FunctionalComponent = () => { const reducer = useAnastasisReducer(); return ( - + - + ); }; const AnastasisClientImpl: FunctionalComponent = () => { - const reducer = useContext(WithReducer)!; - const reducerState = reducer.currentReducerState; - if (!reducerState) { - return ; + 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 ( - reducerState.backup_state === BackupStates.ContinentSelecting || - reducerState.recovery_state === RecoveryStates.ContinentSelecting + state.backup_state === BackupStates.ContinentSelecting || + state.recovery_state === RecoveryStates.ContinentSelecting ) { return ( - + ); } if ( - reducerState.backup_state === BackupStates.CountrySelecting || - reducerState.recovery_state === RecoveryStates.CountrySelecting + state.backup_state === BackupStates.CountrySelecting || + state.recovery_state === RecoveryStates.CountrySelecting ) { return ( - + ); } if ( - reducerState.backup_state === BackupStates.UserAttributesCollecting || - reducerState.recovery_state === RecoveryStates.UserAttributesCollecting + state.backup_state === BackupStates.UserAttributesCollecting || + state.recovery_state === RecoveryStates.UserAttributesCollecting ) { return ( - + ); } - if (reducerState.backup_state === BackupStates.AuthenticationsEditing) { + if (state.backup_state === BackupStates.AuthenticationsEditing) { return ( - + ); } - if (reducerState.backup_state === BackupStates.PoliciesReviewing) { + if (state.backup_state === BackupStates.PoliciesReviewing) { return ( - + ); } - if (reducerState.backup_state === BackupStates.SecretEditing) { - return ; + if (state.backup_state === BackupStates.SecretEditing) { + return ; } - if (reducerState.backup_state === BackupStates.BackupFinished) { - const backupState: ReducerStateBackup = reducerState; - return ; + if (state.backup_state === BackupStates.BackupFinished) { + return ; } - if (reducerState.backup_state === BackupStates.TruthsPaying) { - return ; + if (state.backup_state === BackupStates.TruthsPaying) { + return ; } - if (reducerState.backup_state === BackupStates.PoliciesPaying) { - const backupState: ReducerStateBackup = reducerState; - return ; + if (state.backup_state === BackupStates.PoliciesPaying) { + return ; } - if (reducerState.recovery_state === RecoveryStates.SecretSelecting) { + if (state.recovery_state === RecoveryStates.SecretSelecting) { return ( - + ); } - if (reducerState.recovery_state === RecoveryStates.ChallengeSelecting) { + if (state.recovery_state === RecoveryStates.ChallengeSelecting) { return ( - + ); } - if (reducerState.recovery_state === RecoveryStates.ChallengeSolving) { - return ; + if (state.recovery_state === RecoveryStates.ChallengeSolving) { + return ; } - if (reducerState.recovery_state === RecoveryStates.RecoveryFinished) { + if (state.recovery_state === RecoveryStates.RecoveryFinished) { return ( - + ); } @@ -251,7 +227,9 @@ const AnastasisClientImpl: FunctionalComponent = () => { return (

Bug: Unknown state.

- +
+ +
); }; @@ -282,26 +260,20 @@ export function LabeledInput(props: LabeledInputProps): VNode { ); } -interface ErrorBannerProps { - reducer: AnastasisReducerApi; -} - /** - * Show a dismissable error banner if there is a current error. + * Show a dismissible error banner if there is a current error. */ -function ErrorBanner(props: ErrorBannerProps): VNode | null { - const currentError = props.reducer.currentError; - if (currentError) { - return ( -
-

Error: {JSON.stringify(currentError)}

- -
- ); - } - return null; +function ErrorBanner(): VNode | null { + const reducer = useAnastasisContext(); + if (!reducer || !reducer.currentError) return null; + return ( +
+

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

+ +
+ ); } export default AnastasisClient; -- cgit v1.2.3