"use client"
import { Suspense, useState } from "react"
import { Header } from "@/components/layout/header"
import { Button } from "@/components/ui/button"
import { Skeleton } from "@/components/ui/skeleton"
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
DialogDescription,
} from "@/components/ui/dialog"
import { BarItemForm } from "@/components/bar/bar-item-form"
import { BarCategoryGroup } from "@/components/bar/bar-category-group"
import {
useBarItems,
useCreateBarItem,
useUpdateBarItem,
useDeleteBarItem,
} from "@/hooks/use-bar"
import type { BarItem } from "@/hooks/use-bar"
import { BarcodeScanDialog } from "@/components/bar/barcode-scan-dialog"
import type { BarcodeLookupResult } from "@/hooks/use-barcode-lookup"
import { Plus, Wine, ScanLine } from "lucide-react"
import type { BarItemCreate } from "@/lib/validators"
export default function BarPage() {
return (
}>
)
}
function BarLoading() {
return (
{Array.from({ length: 3 }, (_, i) => (
{Array.from({ length: 4 }, (_, j) => (
))}
))}
)
}
const CATEGORY_ORDER = [
"SPIRITS",
"LIQUEURS",
"MIXERS",
"BITTERS",
"GARNISHES",
"TOOLS",
]
function BarContent() {
const [addDialogOpen, setAddDialogOpen] = useState(false)
const [scanDialogOpen, setScanDialogOpen] = useState(false)
const [editingItem, setEditingItem] = useState(null)
const [scannedData, setScannedData] = useState | null>(null)
const { data, isLoading, error } = useBarItems()
const createBarItem = useCreateBarItem()
const updateBarItem = useUpdateBarItem()
const deleteBarItem = useDeleteBarItem()
function handleScanResult(result: BarcodeLookupResult) {
const initial: Partial & { imageUrl?: string } = {
barcode: result.barcode,
}
if (result.name) {
initial.name = result.brand
? `${result.brand} ${result.name}`
: result.name
}
if (result.category) {
initial.category = result.category as BarItemCreate["category"]
}
if (result.imageUrl) {
initial.imageUrl = result.imageUrl
}
setScannedData(initial)
// Scan dialog closes itself before calling this — just open add form
setAddDialogOpen(true)
}
function handleCreate(formData: BarItemCreate) {
createBarItem.mutate(formData, {
onSuccess: () => {
setAddDialogOpen(false)
setScannedData(null)
},
})
}
function handleUpdate(formData: BarItemCreate) {
if (!editingItem) return
updateBarItem.mutate(
{ id: editingItem.id, data: formData },
{
onSuccess: () => {
setEditingItem(null)
},
}
)
}
function handleDelete(item: BarItem) {
if (confirm(`Delete "${item.name}" from your bar?`)) {
deleteBarItem.mutate(item.id)
}
}
// Group items by category
const groupedItems = (data?.items || []).reduce>(
(groups, item) => {
const key = item.category
if (!groups[key]) groups[key] = []
groups[key].push(item)
return groups
},
{}
)
const totalItems = data?.items.length || 0
return (
My Bar
{totalItems > 0
? `${totalItems} item${totalItems !== 1 ? "s" : ""} in your bar`
: "Your bar inventory"}
{isLoading ? (
{Array.from({ length: 3 }, (_, i) => (
{Array.from({ length: 4 }, (_, j) => (
))}
))}
) : error ? (
Failed to load bar items. Please try again.
) : totalItems === 0 ? (
Your bar is empty
Add your first item to get started.
) : (
{CATEGORY_ORDER.filter((cat) => groupedItems[cat]?.length > 0).map(
(cat) => (
)
)}
)}
{/* Barcode Scan Dialog */}
{/* Add Item Dialog */}
{/* Edit Item Dialog */}
)
}