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

106 lines
3.3 KiB
TypeScript
Raw Normal View History

2022-06-06 16:46:49 +02:00
/*
This file is part of GNU Anastasis
(C) 2021-2022 Anastasis SARL
GNU Anastasis is free software; you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
GNU Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with
GNU Anastasis; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
2022-06-06 05:54:55 +02:00
import { bytesToString, decodeCrock } from "@gnu-taler/taler-util";
2021-10-19 15:56:52 +02:00
import { h, VNode } from "preact";
import { useEffect, useState } from "preact/hooks";
2022-06-06 05:54:55 +02:00
import { QR } from "../../components/QR.js";
import { useAnastasisContext } from "../../context/anastasis.js";
import { AnastasisClientFrame } from "./index.js";
2021-10-19 15:56:52 +02:00
2021-10-22 06:31:46 +02:00
export function RecoveryFinishedScreen(): VNode {
2021-11-10 14:20:52 +01:00
const reducer = useAnastasisContext();
const [copied, setCopied] = useState(false);
useEffect(() => {
setTimeout(() => {
setCopied(false);
}, 1000);
}, [copied]);
2021-10-22 06:31:46 +02:00
if (!reducer) {
2021-11-10 14:20:52 +01:00
return <div>no reducer in context</div>;
2021-10-22 06:31:46 +02:00
}
2022-04-13 08:44:37 +02:00
if (reducer.currentReducerState?.reducer_type !== "recovery") {
2021-11-10 14:20:52 +01:00
return <div>invalid state</div>;
2021-10-22 06:31:46 +02:00
}
const secretName = reducer.currentReducerState.recovery_document?.secret_name;
2021-11-10 14:20:52 +01:00
const encodedSecret = reducer.currentReducerState.core_secret;
2021-10-22 06:31:46 +02:00
if (!encodedSecret) {
2021-11-10 14:20:52 +01:00
return (
<AnastasisClientFrame title="Recovery Problem" hideNav>
<p>Secret not found</p>
<div
style={{
marginTop: "2em",
display: "flex",
justifyContent: "space-between",
}}
>
<button class="button" onClick={() => reducer.back()}>
Back
</button>
</div>
</AnastasisClientFrame>
);
2021-10-22 06:31:46 +02:00
}
2021-11-10 14:20:52 +01:00
const secret = bytesToString(decodeCrock(encodedSecret.value));
2022-06-24 19:28:45 +02:00
const plainText =
encodedSecret.value.length < 1000 && encodedSecret.mime === "text/plain";
const contentURI = !plainText
? secret
: `data:${encodedSecret.mime},${secret}`;
2021-10-19 15:56:52 +02:00
return (
<AnastasisClientFrame title="Recovery Success" hideNav>
<h2 class="subtitle">Your secret was recovered</h2>
{secretName && (
<p class="block">
<b>Secret name:</b> {secretName}
</p>
)}
<div class="block buttons" disabled={copied}>
2022-06-24 19:28:45 +02:00
{plainText ? (
<button
class="button"
onClick={() => {
navigator.clipboard.writeText(secret);
setCopied(true);
}}
>
{!copied ? "Copy" : "Copied"}
</button>
) : undefined}
<a
class="button is-info"
download={
encodedSecret.filename ? encodedSecret.filename : "secret.file"
}
href={contentURI}
>
<div class="icon is-small ">
<i class="mdi mdi-download" />
</div>
2022-06-24 19:28:45 +02:00
<span>Download content</span>
</a>
</div>
2022-06-24 19:28:45 +02:00
{plainText ? (
<div class="block">
<QR text={secret} />
</div>
) : undefined}
2021-10-19 15:56:52 +02:00
</AnastasisClientFrame>
);
}