203 lines
5.3 KiB
Svelte
203 lines
5.3 KiB
Svelte
<script lang="ts">
|
|
import { PRODUCT_NAME } 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 SunsetLine from './sunset/SunsetLine.svelte';
|
|
import SunsetPlace from './sunset/SunsetPlace.svelte';
|
|
import type { SunsetChoice, SunsetWiring } from './sunset/types.js';
|
|
|
|
let {
|
|
letter = '',
|
|
sunset
|
|
}: {
|
|
/** 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;
|
|
} = $props();
|
|
|
|
// --- 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>();
|
|
|
|
const place = $derived(choice?.kind === 'town' ? choice.town : (sunset?.guess ?? null));
|
|
|
|
$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);
|
|
});
|
|
|
|
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>
|
|
</main>
|
|
{:else}
|
|
<main class="shell">
|
|
<h1 bind:this={heading} tabindex="-1">{PRODUCT_NAME}</h1>
|
|
<p>{en.shell.preparing}</p>
|
|
{#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 type="button" class="quiet" onclick={() => (changing = true)}>{place ? en.sunset.change : en.sunset.choose}</button>
|
|
</div>
|
|
{/if}
|
|
{#if blocks.length > 0}
|
|
<a href={LETTER_HASH}>{en.letter.title}</a>
|
|
{/if}
|
|
</main>
|
|
{/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;
|
|
}
|
|
|
|
.quiet {
|
|
font: inherit;
|
|
background: none;
|
|
border: none;
|
|
padding: var(--space-1) 0;
|
|
color: var(--rose);
|
|
text-decoration: underline;
|
|
cursor: pointer;
|
|
}
|
|
|
|
.quiet:focus-visible {
|
|
outline: 2px solid var(--rose);
|
|
outline-offset: 2px;
|
|
}
|
|
</style>
|