aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/lib/schedule-months.ts
blob: cd109b702b5dec8f60597b48a3875d233ae1c180 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// 行事予定データを月ごとに集計する(ビルド時専用)。
//
// 注意: このモジュールは JSON を import するため、ブラウザ用スクリプトからは読み込まないこと。
// (カレンダー側が必要とする日付計算は src/lib/schedule-dates.ts にある)
import meetings from '@/data/schedule/meetings.json';
import events from '@/data/schedule/events.json';
import { entryDates } from './schedule-dates';

export type Row = { date: string; title: string; place: string; note: string; source: string };
export type Item = { type: 'meeting' | 'event'; row: Row };

export const items: Item[] = [
  ...(meetings.rows as Row[]).map((row) => ({ type: 'meeting' as const, row })),
  ...(events.rows as Row[]).map((row) => ({ type: 'event' as const, row })),
];

const datesOf = (item: Item) => entryDates(item.type, item.row.date, item.row.place);

/** 月ごとの件数(会議・イベントの合計) */
export const monthCounts: Record<string, number> = (() => {
  const counts: Record<string, number> = {};
  for (const item of items) {
    for (const date of new Set(datesOf(item).map((d) => d.slice(0, 7)))) {
      counts[date] = (counts[date] ?? 0) + 1;
    }
  }
  return counts;
})();

/** データに存在する月(YYYY-MM、昇順) */
export const monthKeys: string[] = Object.keys(monthCounts).sort();

/** 指定した月に開催される行だけを返す */
export function rowsInMonth(month: string, type: 'meeting' | 'event'): Row[] {
  return items
    .filter((item) => item.type === type && datesOf(item).some((d) => d.startsWith(month)))
    .map((item) => item.row);
}

/** 年月の表示(2026-11 → 2026年11月) */
export function monthLabel(key: string): string {
  const [y, m] = key.split('-').map(Number);
  return `${y}年${m}月`;
}