"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 { Progress } from "@/components/ui/progress" import { Alert, AlertDescription } from "@/components/ui/alert" import { Cloud, Trash2, RefreshCw, CheckCircle, AlertTriangle, HardDrive } from "lucide-react" import { StorageHealthService } from "@/lib/supabase-service-updated" export default function StorageManagement() { const [storageHealth, setStorageHealth] = useState<{ healthy: boolean stats?: { totalFiles: number; totalSize: number } error?: string } | null>(null) const [isLoading, setIsLoading] = useState(false) const [cleanupResult, setCleanupResult] = useState<{ cleaned: number; error?: string } | null>(null) useEffect(() => { checkStorageHealth() }, []) const checkStorageHealth = async () => { setIsLoading(true) try { const health = await StorageHealthService.checkStorageHealth() setStorageHealth(health) } catch (error) { setStorageHealth({ healthy: false, error: `檢查失敗: ${error}` }) } finally { setIsLoading(false) } } const cleanupOrphanedImages = async () => { if (!confirm("確定要清理孤立的圖片嗎?這將刪除沒有被任何困擾案例引用的圖片。")) { return } setIsLoading(true) try { const result = await StorageHealthService.cleanupOrphanedImages() setCleanupResult(result) // 重新檢查存儲狀態 await checkStorageHealth() } catch (error) { setCleanupResult({ cleaned: 0, error: `清理失敗: ${error}` }) } finally { setIsLoading(false) } } const formatFileSize = (bytes: number): string => { if (bytes === 0) return "0 Bytes" const k = 1024 const sizes = ["Bytes", "KB", "MB", "GB"] const i = Math.floor(Math.log(bytes) / Math.log(k)) return Number.parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + " " + sizes[i] } return (
存儲服務檢查失敗:
{storageHealth.error}
清理沒有被任何困擾案例引用的圖片檔案,釋放存儲空間。
清理過程中發生錯誤:
{cleanupResult.error}
清理完成!
{cleanupResult.cleaned > 0 ? `成功清理了 ${cleanupResult.cleaned} 個孤立的圖片檔案` : "沒有發現需要清理的孤立圖片"}