"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 (
{/* 存儲狀態 */} Supabase Storage 狀態 雲端圖片存儲服務狀態和使用情況
存儲服務狀態
{isLoading ? "檢查中..." : storageHealth?.healthy ? "正常運行" : "服務異常"}
{/* 存儲統計 */} {storageHealth?.stats && (
{storageHealth.stats.totalFiles}
圖片檔案
{formatFileSize(storageHealth.stats.totalSize)}
總使用空間
)} {/* 存儲使用進度條 */} {storageHealth?.stats && (
存儲使用量 {formatFileSize(storageHealth.stats.totalSize)} / 1GB (免費額度)
)} {/* 錯誤訊息 */} {storageHealth?.error && (

存儲服務檢查失敗:

{storageHealth.error}

)}
{/* 存儲管理 */} 存儲管理 管理和優化雲端圖片存儲

清理孤立圖片

清理沒有被任何困擾案例引用的圖片檔案,釋放存儲空間。

{/* 清理結果 */} {cleanupResult && ( {cleanupResult.error ? ( ) : ( )} {cleanupResult.error ? (

清理過程中發生錯誤:

{cleanupResult.error}

) : (

清理完成!

{cleanupResult.cleaned > 0 ? `成功清理了 ${cleanupResult.cleaned} 個孤立的圖片檔案` : "沒有發現需要清理的孤立圖片"}

)}
)} {/* 存儲最佳實踐 */}

💡 存儲最佳實踐

  • • 定期清理孤立圖片以節省存儲空間
  • • 上傳前壓縮大圖片以減少存儲使用量
  • • 避免上傳重複的圖片內容
  • • 使用適當的圖片格式(WebP 通常最優)
) }