"use client" import { useState, useEffect } from "react" import { Copy, Check, Share2, QrCode, Mail } from "lucide-react" import { Button } from "@/components/ui/button" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@/components/ui/dialog" import { useToast } from "@/hooks/use-toast" import { generateShareUrl, getCurrentUrl } from "@/lib/config" import QRCode from "qrcode" interface ShareModalProps { isOpen: boolean onClose: () => void evaluationId?: string projectTitle?: string } export function ShareModal({ isOpen, onClose, evaluationId, projectTitle }: ShareModalProps) { const [qrCodeUrl, setQrCodeUrl] = useState("") const [isGeneratingQR, setIsGeneratingQR] = useState(false) const [copiedLink, setCopiedLink] = useState(false) const [copiedQR, setCopiedQR] = useState(false) const { toast } = useToast() // 生成分享連結 const shareUrl = evaluationId ? generateShareUrl(evaluationId) : getCurrentUrl() // 生成 QR Code useEffect(() => { if (isOpen && shareUrl) { setIsGeneratingQR(true) QRCode.toDataURL(shareUrl, { width: 160, margin: 2, color: { dark: '#000000', light: '#FFFFFF' } }) .then(url => { setQrCodeUrl(url) setIsGeneratingQR(false) }) .catch(err => { console.error('生成 QR Code 失敗:', err) setIsGeneratingQR(false) toast({ title: "錯誤", description: "生成 QR Code 失敗,請稍後再試", variant: "destructive", }) }) } }, [isOpen, shareUrl, toast]) // 複製連結 const copyLink = async () => { try { await navigator.clipboard.writeText(shareUrl) setCopiedLink(true) toast({ title: "成功", description: "連結已複製到剪貼板", }) setTimeout(() => setCopiedLink(false), 2000) } catch (err) { console.error('複製失敗:', err) toast({ title: "錯誤", description: "複製失敗,請手動複製連結", variant: "destructive", }) } } // 複製 QR Code const copyQRCode = async () => { if (!qrCodeUrl) return try { // 將 base64 轉換為 blob const response = await fetch(qrCodeUrl) const blob = await response.blob() // 複製到剪貼板 await navigator.clipboard.write([ new ClipboardItem({ [blob.type]: blob }) ]) setCopiedQR(true) toast({ title: "成功", description: "QR Code 已複製到剪貼板", }) setTimeout(() => setCopiedQR(false), 2000) } catch (err) { console.error('複製 QR Code 失敗:', err) toast({ title: "錯誤", description: "複製 QR Code 失敗,請截圖保存", variant: "destructive", }) } } // 分享到社群媒體 const shareToSocial = (platform: string) => { const encodedUrl = encodeURIComponent(shareUrl) const encodedTitle = encodeURIComponent(`評審結果 - ${projectTitle || 'AI 智能評審系統'}`) let shareUrl_template = "" switch (platform) { case 'line': shareUrl_template = `https://social-plugins.line.me/lineit/share?url=${encodedUrl}&text=${encodedTitle}` break case 'facebook': shareUrl_template = `https://www.facebook.com/sharer/sharer.php?u=${encodedUrl}` break case 'email': const emailBody = encodeURIComponent(`您好,\n\n我想與您分享這個評審結果:\n\n${projectTitle || 'AI 智能評審系統'}\n\n請點擊以下連結查看詳細報告:\n${shareUrl}\n\n感謝!`) shareUrl_template = `mailto:?subject=${encodedTitle}&body=${emailBody}` break default: return } if (platform === 'email') { window.location.href = shareUrl_template } else { window.open(shareUrl_template, '_blank', 'width=600,height=400') } } return ( 分享評審報告 分享此評審結果給其他人查看
{/* 分享連結 */} 分享連結 複製此連結分享給其他人
{shareUrl}
{/* QR Code */} QR Code 掃描 QR Code 快速訪問報告
{isGeneratingQR ? (
) : qrCodeUrl ? (
QR Code
) : (
生成中...
)}
{/* 社群分享 */} 社群分享 直接分享到社群平台
) }