實作完整分享、刪除、下載報告功能

This commit is contained in:
2025-09-24 00:01:37 +08:00
parent 69c9323038
commit 99ff9654d9
35 changed files with 4779 additions and 45 deletions

296
components/share-modal.tsx Normal file
View File

@@ -0,0 +1,296 @@
"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<string>("")
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 (
<Dialog open={isOpen} onOpenChange={onClose}>
<DialogContent className="max-w-lg max-h-[90vh] overflow-y-auto">
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
<Share2 className="h-5 w-5" />
</DialogTitle>
<DialogDescription>
</DialogDescription>
</DialogHeader>
<div className="space-y-4">
{/* 分享連結 */}
<Card>
<CardHeader className="pb-3">
<CardTitle className="text-base"></CardTitle>
<CardDescription className="text-sm">
</CardDescription>
</CardHeader>
<CardContent className="space-y-3">
<div className="flex flex-col sm:flex-row items-start sm:items-center gap-2">
<div className="flex-1 p-2 bg-muted rounded-md text-sm font-mono break-all min-w-0">
{shareUrl}
</div>
<Button
onClick={copyLink}
size="sm"
variant="outline"
className="shrink-0 w-full sm:w-auto hover:bg-primary/5 hover:border-primary/20 hover:text-primary transition-colors"
>
{copiedLink ? (
<>
<Check className="h-4 w-4 mr-2" />
</>
) : (
<>
<Copy className="h-4 w-4 mr-2" />
</>
)}
</Button>
</div>
</CardContent>
</Card>
{/* QR Code */}
<Card>
<CardHeader className="pb-3">
<CardTitle className="text-base flex items-center gap-2">
<QrCode className="h-4 w-4" />
QR Code
</CardTitle>
<CardDescription className="text-sm">
QR Code
</CardDescription>
</CardHeader>
<CardContent className="space-y-3">
<div className="flex flex-col items-center space-y-3">
{isGeneratingQR ? (
<div className="w-40 h-40 flex items-center justify-center bg-muted rounded-lg">
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary"></div>
</div>
) : qrCodeUrl ? (
<div className="flex flex-col items-center space-y-3">
<img
src={qrCodeUrl}
alt="QR Code"
className="w-40 h-40 border rounded-lg"
/>
<Button
onClick={copyQRCode}
size="sm"
variant="outline"
disabled={copiedQR}
className="w-full sm:w-auto hover:bg-primary/5 hover:border-primary/20 hover:text-primary transition-colors disabled:hover:bg-transparent disabled:hover:border-border disabled:hover:text-muted-foreground"
>
{copiedQR ? (
<>
<Check className="h-4 w-4 mr-2" />
</>
) : (
<>
<Copy className="h-4 w-4 mr-2" />
QR Code
</>
)}
</Button>
</div>
) : (
<div className="w-40 h-40 flex items-center justify-center bg-muted rounded-lg text-muted-foreground">
...
</div>
)}
</div>
</CardContent>
</Card>
{/* 社群分享 */}
<Card>
<CardHeader className="pb-3">
<CardTitle className="text-base"></CardTitle>
<CardDescription className="text-sm">
</CardDescription>
</CardHeader>
<CardContent>
<div className="grid grid-cols-2 sm:grid-cols-3 gap-3">
<Button
onClick={() => shareToSocial('line')}
variant="outline"
size="sm"
className="flex flex-col items-center gap-2 h-auto py-4 hover:bg-green-50 hover:border-green-300 hover:text-green-700 transition-colors"
>
<div className="w-7 h-7 bg-green-500 rounded text-white text-sm flex items-center justify-center font-bold">
L
</div>
<span className="text-xs font-medium">LINE</span>
</Button>
<Button
onClick={() => shareToSocial('facebook')}
variant="outline"
size="sm"
className="flex flex-col items-center gap-2 h-auto py-4 hover:bg-blue-50 hover:border-blue-300 hover:text-blue-700 transition-colors"
>
<div className="w-7 h-7 bg-blue-600 rounded text-white text-sm flex items-center justify-center font-bold">
f
</div>
<span className="text-xs font-medium">Facebook</span>
</Button>
<Button
onClick={() => shareToSocial('email')}
variant="outline"
size="sm"
className="flex flex-col items-center gap-2 h-auto py-4 col-span-2 sm:col-span-1 hover:bg-slate-50 hover:border-slate-300 hover:text-slate-700 transition-colors"
>
<Mail className="w-7 h-7 text-blue-600" />
<span className="text-xs font-medium">Email</span>
</Button>
</div>
</CardContent>
</Card>
</div>
</DialogContent>
</Dialog>
)
}

View File

@@ -0,0 +1,141 @@
"use client"
import * as React from "react"
import * as AlertDialogPrimitive from "@radix-ui/react-alert-dialog"
import { cn } from "@/lib/utils"
import { buttonVariants } from "@/components/ui/button"
const AlertDialog = AlertDialogPrimitive.Root
const AlertDialogTrigger = AlertDialogPrimitive.Trigger
const AlertDialogPortal = AlertDialogPrimitive.Portal
const AlertDialogOverlay = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Overlay>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Overlay>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Overlay
className={cn(
"fixed inset-0 z-50 bg-black/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
className
)}
{...props}
ref={ref}
/>
))
AlertDialogOverlay.displayName = AlertDialogPrimitive.Overlay.displayName
const AlertDialogContent = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Content>
>(({ className, ...props }, ref) => (
<AlertDialogPortal>
<AlertDialogOverlay />
<AlertDialogPrimitive.Content
ref={ref}
className={cn(
"fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] sm:rounded-lg",
className
)}
{...props}
/>
</AlertDialogPortal>
))
AlertDialogContent.displayName = AlertDialogPrimitive.Content.displayName
const AlertDialogHeader = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col space-y-2 text-center sm:text-left",
className
)}
{...props}
/>
)
AlertDialogHeader.displayName = "AlertDialogHeader"
const AlertDialogFooter = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2",
className
)}
{...props}
/>
)
AlertDialogFooter.displayName = "AlertDialogFooter"
const AlertDialogTitle = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Title>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Title>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Title
ref={ref}
className={cn("text-lg font-semibold", className)}
{...props}
/>
))
AlertDialogTitle.displayName = AlertDialogPrimitive.Title.displayName
const AlertDialogDescription = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Description>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Description>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Description
ref={ref}
className={cn("text-sm text-muted-foreground", className)}
{...props}
/>
))
AlertDialogDescription.displayName =
AlertDialogPrimitive.Description.displayName
const AlertDialogAction = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Action>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Action>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Action
ref={ref}
className={cn(buttonVariants(), className)}
{...props}
/>
))
AlertDialogAction.displayName = AlertDialogPrimitive.Action.displayName
const AlertDialogCancel = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Cancel>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Cancel>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Cancel
ref={ref}
className={cn(
buttonVariants({ variant: "outline" }),
"mt-2 sm:mt-0",
className
)}
{...props}
/>
))
AlertDialogCancel.displayName = AlertDialogPrimitive.Cancel.displayName
export {
AlertDialog,
AlertDialogPortal,
AlertDialogOverlay,
AlertDialogTrigger,
AlertDialogContent,
AlertDialogHeader,
AlertDialogFooter,
AlertDialogTitle,
AlertDialogDescription,
AlertDialogAction,
AlertDialogCancel,
}

122
components/ui/dialog.tsx Normal file
View File

@@ -0,0 +1,122 @@
"use client"
import * as React from "react"
import * as DialogPrimitive from "@radix-ui/react-dialog"
import { X } from "lucide-react"
import { cn } from "@/lib/utils"
const Dialog = DialogPrimitive.Root
const DialogTrigger = DialogPrimitive.Trigger
const DialogPortal = DialogPrimitive.Portal
const DialogClose = DialogPrimitive.Close
const DialogOverlay = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Overlay>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Overlay>
>(({ className, ...props }, ref) => (
<DialogPrimitive.Overlay
ref={ref}
className={cn(
"fixed inset-0 z-50 bg-background/80 backdrop-blur-sm data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
className
)}
{...props}
/>
))
DialogOverlay.displayName = DialogPrimitive.Overlay.displayName
const DialogContent = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content>
>(({ className, children, ...props }, ref) => (
<DialogPortal>
<DialogOverlay />
<DialogPrimitive.Content
ref={ref}
className={cn(
"fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] sm:rounded-lg",
className
)}
{...props}
>
{children}
<DialogPrimitive.Close className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-accent data-[state=open]:text-muted-foreground">
<X className="h-4 w-4" />
<span className="sr-only"></span>
</DialogPrimitive.Close>
</DialogPrimitive.Content>
</DialogPortal>
))
DialogContent.displayName = DialogPrimitive.Content.displayName
const DialogHeader = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col space-y-1.5 text-center sm:text-left",
className
)}
{...props}
/>
)
DialogHeader.displayName = "DialogHeader"
const DialogFooter = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2",
className
)}
{...props}
/>
)
DialogFooter.displayName = "DialogFooter"
const DialogTitle = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Title>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Title>
>(({ className, ...props }, ref) => (
<DialogPrimitive.Title
ref={ref}
className={cn(
"text-lg font-semibold leading-none tracking-tight",
className
)}
{...props}
/>
))
DialogTitle.displayName = DialogPrimitive.Title.displayName
const DialogDescription = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Description>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Description>
>(({ className, ...props }, ref) => (
<DialogPrimitive.Description
ref={ref}
className={cn("text-sm text-muted-foreground", className)}
{...props}
/>
))
DialogDescription.displayName = DialogPrimitive.Description.displayName
export {
Dialog,
DialogPortal,
DialogOverlay,
DialogClose,
DialogTrigger,
DialogContent,
DialogHeader,
DialogFooter,
DialogTitle,
DialogDescription,
}

35
components/ui/toaster.tsx Normal file
View File

@@ -0,0 +1,35 @@
"use client"
import {
Toast,
ToastClose,
ToastDescription,
ToastProvider,
ToastTitle,
ToastViewport,
} from "@/components/ui/toast"
import { useToast } from "@/hooks/use-toast"
export function Toaster() {
const { toasts } = useToast()
return (
<ToastProvider>
{toasts.map(function ({ id, title, description, action, ...props }) {
return (
<Toast key={id} {...props}>
<div className="grid gap-1">
{title && <ToastTitle>{title}</ToastTitle>}
{description && (
<ToastDescription>{description}</ToastDescription>
)}
</div>
{action}
<ToastClose />
</Toast>
)
})}
<ToastViewport />
</ToastProvider>
)
}