"use client" import { useState } from "react" import { useAuth } from "@/contexts/auth-context" import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "@/components/ui/dialog" import { Button } from "@/components/ui/button" import { Badge } from "@/components/ui/badge" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Star, Eye, Heart, Info, MessageSquare, User, Calendar, Building, TrendingUp, Users, BarChart3, } from "lucide-react" import { FavoriteButton } from "./favorite-button" import { ReviewSystem } from "./reviews/review-system" interface AppDetailDialogProps { open: boolean onOpenChange: (open: boolean) => void app: { id: number name: string type: string department: string description: string icon: any creator: string featured: boolean judgeScore: number } } // Usage statistics data - empty for production const getAppUsageStats = (appId: string, startDate: string, endDate: string) => { return { dailyUsers: 0, weeklyUsers: 0, monthlyUsers: 0, totalSessions: 0, topDepartments: [], trendData: [], } } export function AppDetailDialog({ open, onOpenChange, app }: AppDetailDialogProps) { const { user, addToRecentApps, getAppLikes, incrementViewCount, getViewCount, getAppRating } = useAuth() const [currentRating, setCurrentRating] = useState(getAppRating(app.id.toString())) const [reviewCount, setReviewCount] = useState(0) const [appStats, setAppStats] = useState({ basic: { views: 0, likes: 0, rating: 0, reviewCount: 0 }, usage: { dailyUsers: 0, weeklyUsers: 0, monthlyUsers: 0, totalSessions: 0, topDepartments: [], trendData: [] } }) const [isLoadingStats, setIsLoadingStats] = useState(false) // Date range for usage trends const [startDate, setStartDate] = useState(() => { const date = new Date() date.setDate(date.getDate() - 6) // Default to last 7 days return date.toISOString().split("T")[0] }) const [endDate, setEndDate] = useState(() => { return new Date().toISOString().split("T")[0] }) const IconComponent = app.icon const likes = getAppLikes(app.id.toString()) const views = getViewCount(app.id.toString()) const usageStats = getAppUsageStats(app.id.toString(), startDate, endDate) const getTypeColor = (type: string) => { const colors = { 文字處理: "bg-blue-100 text-blue-800 border-blue-200", 圖像生成: "bg-purple-100 text-purple-800 border-purple-200", 語音辨識: "bg-green-100 text-green-800 border-green-200", 推薦系統: "bg-orange-100 text-orange-800 border-orange-200", } return colors[type as keyof typeof colors] || "bg-gray-100 text-gray-800 border-gray-200" } const handleRatingUpdate = (newRating: number, newReviewCount: number) => { setCurrentRating(newRating) setReviewCount(newReviewCount) } // 載入應用統計數據 const loadAppStats = async () => { if (!app.id) return setIsLoadingStats(true) try { const response = await fetch(`/api/apps/${app.id}/stats`) const data = await response.json() if (data.success) { setAppStats(data.data) setCurrentRating(data.data.basic.rating) setReviewCount(data.data.basic.reviewCount) } } catch (error) { console.error('載入應用統計數據錯誤:', error) } finally { setIsLoadingStats(false) } } // 當對話框打開時載入統計數據 React.useEffect(() => { if (open && app.id) { loadAppStats() } }, [open, app.id]) const handleTryApp = () => { if (user) { addToRecentApps(app.id.toString()) } // Increment view count when trying the app incrementViewCount(app.id.toString()) // Open external app URL in new tab const appUrls: Record = { "1": "https://dify.example.com/chat-assistant", "2": "https://image-gen.example.com", "3": "https://speech.example.com", "4": "https://recommend.example.com", "5": "https://text-analysis.example.com", "6": "https://ai-writing.example.com", } const appUrl = appUrls[app.id.toString()] if (appUrl) { window.open(appUrl, "_blank", "noopener,noreferrer") } } // Helper function to group data by month/year for section headers const getDateSections = (trendData: any[]) => { const sections: { [key: string]: { startIndex: number; endIndex: number; label: string } } = {} trendData.forEach((day, index) => { const date = new Date(day.date) const yearMonth = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}` const label = date.toLocaleDateString("zh-TW", { year: "numeric", month: "long" }) if (!sections[yearMonth]) { sections[yearMonth] = { startIndex: index, endIndex: index, label } } else { sections[yearMonth].endIndex = index } }) return sections } return (
{app.name} {app.description}
{app.type} {app.department} {app.featured && ( 精選 )}
{likes} 讚
{views} 瀏覽
{currentRating.toFixed(1)} {reviewCount > 0 && ({reviewCount} 評價)}
應用概覽 使用統計 用戶評價 應用詳情

開發者

{app.creator}

所屬部門

{app.department}

發布日期

2024年1月15日

功能特色

  • • 智能化處理能力
  • • 高效能運算
  • • 用戶友好介面
  • • 多語言支援

詳細描述

{app.description} 這個應用採用了最新的人工智能技術, 為用戶提供了卓越的體驗。無論是在處理複雜任務還是日常工作中, 都能展現出色的性能和可靠性。我們持續優化和改進, 確保為用戶帶來最佳的使用體驗。

{/* 基本統計數據 */}
總瀏覽量
{isLoadingStats ? '...' : appStats.basic.views}

累計瀏覽次數

收藏數
{isLoadingStats ? '...' : appStats.basic.likes}

用戶收藏數量

平均評分
{isLoadingStats ? '...' : appStats.basic.rating.toFixed(1)}

用戶評分

評價數量
{isLoadingStats ? '...' : appStats.basic.reviewCount}

用戶評價總數

{/* 使用趨勢 */}
今日使用者
{isLoadingStats ? '...' : appStats.usage.dailyUsers}

活躍用戶數

本週使用者
{isLoadingStats ? '...' : appStats.usage.weeklyUsers}

週活躍用戶

總使用次數
{isLoadingStats ? '...' : appStats.usage.totalSessions.toLocaleString()}

累計使用次數

{/* Usage Trends with Date Range */}
使用趨勢 查看指定時間範圍內的使用者活躍度
setStartDate(e.target.value)} className="w-36" />
setEndDate(e.target.value)} className="w-36" />
{/* Chart Container with Horizontal Scroll */}
{/* Month/Year Section Headers */}
{(() => { const sections = getDateSections(usageStats.trendData) const totalBars = usageStats.trendData.length return Object.entries(sections).map(([key, section]) => { const width = ((section.endIndex - section.startIndex + 1) / totalBars) * 100 const left = (section.startIndex / totalBars) * 100 return (
{section.label}
) }) })()}
{/* Chart Bars */}
{usageStats.trendData.map((day, index) => { const maxUsers = Math.max(...usageStats.trendData.map((d) => d.users)) const minUsers = Math.min(...usageStats.trendData.map((d) => d.users)) const range = maxUsers - minUsers const normalizedHeight = range > 0 ? ((day.users - minUsers) / range) * 70 + 15 : 40 const currentDate = new Date(day.date) const prevDate = index > 0 ? new Date(usageStats.trendData[index - 1].date) : null // Check if this is the start of a new month/year for divider const isNewMonth = !prevDate || currentDate.getMonth() !== prevDate.getMonth() || currentDate.getFullYear() !== prevDate.getFullYear() return (
{/* Month divider line */} {isNewMonth && index > 0 && (
)}
{/* Value label */}
{day.users}
{/* Consistent day-only labels */}
{currentDate.getDate()}日
) })}
{/* Scroll Hint */} {usageStats.trendData.length > 20 && (
💡 提示:圖表可左右滑動查看更多數據
)}
{/* Department Usage */} 部門使用分布 各部門使用者比例
{usageStats.topDepartments.map((dept) => (
{dept.name}
{dept.users} 人 {dept.percentage}%
))}
) }