wallet-core/packages/anastasis-webui/src/pages/home/SecretSelectionScreen.tsx

141 lines
5.1 KiB
TypeScript
Raw Normal View History

2021-10-19 15:56:52 +02:00
/* eslint-disable @typescript-eslint/camelcase */
import { h, VNode } from "preact";
import { useState } from "preact/hooks";
2021-10-22 06:31:46 +02:00
import { useAnastasisContext } from "../../context/anastasis";
import { AnastasisClientFrame } from "./index";
2021-10-19 15:56:52 +02:00
2021-10-22 06:31:46 +02:00
export function SecretSelectionScreen(): VNode {
2021-10-19 15:56:52 +02:00
const [selectingVersion, setSelectingVersion] = useState<boolean>(false);
const [otherProvider, setOtherProvider] = useState<string>("");
2021-10-22 06:31:46 +02:00
const reducer = useAnastasisContext()
const currentVersion = reducer?.currentReducerState
&& ("recovery_document" in reducer.currentReducerState)
&& reducer.currentReducerState.recovery_document?.version;
const [otherVersion, setOtherVersion] = useState<number>(currentVersion || 0);
if (!reducer) {
return <div>no reducer in context</div>
}
if (!reducer.currentReducerState || reducer.currentReducerState.recovery_state === undefined) {
return <div>invalid state</div>
}
2021-10-19 15:56:52 +02:00
function selectVersion(p: string, n: number): void {
2021-10-22 06:31:46 +02:00
if (!reducer) return;
2021-10-19 15:56:52 +02:00
reducer.runTransaction(async (tx) => {
await tx.transition("change_version", {
version: n,
provider_url: p,
});
});
2021-10-27 20:13:35 +02:00
setSelectingVersion(false);
2021-10-19 15:56:52 +02:00
}
2021-10-22 06:31:46 +02:00
2021-10-27 20:13:35 +02:00
const providerList = Object.keys(reducer.currentReducerState.authentication_providers ?? {})
2021-10-22 06:31:46 +02:00
const recoveryDocument = reducer.currentReducerState.recovery_document
if (!recoveryDocument) {
return (
<AnastasisClientFrame hideNext="Recovery document not found" title="Recovery: Problem">
2021-10-27 20:13:35 +02:00
<p>No recovery document found, try with another provider</p>
<table class="table">
<tr>
<td><b>Provider</b></td>
<td>
<select onChange={(e) => setOtherProvider((e.target as any).value)}>
<option key="none" disabled selected > Choose another provider </option>
{providerList.map(prov => (
<option key={prov} value={prov}>
{prov}
</option>
))}
</select>
</td>
</tr>
</table>
2021-10-22 06:31:46 +02:00
</AnastasisClientFrame>
)
}
2021-10-19 15:56:52 +02:00
if (selectingVersion) {
return (
<AnastasisClientFrame hideNav title="Recovery: Select secret">
<p>Select a different version of the secret</p>
2021-10-27 20:13:35 +02:00
<table class="table">
<tr>
<td><b>Provider</b></td>
<td>
<select onChange={(e) => setOtherProvider((e.target as any).value)}>
{providerList.map(prov => (
<option key={prov} selected={prov === recoveryDocument.provider_url} value={prov}>
{prov}
</option>
))}
</select>
</td>
</tr>
<tr>
<td><b>Version</b></td>
<td>
<input
value={otherVersion}
onChange={(e) => setOtherVersion(Number((e.target as HTMLInputElement).value))}
type="number" />
</td>
<td>
<a onClick={() => setOtherVersion(0)}>set to latest version</a>
</td>
</tr>
</table>
<div style={{ marginTop: '2em', display: 'flex', justifyContent: 'space-between' }}>
<button class="button" onClick={() => setSelectingVersion(false)}>Cancel</button>
<button class="button is-info" onClick={() => selectVersion(otherProvider, otherVersion)}>Confirm</button>
2021-10-19 15:56:52 +02:00
</div>
2021-10-27 20:13:35 +02:00
2021-10-19 15:56:52 +02:00
</AnastasisClientFrame>
);
}
return (
<AnastasisClientFrame title="Recovery: Select secret">
2021-11-04 19:17:57 +01:00
<div class="columns">
<div class="column is-half">
<div class="box">
<h1 class="subtitle">{recoveryDocument.provider_url}</h1>
<table class="table">
<tr>
<td>
<b>Secret version</b>
<span class="icon has-tooltip-right" data-tooltip="Secret version to be recovered">
<i class="mdi mdi-information" />
</span>
</td>
<td>{recoveryDocument.version}</td>
<td><a onClick={() => setSelectingVersion(true)}>use another version</a></td>
</tr>
<tr>
<td>
<b>Secret name</b>
<span class="icon has-tooltip-right" data-tooltip="Secret identifier">
<i class="mdi mdi-information" />
</span>
</td>
<td>{recoveryDocument.secret_name}</td>
<td> </td>
</tr>
</table>
<div class="buttons is-right">
<button class="button" disabled onClick={() => reducer.reset()}>Use this provider</button>
</div>
</div>
<div class="buttons is-right">
<button class="button" disabled onClick={() => reducer.reset()}>Change provider</button>
</div>
</div>
<div class="column is-two-third">
<p>Secret found, you can select another version or continue to the challenges solving</p>
</div>
</div>
2021-10-19 15:56:52 +02:00
</AnastasisClientFrame>
);
}