|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { |
| 4 | + IconCalendarOff, |
| 5 | + IconCalendarStats, |
| 6 | + IconChartBar, |
| 7 | + IconClockHour12, |
| 8 | + IconSunHigh, |
| 9 | + IconSunset2, |
| 10 | +} from '@tabler/icons-react'; |
| 11 | +import type { ParticipationSummaryResponse } from '@tecnova/shared/schemas'; |
| 12 | +import { Alert, AlertDescription, AlertTitle } from '@tecnova/ui/components/alert'; |
| 13 | +import { Button } from '@tecnova/ui/components/button'; |
| 14 | +import { Card, CardContent, CardHeader, CardTitle } from '@tecnova/ui/components/card'; |
| 15 | +import { Input } from '@tecnova/ui/components/input'; |
| 16 | +import { Skeleton } from '@tecnova/ui/components/skeleton'; |
| 17 | +import { |
| 18 | + Table, |
| 19 | + TableBody, |
| 20 | + TableCell, |
| 21 | + TableHead, |
| 22 | + TableHeader, |
| 23 | + TableRow, |
| 24 | +} from '@tecnova/ui/components/table'; |
| 25 | +import { TableSkeleton } from '@tecnova/ui/components/table-skeleton'; |
| 26 | +import { apiErrorMessage, apiJson } from '@tecnova/ui/lib/api-client'; |
| 27 | +import { formatJstDate } from '@tecnova/ui/lib/format'; |
| 28 | +import { cn } from '@tecnova/ui/lib/utils'; |
| 29 | +import { useCallback, useEffect, useState } from 'react'; |
| 30 | +import { PageHeader } from '@/components/page-header'; |
| 31 | + |
| 32 | +type SummaryState = |
| 33 | + | { kind: 'loading' } |
| 34 | + | { kind: 'ok'; data: ParticipationSummaryResponse } |
| 35 | + | { kind: 'error'; message: string }; |
| 36 | + |
| 37 | +export default function StatsPage() { |
| 38 | + const [summary, setSummary] = useState<SummaryState>({ kind: 'loading' }); |
| 39 | + // 入力中の値(適用ボタンを押すまで反映しない)。空文字 = フィルタなし。 |
| 40 | + const [fromInput, setFromInput] = useState(''); |
| 41 | + const [toInput, setToInput] = useState(''); |
| 42 | + // 実際に API へ送る確定済みレンジ。 |
| 43 | + const [appliedFrom, setAppliedFrom] = useState(''); |
| 44 | + const [appliedTo, setAppliedTo] = useState(''); |
| 45 | + |
| 46 | + const loadSummary = useCallback(async (from: string, to: string) => { |
| 47 | + setSummary({ kind: 'loading' }); |
| 48 | + try { |
| 49 | + const params = new URLSearchParams(); |
| 50 | + if (from) params.set('from', from); |
| 51 | + if (to) params.set('to', to); |
| 52 | + const query = params.toString(); |
| 53 | + const path = query ? `/api/stats/participation?${query}` : '/api/stats/participation'; |
| 54 | + const data = await apiJson<ParticipationSummaryResponse>(path); |
| 55 | + setSummary({ kind: 'ok', data }); |
| 56 | + } catch (e) { |
| 57 | + setSummary({ kind: 'error', message: apiErrorMessage(e) }); |
| 58 | + } |
| 59 | + }, []); |
| 60 | + |
| 61 | + useEffect(() => { |
| 62 | + void loadSummary(appliedFrom, appliedTo); |
| 63 | + }, [appliedFrom, appliedTo, loadSummary]); |
| 64 | + |
| 65 | + const applyFilter = () => { |
| 66 | + setAppliedFrom(fromInput); |
| 67 | + setAppliedTo(toInput); |
| 68 | + }; |
| 69 | + |
| 70 | + const clearFilter = () => { |
| 71 | + setFromInput(''); |
| 72 | + setToInput(''); |
| 73 | + setAppliedFrom(''); |
| 74 | + setAppliedTo(''); |
| 75 | + }; |
| 76 | + |
| 77 | + const hasFilter = appliedFrom !== '' || appliedTo !== ''; |
| 78 | + |
| 79 | + return ( |
| 80 | + <main className="flex flex-1 flex-col gap-6 p-4 md:p-8"> |
| 81 | + <PageHeader |
| 82 | + title="集計" |
| 83 | + description="ターム単位の参加回数を期間で集計します" |
| 84 | + actions={ |
| 85 | + <> |
| 86 | + <Input |
| 87 | + type="date" |
| 88 | + aria-label="集計開始日" |
| 89 | + value={fromInput} |
| 90 | + max={toInput || undefined} |
| 91 | + onChange={(e) => setFromInput(e.target.value)} |
| 92 | + className="w-40" |
| 93 | + /> |
| 94 | + <span className="text-sm text-muted-foreground">〜</span> |
| 95 | + <Input |
| 96 | + type="date" |
| 97 | + aria-label="集計終了日" |
| 98 | + value={toInput} |
| 99 | + min={fromInput || undefined} |
| 100 | + onChange={(e) => setToInput(e.target.value)} |
| 101 | + className="w-40" |
| 102 | + /> |
| 103 | + <Button |
| 104 | + type="button" |
| 105 | + size="sm" |
| 106 | + onClick={applyFilter} |
| 107 | + disabled={summary.kind === 'loading'} |
| 108 | + > |
| 109 | + 適用 |
| 110 | + </Button> |
| 111 | + {hasFilter && ( |
| 112 | + <Button type="button" variant="outline" size="sm" onClick={clearFilter}> |
| 113 | + 全期間 |
| 114 | + </Button> |
| 115 | + )} |
| 116 | + </> |
| 117 | + } |
| 118 | + /> |
| 119 | + |
| 120 | + <StatsBody summary={summary} /> |
| 121 | + </main> |
| 122 | + ); |
| 123 | +} |
| 124 | + |
| 125 | +function StatsBody({ summary }: { summary: SummaryState }) { |
| 126 | + if (summary.kind === 'loading') { |
| 127 | + return ( |
| 128 | + <> |
| 129 | + <section className="grid gap-4 md:grid-cols-3 lg:grid-cols-5"> |
| 130 | + <Skeleton className="h-24 w-full" /> |
| 131 | + <Skeleton className="h-24 w-full" /> |
| 132 | + <Skeleton className="h-24 w-full" /> |
| 133 | + <Skeleton className="h-24 w-full" /> |
| 134 | + <Skeleton className="h-24 w-full" /> |
| 135 | + </section> |
| 136 | + <TableSkeleton columns={5} rows={8} /> |
| 137 | + </> |
| 138 | + ); |
| 139 | + } |
| 140 | + |
| 141 | + if (summary.kind === 'error') { |
| 142 | + return ( |
| 143 | + <Alert variant="destructive"> |
| 144 | + <AlertTitle>集計を読み込めませんでした</AlertTitle> |
| 145 | + <AlertDescription>{summary.message}</AlertDescription> |
| 146 | + </Alert> |
| 147 | + ); |
| 148 | + } |
| 149 | + |
| 150 | + const { totals, byDate } = summary.data; |
| 151 | + |
| 152 | + return ( |
| 153 | + <> |
| 154 | + <section className="grid gap-4 md:grid-cols-3 lg:grid-cols-5"> |
| 155 | + <SummaryCard label="総参加回数" value={totals.total} Icon={IconChartBar} /> |
| 156 | + <SummaryCard |
| 157 | + label="朝" |
| 158 | + value={totals.morning} |
| 159 | + Icon={IconSunHigh} |
| 160 | + iconClassName="text-sky-600" |
| 161 | + /> |
| 162 | + <SummaryCard |
| 163 | + label="昼" |
| 164 | + value={totals.afternoon} |
| 165 | + Icon={IconClockHour12} |
| 166 | + iconClassName="text-amber-600" |
| 167 | + /> |
| 168 | + <SummaryCard |
| 169 | + label="夕方" |
| 170 | + value={totals.evening} |
| 171 | + Icon={IconSunset2} |
| 172 | + iconClassName="text-violet-600" |
| 173 | + /> |
| 174 | + <SummaryCard label="開催日数" value={totals.days} Icon={IconCalendarStats} /> |
| 175 | + </section> |
| 176 | + |
| 177 | + <Card className="p-0"> |
| 178 | + <Table> |
| 179 | + <TableHeader> |
| 180 | + <TableRow> |
| 181 | + <TableHead>開催日</TableHead> |
| 182 | + <TableHead className="text-right">朝</TableHead> |
| 183 | + <TableHead className="text-right">昼</TableHead> |
| 184 | + <TableHead className="text-right">夕方</TableHead> |
| 185 | + <TableHead className="text-right">計</TableHead> |
| 186 | + </TableRow> |
| 187 | + </TableHeader> |
| 188 | + <TableBody> |
| 189 | + {byDate.length === 0 ? ( |
| 190 | + <TableRow> |
| 191 | + <TableCell colSpan={5}> |
| 192 | + <div className="flex flex-col items-center gap-2 py-10 text-muted-foreground"> |
| 193 | + <IconCalendarOff className="size-8" /> |
| 194 | + <span className="text-sm">この期間の参加実績はありません</span> |
| 195 | + </div> |
| 196 | + </TableCell> |
| 197 | + </TableRow> |
| 198 | + ) : ( |
| 199 | + byDate.map((row) => ( |
| 200 | + <TableRow key={row.date}> |
| 201 | + <TableCell>{formatJstDate(row.date)}</TableCell> |
| 202 | + <TableCell className="text-right tabular-nums">{row.morning}</TableCell> |
| 203 | + <TableCell className="text-right tabular-nums">{row.afternoon}</TableCell> |
| 204 | + <TableCell className="text-right tabular-nums">{row.evening}</TableCell> |
| 205 | + <TableCell className="text-right font-medium tabular-nums">{row.total}</TableCell> |
| 206 | + </TableRow> |
| 207 | + )) |
| 208 | + )} |
| 209 | + </TableBody> |
| 210 | + </Table> |
| 211 | + </Card> |
| 212 | + </> |
| 213 | + ); |
| 214 | +} |
| 215 | + |
| 216 | +function SummaryCard({ |
| 217 | + label, |
| 218 | + value, |
| 219 | + Icon, |
| 220 | + iconClassName, |
| 221 | +}: { |
| 222 | + label: string; |
| 223 | + value: number; |
| 224 | + Icon: typeof IconChartBar; |
| 225 | + iconClassName?: string; |
| 226 | +}) { |
| 227 | + return ( |
| 228 | + <Card> |
| 229 | + <CardHeader className="flex flex-row items-center justify-between gap-2 space-y-0"> |
| 230 | + <CardTitle className="text-sm font-medium text-muted-foreground">{label}</CardTitle> |
| 231 | + <Icon className={cn('size-5 text-muted-foreground', iconClassName)} /> |
| 232 | + </CardHeader> |
| 233 | + <CardContent> |
| 234 | + <div className="text-3xl font-bold">{value}</div> |
| 235 | + </CardContent> |
| 236 | + </Card> |
| 237 | + ); |
| 238 | +} |
0 commit comments