"use client" import { useState, useEffect } from "react" import { Button } from "@/components/ui/button" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Badge } from "@/components/ui/badge" import { Alert, AlertDescription } from "@/components/ui/alert" import { Progress } from "@/components/ui/progress" import { Database, Upload, CheckCircle, XCircle, AlertTriangle, Loader2, Trash2, RefreshCw } from "lucide-react" import { MigrationService, testSupabaseConnection } from "@/lib/supabase-service" interface MigrationDialogProps { onComplete?: () => void onSkip?: () => void } export default function MigrationDialog({ onComplete, onSkip }: MigrationDialogProps) { const [step, setStep] = useState<"check" | "migrate" | "complete" | "error">("check") const [localDataCount, setLocalDataCount] = useState(0) const [migrationResult, setMigrationResult] = useState<{ success: number failed: number errors: string[] } | null>(null) const [isConnected, setIsConnected] = useState(false) const [isLoading, setIsLoading] = useState(false) const [progress, setProgress] = useState(0) useEffect(() => { checkLocalData() checkConnection() }, []) const checkLocalData = () => { try { const wishes = JSON.parse(localStorage.getItem("wishes") || "[]") setLocalDataCount(wishes.length) } catch (error) { console.error("Error checking local data:", error) setLocalDataCount(0) } } const checkConnection = async () => { setIsLoading(true) try { const connected = await testSupabaseConnection() setIsConnected(connected) } catch (error) { console.error("Connection check failed:", error) setIsConnected(false) } finally { setIsLoading(false) } } const startMigration = async () => { if (!isConnected) { alert("請先確保 Supabase 連接正常") return } setStep("migrate") setIsLoading(true) setProgress(0) try { // 模擬進度更新 const progressInterval = setInterval(() => { setProgress((prev) => Math.min(prev + 10, 90)) }, 200) const result = await MigrationService.migrateWishesFromLocalStorage() clearInterval(progressInterval) setProgress(100) setMigrationResult(result) if (result.success > 0) { setStep("complete") } else { setStep("error") } } catch (error) { console.error("Migration failed:", error) setMigrationResult({ success: 0, failed: localDataCount, errors: [`遷移過程失敗: ${error}`], }) setStep("error") } finally { setIsLoading(false) } } const clearLocalData = () => { if (confirm("確定要清除本地數據嗎?此操作無法復原。")) { MigrationService.clearLocalStorageData() setLocalDataCount(0) onComplete?.() } } const skipMigration = () => { if (confirm("跳過遷移將繼續使用本地存儲。確定要跳過嗎?")) { onSkip?.() } } if (localDataCount === 0) { return ( 準備就緒 沒有發現本地數據,可以直接開始使用 Supabase 開始使用 ) } return ( 數據遷移到 Supabase 發現 {localDataCount} 個本地困擾案例,建議遷移到雲端數據庫 {/* 連接狀態 */} Supabase 連接狀態 {isConnected ? "已連接" : "未連接"} {step === "check" && ( 遷移優勢: • 數據永久保存,不會因清除瀏覽器而丟失 • 支援多設備同步訪問 • 更好的性能和穩定性 • 支援更多用戶同時使用 開始遷移 暫時跳過 )} {step === "migrate" && ( 正在遷移數據... 請稍候,正在將 {localDataCount} 個案例遷移到雲端 )} {step === "complete" && migrationResult && ( 遷移完成! {migrationResult.success} 成功遷移 {migrationResult.failed} 遷移失敗 {migrationResult.errors.length > 0 && ( 查看錯誤詳情 {migrationResult.errors.map((error, index) => ( • {error} ))} )} 清除本地數據並完成 保留本地數據 )} {step === "error" && migrationResult && ( 遷移失敗 遷移過程中遇到問題: {migrationResult.errors.map((error, index) => ( • {error} ))} 重試遷移 跳過遷移 )} ) }
遷移優勢:
正在遷移數據...
請稍候,正在將 {localDataCount} 個案例遷移到雲端
遷移過程中遇到問題: