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 (
|
2021-11-01 20:10:49 +01:00
|
|
|
<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-10-27 20:13:35 +02:00
|
|
|
<p>Secret found, you can select another version or continue to the challenges solving</p>
|
|
|
|
<table class="table">
|
|
|
|
<tr>
|
|
|
|
<td>
|
|
|
|
<b>Provider</b>
|
|
|
|
<span class="icon has-tooltip-right" data-tooltip="Service provider backing up your secret">
|
|
|
|
<i class="mdi mdi-information" />
|
|
|
|
</span>
|
|
|
|
</td>
|
|
|
|
<td>{recoveryDocument.provider_url}</td>
|
|
|
|
<td><a onClick={() => setSelectingVersion(true)}>use another provider</a></td>
|
|
|
|
</tr>
|
|
|
|
<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>
|
2021-10-19 15:56:52 +02:00
|
|
|
</AnastasisClientFrame>
|
|
|
|
);
|
|
|
|
}
|