[ui] SunsetPlace: Ask where the sunset is, and say when tonight's sunset is
This commit is contained in:
@@ -1,11 +1,71 @@
|
||||
<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';
|
||||
|
||||
/** The author's Markdown from docs/LETTER.md. Empty means there is no way to the page. */
|
||||
let { letter = '' }: { letter?: string } = $props();
|
||||
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.
|
||||
@@ -45,6 +105,28 @@
|
||||
<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}
|
||||
@@ -87,4 +169,34 @@
|
||||
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>
|
||||
|
||||
Reference in New Issue
Block a user