"use client" import { useState, useEffect } from "react" import Link from "next/link" import { Button } from "@/components/ui/button" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Badge } from "@/components/ui/badge" import { Sparkles, ArrowLeft, BarChart3, TrendingUp, Users, Target, BookOpen, ChevronDown, ChevronUp, TrendingDown, Minus, Shield, Eye, EyeOff, } from "lucide-react" import RadarChart from "@/components/radar-chart" import HeaderMusicControl from "@/components/header-music-control" import { categories, categorizeWishMultiple, type Wish } from "@/lib/categorization" interface CategoryData { name: string count: number percentage: number color: string keywords: string[] description?: string difficulty?: { level: number stars: string label: string estimatedTime: string techStack: string[] solutionType: string complexity: string } } interface AnalyticsData { totalWishes: number publicWishes: number privateWishes: number categories: CategoryData[] recentTrends: { thisWeek: number lastWeek: number growth: number growthLabel: string growthIcon: "up" | "down" | "flat" growthColor: string } topKeywords: { word: string; count: number }[] } export default function AnalyticsPage() { const [wishes, setWishes] = useState([]) const [analytics, setAnalytics] = useState(null) const [showCategoryGuide, setShowCategoryGuide] = useState(false) // 分析許願內容(包含所有數據,包括私密的) const analyzeWishes = (wishList: (Wish & { isPublic?: boolean })[]): AnalyticsData => { const totalWishes = wishList.length const publicWishes = wishList.filter((wish) => wish.isPublic !== false).length const privateWishes = wishList.filter((wish) => wish.isPublic === false).length const categoryStats: { [key: string]: number } = {} const keywordCount: { [key: string]: number } = {} // 初始化分類統計 categories.forEach((cat) => { categoryStats[cat.name] = 0 }) categoryStats["其他問題"] = 0 // 分析每個許願(多標籤統計)- 包含所有數據 wishList.forEach((wish) => { const wishCategories = categorizeWishMultiple(wish) wishCategories.forEach((category) => { categoryStats[category.name]++ // 統計關鍵字 if (category.keywords) { const fullText = `${wish.title} ${wish.currentPain} ${wish.expectedSolution} ${wish.expectedEffect}`.toLowerCase() category.keywords.forEach((keyword: string) => { if (fullText.includes(keyword.toLowerCase())) { keywordCount[keyword] = (keywordCount[keyword] || 0) + 1 } }) } }) }) // 計算百分比和準備數據 const categoriesData: CategoryData[] = categories.map((cat) => ({ name: cat.name, count: categoryStats[cat.name] || 0, percentage: totalWishes > 0 ? Math.round(((categoryStats[cat.name] || 0) / totalWishes) * 100) : 0, color: cat.color, keywords: cat.keywords, description: cat.description, })) // 改進的趨勢計算 const now = new Date() const oneWeekAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000) const twoWeeksAgo = new Date(now.getTime() - 14 * 24 * 60 * 60 * 1000) const thisWeek = wishList.filter((wish) => new Date(wish.createdAt) >= oneWeekAgo).length const lastWeek = wishList.filter((wish) => { const date = new Date(wish.createdAt) return date >= twoWeeksAgo && date < oneWeekAgo }).length // 改進的成長趨勢計算 let growth = 0 let growthLabel = "持平" let growthIcon: "up" | "down" | "flat" = "flat" let growthColor = "#6B7280" if (lastWeek === 0 && thisWeek > 0) { // 上週沒有,本週有 → 全新開始 growth = 100 growthLabel = "開始增長" growthIcon = "up" growthColor = "#10B981" } else if (lastWeek === 0 && thisWeek === 0) { // 兩週都沒有 growth = 0 growthLabel = "尚無數據" growthIcon = "flat" growthColor = "#6B7280" } else if (lastWeek > 0) { // 正常計算成長率 growth = Math.round(((thisWeek - lastWeek) / lastWeek) * 100) if (growth > 0) { growthLabel = "持續增長" growthIcon = "up" growthColor = "#10B981" } else if (growth < 0) { growthLabel = "有所下降" growthIcon = "down" growthColor = "#EF4444" } else { growthLabel = "保持穩定" growthIcon = "flat" growthColor = "#6B7280" } } // 取得熱門關鍵字 const topKeywords = Object.entries(keywordCount) .sort(([, a], [, b]) => b - a) .slice(0, 15) .map(([word, count]) => ({ word, count })) return { totalWishes, publicWishes, privateWishes, categories: categoriesData, recentTrends: { thisWeek, lastWeek, growth, growthLabel, growthIcon, growthColor, }, topKeywords, } } useEffect(() => { const savedWishes = JSON.parse(localStorage.getItem("wishes") || "[]") setWishes(savedWishes) setAnalytics(analyzeWishes(savedWishes)) }, []) if (!analytics) { return (
正在分析數據...
) } // 根據成長趨勢選擇圖標 const GrowthIcon = analytics.recentTrends.growthIcon === "up" ? TrendingUp : analytics.recentTrends.growthIcon === "down" ? TrendingDown : Minus return (
{/* 星空背景 */}
{[...Array(30)].map((_, i) => (
))}
{/* Header - 修復跑版問題 */}
{/* Logo 區域 - 防止文字換行 */}

心願星河

{/* 導航區域 */}
{/* Main Content */}
{/* 頁面標題 */}

問題洞察分析

完整數據分析

深入分析職場困擾的分布和趨勢

包含所有提交的案例數據,協助管理者了解真實狀況

{/* 隱私說明卡片 */}
數據隱私說明
本分析包含所有提交的案例,包括選擇保持私密的困擾

公開案例 ({analytics.publicWishes} 個)

這些案例會顯示在「聆聽心聲」頁面,供其他人查看和產生共鳴

私密案例 ({analytics.privateWishes} 個)

這些案例保持匿名且私密,僅用於統計分析,幫助了解整體趨勢

給管理者的說明: 此分析包含完整的問題數據,能幫助您了解團隊面臨的真實挑戰。私密案例雖然不會公開顯示, 但其數據對於制定改善策略同樣重要。所有個人身份資訊都已匿名化處理。

{/* 統計概覽 */}
{analytics.totalWishes}
總案例數
公開 {analytics.publicWishes} + 私密 {analytics.privateWishes}
{analytics.recentTrends.thisWeek}
本週新增
{analytics.categories.filter((c) => c.count > 0).length}
問題領域
{analytics.recentTrends.growth > 0 ? "+" : ""} {analytics.recentTrends.growth}%
{analytics.recentTrends.growthLabel}
{/* 詳細說明 */}
上週: {analytics.recentTrends.lastWeek} 個
{/* 分類指南 */}
問題分類說明 了解我們如何分類和分析各種職場困擾
{showCategoryGuide && (
{categories.map((category, index) => (
{category.icon}

{category.name}

{category.description}

{/* 關鍵字示例 */}
常見關鍵字:
{category.keywords.slice(0, 6).map((keyword, idx) => ( {keyword} ))} {category.keywords.length > 6 && ( +{category.keywords.length - 6} )}
))}
)}
{/* 手機版:垂直佈局,桌面版:並排佈局 */}
{/* 雷達圖 - 手機版給予更多高度 */}
問題分布圖譜
各類職場困擾的完整案例分布(包含私密數據)
{/* 手機版使用更大的高度,桌面版保持原有高度 */}
{/* 分類詳細統計 */}
完整案例統計 含私密數據
每個領域的所有案例數量(包含公開和私密案例) {analytics.categories.filter((cat) => cat.count > 0).length > 0 && ( 共 {analytics.categories.filter((cat) => cat.count > 0).length} 個活躍分類 {analytics.categories.filter((cat) => cat.count > 0).length > 4 && ",可滾動查看全部"} )}
{/* 設定固定高度並添加滾動 */}
{analytics.categories .filter((cat) => cat.count > 0) .sort((a, b) => b.count - a.count) .map((category, index) => (
{categories.find((cat) => cat.name === category.name)?.icon || "❓"}
{category.name}
{/* 添加排名標示 */} {index < 3 && ( TOP {index + 1} )}
{category.count} 個案例
{category.description && (
{category.description}
)}
{category.percentage}%
))}
{/* 滾動提示 */} {analytics.categories.filter((cat) => cat.count > 0).length > 4 && (
向下滾動查看更多分類
)}
{/* 多維度分析說明 */}
完整數據分析優勢
包含私密案例的全面分析,提供更準確的洞察

🔍 真實全貌

包含所有案例數據,不受公開意願影響,呈現最真實的問題狀況

📊 精準決策

基於完整數據制定改善策略,避免因樣本偏差導致的決策失誤

🎯 隱藏問題

發現那些員工不願公開但確實存在的問題,提前預防和解決

🔒 隱私保護

在保護個人隱私的前提下,最大化數據的分析價值

{/* 熱門關鍵字 */} {analytics.topKeywords.length > 0 && (
最常見的問題關鍵字
在所有案例中最常出現的詞彙,反映團隊面臨的核心挑戰
{analytics.topKeywords.map((keyword, index) => ( {keyword.word} ({keyword.count}) ))}
)}
) }