diff options
| author | Yasutake Yohei <61961825+yasutakeyohei@users.noreply.github.com> | 2026-11-17 23:37:02 +0900 |
|---|---|---|
| committer | Yasutake Yohei <61961825+yasutakeyohei@users.noreply.github.com> | 2026-11-17 23:37:02 +0900 |
| commit | 82bc85b2bb95172c20f6aea5c6d2b0f9fc9a19d0 (patch) | |
| tree | 993da03c9e1cc24fff34be000295e6355366f9a7 /src/pages/kodaira | |
| parent | e6eaa40e0fac60fbd1b03e6b7e5241e8bc01ac49 (diff) | |
today.astro: 今日と明日の予定を表示するアプリ画面を追加
ビルド時に日付を計算した data.json を端末側で絞り込むため、日付が変わっても再ビルドなしで今日の予定が出る。通信できないときは Service Worker のキャッシュを使う。
Diffstat (limited to 'src/pages/kodaira')
| -rw-r--r-- | src/pages/kodaira/events/data.json.ts | 57 | ||||
| -rw-r--r-- | src/pages/kodaira/events/today.astro | 186 |
2 files changed, 243 insertions, 0 deletions
diff --git a/src/pages/kodaira/events/data.json.ts b/src/pages/kodaira/events/data.json.ts new file mode 100644 index 0000000..67bda61 --- /dev/null +++ b/src/pages/kodaira/events/data.json.ts @@ -0,0 +1,57 @@ +// 「今日の予定」アプリ用のデータ(/kodaira/events/data.json)。 +// 日付の解釈はビルド時に済ませ、端末側は今日・明日の日付で絞り込むだけにする。 +import type { APIRoute } from 'astro'; +import events from '@/data/schedule/events.json'; +import meetings from '@/data/schedule/meetings.json'; +import { entryDates } from '@/lib/schedule-dates'; + +type Row = { date: string; title: string; place: string; note: string }; + +const strip = (value: string) => + String(value ?? '') + .replace(/<[^>]*>/g, '') + .replace(/ /g, ' ') + .replace(/&/g, '&') + .replace(/</g, '<') + .replace(/>/g, '>') + .replace(/"/g, '"') + .replace(/'/g, "'") + .replace(/\s+/g, ' ') + .trim(); + +const pad = (n: number) => String(n).padStart(2, '0'); +const toKey = (d: Date) => `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`; + +const now = new Date(); +const from = new Date(now); +from.setDate(from.getDate() - 31); +const to = new Date(now); +to.setDate(to.getDate() + 400); +const fromKey = toKey(from); +const toKeyStr = toKey(to); + +const collect = (rows: Row[], type: 'event' | 'meeting') => + rows + .map((row) => { + const dates = entryDates(type, row.date, row.place).filter( + (d) => d >= fromKey && d <= toKeyStr, + ); + if (dates.length === 0) return null; + return { + t: strip(row.title), + p: strip(row.place), + n: strip(row.note).slice(0, 140), + dates, + }; + }) + .filter(Boolean); + +const data = { + generated: now.toISOString(), + rows: [...collect(events.rows as Row[], 'event'), ...collect(meetings.rows as Row[], 'meeting')], +}; + +export const GET: APIRoute = () => + new Response(JSON.stringify(data), { + headers: { 'Content-Type': 'application/json; charset=utf-8' }, + }); diff --git a/src/pages/kodaira/events/today.astro b/src/pages/kodaira/events/today.astro new file mode 100644 index 0000000..47172ce --- /dev/null +++ b/src/pages/kodaira/events/today.astro @@ -0,0 +1,186 @@ +--- +// 「今日の予定」アプリの画面(/kodaira/events/today/)。 +// データは /kodaira/events/data.json(ビルド時に日付を計算済み)から読み、 +// 端末側で今日・明日の分だけを表示する。日付が変わっても再ビルドは不要。 +const title = '今日の小平市'; +--- + +<!doctype html> +<html lang="ja"> + <head> + <meta charset="utf-8" /> + <meta name="viewport" content="width=device-width, initial-scale=1" /> + <title>{title}|小平市のイベント・行事予定</title> + <meta name="robots" content="noindex" /> + <link rel="manifest" href="/manifest-events.webmanifest" /> + <meta name="theme-color" content="#1565c0" /> + <link rel="apple-touch-icon" href="/icons/event-192.png" /> + <meta name="apple-mobile-web-app-capable" content="yes" /> + <meta name="apple-mobile-web-app-title" content="今日の小平市" /> + <style is:global> + :root { + color-scheme: light dark; + --bg: #f6f7f9; + --card: #fff; + --text: #1a1a1a; + --muted: #5b6470; + --line: #e3e6ea; + --accent: #1565c0; + } + @media (prefers-color-scheme: dark) { + :root { + --bg: #12151a; + --card: #1b1f26; + --text: #e9edf2; + --muted: #9aa4b1; + --line: #2a3038; + --accent: #6aa9ff; + } + } + * { box-sizing: border-box; } + body { + margin: 0; + background: var(--bg); + color: var(--text); + font-family: system-ui, -apple-system, 'Hiragino Sans', 'Noto Sans JP', sans-serif; + line-height: 1.5; + -webkit-text-size-adjust: 100%; + } + header { + position: sticky; + top: 0; + z-index: 2; + padding: calc(0.8rem + env(safe-area-inset-top)) 1rem 0.8rem; + background: var(--accent); + color: #fff; + } + header h1 { margin: 0; font-size: 1.15rem; } + header p { margin: 0.2rem 0 0; font-size: 0.8rem; opacity: 0.9; } + main { padding: 0.9rem 0.8rem 1.5rem; max-width: 640px; margin: 0 auto; } + section { margin-bottom: 1.2rem; } + h2 { + margin: 0 0 0.5rem; + font-size: 0.95rem; + color: var(--muted); + font-weight: 600; + } + ul { list-style: none; margin: 0; padding: 0; } + li { + display: flex; + gap: 0.6rem; + padding: 0.7rem 0.8rem; + margin-bottom: 0.5rem; + background: var(--card); + border: 1px solid var(--line); + border-radius: 10px; + } + .time { + flex: 0 0 3.4rem; + font-variant-numeric: tabular-nums; + font-weight: 700; + color: var(--accent); + } + .body { flex: 1 1 auto; min-width: 0; } + .name { font-weight: 600; overflow-wrap: anywhere; } + .place { color: var(--muted); font-size: 0.82rem; overflow-wrap: anywhere; } + .empty { color: var(--muted); font-size: 0.9rem; padding: 0.4rem 0.2rem; } + footer { + max-width: 640px; + margin: 0 auto; + padding: 0 1rem calc(1.4rem + env(safe-area-inset-bottom)); + color: var(--muted); + font-size: 0.78rem; + } + footer a { color: var(--accent); } + .note { margin: 0.4rem 0; } + </style> + </head> + <body> + <header> + <h1>{title}</h1> + <p id="range">読み込み中…</p> + </header> + + <main id="app"></main> + + <footer> + <p class="note"> + ※すべてのイベントを掲載できていない場合や、内容に誤りがある場合があります。最新の情報は、小平市や各主催者の公式発表をご確認ください。 + </p> + <p class="note"><a href="/kodaira/events/">1週間分をすべて見る</a></p> + <p class="note" id="updated"></p> + </footer> + + <script> + const WEEK = ['日', '月', '火', '水', '木', '金', '土']; + const pad = (n) => String(n).padStart(2, '0'); + const keyOf = (d) => `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`; + const labelOf = (d) => `${d.getMonth() + 1}月${d.getDate()}日(${WEEK[d.getDay()]})`; + + const startTime = (place) => { + const m = String(place).match(/(午前|午後)?\s*(\d{1,2})時(?:(\d{1,2})分|半)?/); + if (!m) return ''; + let hour = Number(m[2]); + const minute = m[3] ? Number(m[3]) : m[0].includes('半') ? 30 : 0; + if (m[1] === '午後' && hour < 12) hour += 12; + if (m[1] === '午前' && hour === 12) hour = 0; + if (!m[1] && hour < 7) hour += 12; + return `${pad(hour)}:${pad(minute)}`; + }; + + const venueOf = (place) => { + const parts = String(place).split('/'); + return (parts.length > 1 ? parts[parts.length - 1] : '') + .replace(/([^)]*)/g, '') + .replace(/^注)/, '') + .trim(); + }; + + const esc = (s) => + String(s).replace(/[&<>"]/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"' })[c]); + + const render = (rows, key) => { + const list = rows + .filter((row) => row.dates.includes(key)) + .map((row) => ({ ...row, time: startTime(row.p), venue: venueOf(row.p) })) + .sort((a, b) => (a.time || '99:99').localeCompare(b.time || '99:99')); + if (list.length === 0) return '<p class="empty">予定はありません。</p>'; + return `<ul>${list + .map( + (row) => + `<li><div class="time">${esc(row.time)}</div><div class="body"><div class="name">${esc( + row.t, + )}</div>${ + row.venue ? `<div class="place">${esc(row.venue)}</div>` : '' + }</div></li>`, + ) + .join('')}</ul>`; + }; + + const today = new Date(); + const tomorrow = new Date(today); + tomorrow.setDate(tomorrow.getDate() + 1); + + document.getElementById('range').textContent = `${labelOf(today)}〜${labelOf(tomorrow)}`; + + fetch('/kodaira/events/data.json') + .then((res) => (res.ok ? res.json() : Promise.reject(new Error(String(res.status))))) + .then((data) => { + const rows = Array.isArray(data.rows) ? data.rows : []; + document.getElementById('app').innerHTML = ` + <section><h2>今日 ${labelOf(today)}</h2>${render(rows, keyOf(today))}</section> + <section><h2>明日 ${labelOf(tomorrow)}</h2>${render(rows, keyOf(tomorrow))}</section>`; + const generated = data.generated ? new Date(data.generated) : null; + if (generated && !Number.isNaN(generated.getTime())) { + document.getElementById('updated').textContent = + `データ更新:${generated.getFullYear()}年${generated.getMonth() + 1}月${generated.getDate()}日 ` + + `${pad(generated.getHours())}:${pad(generated.getMinutes())}`; + } + }) + .catch(() => { + document.getElementById('app').innerHTML = + '<p class="empty">予定を読み込めませんでした。通信状態を確認して、もう一度開いてください。</p>'; + }); + </script> + </body> +</html> |
