Skip to content

armgit / ente

Public Imported from GitHub
0

💚 End-to-end encrypted cloud for everything.

Polish twoof3 recovery flow and offline page

Manav Rathi8f6a6feb4ed7
diff --git a/web/apps/twoof3/src/features/twoof3/utils/export.ts b/web/apps/twoof3/src/features/twoof3/utils/export.ts
index 7227a8fb80..ec9d3e30f6 100644
--- a/web/apps/twoof3/src/features/twoof3/utils/export.ts
+++ b/web/apps/twoof3/src/features/twoof3/utils/export.ts
@@ -20,42 +20,73 @@ export const MAX_PRINTED_SHARE_LENGTH =
export const MAX_SECRET_BYTES_FOR_PRINTED_CARD =
maxSecretBytesForEncodedShareLength(MAX_PRINTED_SHARE_LENGTH);
-const wrapCanvasText = (
+const wrapCanvasTextToWidth = (
context: CanvasRenderingContext2D,
text: string,
maxWidth: number,
maxLines: number,
) => {
- const words = text.trim().split(/\s+/u).filter(Boolean);
- if (words.length === 0) return [""];
+ const normalized = text.trim().replace(/\s+/gu, " ");
+ if (!normalized) return [""];
const lines: string[] = [];
- let current = "";
+ let remaining = normalized;
- for (const word of words) {
- const candidate = current ? `${current} ${word}` : word;
- if (context.measureText(candidate).width <= maxWidth) {
- current = candidate;
- continue;
+ while (remaining && lines.length < maxLines) {
+ if (context.measureText(remaining).width <= maxWidth) {
+ lines.push(remaining);
+ remaining = "";
+ break;
}
- if (current) lines.push(current);
- current = word;
+ let overflowIndex = -1;
+ for (let index = 0; index < remaining.length; index += 1) {
+ if (context.measureText(remaining.slice(0, index + 1)).width > maxWidth) {
+ overflowIndex = index;
+ break;
+ }
+ }
- if (lines.length === maxLines - 1) break;
- }
+ if (overflowIndex <= 0) return null;
- if (current && lines.length < maxLines) {
- lines.push(current);
+ const whitespaceIndex = remaining.lastIndexOf(" ", overflowIndex - 1);
+ const splitIndex = whitespaceIndex > 0 ? whitespaceIndex : overflowIndex;
+ const line = remaining.slice(0, splitIndex).trimEnd();
+ if (!line) return null;
+
+ lines.push(line);
+ remaining = remaining
+ .slice(whitespaceIndex > 0 ? splitIndex + 1 : splitIndex)
+ .trimStart();
}
- const consumedWords = lines.join(" ").split(/\s+/u).filter(Boolean).length;
- if (consumedWords < words.length && lines.length > 0) {
- const lastLine = lines[lines.length - 1]!;
- lines[lines.length - 1] = `${lastLine.replace(/[. ]+$/u, "")}…`;
+ return remaining ? null : lines;
+};
+
+const fitTitleLayout = (
+ context: CanvasRenderingContext2D,
+ title: string,
+ maxWidth: number,
+ maxLines: number,
+) => {
+ for (let fontSize = 54; fontSize >= 34; fontSize -= 2) {
+ context.font = `700 ${fontSize}px "Space Grotesk", sans-serif`;
+ const lines = wrapCanvasTextToWidth(context, title, maxWidth, maxLines);
+ if (!lines) continue;
+
+ return {
+ fontSize,
+ lineHeight: Math.round(fontSize * 1.14),
+ lines,
+ };
}
- return lines;
+ context.font = '700 34px "Space Grotesk", sans-serif';
+ return {
+ fontSize: 34,
+ lineHeight: 39,
+ lines: wrapCanvasTextToWidth(context, title, maxWidth, maxLines) ?? [title],
+ };
};
const balanceCanvasText = (
@@ -138,6 +169,7 @@ const ensureShareCardFonts = async () => {
await Promise.allSettled([
document.fonts.load('700 54px "Space Grotesk"'),
+ document.fonts.load('700 34px "Space Grotesk"'),
document.fonts.load('700 36px "Space Grotesk"'),
document.fonts.load('500 32px "Space Grotesk"'),
document.fonts.load('700 22px "Space Grotesk"'),
@@ -193,10 +225,10 @@ export const renderShareCard = async ({
context.fillText(APP_LINK, CARD_WIDTH - 124 - appLinkWidth, 166);
context.fillStyle = "#111111";
- context.font = '700 54px "Space Grotesk", sans-serif';
- const titleLines = wrapCanvasText(context, title, 940, 2);
- titleLines.forEach((line, index) => {
- context.fillText(line, 120, 304 + index * 66);
+ const titleLayout = fitTitleLayout(context, title, 940, 2);
+ context.font = `700 ${titleLayout.fontSize}px "Space Grotesk", sans-serif`;
+ titleLayout.lines.forEach((line, index) => {
+ context.fillText(line, 120, 304 + index * titleLayout.lineHeight);
});
context.fillStyle = "#ffffff";
@@ -215,7 +247,7 @@ export const renderShareCard = async ({
context.fillStyle = "rgba(17,17,17,0.45)";
context.font = '500 22px "Space Grotesk", sans-serif';
- const fingerprintLabel = `#${fingerprint}`;
+ const fingerprintLabel = `ID ${fingerprint}`;
const fingerprintWidth = context.measureText(fingerprintLabel).width;
context.fillText(
fingerprintLabel,
@@ -277,87 +309,193 @@ export const createOfflineRecoveryHtml = async () => {
:root {
color-scheme: light;
--bg: #ffffff;
+ --shell: #ffffff;
--paper: #ffffff;
- --field: rgba(252, 239, 93, 0.12);
+ --field-soft: rgba(252, 239, 93, 0.1);
--ink: #111111;
--muted: rgba(17,17,17,0.72);
--yellow: rgb(252, 239, 93);
--line: rgba(17, 17, 17, 0.18);
--line-strong: rgba(17, 17, 17, 0.22);
}
+ [hidden] { display: none !important; }
* { box-sizing: border-box; }
body {
margin: 0;
- font-family: "Space Grotesk", "Helvetica Neue", sans-serif;
+ font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
background: var(--bg);
color: var(--ink);
}
main {
- width: min(960px, calc(100vw - 32px));
+ width: min(1120px, calc(100vw - 32px));
margin: 0 auto;
- padding: 32px 0 56px;
+ padding: 20px 0 40px;
}
- .panel {
- background: var(--paper);
+ .topbar {
+ display: flex;
+ justify-content: space-between;
+ align-items: center;
+ gap: 16px;
+ padding: 0 4px 18px;
+ }
+ .brand {
+ font-weight: 700;
+ letter-spacing: -0.07em;
+ font-size: clamp(1.7rem, 4vw, 2rem);
+ }
+ .byline {
+ color: var(--muted);
+ font-size: 0.95rem;
+ display: flex;
+ align-items: center;
+ gap: 0.35rem;
+ }
+ .byline a {
+ color: var(--ink);
+ font-weight: 700;
+ text-decoration: none;
+ }
+ .byline a:hover {
+ text-decoration: underline;
+ }
+ .shell {
+ background: var(--shell);
border: 2px solid var(--ink);
- border-radius: 24px;
+ border-radius: 28px;
+ overflow: hidden;
+ box-shadow: 0 16px 34px rgba(17,17,17,0.04);
+ }
+ .panel {
padding: 24px;
}
h1 {
- font-family: "Space Grotesk", sans-serif;
- font-size: clamp(2.1rem, 5vw, 3.4rem);
+ font-size: clamp(2rem, 5vw, 2.7rem);
line-height: 0.92;
letter-spacing: -0.06em;
margin: 0 0 12px;
}
- p { color: var(--muted); line-height: 1.6; }
+ p {
+ color: var(--muted);
+ line-height: 1.6;
+ margin: 0;
+ }
.eyebrow {
display: inline-flex;
align-items: center;
- gap: 8px;
- margin-bottom: 16px;
- padding: 10px 14px;
+ margin-bottom: 18px;
+ padding: 7px 14px;
border-radius: 999px;
- background: var(--yellow);
- border: 2px solid var(--ink);
+ background: var(--paper);
+ border: 1.5px solid var(--line-strong);
font-weight: 700;
+ font-size: 0.82rem;
}
.grid {
display: grid;
- grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
+ grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 16px;
margin-top: 24px;
}
- textarea, input[type="file"] {
+ .slot {
+ padding: 16px;
+ border: 1.5px solid var(--line-strong);
+ border-radius: 24px;
+ background: var(--paper);
+ transition: border-color 140ms ease, background-color 140ms ease, transform 140ms ease;
+ }
+ .slot.dragover {
+ border-color: var(--ink);
+ background: var(--field-soft);
+ transform: translateY(-1px);
+ }
+ .slot-head {
+ display: flex;
+ justify-content: space-between;
+ align-items: center;
+ gap: 12px;
+ }
+ .slot-chip {
+ width: fit-content;
+ padding: 6px 12px;
+ border-radius: 999px;
+ border: 1.5px solid var(--line-strong);
+ font-size: 0.84rem;
+ font-weight: 700;
+ }
+ .slot-copy {
+ min-height: 2.8em;
+ margin-top: 12px;
+ font-size: 0.88rem;
+ }
+ textarea {
width: 100%;
border-radius: 18px;
- border: 2px solid var(--ink);
- background: white;
+ border: 1.5px solid var(--line-strong);
+ background: var(--paper);
padding: 14px 16px;
font: inherit;
+ color: var(--ink);
}
textarea {
min-height: 180px;
resize: vertical;
+ margin-top: 8px;
+ }
+ textarea:focus {
+ outline: none;
+ border-color: var(--ink);
}
- button {
- margin-top: 18px;
- border: 0;
+ .upload,
+ .action,
+ .copy {
+ appearance: none;
+ border: 1.5px solid var(--line-strong);
border-radius: 999px;
- padding: 14px 20px;
- background: var(--yellow);
+ padding: 10px 14px;
+ background: var(--paper);
color: var(--ink);
font: inherit;
font-weight: 700;
cursor: pointer;
+ text-decoration: none;
+ }
+ .upload:hover,
+ .copy:hover {
+ border-color: var(--ink);
+ }
+ .upload input {
+ display: none;
+ }
+ .actions {
+ margin-top: 16px;
+ display: flex;
+ flex-wrap: wrap;
+ gap: 12px;
+ align-items: center;
+ }
+ .action {
+ padding: 14px 20px;
+ border-color: var(--ink);
+ background: var(--yellow);
+ color: #111111;
+ }
+ .action[disabled] {
+ background: transparent;
+ color: var(--muted);
+ border-color: var(--line-strong);
+ cursor: default;
+ }
+ .helper {
+ color: var(--muted);
+ font-size: 0.9rem;
}
.result {
- margin-top: 18px;
white-space: pre-wrap;
background: var(--paper);
- border: 2px solid var(--ink);
- padding: 16px;
+ border: 1.5px solid var(--line-strong);
+ padding: 14px 16px;
border-radius: 18px;
+ min-height: 132px;
}
.error {
color: var(--ink);
@@ -367,27 +505,75 @@ export const createOfflineRecoveryHtml = async () => {
padding: 12px 14px;
margin-top: 12px;
}
+ .result-card {
+ margin-top: 12px;
+ padding: 16px;
+ border: 1.5px solid var(--ink);
+ border-radius: 24px;
+ background: var(--field-soft);
+ }
+ .result-head {
+ display: flex;
+ justify-content: space-between;
+ align-items: center;
+ gap: 12px;
+ margin-bottom: 12px;
+ }
+ .result-title {
+ font-weight: 700;
+ font-size: 1rem;
+ }
+ @media (max-width: 720px) {
+ .topbar {
+ padding-bottom: 14px;
+ }
+ .panel {
+ padding: 20px;
+ }
+ }
</style>
</head>
<body>
<main>
- <div class="panel">
- <div class="eyebrow">Recover later</div>
- <h1>Recover with any two cards.</h1>
- <p>This file works offline. Upload two card images, or paste the two code strings manually.</p>
- <div class="grid">
- <div>
- <input id="file-a" type="file" accept="image/*,text/plain" />
- <textarea id="input-a" placeholder="Paste share 1"></textarea>
+ <div class="topbar">
+ <div class="brand">2of3</div>
+ <div class="byline">by <a href="https://ente.io" target="_blank" rel="noreferrer">Ente</a></div>
+ </div>
+ <div class="shell">
+ <div class="panel">
+ <div class="eyebrow">Recover later</div>
+ <h1>Use any 2 cards to recover</h1>
+ <p>This file works offline. Upload two card images, or paste their codes.</p>
+ <div class="grid">
+ <div class="slot" id="slot-a">
+ <div class="slot-head">
+ <div class="slot-chip">Card A</div>
+ <label class="upload">Upload image<input id="file-a" type="file" accept="image/*,text/plain" /></label>
+ </div>
+ <p class="slot-copy" id="meta-a">Drop a saved card image here, or paste a copied code.</p>
+ <textarea id="input-a" placeholder="Paste code A"></textarea>
+ </div>
+ <div class="slot" id="slot-b">
+ <div class="slot-head">
+ <div class="slot-chip">Card B</div>
+ <label class="upload">Upload image<input id="file-b" type="file" accept="image/*,text/plain" /></label>
+ </div>
+ <p class="slot-copy" id="meta-b">Drop a saved card image here, or paste a copied code.</p>
+ <textarea id="input-b" placeholder="Paste code B"></textarea>
+ </div>
+ </div>
+ <div id="error" class="error" hidden></div>
+ <div class="actions">
+ <button id="recover" class="action">Recover secret</button>
</div>
- <div>
- <input id="file-b" type="file" accept="image/*,text/plain" />
- <textarea id="input-b" placeholder="Paste share 2"></textarea>
+ <div id="result-card" class="result-card" hidden>
+ <div class="result-head">
+ <div class="result-title">Recovered secret</div>
+ <button id="copy-result" class="copy" type="button">Copy</button>
+ </div>
+ <div id="result" class="result"></div>
</div>
</div>
- <button id="recover">Recover secret</button>
- <div id="error" class="error"></div>
- <div id="result" class="result" hidden></div>
</div>
</main>
<script>${escapeScriptTagContent(OFFLINE_QR_DECODER_SOURCE)}</script>
@@ -463,7 +649,7 @@ export const createOfflineRecoveryHtml = async () => {
const combineShares = (firstInput, secondInput) => {
const first = parseShare(firstInput);
const second = parseShare(secondInput);
- if (first.id !== second.id || first.length !== second.length) throw new Error("These two cards are from different sets. Match the # on both cards.");
+ if (first.id !== second.id || first.length !== second.length) throw new Error("These two cards are from different sets. Match the ID on both cards.");
if (first.index === second.index) throw new Error("Use two different cards from the same set.");
const output = new Uint8Array(first.length);
const denominator = first.index ^ second.index;
@@ -584,49 +770,104 @@ export const createOfflineRecoveryHtml = async () => {
}
throw lastError || new Error("Could not read that QR code.");
};
+ const describeSlotValue = (value, fileName = "") => {
+ if (!value.trim()) return "Drop a saved card image here, or paste a copied code.";
+ try {
+ const parsed = parseShare(value);
+ const fingerprint = parsed.id.slice(0, 8).toUpperCase();
+ return (fileName ? fileName + " · " : "") + "Card " + parsed.index + " from ID " + fingerprint;
+ } catch {
+ return fileName || "Drop a saved card image here, or paste a copied code.";
+ }
+ };
const fileToText = async (file) => {
if (file.type.startsWith("text/")) return (await file.text()).trim();
const value = decodeQrImage(await imageDataFromFile(file));
parseShare(value);
return value;
};
- const bindFile = (fileId, textareaId) => {
+ const bindSlot = (slotId, fileId, textareaId, metaId) => {
+ const slot = document.getElementById(slotId);
const fileInput = document.getElementById(fileId);
const textarea = document.getElementById(textareaId);
+ const meta = document.getElementById(metaId);
const errorNode = document.getElementById("error");
- fileInput.addEventListener("change", async (event) => {
- const file = event.target.files && event.target.files[0];
+ const applyFile = async (file) => {
if (!file) return;
try {
- textarea.value = (await fileToText(file)).trim();
+ const value = (await fileToText(file)).trim();
+ textarea.value = value;
+ meta.textContent = describeSlotValue(value, file.name);
+ errorNode.hidden = true;
errorNode.textContent = "";
} catch (error) {
textarea.value = "";
+ meta.textContent = describeSlotValue("");
+ errorNode.hidden = false;
errorNode.textContent = error.message || String(error);
}
+ };
+ fileInput.addEventListener("change", async (event) => {
+ const file = event.target.files && event.target.files[0];
+ await applyFile(file);
+ });
+ textarea.addEventListener("input", (event) => {
+ meta.textContent = describeSlotValue(event.target.value.trim());
+ });
+ slot.addEventListener("dragenter", (event) => {
+ event.preventDefault();
+ slot.classList.add("dragover");
+ });
+ slot.addEventListener("dragover", (event) => {
+ event.preventDefault();
+ event.dataTransfer.dropEffect = "copy";
+ slot.classList.add("dragover");
+ });
+ slot.addEventListener("dragleave", () => {
+ slot.classList.remove("dragover");
+ });
+ slot.addEventListener("drop", async (event) => {
+ event.preventDefault();
+ slot.classList.remove("dragover");
+ const file = event.dataTransfer.files && event.dataTransfer.files[0];
+ await applyFile(file);
});
};
- bindFile("file-a", "input-a");
- bindFile("file-b", "input-b");
+ bindSlot("slot-a", "file-a", "input-a", "meta-a");
+ bindSlot("slot-b", "file-b", "input-b", "meta-b");
document.getElementById("recover").addEventListener("click", () => {
const errorNode = document.getElementById("error");
const resultNode = document.getElementById("result");
+ const resultCard = document.getElementById("result-card");
+ errorNode.hidden = true;
errorNode.textContent = "";
- resultNode.hidden = true;
+ resultCard.hidden = true;
const firstInput = document.getElementById("input-a").value.trim();
const secondInput = document.getElementById("input-b").value.trim();
if (!firstInput || !secondInput) {
+ errorNode.hidden = false;
errorNode.textContent = "Upload or paste two cards first.";
return;
}
try {
const secret = combineShares(firstInput, secondInput);
resultNode.textContent = secret;
- resultNode.hidden = false;
+ resultCard.hidden = false;
} catch (error) {
+ errorNode.hidden = false;
errorNode.textContent = error.message || String(error);
}
});
+ document.getElementById("copy-result").addEventListener("click", async () => {
+ const button = document.getElementById("copy-result");
+ const resultNode = document.getElementById("result");
+ if (!resultNode.textContent.trim()) return;
+ await navigator.clipboard.writeText(resultNode.textContent);
+ button.textContent = "Copied";
+ window.setTimeout(() => {
+ button.textContent = "Copy";
+ }, 1600);
+ });
</script>
</body>
</html>`;
diff --git a/web/apps/twoof3/src/features/twoof3/utils/shamir.ts b/web/apps/twoof3/src/features/twoof3/utils/shamir.ts
index 41c701baf1..47cf0f5ef6 100644
--- a/web/apps/twoof3/src/features/twoof3/utils/shamir.ts
+++ b/web/apps/twoof3/src/features/twoof3/utils/shamir.ts
@@ -225,7 +225,7 @@ export const combineShares = (firstInput: string, secondInput: string) => {
if (first.id !== second.id || first.length !== second.length) {
throw new Error(
- "These two cards are from different sets. Match the # on both cards.",
+ "These two cards are from different sets. Match the ID on both cards.",
);
}
diff --git a/web/apps/twoof3/src/pages/_app.tsx b/web/apps/twoof3/src/pages/_app.tsx
index 02e0d9892b..f5bf127c08 100644
--- a/web/apps/twoof3/src/pages/_app.tsx
+++ b/web/apps/twoof3/src/pages/_app.tsx
@@ -1,5 +1,6 @@
import "@fontsource/space-grotesk/400.css";
import "@fontsource/space-grotesk/500.css";
+import "@fontsource/space-grotesk/600.css";
import "@fontsource/space-grotesk/700.css";
import {
CssBaseline,
@@ -39,9 +40,24 @@ const App = ({ Component, pageProps }: AppProps) => {
typography: {
fontFamily:
'"Space Grotesk", "Inter Variable", "Helvetica Neue", sans-serif',
+ fontWeightRegular: 500,
+ fontWeightMedium: 600,
+ fontWeightBold: 700,
h1: { fontWeight: 700, letterSpacing: "-0.05em" },
h2: { fontWeight: 700, letterSpacing: "-0.04em" },
h3: { fontWeight: 700, letterSpacing: "-0.04em" },
+ body1: {
+ fontWeight: 500,
+ lineHeight: 1.55,
+ letterSpacing: "-0.012em",
+ },
+ body2: {
+ fontWeight: 500,
+ lineHeight: 1.55,
+ letterSpacing: "-0.012em",
+ },
+ subtitle1: { fontWeight: 500 },
+ subtitle2: { fontWeight: 500 },
button: {
fontWeight: 700,
letterSpacing: "-0.01em",
diff --git a/web/apps/twoof3/src/pages/index.tsx b/web/apps/twoof3/src/pages/index.tsx
index 8936c3358e..4cfa3e2f97 100644
--- a/web/apps/twoof3/src/pages/index.tsx
+++ b/web/apps/twoof3/src/pages/index.tsx
@@ -26,10 +26,19 @@ import {
splitSecret,
} from "features/twoof3/utils/shamir";
import Head from "next/head";
-import type { ChangeEvent, ReactNode } from "react";
-import { useDeferredValue, useEffect, useState } from "react";
+import type { ChangeEvent, DragEvent, ReactNode } from "react";
+import {
+ memo,
+ startTransition,
+ useCallback,
+ useDeferredValue,
+ useEffect,
+ useRef,
+ useState,
+} from "react";
const APP_LINK = "2of3.ente.io";
+const MAX_CARD_TITLE_LENGTH = 48;
const textEncoder = new TextEncoder();
const byteLength = (value: string) => textEncoder.encode(value).length;
@@ -45,6 +54,9 @@ const trimToByteLength = (value: string, maxBytes: number) => {
return chars.join("");
};
+const trimToCharacterLength = (value: string, maxChars: number) =>
+ Array.from(value).slice(0, maxChars).join("");
+
interface ShareCardState {
encoded: string;
fingerprint: string;
@@ -85,6 +97,11 @@ const EMPTY_SPLIT_SNAPSHOT: SplitSnapshot = {
secret: "",
};
+const RECOVER_SLOT_LABELS = ["A", "B"] as const;
+
+const recoverSlotLabel = (index: number) =>
+ RECOVER_SLOT_LABELS[index] ?? String(index + 1);
+
const fieldSx = {
"& .MuiOutlinedInput-root": {
borderRadius: "18px",
@@ -144,7 +161,7 @@ const Note = ({
</Box>
);
-const QrPreview = ({
+const QrPreview = memo(({
value,
}: {
value: NonNullable<ReturnType<typeof createQrSvgData>>;
@@ -180,6 +197,207 @@ const QrPreview = ({
/>
))}
</svg>
+));
+
+interface ShareCardPanelProps {
+ busy: boolean;
+ card: ShareCardState | undefined;
+ cardLabel: string;
+ copied: boolean;
+ index: number;
+ onCardAction: (
+ kind: "download" | "print" | "share",
+ card: ShareCardState,
+ displayIndex: number,
+ label: string,
+ ) => void;
+ onCopyCardCode: (card: ShareCardState, displayIndex: number) => void;
+}
+
+const ShareCardPanel = memo(
+ ({
+ busy,
+ card,
+ cardLabel,
+ copied,
+ index,
+ onCardAction,
+ onCopyCardCode,
+ }: ShareCardPanelProps) => {
+ const fingerprint = card ? `ID ${card.fingerprint}` : null;
+
+ return (
+ <Stack
+ sx={{
+ border: "1.5px solid",
+ borderColor: "var(--ink)",
+ borderRadius: "26px",
+ bgcolor: "var(--paper)",
+ boxShadow: "0 14px 28px rgba(17,17,17,0.06)",
+ minHeight: 0,
+ overflow: "hidden",
+ }}
+ >
+ <Box
+ sx={{
+ px: 1.35,
+ py: 1.05,
+ bgcolor: "var(--accent)",
+ borderBottom: "1.5px solid",
+ borderColor: "var(--ink)",
+ }}
+ >
+ <Stack
+ direction="row"
+ justifyContent="space-between"
+ alignItems="center"
+ spacing={1}
+ >
+ <Typography
+ sx={{
+ fontWeight: 700,
+ fontSize: "0.92rem",
+ color: "#111111",
+ }}
+ >
+ Card {index + 1}
+ </Typography>
+ <Typography
+ sx={{
+ color: "rgba(17,17,17,0.62)",
+ fontSize: "0.8rem",
+ fontWeight: 500,
+ }}
+ >
+ {APP_LINK}
+ </Typography>
+ </Stack>
+ </Box>
+
+ <Stack spacing={1.2} sx={{ p: 1.35 }}>
+ <Typography
+ sx={{
+ fontWeight: 700,
+ fontSize: "1rem",
+ lineHeight: 1.2,
+ }}
+ >
+ {cardLabel}
+ </Typography>
+
+ <Box
+ sx={{
+ aspectRatio: "1 / 1",
+ border: "1.5px solid",
+ borderColor: "var(--line-strong)",
+ bgcolor: "#ffffff",
+ borderRadius: "22px",
+ overflow: "hidden",
+ p: 1.35,
+ display: "grid",
+ placeItems: "center",
+ }}
+ >
+ {card ? (
+ <QrPreview value={card.qr} />
+ ) : (
+ <Typography
+ sx={{
+ textAlign: "center",
+ color: "#111111",
+ opacity: 0.6,
+ px: 3,
+ fontSize: "0.95rem",
+ }}
+ >
+ Enter a secret to generate card {index + 1}.
+ </Typography>
+ )}
+ </Box>
+
+ {card && (
+ <Stack
+ direction="row"
+ justifyContent="space-between"
+ alignItems="center"
+ spacing={1}
+ sx={{ mt: 0.15 }}
+ >
+ <Button
+ variant="text"
+ size="small"
+ disabled={busy}
+ onClick={() => {
+ onCopyCardCode(card, index);
+ }}
+ sx={{
+ minWidth: 0,
+ px: 0,
+ py: 0,
+ color: "var(--muted)",
+ fontSize: "0.8rem",
+ fontWeight: 700,
+ textTransform: "none",
+ "&:hover": {
+ bgcolor: "transparent",
+ color: "var(--ink)",
+ },
+ }}
+ >
+ {copied ? "Copied" : "Copy code"}
+ </Button>
+ <Typography
+ sx={{
+ color: "var(--muted)",
+ fontSize: "0.78rem",
+ fontWeight: 500,
+ textAlign: "right",
+ }}
+ >
+ {fingerprint}
+ </Typography>
+ </Stack>
+ )}
+
+ <Stack direction="row" spacing={0.8} useFlexGap flexWrap="wrap">
+ <ActionButton
+ disabled={!card || busy}
+ onClick={() => {
+ if (!card) return;
+ onCardAction("print", card, index, cardLabel);
+ }}
+ >
+ Print
+ </ActionButton>
+ <ActionButton
+ disabled={!card || busy}
+ onClick={() => {
+ if (!card) return;
+ onCardAction("download", card, index, cardLabel);
+ }}
+ >
+ Download
+ </ActionButton>
+ <ActionButton
+ disabled={!card || busy}
+ onClick={() => {
+ if (!card) return;
+ onCardAction("share", card, index, cardLabel);
+ }}
+ >
+ Share
+ </ActionButton>
+ </Stack>
+ </Stack>
+ </Stack>
+ );
+ },
+ (previous, next) =>
+ previous.busy === next.busy &&
+ previous.card?.encoded === next.card?.encoded &&
+ previous.cardLabel === next.cardLabel &&
+ previous.copied === next.copied &&
+ previous.index === next.index,
);
const ActionButton = ({
@@ -298,9 +516,13 @@ const Page = () => {
const [recoverError, setRecoverError] = useState<string | null>(null);
const [recoveredSecret, setRecoveredSecret] = useState("");
const [copiedRecoveredSecret, setCopiedRecoveredSecret] = useState(false);
+ const [recoverDropTarget, setRecoverDropTarget] = useState<number | null>(
+ null,
+ );
const [splitSnapshot, setSplitSnapshot] =
useState<SplitSnapshot>(EMPTY_SPLIT_SNAPSHOT);
const deferredSecret = useDeferredValue(secret);
+ const isCardsStaleRef = useRef(false);
const cards = splitSnapshot.cards;
const isCardsStale = splitSnapshot.secret !== secret;
@@ -327,20 +549,26 @@ const Page = () => {
const canRecover =
hasTwoRecoverValues && !recoverMismatch && !recoverDuplicateCards;
const recoverStatus = recoverMismatch
- ? "These two cards are from different sets. Match the # on both cards."
+ ? "These two cards are from different sets. Match the ID on both cards."
: recoverDuplicateCards
? "Use two different cards from the same set."
: recoverError;
+ isCardsStaleRef.current = isCardsStale;
+
useEffect(() => {
if (!secret.trim()) {
- setSplitSnapshot(EMPTY_SPLIT_SNAPSHOT);
+ startTransition(() => {
+ setSplitSnapshot(EMPTY_SPLIT_SNAPSHOT);
+ });
}
}, [secret]);
useEffect(() => {
if (!deferredSecret.trim()) return;
- setSplitSnapshot(splitIfPossible(deferredSecret));
+ startTransition(() => {
+ setSplitSnapshot(splitIfPossible(deferredSecret));
+ });
}, [deferredSecret]);
useEffect(() => {
@@ -369,39 +597,58 @@ const Page = () => {
return () => window.clearTimeout(timer);
}, [copiedRecoveredSecret]);
- const makeCardFile = async (card: ShareCardState) => {
- const canvas = await renderShareCard({
- qrModules: card.qr.modules,
- qrSize: card.qr.viewBoxSize,
- shareIndex: card.index,
- shareText: card.encoded,
- title: cardLabel,
- });
- const blob = await canvasToBlob(canvas);
- const filename = `${sanitizeFilename(cardLabel)}-set-${card.fingerprint.toLowerCase()}-card-${card.index}.png`;
- return new File([blob], filename, { type: "image/png" });
- };
-
- const runCardAction = async (
- index: number,
- action: (file: File, card: ShareCardState) => Promise<void> | void,
- ) => {
- const card = cards[index];
- if (!card || isCardsStale) return;
- setActionError(null);
- setBusyCard(index);
+ const makeCardFile = useCallback(
+ async (card: ShareCardState, label: string) => {
+ const canvas = await renderShareCard({
+ qrModules: card.qr.modules,
+ qrSize: card.qr.viewBoxSize,
+ shareIndex: card.index,
+ shareText: card.encoded,
+ title: label,
+ });
+ const blob = await canvasToBlob(canvas);
+ const filename = `${sanitizeFilename(label)}-set-${card.fingerprint.toLowerCase()}-card-${card.index}.png`;
+ return new File([blob], filename, { type: "image/png" });
+ },
+ [],
+ );
- try {
- const file = await makeCardFile(card);
- await action(file, card);
- } catch (error) {
- setActionError(
- error instanceof Error ? error.message : "That action failed.",
- );
- } finally {
- setBusyCard(null);
- }
- };
+ const onCardAction = useCallback(
+ async (
+ kind: "download" | "print" | "share",
+ card: ShareCardState,
+ displayIndex: number,
+ label: string,
+ ) => {
+ if (isCardsStaleRef.current) return;
+ setActionError(null);
+ setBusyCard(displayIndex);
+
+ try {
+ const file = await makeCardFile(card, label);
+ if (kind === "print") {
+ printBlob(file, file.name);
+ } else if (kind === "download") {
+ downloadBlob(file, file.name);
+ } else {
+ try {
+ await shareFiles([file]);
+ } catch {
+ downloadBlob(file, file.name);
+ }
+ }
+ } catch (error) {
+ setActionError(
+ error instanceof Error
+ ? error.message
+ : "That action failed.",
+ );
+ } finally {
+ setBusyCard(null);
+ }
+ },
+ [makeCardFile],
+ );
const onDownloadAll = async () => {
if (!isReady) return;
@@ -410,7 +657,7 @@ const Page = () => {
try {
for (const card of cards) {
- const file = await makeCardFile(card);
+ const file = await makeCardFile(card, cardLabel);
downloadBlob(file, file.name);
}
@@ -439,7 +686,7 @@ const Page = () => {
if (recoverMismatch) {
setRecoverError(
- "These two cards are from different sets. Match the # on both cards.",
+ "These two cards are from different sets. Match the ID on both cards.",
);
return;
}
@@ -464,29 +711,29 @@ const Page = () => {
}
};
- const onCopyCardCode = async (card: ShareCardState) => {
- if (isCardsStale) return;
- setActionError(null);
-
- try {
- await copyTextToClipboard(card.encoded);
- setCopiedCardIndex(card.index - 1);
- } catch (error) {
- setActionError(
- error instanceof Error
- ? error.message
- : "Could not copy that code.",
- );
- }
- };
+ const onCopyCardCode = useCallback(
+ async (card: ShareCardState, displayIndex: number) => {
+ if (isCardsStaleRef.current) return;
+ setActionError(null);
+
+ try {
+ await copyTextToClipboard(card.encoded);
+ setCopiedCardIndex(displayIndex);
+ } catch (error) {
+ setActionError(
+ error instanceof Error
+ ? error.message
+ : "Could not copy that code.",
+ );
+ }
+ },
+ [],
+ );
const onRecoverFile = async (
slotIndex: number,
- event: ChangeEvent<HTMLInputElement>,
+ file: File,
) => {
- const file = event.target.files?.[0];
- if (!file) return;
-
try {
const value = await readShareFile(file);
parseShare(value);
@@ -502,11 +749,40 @@ const Page = () => {
? error.message
: "Could not read that share.",
);
+ } finally {
+ setRecoverDropTarget((current) =>
+ current === slotIndex ? null : current,
+ );
+ }
+ };
+
+ const onRecoverFileChange = async (
+ slotIndex: number,
+ event: ChangeEvent<HTMLInputElement>,
+ ) => {
+ const file = event.target.files?.[0];
+ if (!file) return;
+
+ try {
+ await onRecoverFile(slotIndex, file);
} finally {
event.target.value = "";
}
};
+ const onRecoverDrop = async (
+ slotIndex: number,
+ event: DragEvent<HTMLDivElement>,
+ ) => {
+ event.preventDefault();
+ setRecoverDropTarget(null);
+
+ const file = event.dataTransfer.files[0];
+ if (!file) return;
+
+ await onRecoverFile(slotIndex, file);
+ };
+
return (
<>
<Head>
@@ -709,8 +985,18 @@ const Page = () => {
value={title}
placeholder={todayLabel()}
onChange={(event) =>
- setTitle(event.target.value)
+ setTitle(
+ trimToCharacterLength(
+ event.target.value,
+ MAX_CARD_TITLE_LENGTH,
+ ),
+ )
}
+ slotProps={{
+ htmlInput: {
+ maxLength: MAX_CARD_TITLE_LENGTH,
+ },
+ }}
sx={fieldSx}
/>
<Typography
@@ -814,257 +1100,18 @@ const Page = () => {
>
{Array.from({ length: 3 }, (_, index) => {
const card = cards[index];
- const fingerprint = card
- ? `#${card.fingerprint}`
- : null;
return (
- <Stack
+ <ShareCardPanel
key={index}
- sx={{
- border: "1.5px solid",
- borderColor: "var(--ink)",
- borderRadius: "26px",
- bgcolor: "var(--paper)",
- boxShadow:
- "0 14px 28px rgba(17,17,17,0.06)",
- minHeight: 0,
- overflow: "hidden",
- }}
- >
- <Box
- sx={{
- px: 1.35,
- py: 1.05,
- bgcolor:
- "var(--accent)",
- borderBottom:
- "1.5px solid",
- borderColor:
- "var(--ink)",
- }}
- >
- <Stack
- direction="row"
- justifyContent="space-between"
- alignItems="center"
- spacing={1}
- >
- <Typography
- sx={{
- fontWeight: 700,
- fontSize:
- "0.92rem",
- color: "#111111",
- }}
- >
- Card {index + 1}
- </Typography>
- <Typography
- sx={{
- color: "rgba(17,17,17,0.62)",
- fontSize:
- "0.8rem",
- fontWeight: 500,
- }}
- >
- {APP_LINK}
- </Typography>
- </Stack>
- </Box>
-
- <Stack
- spacing={1.2}
- sx={{ p: 1.35 }}
- >
- <Typography
- sx={{
- fontWeight: 700,
- fontSize: "1rem",
- lineHeight: 1.2,
- }}
- >
- {cardLabel}
- </Typography>
-
- <Box
- sx={{
- aspectRatio:
- "1 / 1",
- border: "1.5px solid",
- borderColor:
- "var(--line-strong)",
- bgcolor: "#ffffff",
- borderRadius:
- "22px",
- overflow: "hidden",
- p: 1.35,
- display: "grid",
- placeItems:
- "center",
- }}
- >
- {card ? (
- <QrPreview
- value={card.qr}
- />
- ) : (
- <Typography
- sx={{
- textAlign:
- "center",
- color: "#111111",
- opacity: 0.6,
- px: 3,
- fontSize:
- "0.95rem",
- }}
- >
- Enter a secret
- to generate card{" "}
- {index + 1}.
- </Typography>
- )}
- </Box>
-
- {card && (
- <Stack
- direction="row"
- justifyContent="space-between"
- alignItems="center"
- spacing={1}
- sx={{ mt: 0.15 }}
- >
- <Button
- variant="text"
- size="small"
- disabled={
- isCardsStale
- }
- onClick={() => {
- void onCopyCardCode(
- card,
- );
- }}
- sx={{
- minWidth: 0,
- px: 0,
- py: 0,
- color: "var(--muted)",
- fontSize:
- "0.8rem",
- fontWeight: 700,
- textTransform:
- "none",
- "&:hover": {
- bgcolor:
- "transparent",
- color: "var(--ink)",
- },
- }}
- >
- {copiedCardIndex ===
- index
- ? "Copied"
- : "Copy code"}
- </Button>
- <Typography
- sx={{
- color: "var(--muted)",
- fontSize:
- "0.78rem",
- fontWeight: 500,
- textAlign:
- "right",
- }}
- >
- {fingerprint}
- </Typography>
- </Stack>
- )}
-
- <Stack
- direction="row"
- spacing={0.8}
- useFlexGap
- flexWrap="wrap"
- >
- <ActionButton
- disabled={
- !card ||
- isCardsStale ||
- busyCard ===
- index
- }
- onClick={() => {
- void runCardAction(
- index,
- (file) => {
- printBlob(
- file,
- file.name,
- );
- },
- );
- }}
- >
- Print
- </ActionButton>
- <ActionButton
- disabled={
- !card ||
- isCardsStale ||
- busyCard ===
- index
- }
- onClick={() => {
- void runCardAction(
- index,
- (file) => {
- downloadBlob(
- file,
- file.name,
- );
- },
- );
- }}
- >
- Download
- </ActionButton>
- <ActionButton
- disabled={
- !card ||
- isCardsStale ||
- busyCard ===
- index
- }
- onClick={() => {
- void runCardAction(
- index,
- async (
- file,
- ) => {
- try {
- await shareFiles(
- [
- file,
- ],
- );
- } catch {
- downloadBlob(
- file,
- file.name,
- );
- }
- },
- );
- }}
- >
- Share
- </ActionButton>
- </Stack>
- </Stack>
- </Stack>
+ busy={busyCard === index}
+ card={card}
+ cardLabel={cardLabel}
+ copied={copiedCardIndex === index}
+ index={index}
+ onCardAction={onCardAction}
+ onCopyCardCode={onCopyCardCode}
+ />
);
})}
</Box>
@@ -1126,8 +1173,8 @@ const Page = () => {
maxWidth: 680,
}}
>
- Upload two card images, or paste two
- codes if you copied them earlier.
+ Upload two card images, or paste their
+ codes.
</Typography>
</Box>
@@ -1144,18 +1191,54 @@ const Page = () => {
{recoverSlots.map((slot, index) => {
const slotDetails =
recoverSlotDetails[index];
+ const slotLabel =
+ recoverSlotLabel(index);
+ const isDropActive =
+ recoverDropTarget === index;
return (
<Stack
key={index}
spacing={1.15}
+ onDragEnter={(event) => {
+ event.preventDefault();
+ setRecoverDropTarget(index);
+ }}
+ onDragLeave={() => {
+ setRecoverDropTarget(
+ (current) =>
+ current === index
+ ? null
+ : current,
+ );
+ }}
+ onDragOver={(event) => {
+ event.preventDefault();
+ event.dataTransfer.dropEffect =
+ "copy";
+ setRecoverDropTarget(index);
+ }}
+ onDrop={(event) => {
+ void onRecoverDrop(
+ index,
+ event,
+ );
+ }}
sx={{
p: 1.45,
border: "1.5px solid",
- borderColor:
- "var(--line-strong)",
+ borderColor: isDropActive
+ ? "var(--ink)"
+ : "var(--line-strong)",
borderRadius: "24px",
- bgcolor: "var(--paper)",
+ bgcolor: isDropActive
+ ? "var(--field-soft)"
+ : "var(--paper)",
+ transition:
+ "border-color 140ms ease, background-color 140ms ease, transform 140ms ease",
+ transform: isDropActive
+ ? "translateY(-1px)"
+ : "translateY(0)",
}}
>
<Stack
@@ -1183,7 +1266,7 @@ const Page = () => {
"0.84rem",
}}
>
- Card {index + 1}
+ Card {slotLabel}
</Typography>
</Box>
<Button
@@ -1218,7 +1301,7 @@ const Page = () => {
onChange={(
event,
) => {
- void onRecoverFile(
+ void onRecoverFileChange(
index,
event,
);
@@ -1238,13 +1321,13 @@ const Page = () => {
}}
>
{slotDetails
- ? `${slot.fileName ? `${slot.fileName} · ` : ""}Card ${slotDetails.index} from #${slotDetails.fingerprint}`
+ ? `${slot.fileName ? `${slot.fileName} · ` : ""}Card ${slotDetails.index} from ID ${slotDetails.fingerprint}`
: slot.fileName
? slot.fileName
- : "Upload a saved card image, or paste a copied code."}
+ : "Drop a saved card image here, or paste a copied code."}
</Typography>
<TextField
- label={`Code ${index + 1}`}
+ label={`Code ${slotLabel}`}
multiline
minRows={6}
value={slot.value}
@@ -1270,7 +1353,7 @@ const Page = () => {
),
)
}
- placeholder={`Paste code ${index + 1}`}
+ placeholder={`Paste code ${slotLabel}`}
sx={fieldSx}
/>
</Stack>