Files
Rosea/packages/ui/src/rose/KeepSafe.svelte
T

145 lines
4.2 KiB
Svelte

<script lang="ts">
import { Button } from '@domi/ui';
import { en } from '../copy/en.js';
import type { RoseWiring } from './types.js';
/**
* Keeping her (keep-her-safe.md): what the browser has promised, asking it to keep
* her, saving her to a file, and bringing her back from one. Decides nothing; the
* file's shape is checked by storage.
*/
let { wiring, currentKey, today, onrestored }: { wiring: RoseWiring; currentKey: string; today: string; onrestored: () => void } = $props();
const uid = $props.id();
let persisted = $state<boolean | null | undefined>(undefined);
let refused = $state(false);
let said = $state('');
const inApp = $derived(wiring.keeping.where === 'app');
$effect(() => {
// In an app there is no browser to ask, and the answer would always be no (A1).
if (inApp) return;
wiring.keeping.persisted().then(
(p) => (persisted = p),
(error: unknown) => {
console.error('keep: the browser could not say whether it keeps the garden', error);
persisted = null;
}
);
});
async function ask(): Promise<void> {
try {
const granted = await wiring.keeping.persist();
persisted = granted;
refused = granted !== true;
} catch (error) {
console.error('keep: asking the browser to keep the garden failed', error);
refused = true;
}
}
async function save(): Promise<void> {
try {
const file = await wiring.exportRose(currentKey);
said = '';
const written = await wiring.saveFile(`rose-${today}.json`, JSON.stringify(file, null, 1));
if (written === true) said = en.rose.safe.saved;
} catch (error) {
console.error('keep: the rose could not be saved to a file', error);
}
}
async function bringBack(event: Event & { currentTarget: HTMLInputElement }): Promise<void> {
const input = event.currentTarget;
const file = input.files?.[0];
if (!file) return;
try {
const { added, petals } = await wiring.importRose(JSON.parse(await file.text()), currentKey);
said = added === 0 && petals === 0 ? en.rose.safe.alreadyHere : en.rose.safe.broughtBack(added);
onrestored();
} catch (error) {
// A wrong file is an ordinary thing to choose; the player is told calmly, the console fully.
console.warn('keep: the chosen file is not a rose', error);
said = en.rose.safe.notARose;
} finally {
input.value = '';
}
}
</script>
<section class="safe" aria-labelledby="{uid}-title">
<h3 id="{uid}-title">{en.rose.safe.title}</h3>
{#if inApp}
<p>{en.rose.safe.app}</p>
{:else if persisted !== undefined}
<p>{refused ? en.rose.safe.refused : persisted === true ? en.rose.safe.kept : persisted === false ? en.rose.safe.mayClear : en.rose.safe.unknown}</p>
{/if}
{#if !inApp && persisted === false && !refused}
<Button variant="quiet" onclick={() => void ask()}>{en.rose.safe.ask}</Button>
{/if}
{#if !inApp && wiring.installHint}
<p>{en.rose.safe.install}</p>
{/if}
<Button onclick={() => void save()}>{en.rose.safe.save}</Button>
<p class="note">{en.rose.safe.fileNote}</p>
<!-- A label, so the file picker stays the browser's own; it wears @domi/ui's plate. -->
<label class="file domi-btn">
<span class="domi-btn__label">{en.rose.safe.bringBack}</span>
<input type="file" accept="application/json,.json" onchange={bringBack} />
</label>
<p class="said" role="status">{said}</p>
</section>
<style>
.safe {
display: grid;
gap: var(--space-2);
justify-items: center;
max-width: 24rem;
text-align: center;
}
h3 {
margin: 0;
font-family: var(--font-display);
font-weight: 400;
font-size: 1.1rem;
}
p {
margin: 0;
}
.note,
.said {
color: var(--ink-soft);
font-size: 0.9rem;
}
.said:empty {
display: none;
}
.file {
position: relative;
}
/* The real input stays operable and labelled; only its default look is hidden. */
.file input {
position: absolute;
inset: 0;
opacity: 0;
cursor: pointer;
}
/* The plate's own focus ring, on the label that holds the real input. */
.file:focus-within {
outline: 2px solid var(--domi-accent);
outline-offset: 3px;
}
</style>