|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useEffect } from 'react'; |
| 4 | +import { useRouter } from 'next/navigation'; |
| 5 | +import { |
| 6 | + BookOpen, |
| 7 | + CalendarDays, |
| 8 | + Flame, |
| 9 | + Pencil, |
| 10 | +} from 'lucide-react'; |
| 11 | +import { useAuth } from '@/hooks/useAuth'; |
| 12 | +import { useStatistics } from '@/hooks/useStatistics'; |
| 13 | +import Header from '@/components/layout/Header'; |
| 14 | +import DashboardLoading from '@/app/dashboard/loading'; |
| 15 | +import StatsCard from '@/components/statistics/StatsCard'; |
| 16 | +import TrendChart from '@/components/statistics/TrendChart'; |
| 17 | +import FrameworkBreakdown from '@/components/statistics/FrameworkBreakdown'; |
| 18 | +import PeriodComparison from '@/components/statistics/PeriodComparison'; |
| 19 | +import StreakDisplay from '@/components/statistics/StreakDisplay'; |
| 20 | +import GrowthScore from '@/components/statistics/GrowthScore'; |
| 21 | + |
| 22 | +export default function AnalyticsPage() { |
| 23 | + const { user, signOut, isLoading: authLoading } = useAuth(); |
| 24 | + const router = useRouter(); |
| 25 | + const { summary, trends, distribution, isLoading, error, refetch } = |
| 26 | + useStatistics(user?.id); |
| 27 | + |
| 28 | + useEffect(() => { |
| 29 | + if (!authLoading && !user) { |
| 30 | + const currentPath = `${window.location.pathname}${window.location.search}`; |
| 31 | + router.push(`/auth?next=${encodeURIComponent(currentPath)}`); |
| 32 | + } |
| 33 | + }, [user, authLoading, router]); |
| 34 | + |
| 35 | + const handleSignOut = async () => { |
| 36 | + await signOut(); |
| 37 | + router.push('/auth'); |
| 38 | + }; |
| 39 | + |
| 40 | + if (authLoading || !user) { |
| 41 | + return <DashboardLoading />; |
| 42 | + } |
| 43 | + |
| 44 | + return ( |
| 45 | + <div className="min-h-screen bg-[#fafafa]"> |
| 46 | + <Header |
| 47 | + isAuthenticated={!!user} |
| 48 | + userName={user.name} |
| 49 | + onSignOut={handleSignOut} |
| 50 | + title="統計・トレンド分析" |
| 51 | + showBackButton |
| 52 | + backHref="/dashboard" |
| 53 | + /> |
| 54 | + |
| 55 | + <main className="max-w-7xl mx-auto px-4 py-8"> |
| 56 | + {error && ( |
| 57 | + <div className="mb-6 p-4 bg-red-50 border border-red-200 rounded-lg flex items-center justify-between"> |
| 58 | + <p className="text-red-800">{error}</p> |
| 59 | + <button |
| 60 | + type="button" |
| 61 | + onClick={() => refetch()} |
| 62 | + className="px-3 py-1 bg-red-600 text-white rounded hover:bg-red-700 text-sm" |
| 63 | + > |
| 64 | + 再読み込み |
| 65 | + </button> |
| 66 | + </div> |
| 67 | + )} |
| 68 | + |
| 69 | + {isLoading && !summary ? ( |
| 70 | + <div className="space-y-6"> |
| 71 | + <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4"> |
| 72 | + {[1, 2, 3, 4].map((i) => ( |
| 73 | + <div |
| 74 | + key={i} |
| 75 | + className="h-28 bg-white rounded-lg border border-gray-200 animate-pulse" |
| 76 | + /> |
| 77 | + ))} |
| 78 | + </div> |
| 79 | + <div className="h-80 bg-white rounded-lg border border-gray-200 animate-pulse" /> |
| 80 | + </div> |
| 81 | + ) : summary && trends && distribution ? ( |
| 82 | + <div className="space-y-6"> |
| 83 | + <section> |
| 84 | + <h2 className="text-lg font-semibold text-gray-900 mb-3"> |
| 85 | + KPI サマリー |
| 86 | + </h2> |
| 87 | + <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4"> |
| 88 | + <StatsCard |
| 89 | + label="総振り返り数" |
| 90 | + value={summary.basicStats.total} |
| 91 | + unit="件" |
| 92 | + icon={BookOpen} |
| 93 | + iconColor="text-blue-500" |
| 94 | + /> |
| 95 | + <StatsCard |
| 96 | + label="今月の振り返り" |
| 97 | + value={summary.basicStats.thisMonth} |
| 98 | + unit="件" |
| 99 | + icon={CalendarDays} |
| 100 | + iconColor="text-emerald-500" |
| 101 | + /> |
| 102 | + <StatsCard |
| 103 | + label="現在の連続日数" |
| 104 | + value={summary.streak.currentStreak} |
| 105 | + unit="日" |
| 106 | + icon={Flame} |
| 107 | + iconColor="text-orange-500" |
| 108 | + description={`ベスト: ${summary.streak.bestStreak} 日`} |
| 109 | + /> |
| 110 | + <StatsCard |
| 111 | + label="平均文字数" |
| 112 | + value={summary.basicStats.averageCharacters} |
| 113 | + unit="文字" |
| 114 | + icon={Pencil} |
| 115 | + iconColor="text-purple-500" |
| 116 | + description="1件あたりの平均" |
| 117 | + /> |
| 118 | + </div> |
| 119 | + </section> |
| 120 | + |
| 121 | + <section className="grid grid-cols-1 lg:grid-cols-3 gap-4"> |
| 122 | + <div className="lg:col-span-2"> |
| 123 | + <TrendChart trends={trends} /> |
| 124 | + </div> |
| 125 | + <GrowthScore score={summary.growthScore} /> |
| 126 | + </section> |
| 127 | + |
| 128 | + <section className="grid grid-cols-1 lg:grid-cols-2 gap-4"> |
| 129 | + <PeriodComparison |
| 130 | + title="前月比" |
| 131 | + currentLabel="今月" |
| 132 | + previousLabel="先月" |
| 133 | + data={summary.monthComparison} |
| 134 | + /> |
| 135 | + <PeriodComparison |
| 136 | + title="前週比" |
| 137 | + currentLabel="今週" |
| 138 | + previousLabel="先週" |
| 139 | + data={summary.weekComparison} |
| 140 | + /> |
| 141 | + </section> |
| 142 | + |
| 143 | + <section className="grid grid-cols-1 lg:grid-cols-2 gap-4"> |
| 144 | + <StreakDisplay streak={summary.streak} /> |
| 145 | + <FrameworkBreakdown distribution={distribution.frameworks} /> |
| 146 | + </section> |
| 147 | + </div> |
| 148 | + ) : null} |
| 149 | + </main> |
| 150 | + </div> |
| 151 | + ); |
| 152 | +} |
0 commit comments