Intermediate
Inventory Manager
Track stock levels with low-stock alerts.
Complete Code
"use client";
import { useState, useEffect, useMemo } from "react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
import { Badge } from "@/components/ui/badge";
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import { Plus, Minus, Package, AlertTriangle } from "lucide-react";
interface Item {
id: string;
name: string;
quantity: number;
minStock: number;
category: string;
}
const STORAGE_KEY = "work-construct-inventory";
export default function App() {
const [items, setItems] = useState<Item[]>(() => {
if (typeof window === "undefined") return [];
const saved = localStorage.getItem(STORAGE_KEY);
return saved ? JSON.parse(saved) : [];
});
const [newItem, setNewItem] = useState({
name: "",
quantity: "",
minStock: "",
category: "",
});
const [dialogOpen, setDialogOpen] = useState(false);
useEffect(() => {
localStorage.setItem(STORAGE_KEY, JSON.stringify(items));
}, [items]);
const addItem = () => {
if (!newItem.name.trim() || !newItem.quantity) return;
setItems([
...items,
{
id: crypto.randomUUID(),
name: newItem.name.trim(),
quantity: parseInt(newItem.quantity),
minStock: parseInt(newItem.minStock) || 5,
category: newItem.category.trim() || "General",
},
]);
setNewItem({ name: "", quantity: "", minStock: "", category: "" });
setDialogOpen(false);
};
const updateQuantity = (id: string, delta: number) => {
setItems(
items.map((item) =>
item.id === id
? { ...item, quantity: Math.max(0, item.quantity + delta) }
: item
)
);
};
const lowStockItems = useMemo(
() => items.filter((item) => item.quantity <= item.minStock),
[items]
);
const totalItems = useMemo(
() => items.reduce((sum, item) => sum + item.quantity, 0),
[items]
);
return (
<div className="min-h-screen bg-background p-4 sm:p-6">
<div className="mx-auto max-w-4xl space-y-6">
{/* Header */}
<div className="flex items-center justify-between">
<div>
<h1 className="text-2xl font-bold">Inventory</h1>
<p className="text-sm text-muted-foreground">
{items.length} products · {totalItems} total units
</p>
</div>
<Dialog open={dialogOpen} onOpenChange={setDialogOpen}>
<DialogTrigger asChild>
<Button>
<Plus className="mr-2 h-4 w-4" />
Add Item
</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Add New Item</DialogTitle>
</DialogHeader>
<div className="space-y-4 pt-4">
<div className="space-y-2">
<Label htmlFor="name">Item Name</Label>
<Input
id="name"
value={newItem.name}
onChange={(e) =>
setNewItem({ ...newItem, name: e.target.value })
}
placeholder="Widget A"
/>
</div>
<div className="grid grid-cols-2 gap-4">
<div className="space-y-2">
<Label htmlFor="quantity">Quantity</Label>
<Input
id="quantity"
type="number"
min="0"
value={newItem.quantity}
onChange={(e) =>
setNewItem({ ...newItem, quantity: e.target.value })
}
placeholder="0"
/>
</div>
<div className="space-y-2">
<Label htmlFor="minStock">Min Stock</Label>
<Input
id="minStock"
type="number"
min="0"
value={newItem.minStock}
onChange={(e) =>
setNewItem({ ...newItem, minStock: e.target.value })
}
placeholder="5"
/>
</div>
</div>
<div className="space-y-2">
<Label htmlFor="category">Category</Label>
<Input
id="category"
value={newItem.category}
onChange={(e) =>
setNewItem({ ...newItem, category: e.target.value })
}
placeholder="General"
/>
</div>
<Button onClick={addItem} className="w-full">
Add Item
</Button>
</div>
</DialogContent>
</Dialog>
</div>
{/* Low Stock Alert */}
{lowStockItems.length > 0 && (
<Card className="border-amber-500/50 bg-amber-500/5">
<CardHeader className="pb-2">
<CardTitle className="flex items-center gap-2 text-amber-600">
<AlertTriangle className="h-5 w-5" />
Low Stock Alert
</CardTitle>
<CardDescription>
{lowStockItems.length} item(s) need restocking
</CardDescription>
</CardHeader>
<CardContent>
<div className="flex flex-wrap gap-2">
{lowStockItems.map((item) => (
<Badge key={item.id} variant="outline">
{item.name} ({item.quantity})
</Badge>
))}
</div>
</CardContent>
</Card>
)}
{/* Inventory Table */}
<Card>
<CardContent className="p-0">
{items.length === 0 ? (
<div className="py-12 text-center">
<Package className="mx-auto h-12 w-12 text-muted-foreground" />
<p className="mt-4 text-sm text-muted-foreground">
No items yet. Add your first item!
</p>
</div>
) : (
<Table>
<TableHeader>
<TableRow>
<TableHead>Item</TableHead>
<TableHead>Category</TableHead>
<TableHead className="text-center">Quantity</TableHead>
<TableHead className="text-right">Actions</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{items.map((item) => (
<TableRow key={item.id}>
<TableCell className="font-medium">
{item.name}
{item.quantity <= item.minStock && (
<Badge
variant="destructive"
className="ml-2 text-xs"
>
Low
</Badge>
)}
</TableCell>
<TableCell>
<Badge variant="secondary">{item.category}</Badge>
</TableCell>
<TableCell className="text-center font-mono">
{item.quantity}
</TableCell>
<TableCell className="text-right">
<div className="flex justify-end gap-1">
<Button
variant="outline"
size="icon"
onClick={() => updateQuantity(item.id, -1)}
aria-label="Decrease quantity"
>
<Minus className="h-4 w-4" />
</Button>
<Button
variant="outline"
size="icon"
onClick={() => updateQuantity(item.id, 1)}
aria-label="Increase quantity"
>
<Plus className="h-4 w-4" />
</Button>
</div>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
)}
</CardContent>
</Card>
</div>
</div>
);
}