import React, { useState, useEffect } from "react"; import { initializeApp } from "firebase/app"; import { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword, onAuthStateChanged, signOut, } from "firebase/auth"; import { getFirestore, collection, addDoc, getDocs, query, where, deleteDoc, doc, } from "firebase/firestore"; import { getStorage, ref, uploadBytes, getDownloadURL, deleteObject, } from "firebase/storage"; import { Card, CardContent } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { UploadCloud, FolderPlus, LogOut, Trash2, Moon, Sun } from "lucide-react"; import { motion } from "framer-motion"; // 🔥 GANTI DENGAN CONFIG FIREBASE KAMU const firebaseConfig = { apiKey: "YOUR_API_KEY", authDomain: "YOUR_AUTH_DOMAIN", projectId: "YOUR_PROJECT_ID", storageBucket: "YOUR_STORAGE_BUCKET", messagingSenderId: "YOUR_SENDER_ID", appId: "YOUR_APP_ID", }; const app = initializeApp(firebaseConfig); const auth = getAuth(app); const db = getFirestore(app); const storage = getStorage(app); const MAX_STORAGE_MB = 50; // 📦 batas kapasitas per user export default function CloudStorageFull() { const [user, setUser] = useState(null); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [files, setFiles] = useState([]); const [folders, setFolders] = useState([]); const [currentFolder, setCurrentFolder] = useState("root"); const [search, setSearch] = useState(""); const [darkMode, setDarkMode] = useState(false); const [usedStorage, setUsedStorage] = useState(0); useEffect(() => { onAuthStateChanged(auth, (u) => { if (u) { setUser(u); loadFiles(u.uid); loadFolders(u.uid); } else { setUser(null); } }); }, []); const register = async () => { await createUserWithEmailAndPassword(auth, email, password); }; const login = async () => { await signInWithEmailAndPassword(auth, email, password); }; const logout = async () => { await signOut(auth); }; const loadFiles = async (uid) => { const q = query(collection(db, "files"), where("uid", "==", uid)); const snapshot = await getDocs(q); const data = snapshot.docs.map((doc) => ({ id: doc.id, ...doc.data() })); setFiles(data); const total = data.reduce((acc, f) => acc + f.sizeMB, 0); setUsedStorage(total); }; const loadFolders = async (uid) => { const q = query(collection(db, "folders"), where("uid", "==", uid)); const snapshot = await getDocs(q); setFolders(snapshot.docs.map((doc) => ({ id: doc.id, ...doc.data() }))); }; const createFolder = async () => { const name = prompt("Nama folder:"); if (!name) return; await addDoc(collection(db, "folders"), { uid: user.uid, name, }); loadFolders(user.uid); }; const uploadFile = async (e) => { const file = e.target.files[0]; if (!file) return; const sizeMB = file.size / 1024 / 1024; if (usedStorage + sizeMB > MAX_STORAGE_MB) { alert("Storage penuh! Maks 50MB."); return; } const storageRef = ref(storage, `${user.uid}/${file.name}`); await uploadBytes(storageRef, file); const url = await getDownloadURL(storageRef); await addDoc(collection(db, "files"), { uid: user.uid, name: file.name, url, folder: currentFolder, sizeMB, }); loadFiles(user.uid); }; const deleteFile = async (file) => { await deleteObject(ref(storage, `${user.uid}/${file.name}`)); await deleteDoc(doc(db, "files", file.id)); loadFiles(user.uid); }; if (!user) { return (

Login / Register

setEmail(e.target.value)} /> setPassword(e.target.value)} />
); } const filteredFiles = files.filter( (f) => f.folder === currentFolder && f.name.toLowerCase().includes(search.toLowerCase()) ); return (

My Drive

setSearch(e.target.value)} />

Storage: {usedStorage.toFixed(2)} MB / {MAX_STORAGE_MB} MB

{folders.map((folder) => ( ))}
{filteredFiles.map((file) => (

{file.name}

Open
))}
); }