[app] KeepSafe: Wire persist, the file download and import; e2e restores her on a fresh profile

This commit is contained in:
2026-11-02 00:44:22 +01:00
parent 512c300973
commit e63563755b
2 changed files with 95 additions and 2 deletions
+38 -2
View File
@@ -48,13 +48,49 @@
}
}
// --- Keeping her (keep-her-safe.md) -------------------------------------
const browserStorage = typeof navigator !== 'undefined' ? navigator.storage : undefined;
const keeping: RoseWiring['keeping'] = {
persisted: async () => (browserStorage?.persisted ? browserStorage.persisted() : null),
persist: async () => (browserStorage?.persist ? browserStorage.persist() : null)
};
const installed = (): boolean =>
matchMedia('(display-mode: standalone)').matches || (navigator as Navigator & { standalone?: boolean }).standalone === true;
// An installed app is asked once, after the first care of a visit: by then there is
// something to lose, and installed apps are not met with a permission prompt (K5).
let askedToKeep = false;
function keepAfterCare(): void {
if (askedToKeep || !installed()) return;
askedToKeep = true;
keeping.persist().catch((error: unknown) => console.error('keep: asking the browser to keep the garden failed', error));
}
function saveFile(name: string, text: string): void {
// A file made here and handed over; nothing is sent anywhere.
const url = URL.createObjectURL(new Blob([text], { type: 'application/json' }));
const a = Object.assign(document.createElement('a'), { href: url, download: name });
document.body.append(a);
a.click();
a.remove();
setTimeout(() => URL.revokeObjectURL(url), 10_000);
}
const rose: RoseWiring = {
identity: () => (identity ??= findIdentity()),
loadCare: () => storage.loadCare(),
appendCare: (event) => storage.appendCare(event),
appendCare: async (event) => {
await storage.appendCare(event);
keepAfterCare();
},
clientVersion: version,
deviceZone,
now: () => Date.now()
now: () => Date.now(),
pastKeys: () => storage.loadPastKeys(),
exportRose: (currentKey) => storage.exportRose(currentKey),
importRose: (file, currentKey) => storage.importRose(file, currentKey),
saveFile,
keeping,
installHint: /iPhone|iPad|iPod/.test(navigator.userAgent) && !installed()
};
</script>