[ui] SunsetPlace: Ask where the sunset is, and say when tonight's sunset is
This commit is contained in:
@@ -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