import React, { useState, useEffect, useRef } from 'react'; import { initializeApp } from 'firebase/app'; import { getAuth, signInAnonymously, onAuthStateChanged, signInWithCustomToken } from 'firebase/auth'; import { getFirestore, collection, onSnapshot, addDoc, deleteDoc, doc, setLogLevel } from 'firebase/firestore'; import { ArrowRight, Facebook, Instagram, MapPin, Plus, Trash2, X } from 'lucide-react'; // --- Firebase Configuration --- // These variables are provided by the environment. const firebaseConfig = typeof __firebase_config !== 'undefined' ? JSON.parse(__firebase_config) : {}; const appId = typeof __app_id !== 'undefined' ? __app_id : 'default-paleteria-app'; // --- Main App Component --- export default function App() { const [db, setDb] = useState(null); const [auth, setAuth] = useState(null); const [userId, setUserId] = useState(null); const [isAuthReady, setIsAuthReady] = useState(false); // --- Component State --- const [inventory, setInventory] = useState([]); const [showAddForm, setShowAddForm] = useState(false); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); // --- Initialize Firebase and Authentication --- useEffect(() => { try { const app = initializeApp(firebaseConfig); const firestoreDb = getFirestore(app); const firebaseAuth = getAuth(app); setLogLevel('debug'); // For detailed console logs setDb(firestoreDb); setAuth(firebaseAuth); const unsubscribe = onAuthStateChanged(firebaseAuth, async (user) => { if (user) { setUserId(user.uid); } else { // If no user, try custom token or fall back to anonymous sign-in try { if (typeof __initial_auth_token !== 'undefined' && __initial_auth_token) { await signInWithCustomToken(firebaseAuth, __initial_auth_token); } else { await signInAnonymously(firebaseAuth); } } catch (authError) { console.error("Authentication Error:", authError); setError("Could not connect to authentication service."); } } setIsAuthReady(true); }); return () => unsubscribe(); } catch (e) { console.error("Firebase Initialization Error:", e); setError("Failed to initialize the application. Please check the configuration."); setIsLoading(false); } }, []); // --- Firestore Real-time Data Fetching --- useEffect(() => { if (isAuthReady && db && userId) { setIsLoading(true); const productsCollectionPath = `artifacts/${appId}/users/${userId}/products`; const productsCollection = collection(db, productsCollectionPath); const unsubscribe = onSnapshot(productsCollection, (snapshot) => { const productsData = snapshot.docs.map(doc => ({ id: doc.id, ...doc.data() })); setInventory(productsData); setIsLoading(false); }, (err) => { console.error("Firestore Snapshot Error:", err); setError("Failed to load inventory. Please try again later."); setIsLoading(false); }); return () => unsubscribe(); } else if (isAuthReady && !userId) { // Handle case where authentication is ready but no user is signed in setIsLoading(false); setError("Authentication failed. Inventory cannot be loaded."); } }, [isAuthReady, db, userId]); // --- Event Handlers --- const handleAddItem = async (item) => { if (!db || !userId) return; try { const productsCollectionPath = `artifacts/${appId}/users/${userId}/products`; await addDoc(collection(db, productsCollectionPath), item); setShowAddForm(false); } catch (e) { console.error("Error adding document: ", e); setError("Failed to add item."); } }; const handleDeleteItem = async (id) => { if (!db || !userId) return; // Using a custom modal for confirmation would be better, but window.confirm is used for simplicity. // In a real app, replace this with a UI component to avoid browser blocking dialogs. if (window.confirm("Are you sure you want to delete this item?")) { try { const productDocPath = `artifacts/${appId}/users/${userId}/products/${id}`; await deleteDoc(doc(db, productDocPath)); } catch (e) { console.error("Error deleting document: ", e); setError("Failed to delete item."); } } }; return (
Experience the best paletas, ice cream, and aguas frescas in town.
View Our MenuHandcrafted with the finest ingredients.
Loading Inventory...
Your inventory is empty.
Click "Add Item" to build your menu!
{item.description}
We are waiting for you!
123 Calle del Sabor
Frutaville, TX 75001
Monday - Saturday: 9:00 AM - 8:00 PM
Sunday: 10:00 AM - 6:00 PM
Google Maps will be here