"use client" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Badge } from "@/components/ui/badge" import { Button } from "@/components/ui/button" import { MessageCircle, Target, Lightbulb, Calendar, Sparkles, Clock, Zap, ChevronDown, ChevronUp, Heart, Users, } from "lucide-react" import { categorizeWishMultiple, type Wish } from "@/lib/categorization" import { generateSolutionRecommendations, type SolutionCategory } from "@/lib/solution-recommendations" import { useState, useEffect } from "react" import { soundManager } from "@/lib/sound-effects" import ImageGallery from "@/components/image-gallery" import { restoreImageFile, type ImageFile } from "@/lib/image-utils" import { LikeService } from "@/lib/supabase-service" interface WishCardProps { wish: Wish & { images?: any[]; like_count?: number } // 添加圖片支援和點讚數 } export default function WishCard({ wish }: WishCardProps) { const [showSolutions, setShowSolutions] = useState(false) const [selectedSolution, setSelectedSolution] = useState(null) const [likeCount, setLikeCount] = useState(0) const [hasLiked, setHasLiked] = useState(false) const [isLiking, setIsLiking] = useState(false) // 載入點讚數據 useEffect(() => { const loadLikeData = async () => { try { // 從 Supabase 獲取用戶已點讚的困擾列表 const userLikedWishes = await LikeService.getUserLikedWishes() // 設置點讚狀態 setHasLiked(userLikedWishes.includes(wish.id)) // 點讚數從 wish 的 like_count 字段獲取,如果沒有則默認為 0 setLikeCount(wish.like_count || 0) } catch (error) { console.error("載入點讚數據失敗:", error) // 如果 Supabase 連接失敗,回退到 localStorage const likes = JSON.parse(localStorage.getItem("wishLikes") || "{}") const likedWishes = JSON.parse(localStorage.getItem("userLikedWishes") || "[]") setLikeCount(likes[wish.id] || 0) setHasLiked(likedWishes.includes(wish.id)) } } loadLikeData() }, [wish.id, wish.like_count]) const handleLike = async () => { if (hasLiked || isLiking) return setIsLiking(true) // 播放點讚音效 await soundManager.play("click") try { // 使用 Supabase 點讚服務 const success = await LikeService.likeWish(wish.id) if (success) { // 更新本地狀態 setLikeCount(prev => prev + 1) setHasLiked(true) // 播放成功音效 setTimeout(async () => { await soundManager.play("success") }, 300) } else { // 已經點讚過 console.log("已經點讚過此困擾") } } catch (error) { console.error("點讚失敗:", error) // 如果 Supabase 失敗,回退到 localStorage const likes = JSON.parse(localStorage.getItem("wishLikes") || "{}") const likedWishes = JSON.parse(localStorage.getItem("userLikedWishes") || "[]") likes[wish.id] = (likes[wish.id] || 0) + 1 likedWishes.push(wish.id) localStorage.setItem("wishLikes", JSON.stringify(likes)) localStorage.setItem("userLikedWishes", JSON.stringify(likedWishes)) setLikeCount(likes[wish.id]) setHasLiked(true) // 播放成功音效 setTimeout(async () => { await soundManager.play("success") }, 300) } finally { setIsLiking(false) } } const formatDate = (dateString: string) => { const date = new Date(dateString) return date.toLocaleDateString("zh-TW", { year: "numeric", month: "long", day: "numeric", }) } // 多標籤自動分類,最多3個 const categories = categorizeWishMultiple(wish).slice(0, 3) // 生成解決方案建議 const solutionRecommendation = generateSolutionRecommendations(wish) // 轉換圖片數據格式 - 使用 restoreImageFile 恢復圖片 const images: ImageFile[] = (wish.images || []).map((img) => restoreImageFile(img)) const getDifficultyColor = (difficulty: string) => { switch (difficulty) { case "easy": return "bg-green-500/20 text-green-300 border-green-400/40" case "medium": return "bg-yellow-500/20 text-yellow-300 border-yellow-400/40" case "hard": return "bg-orange-500/20 text-orange-300 border-orange-400/40" default: return "bg-gray-500/20 text-gray-300 border-gray-400/40" } } const getDifficultyLabel = (difficulty: string) => { switch (difficulty) { case "easy": return "容易實現" case "medium": return "中等難度" case "hard": return "需要投入" default: return "未知" } } return ( {/* 背景光效 */}
{/* 頂部裝飾線 */}
{wish.title}
{formatDate(wish.createdAt)} {new Date(wish.createdAt).toLocaleDateString("zh-TW", { month: "short", day: "numeric" })}
{/* 最多3個問題領域標籤 */}
{categories.map((category, index) => (
{category.name} {index === 0 && categories.length > 1 && 主要}
))}
{/* 目前困擾 - 手機優化 */}
{/* 懸停時的光暈效果 */}

遇到的困擾

{wish.currentPain}
{/* 期望解決方式 - 手機優化 */}
{/* 懸停時的光暈效果 */}

期望的解決方式

{wish.expectedSolution}
{/* 預期效果 - 手機優化 */} {wish.expectedEffect && (
{/* 懸停時的光暈效果 */}

預期改善效果

{wish.expectedEffect}
)} {/* 圖片展示區域 */} {images.length > 0 && (
)} {/* 共鳴支持區塊 - 新增 */}
{/* 手機端優化:改為上下布局避免擠壓 */}
{/* 第一行:支持數量和愛心顯示 */}
{likeCount > 0 ? ( {likeCount} 人也遇到 ) : ( 成為第一個支持者 )} {likeCount > 0 ? `${likeCount} 人也遇到相同問題` : "成為第一個表達支持的人"}
{/* 愛心動畫區域 - 手機端縮小 */} {likeCount > 0 && (
{[...Array(Math.min(likeCount, 5))].map((_, i) => ( ))} {likeCount > 5 && +{likeCount - 5}}
)}
{/* 第二行:按讚按鈕 - 手機端更緊湊 */}
{hasLiked && (

感謝你的支持!讓這個問題得到更多關注 💝

)}
{/* AI 解決方案建議區塊 - 改用藍紫色系 */} {solutionRecommendation.recommendations.length > 0 && (
{/* 手機端優化:標題和按鈕分開布局 */}

AI 解決方案建議

信心度 {solutionRecommendation.confidence}% 智能分析
{/* 個人化訊息 */}

{solutionRecommendation.personalizedMessage}

{/* 解決方案建議 */} {showSolutions && (
{solutionRecommendation.recommendations.map((solution, index) => (
setSelectedSolution(selectedSolution?.id === solution.id ? null : solution)} > {/* 手機端優化:重新設計布局結構 */}
{solution.icon}
{/* 標題行:標題和時間 */}
{solution.name}
{solution.timeframe}
{/* 標籤和描述 */}
{getDifficultyLabel(solution.difficulty)}

{solution.description}

{/* 展開的詳細資訊 - 手機端優化 */} {selectedSolution?.id === solution.id && (
✨ 主要效益
{solution.benefits.map((benefit, idx) => (
{benefit}
))}
🛠️ 技術方案
{solution.techStack.map((tech, idx) => ( {tech} ))}
💡 應用實例
{solution.examples.map((example, idx) => (
{example}
))}
)}
))} {/* 專業團隊協助訊息 - 手機端優化 */}
專業團隊支援

我們的 AI 團隊和技術專家會根據這些建議,為你制定具體的實施方案。團隊將主動與你聯繫,協助你逐步改善工作流程!

)}
)}
{/* 底部裝飾 */}
) }