Files
Rosea/packages/ui/src/Shell.svelte
T

336 lines
10 KiB
Svelte

<script lang="ts">
import { Button, DomiCredit, strike } from '@domi/ui';
import { PRODUCT_NAME, type Gardener } from '@rosea/schema';
import { tick } from 'svelte';
import { en } from './copy/en.js';
import Letter from './letter/Letter.svelte';
import { parseLetter } from './letter/parse.js';
import Rose from './rose/Rose.svelte';
import type { RoseWiring } from './rose/types.js';
import ChairSunset from './sunset/ChairSunset.svelte';
import SunsetLine from './sunset/SunsetLine.svelte';
import SunsetPlace from './sunset/SunsetPlace.svelte';
import GardenerChoice from './scene/GardenerChoice.svelte';
import { FIRST_LOOK } from './scene/look.js';
import type { SunsetChoice, SunsetWiring } from './sunset/types.js';
let {
letter = '',
sunset,
rose,
updateReady = false,
onrefresh
}: {
/** The author's Markdown from docs/LETTER.md. Empty means there is no way to the page. */
letter?: string;
/** Where the sunset is, and how to keep it. Absent, the garden shows no sunset at all. */
sunset?: SunsetWiring;
/** The rose and her care log. Absent, the garden has no rose. */
rose?: RoseWiring;
/** A new version of the garden is installed and waiting (calm-refresh.md). */
updateReady?: boolean;
/** Swap to the waiting version and reload. Only ever called by the player's tap. */
onrefresh?: () => void;
} = $props();
// --- The gardener (planet-garden.md G6) -----------------------------------
/** Without gardener wiring the garden asks nothing and shows the first look. */
let gardenerStatus = $state<'loading' | 'asking' | 'set'>('set');
let look = $state.raw<Gardener | null>(null);
let choosingGardener = $state(false);
let gardenerKept = $state(true);
$effect(() => {
const g = rose?.gardener;
if (!g) return;
gardenerStatus = 'loading';
g.load().then(
(saved) => {
look = saved;
gardenerStatus = saved ? 'set' : 'asking';
},
(error: unknown) => {
console.error('gardener: the device could not read the saved gardener; asking again', error);
gardenerKept = false;
gardenerStatus = 'asking';
}
);
});
function chooseGardener(next: Gardener): void {
look = next;
gardenerStatus = 'set';
choosingGardener = false;
rose?.gardener?.save(next).then(
() => (gardenerKept = true),
(error: unknown) => {
console.error('gardener: the device could not keep the gardener', error);
gardenerKept = false;
}
);
}
// --- The sunset (sunset-place.md) ---------------------------------------
let status = $state<'loading' | 'asking' | 'set'>('loading');
let choice = $state<SunsetChoice | null>(null);
let changing = $state(false);
/** False once the device refused to keep the answer: then the garden says it will ask again. */
let kept = $state(true);
let now = $state(0);
let sunLine = $state<HTMLElement>();
/** The in-game sunset is showing (chair-sunset.md). */
let watching = $state(false);
let chairButton = $state<HTMLButtonElement>();
async function stopWatching(): Promise<void> {
watching = false;
await tick();
chairButton?.focus();
}
const place = $derived(choice?.kind === 'town' ? choice.town : (sunset?.guess ?? null));
// One zone for the sunset and the rose's days: the chosen town's, else the device's (rose.md R8).
const roseZone = $derived(choice?.kind === 'town' ? choice.town.timeZone : (rose?.deviceZone ?? 'UTC'));
$effect(() => {
const wiring = sunset;
if (!wiring) return;
now = wiring.now();
wiring.load().then(
(saved) => {
choice = saved;
status = saved ? 'set' : 'asking';
},
(error: unknown) => {
console.error('sunset: the device could not read the saved town; asking again', error);
kept = false;
status = 'asking';
}
);
// The line speaks of tomorrow once tonight's sunset has passed.
const timer = setInterval(() => (now = wiring.now()), 60_000);
return () => clearInterval(timer);
});
// A town chosen in another tab becomes the town here too (two-tabs.md).
$effect(() => {
const wiring = sunset;
return wiring?.changes?.(() => {
wiring.load().then(
(saved) => {
if (saved === null) return;
choice = saved;
status = 'set';
now = wiring.now();
},
(error: unknown) => console.error('sunset: could not read the town another tab chose', error)
);
});
});
async function choose(next: SunsetChoice): Promise<void> {
if (!sunset) return;
choice = next;
changing = false;
status = 'set';
now = sunset.now();
sunset.save(next).then(
() => (kept = true),
(error: unknown) => {
console.error('sunset: the device could not keep the town', error);
kept = false;
}
);
// The player acted, so focus follows them to the answer.
await tick();
sunLine?.focus();
}
const LETTER_HASH = '#letter';
// Any hash but the letter's is the garden. A bare '#' is not a valid link target.
const GARDEN_HASH = '#garden';
const blocks = $derived(parseLetter(letter));
let hash = $state(location.hash);
const view = $derived(hash === LETTER_HASH && blocks.length > 0 ? 'letter' : 'home');
let heading = $state<HTMLHeadingElement>();
// Focus follows the view only after the player moved, never on the first load.
let moved = false;
$effect(() => {
const onChange = (): void => {
moved = true;
hash = location.hash;
};
window.addEventListener('hashchange', onChange);
return () => window.removeEventListener('hashchange', onChange);
});
$effect(() => {
// Read `heading` first: an effect tracks only what it reads, and `moved` is not state.
const target = heading;
if (moved) target?.focus();
});
</script>
{#if view === 'letter'}
<main class="shell page">
<h1 bind:this={heading} tabindex="-1">{en.letter.title}</h1>
<Letter {blocks} />
<a href={GARDEN_HASH}>{en.letter.back}</a>
{@render credit()}
</main>
{:else}
<main class="shell">
<h1 bind:this={heading} tabindex="-1">{PRODUCT_NAME}</h1>
<p>{en.shell.preparing}</p>
{#if rose && (gardenerStatus === 'asking' || choosingGardener)}
<!-- The first open, or Change gardener: Who tends her? (planet-garden.md §1) -->
<GardenerChoice
current={choosingGardener ? look : null}
onchoose={chooseGardener}
{...choosingGardener ? { oncancel: () => (choosingGardener = false) } : {}}
/>
{:else}
{#if rose && gardenerStatus === 'set'}
{#if !gardenerKept}
<p class="hint">{en.gardener.notKept}</p>
{/if}
<Rose
wiring={rose}
timeZone={roseZone}
{place}
look={look ?? FIRST_LOOK}
{...rose.gardener ? { onchangegardener: () => (choosingGardener = true) } : {}}
/>
{/if}
{#if sunset && (status === 'asking' || changing)}
<SunsetPlace
guess={sunset.guess}
current={changing ? place : null}
search={sunset.search}
focusOnOpen={changing}
onchoose={choose}
{...changing ? { oncancel: () => (changing = false) } : {}}
/>
{:else if sunset && status === 'set'}
<div class="sun" tabindex="-1" bind:this={sunLine}>
{#if place}
<SunsetLine {place} guessed={choice?.kind === 'device-zone'} {now} />
{:else}
<p>{en.sunset.noTown}</p>
{/if}
{#if !kept}
<p class="hint">{en.sunset.notKept}</p>
{/if}
<Button variant="quiet" onclick={() => (changing = true)}>{place ? en.sunset.change : en.sunset.choose}</Button>
<!-- @domi/ui's quiet button written as markup (INTEGRATE.md §3): focus comes back here after the sunset. -->
<button type="button" class="domi-btn domi-btn--quiet" bind:this={chairButton} use:strike onclick={() => (watching = true)}>
<span class="domi-btn__label">{en.chair.move}</span>
</button>
</div>
{#if watching}
<ChairSunset {place} guessed={choice?.kind === 'device-zone'} now={sunset.now} onclose={stopWatching} />
{/if}
{/if}
{/if}
{#if blocks.length > 0}
<a href={LETTER_HASH}>{en.letter.title}</a>
{/if}
{@render credit()}
</main>
{/if}
<!--
The licence's credit (CPAL-1.0 Exhibit B, for Rosea and for @domi/ui): its exact words, at the foot of every screen
from the first open on (domi-ui.md D9). link={false}: the address as plain text, because the garden is offline and
a tap must never carry the player out of it, least of all on Android, which has no internet at all.
-->
{#snippet credit()}
<DomiCredit link={false} class="credit" />
{/snippet}
{#if updateReady && onrefresh}
<aside class="update" aria-label={en.update.ready}>
<p role="status">{en.update.ready}</p>
<Button onclick={onrefresh}>{en.update.refresh}</Button>
</aside>
{/if}
<style>
.shell {
min-height: 100dvh;
display: grid;
place-content: center;
gap: var(--space-2);
padding: var(--space-4);
text-align: center;
}
.page {
place-content: start center;
text-align: start;
}
h1 {
margin: 0;
font-family: var(--font-display);
font-weight: 400;
font-size: clamp(2rem, 8vw, 3.5rem);
letter-spacing: 0.02em;
color: var(--rose);
}
h1:focus {
outline: none;
}
p {
margin: 0;
color: var(--ink-soft);
}
a {
color: var(--rose);
}
.sun {
display: grid;
gap: var(--space-1);
justify-items: center;
margin-top: var(--space-3);
}
.sun:focus {
outline: none;
}
.hint {
font-size: 0.9rem;
}
.shell :global(.credit) {
margin-top: var(--space-4);
}
.update {
position: sticky;
bottom: 0;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
gap: var(--space-2);
padding: var(--space-2) var(--space-3);
background: var(--paper);
border-top: 1px solid color-mix(in srgb, var(--ink-soft) 30%, transparent);
}
.update p {
margin: 0;
color: var(--ink-soft);
}
</style>