[ui] KeepSafe: Say what the browser promised, ask it to keep her, save her to a file, bring her back
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
<script lang="ts">
|
||||
import { careEvent, pour, roseAt, ROSE, type Mood, type RoseNow, type Stage } from '@rosea/engine';
|
||||
import { careEvent, localDay, pour, roseAt, ROSE, type Mood, type RoseNow, type Stage } from '@rosea/engine';
|
||||
import type { CareEvent } from '@rosea/schema';
|
||||
import { en } from '../copy/en.js';
|
||||
import KeepSafe from './KeepSafe.svelte';
|
||||
import type { RoseWiring } from './types.js';
|
||||
// The art board's stand-ins until real art is accepted (rose.md R10, docs/ART.md).
|
||||
import bud from '../../../../art/placeholders/rose-bud.svg?url';
|
||||
@@ -24,6 +25,9 @@
|
||||
const MOOD_PICTURE: Record<Mood, string> = { content: fullBloom, thirsty, 'too-full': overwatered, windblown, blushing, waiting };
|
||||
|
||||
let seed = $state<string | null>(null);
|
||||
/** Keys she was named by on another device (keep-her-safe.md K2). */
|
||||
let pastKeys = $state.raw<readonly string[]>([]);
|
||||
let showSafe = $state(false);
|
||||
let events = $state.raw<readonly CareEvent[]>([]);
|
||||
let now = $state(0);
|
||||
let kept = $state(true);
|
||||
@@ -32,10 +36,11 @@
|
||||
$effect(() => {
|
||||
const w = wiring;
|
||||
now = w.now();
|
||||
Promise.all([w.identity(), w.loadCare()]).then(
|
||||
([id, log]) => {
|
||||
Promise.all([w.identity(), w.loadCare(), w.pastKeys()]).then(
|
||||
([id, log, past]) => {
|
||||
seed = id.publicKey;
|
||||
events = log;
|
||||
pastKeys = past;
|
||||
if (!id.kept) kept = false;
|
||||
},
|
||||
(error: unknown) => {
|
||||
@@ -48,7 +53,19 @@
|
||||
return () => clearInterval(timer);
|
||||
});
|
||||
|
||||
const rose = $derived<RoseNow | null>(seed === null ? null : roseAt(events, seed, now, timeZone));
|
||||
const rose = $derived<RoseNow | null>(seed === null ? null : roseAt(events, [seed, ...pastKeys], now, timeZone));
|
||||
|
||||
/** After she is brought back from a file: read her log and keys again. */
|
||||
function restored(): void {
|
||||
Promise.all([wiring.loadCare(), wiring.pastKeys()]).then(
|
||||
([log, past]) => {
|
||||
events = log;
|
||||
pastKeys = past;
|
||||
now = wiring.now();
|
||||
},
|
||||
(error: unknown) => console.error('rose: could not read her back after restoring', error)
|
||||
);
|
||||
}
|
||||
|
||||
function care(act: CareEvent['act'], amount?: number): void {
|
||||
if (seed === null) return;
|
||||
@@ -162,7 +179,10 @@
|
||||
<p class="hint">{en.rose.notKept}</p>
|
||||
{/if}
|
||||
|
||||
<button type="button" class="quiet" aria-expanded={showNumbers} aria-controls="rose-numbers" onclick={() => (showNumbers = !showNumbers)}>{en.rose.numbers}</button>
|
||||
<div class="more">
|
||||
<button type="button" class="quiet" aria-expanded={showNumbers} aria-controls="rose-numbers" onclick={() => (showNumbers = !showNumbers)}>{en.rose.numbers}</button>
|
||||
<button type="button" class="quiet" aria-expanded={showSafe} onclick={() => (showSafe = !showSafe)}>{en.rose.safe.title}</button>
|
||||
</div>
|
||||
{#if showNumbers}
|
||||
<dl id="rose-numbers" class="numbers">
|
||||
<dt>{en.rose.waterLevel}</dt><dd>{percent(rose.water)}</dd>
|
||||
@@ -171,6 +191,9 @@
|
||||
<dt>{en.rose.trend}</dt><dd>{en.rose.trendWords[rose.trend]}</dd>
|
||||
</dl>
|
||||
{/if}
|
||||
{#if showSafe && seed}
|
||||
<KeepSafe {wiring} currentKey={seed} today={localDay(now, timeZone)} onrestored={restored} />
|
||||
{/if}
|
||||
</section>
|
||||
{/if}
|
||||
|
||||
@@ -253,6 +276,11 @@
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.more {
|
||||
display: flex;
|
||||
gap: var(--space-3);
|
||||
}
|
||||
|
||||
.numbers {
|
||||
display: grid;
|
||||
grid-template-columns: auto auto;
|
||||
|
||||
Reference in New Issue
Block a user