"use client"; import { useTheme } from "next-themes"; import { type CSSProperties, useEffect, useId, useState } from "react"; import { FOOTHILLS, RAINIER, RAINIER_SNOW, SEATTLE_HILLS, SKYLINE_SIZE, } from "./rainier-skyline"; import { useT } from "@/components/locale-provider"; import { switchTheme } from "@/lib/theme-transition"; import { cn } from "@/lib/utils"; type Phase = "night" | "dawn" | "day" | "dusk"; const TZ = "America/Los_Angeles"; const SYNODIC_DAYS = 29.530588853; /** A known new moon, 2000-01-06 18:14 UTC. */ const NEW_MOON = Date.UTC(2000, 0, 6, 18, 14); const MOON_NAMES = [ "New moon", "Waxing crescent", "First quarter", "Waxing gibbous", "Full moon", "Waning gibbous", "Last quarter", "Waning crescent", ]; /** Sky, horizon glow, and landscape colors for each part of the day. */ const PALETTE: Record< Phase, { sky: string; glow: string; far: string; snow: string; snowShade: string; mid: string; near: string; } > = { night: { sky: "linear-gradient(180deg,#050b1f 0%,#0b1736 46%,#172a52 80%,#22365e 100%)", glow: "rgb(255 176 128 / 0.16)", far: "#2b3b60", snow: "#d5dff1", snowShade: "#8fa2c6", mid: "#17223e", near: "#0a1022", }, dawn: { sky: "linear-gradient(180deg,#27356b 0%,#6a5a92 38%,#d48aa0 70%,#ffc39b 100%)", glow: "rgb(255 214 170 / 0.5)", far: "#6f5f88", snow: "#ffdcd2", snowShade: "#df9fa5", mid: "#3d3259", near: "#1f1931", }, day: { sky: "linear-gradient(180deg,#2f80d6 0%,#5ea5ec 45%,#9fcdf6 82%,#d6ecfd 100%)", glow: "rgb(255 255 255 / 0.35)", far: "#86a2c6", snow: "#ffffff", snowShade: "#cfdbec", mid: "#5d7ca3", near: "#3a5476", }, dusk: { sky: "linear-gradient(180deg,#1b2457 0%,#5b3f80 40%,#c45f80 72%,#ff9b63 100%)", glow: "rgb(255 170 110 / 0.45)", far: "#5e4b75", snow: "#ffc6b4", snowShade: "#d68b93", mid: "#352a52", near: "#1a1630", }, }; /** The last hour or so of daylight, when the light turns warm. */ const GOLDEN: (typeof PALETTE)["day"] = { sky: "linear-gradient(180deg,#3a78c6 0%,#78a7dc 42%,#e6c09a 84%,#ffd2a0 100%)", glow: "rgb(255 200 140 / 0.5)", far: "#8b92b4", snow: "#fff1de", snowShade: "#e0c2b2", mid: "#5f6e98", near: "#3b496f", }; // Integer LCG so the star field is identical on the server and in every browser. let seed = 11; const rand = () => ((seed = (seed * 16807) % 2147483647) - 1) / 2147483646; const STARS = Array.from({ length: 30 }, () => { const size = rand(); return { x: (rand() * 100).toFixed(1), y: (rand() * 66).toFixed(1), size: size < 0.12 ? 2 : size < 0.45 ? 1.5 : 1, opacity: (0.3 + rand() * 0.6).toFixed(2), twinkle: rand() < 0.3 ? (2.4 + rand() * 3).toFixed(1) : null, }; }); const GRAIN = "url(\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='2' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='.25'/%3E%3C/svg%3E\")"; const clock = (h: number, hour12: boolean) => { const minutes = Math.round(h * 60) % (24 * 60); const hh = Math.floor(minutes / 60); const mm = String(minutes % 60).padStart(2, "0"); return hour12 ? `${((hh + 11) % 12) + 1}:${mm} ${hh >= 12 ? "PM" : "AM"}` : `${hh}:${mm}`; }; const LAT = 47.6062; const LON = -122.3321; const rad = Math.PI / 180; /** Sunrise, solar noon, and sunset in local hours (NOAA's solar equations, accurate to about a minute). */ function sunTimes(doy: number, utcOffset: number) { const g = ((2 * Math.PI) / 365) * (doy - 1); const eqTime = 229.18 * (0.000075 + 0.001868 * Math.cos(g) - 0.032077 * Math.sin(g) - 0.014615 * Math.cos(2 * g) - 0.040849 * Math.sin(2 * g)); const decl = 0.006918 - 0.399912 * Math.cos(g) + 0.070257 * Math.sin(g) - 0.006758 * Math.cos(2 * g) + 0.000907 * Math.sin(2 * g) - 0.002697 * Math.cos(3 * g) + 0.00148 * Math.sin(3 * g); const ha = Math.acos( Math.cos(90.833 * rad) / (Math.cos(LAT * rad) * Math.cos(decl)) - Math.tan(LAT * rad) * Math.tan(decl), ) / rad; const local = (minutes: number) => minutes / 60 + utcOffset; return { rise: local(720 - 4 * (LON + ha) - eqTime), noon: local(720 - 4 * LON - eqTime), set: local(720 - 4 * (LON - ha) - eqTime), }; } /** Seattle's sky right now: the sun's real position and the moon's phase and rough place. */ function seattleSky(now: Date, hour12: boolean) { const parts = Object.fromEntries( new Intl.DateTimeFormat("en-US", { timeZone: TZ, hour: "numeric", minute: "2-digit", hourCycle: "h23", timeZoneName: "short", month: "numeric", day: "numeric", year: "numeric", }) .formatToParts(now) .map((p) => [p.type, p.value]), ); const hour = Number(parts.hour) + Number(parts.minute) / 60; const year = Number(parts.year); const doy = (Date.UTC(year, Number(parts.month) - 1, Number(parts.day)) - Date.UTC(year, 0, 0)) / 864e5; const { rise, noon, set } = sunTimes( doy, parts.timeZoneName === "PDT" ? -7 : -8, ); let phase: Phase = "night"; if (hour >= rise - 0.6 && hour < rise + 0.7) phase = "dawn"; else if (hour >= rise + 0.7 && hour < set - 0.8) phase = "day"; else if (hour >= set - 0.8 && hour < set + 0.6) phase = "dusk"; // The moon transits about age × 24.8 h after the sun and is up for roughly 12.4 h. const age = ((((now.getTime() - NEW_MOON) / 864e5 / SYNODIC_DAYS) % 1) + 1) % 1; const moon = ((((hour - (noon + age * 24.8 - 6.2)) % 24) + 24) % 24) / 12.4; let label = MOON_NAMES[Math.round(age * 8) % 8]; if (phase === "dawn") label = hour < rise ? "First light" : "Sunrise"; else if (phase === "dusk") label = hour < set ? "Sunset" : "Blue hour"; else if (phase === "day") label = hour > set - 1.8 ? "Golden hour" : "Daylight"; const time = new Intl.DateTimeFormat("en-US", { timeZone: TZ, hour: "numeric", minute: "2-digit", ...(hour12 ? {} : { hourCycle: "h23" as const }), }).format(now); const sunUp = hour >= rise && hour < set; return { phase, label, next: sunUp ? { kind: "sunset" as const, at: clock(set, hour12) } : { kind: "sunrise" as const, at: clock(rise, hour12) }, time: time.replace(/\s?[AP]M$/, ""), meridiem: hour12 ? (/PM$/.test(time) ? "PM" : "AM") : "", sun: (hour - rise) / (set - rise), moon, age, }; } /** Position along the sky's arc, from the left horizon (0) to the right (1). */ const arc = (p: number): CSSProperties => ({ left: `${6 + p * 88}%`, top: `${78 - Math.sin(Math.PI * Math.min(1, Math.max(0, p))) * 30}%`, }); /** The moon's lit shape for its age (0 new, 0.5 full), as seen from the northern hemisphere. */ function Moon({ age, className, craters, }: { age: number; className?: string; craters?: boolean; }) { const clip = useId().replace(/[^\w-]/g, ""); const k = Math.cos(2 * Math.PI * age); const waxing = age < 0.5; const lit = `M0 -10A10 10 0 0 ${waxing ? 1 : 0} 0 10A${(Math.abs(k) * 10).toFixed(2)} 10 0 0 ${ waxing === k > 0 ? 0 : 1 } 0 -10Z`; return ( {craters && ( <> )} ); } const DIGITS = "font-display text-[46px] font-semibold tracking-[-0.025em] tabular-nums sm:text-[58px]"; /** * Clock digits made of glass, like the iOS 26 Lock Screen: a clear body that * lets the sky through, a bevel lit from the upper left, and a bright top rim. */ function GlassTime({ time, meridiem }: { time: string; meridiem: string }) { const id = useId().replace(/[^\w-]/g, ""); return ( {time} {/* An invisible copy of the digits sets the pen position for AM/PM. */} {time} {meridiem} ); } function PhaseIcon({ phase, age }: { phase: Phase; age: number }) { if (phase === "night") return ; if (phase === "day") { return ( ); } return ( ); } /** * An Apple Weather-style widget for Seattle's real sky right now: the sun or * the moon in its actual phase over Mount Rainier's true skyline from Kerry * Park. It doubles as the light switch for the site. */ export function SkyCard() { const { sky: t, nav } = useT(); const { resolvedTheme, setTheme } = useTheme(); const [sky, setSky] = useState | null>(null); useEffect(() => { const tick = () => setSky(seattleSky(new Date(), t.hour12)); tick(); const id = window.setInterval(tick, 20_000); return () => window.clearInterval(id); }, [t.hour12]); const phase = sky?.phase ?? "night"; const colors = sky?.label === "Golden hour" ? GOLDEN : PALETTE[phase]; const [width, height] = SKYLINE_SIZE; const sunVisible = sky !== null && sky.sun >= -0.02 && sky.sun <= 1.02; const moonVisible = sky !== null && sky.moon <= 1 && sky.age > 0.03 && sky.age < 0.97; return (
{STARS.map((s, i) => ( ))}
{sky && moonVisible && ( )} {sky && sunVisible && ( )}

{t.city}

{sky && } {sky ? (t.labels[sky.label] ?? sky.label) : t.city}

{sky ? `${t[sky.next.kind]} ${sky.next.at}` : t.sunrise}

); }