import { h, VNode } from "preact"; import { useState } from "preact/hooks"; import { AsyncButton } from "../../components/AsyncButton"; import { PhoneNumberInput } from "../../components/fields/NumberInput"; import { useAnastasisContext } from "../../context/anastasis"; import { AddingProviderScreen } from "./AddingProviderScreen"; import { AnastasisClientFrame } from "./index"; export function SecretSelectionScreen(): VNode { const [selectingVersion, setSelectingVersion] = useState(false); const reducer = useAnastasisContext(); const [manageProvider, setManageProvider] = useState(false); const currentVersion = (reducer?.currentReducerState && "recovery_document" in reducer.currentReducerState && reducer.currentReducerState.recovery_document?.version) || 0; if (!reducer) { return
no reducer in context
; } if ( !reducer.currentReducerState || reducer.currentReducerState.recovery_state === undefined ) { return
invalid state
; } async function doSelectVersion(p: string, n: number): Promise { if (!reducer) return Promise.resolve(); return reducer.runTransaction(async (tx) => { await tx.transition("change_version", { version: n, provider_url: p, }); setSelectingVersion(false); }); } const providerList = Object.keys( reducer.currentReducerState.authentication_providers ?? {}, ); const recoveryDocument = reducer.currentReducerState.recovery_document; if (!recoveryDocument) { return ( doSelectVersion(newProv, 0)} /> ); } if (selectingVersion) { return ( setSelectingVersion(false)} onConfirm={doSelectVersion} /> ); } if (manageProvider) { return setManageProvider(false)} />; } return (

{recoveryDocument.provider_url}

{currentVersion === 0 ? (

Set to recover the latest version

) : (

Set to recover the version number {currentVersion}

)}

Secret found, you can select another version or continue to the challenges solving

setManageProvider(true)}> Manage recovery providers

); } function ChooseAnotherProviderScreen({ providers, selected, onChange, }: { selected: string; providers: string[]; onChange: (prov: string) => void; }): VNode { return (

No recovery document found, try with another provider

); } function SelectOtherVersionProviderScreen({ providers, provider, version, onConfirm, onCancel, }: { onCancel: () => void; provider: string; version: number; providers: string[]; onConfirm: (prov: string, v: number) => Promise; }): VNode { const [otherProvider, setOtherProvider] = useState(provider); const [otherVersion, setOtherVersion] = useState( version > 0 ? String(version) : "", ); return (

Provider {otherProvider}

{version === 0 ? (

Set to recover the latest version

) : (

Set to recover the version number {version}

)}

Specify other version below or use the latest

onConfirm(otherProvider, 0)} > Use latest onConfirm(otherProvider, parseInt(otherVersion, 10)) } > Confirm
.
); }