[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>
|
||||
|
||||
@@ -10,6 +10,27 @@ export const en = {
|
||||
/** The one line on the Phase 0 page. True, calm, and asks nothing. */
|
||||
preparing: 'The garden is being prepared.'
|
||||
},
|
||||
sunset: {
|
||||
/** ROADMAP Phase 1, word for word. */
|
||||
question: 'Where do you want your sunset?',
|
||||
why: 'The garden follows the real sun. Choose a town near you. It is kept on this device only.',
|
||||
searchLabel: 'Town or city',
|
||||
/** Said aloud as the list changes. Never "not found": a small town is not a mistake. */
|
||||
found: (n: number): string => (n === 0 ? 'No town by that name here. A larger one nearby will do.' : n === 1 ? 'One town' : `${n} towns`),
|
||||
skip: (guess: string): string => `Not now. Use ${guess}`,
|
||||
skipNoGuess: 'Not now',
|
||||
keep: (town: string): string => `Keep ${town}`,
|
||||
change: 'Change town',
|
||||
choose: 'Choose a town',
|
||||
tonight: (time: string, town: string): string => `Tonight the sun sets at ${time} in ${town}.`,
|
||||
tomorrow: (time: string, town: string): string => `Tomorrow the sun sets at ${time} in ${town}.`,
|
||||
polarDay: (time: string, town: string): string => `In ${town} the sun stays up tonight. The light is lowest at ${time}.`,
|
||||
polarNight: (time: string, town: string): string => `In ${town} the sun stays below the horizon today. The twilight is brightest at ${time}.`,
|
||||
guessed: "A guess from this device's time zone.",
|
||||
noTown: 'Choose a town to see when the sun sets.',
|
||||
/** When the device refuses to keep anything (a private window). Calm, and true. */
|
||||
notKept: 'This device could not keep your choice, so the garden will ask again next time.'
|
||||
},
|
||||
letter: {
|
||||
/** The help section's name (ROADMAP §1). The letter itself is the author's, in docs/LETTER.md. */
|
||||
title: 'A Letter from the Gardener',
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
export { default as Shell } from './Shell.svelte';
|
||||
export { en, type Copy } from './copy/en.js';
|
||||
export { parseLetter, type Inline, type LetterBlock } from './letter/parse.js';
|
||||
export type { SunsetChoice, SunsetWiring } from './sunset/types.js';
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
<script lang="ts">
|
||||
import { localDay, nextInvitation, type NamedPlace } from '@rosea/engine';
|
||||
import { en } from '../copy/en.js';
|
||||
|
||||
/**
|
||||
* The next sunset, or its polar alternative, in one sentence. The engine says
|
||||
* when; this only reads it on the town's clock, in the device's own time format
|
||||
* (sunset-place.md P6).
|
||||
*/
|
||||
let { place, guessed = false, now }: { place: NamedPlace; guessed?: boolean; now: number } = $props();
|
||||
|
||||
const line = $derived.by(() => {
|
||||
const { sun, at } = nextInvitation(now, place);
|
||||
const time = new Intl.DateTimeFormat(undefined, { hour: 'numeric', minute: '2-digit', timeZone: place.timeZone }).format(at);
|
||||
switch (sun.kind) {
|
||||
case 'polar-day':
|
||||
return en.sunset.polarDay(time, place.name);
|
||||
case 'polar-night':
|
||||
return en.sunset.polarNight(time, place.name);
|
||||
case 'sun-sets':
|
||||
// Tonight's sunset may fall after midnight (sun.md D2), so "tomorrow" only
|
||||
// when it belongs to a later day than today.
|
||||
return sun.day > localDay(now, place.timeZone) ? en.sunset.tomorrow(time, place.name) : en.sunset.tonight(time, place.name);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<p class="sunset">{line}</p>
|
||||
{#if guessed}
|
||||
<p class="hint">{en.sunset.guessed}</p>
|
||||
{/if}
|
||||
|
||||
<style>
|
||||
p {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.sunset {
|
||||
font-size: 1.1rem;
|
||||
}
|
||||
|
||||
.hint {
|
||||
color: var(--ink-soft);
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,189 @@
|
||||
<script lang="ts">
|
||||
import type { NamedPlace } from '@rosea/engine';
|
||||
import { en } from '../copy/en.js';
|
||||
import type { SunsetChoice } from './types.js';
|
||||
|
||||
/**
|
||||
* "Where do you want your sunset?" A search over towns on the device, and a way
|
||||
* to say not now. Decides nothing: it reports the answer (sunset-place.md).
|
||||
*/
|
||||
let {
|
||||
guess,
|
||||
current = null,
|
||||
search,
|
||||
focusOnOpen = false,
|
||||
onchoose,
|
||||
oncancel
|
||||
}: {
|
||||
guess: NamedPlace | null;
|
||||
/** The town already set, when the player is changing it. */
|
||||
current?: NamedPlace | null;
|
||||
search: (query: string) => Promise<readonly NamedPlace[]>;
|
||||
focusOnOpen?: boolean;
|
||||
onchoose: (choice: SunsetChoice) => void;
|
||||
oncancel?: () => void;
|
||||
} = $props();
|
||||
|
||||
const uid = $props.id();
|
||||
let query = $state('');
|
||||
// Raw: the towns are handed on as they came, never wrapped in a state Proxy.
|
||||
let results = $state.raw<readonly NamedPlace[]>([]);
|
||||
let input = $state<HTMLInputElement>();
|
||||
|
||||
// Only the latest query's answer is shown, however the searches finish.
|
||||
let asked = 0;
|
||||
$effect(() => {
|
||||
const q = query;
|
||||
const ticket = ++asked;
|
||||
if (q.trim() === '') {
|
||||
results = [];
|
||||
return;
|
||||
}
|
||||
search(q).then(
|
||||
(found) => {
|
||||
if (ticket === asked) results = found;
|
||||
},
|
||||
(error: unknown) => {
|
||||
console.error('sunset: the town search failed', error);
|
||||
if (ticket === asked) results = [];
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
$effect(() => {
|
||||
if (focusOnOpen) input?.focus();
|
||||
});
|
||||
|
||||
const where = (t: NamedPlace): string => [t.region, t.country].filter(Boolean).join(', ');
|
||||
</script>
|
||||
|
||||
<section class="ask" aria-labelledby="{uid}-q">
|
||||
<h2 id="{uid}-q">{en.sunset.question}</h2>
|
||||
<p class="why">{en.sunset.why}</p>
|
||||
|
||||
<label for="{uid}-search">{en.sunset.searchLabel}</label>
|
||||
<input
|
||||
id="{uid}-search"
|
||||
type="search"
|
||||
autocomplete="off"
|
||||
spellcheck="false"
|
||||
enterkeyhint="search"
|
||||
bind:value={query}
|
||||
bind:this={input}
|
||||
/>
|
||||
<p class="found" role="status" aria-live="polite">{query.trim() === '' ? '' : en.sunset.found(results.length)}</p>
|
||||
|
||||
{#if results.length > 0}
|
||||
<ul>
|
||||
{#each results as town (`${town.name}|${town.lat}|${town.lon}`)}
|
||||
<li>
|
||||
<button type="button" onclick={() => onchoose({ kind: 'town', town })}>
|
||||
<span class="name">{town.name}</span>
|
||||
<span class="where">{where(town)}</span>
|
||||
</button>
|
||||
</li>
|
||||
{/each}
|
||||
</ul>
|
||||
{/if}
|
||||
|
||||
<div class="other">
|
||||
{#if current && oncancel}
|
||||
<button type="button" class="quiet" onclick={oncancel}>{en.sunset.keep(current.name)}</button>
|
||||
{/if}
|
||||
<button type="button" class="quiet" onclick={() => onchoose({ kind: 'device-zone' })}>
|
||||
{guess ? en.sunset.skip(guess.name) : en.sunset.skipNoGuess}
|
||||
</button>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<style>
|
||||
.ask {
|
||||
display: grid;
|
||||
gap: var(--space-2);
|
||||
width: min(28rem, 100%);
|
||||
margin-inline: auto;
|
||||
text-align: start;
|
||||
}
|
||||
|
||||
h2 {
|
||||
margin: 0;
|
||||
font-family: var(--font-display);
|
||||
font-weight: 400;
|
||||
font-size: 1.4rem;
|
||||
color: var(--ink);
|
||||
}
|
||||
|
||||
.why,
|
||||
.found {
|
||||
margin: 0;
|
||||
color: var(--ink-soft);
|
||||
}
|
||||
|
||||
.found:empty {
|
||||
display: none;
|
||||
}
|
||||
|
||||
label {
|
||||
font-size: 0.95rem;
|
||||
}
|
||||
|
||||
input {
|
||||
font: inherit;
|
||||
padding: var(--space-2);
|
||||
border: 1px solid var(--ink-soft);
|
||||
border-radius: 0.5rem;
|
||||
background: var(--paper);
|
||||
color: var(--ink);
|
||||
}
|
||||
|
||||
ul {
|
||||
list-style: none;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
display: grid;
|
||||
gap: var(--space-1);
|
||||
}
|
||||
|
||||
button {
|
||||
font: inherit;
|
||||
cursor: pointer;
|
||||
border-radius: 0.5rem;
|
||||
color: var(--ink);
|
||||
}
|
||||
|
||||
li button {
|
||||
width: 100%;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
gap: var(--space-2);
|
||||
padding: var(--space-2);
|
||||
text-align: start;
|
||||
background: var(--paper);
|
||||
border: 1px solid color-mix(in srgb, var(--ink-soft) 40%, transparent);
|
||||
}
|
||||
|
||||
.where {
|
||||
color: var(--ink-soft);
|
||||
text-align: end;
|
||||
}
|
||||
|
||||
.other {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: var(--space-2);
|
||||
}
|
||||
|
||||
.quiet {
|
||||
background: none;
|
||||
border: none;
|
||||
padding: var(--space-1) 0;
|
||||
color: var(--rose);
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
button:focus-visible,
|
||||
input:focus-visible {
|
||||
outline: 2px solid var(--rose);
|
||||
outline-offset: 2px;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,19 @@
|
||||
import type { NamedPlace } from '@rosea/engine';
|
||||
|
||||
/** What the player answered: a town, or "not now" (the guess from the device's zone). */
|
||||
export type SunsetChoice = { readonly kind: 'town'; readonly town: NamedPlace } | { readonly kind: 'device-zone' };
|
||||
|
||||
/**
|
||||
* Everything the garden needs to ask where the sunset is and to show it. The app
|
||||
* provides it; ui imports neither content nor storage (CLAUDE.md §2).
|
||||
*/
|
||||
export interface SunsetWiring {
|
||||
/** The device's guess for its own zone, or null for a zone of no place (UTC). */
|
||||
readonly guess: NamedPlace | null;
|
||||
/** Towns matching a query. The app loads the town list on the first call. */
|
||||
readonly search: (query: string) => Promise<readonly NamedPlace[]>;
|
||||
readonly load: () => Promise<SunsetChoice | null>;
|
||||
readonly save: (choice: SunsetChoice) => Promise<void>;
|
||||
/** The clock. A parameter, so tests and the e2e suite can fix it. */
|
||||
readonly now: () => number;
|
||||
}
|
||||
Reference in New Issue
Block a user