import { PlusCircle, Trash2 } from "lucide-react"; import { Button } from "@/src/components/ui/button"; import { Input } from "@/src/components/ui/input"; import { useState } from "react"; type UsageDetailsEditorProps = { usageDetails: Record; onChange: (usageDetails: Record) => void; }; export type { UsageDetailsEditorProps }; export function UsageDetailsEditor({ usageDetails, onChange, }: UsageDetailsEditorProps) { const [entries, setEntries] = useState>( Object.entries(usageDetails).map(([key, value]) => ({ key, value })), ); const handleUpdate = (newEntries: Array<{ key: string; value: number }>) => { setEntries(newEntries); const newUsageDetails = Object.fromEntries( newEntries .filter((e) => e.key.trim() !== "") .map((e) => [e.key, e.value]), ); onChange(newUsageDetails); }; const handleAddRow = () => { handleUpdate([...entries, { key: "", value: 0 }]); }; const handleRemoveRow = (index: number) => { handleUpdate(entries.filter((_, i) => i !== index)); }; const handleKeyChange = (index: number, key: string) => { const newEntries = [...entries]; newEntries[index] = { ...newEntries[index], key }; handleUpdate(newEntries); }; const handleValueChange = (index: number, value: number) => { const newEntries = [...entries]; newEntries[index] = { ...newEntries[index], value }; handleUpdate(newEntries); }; const applyTemplate = (template: Record) => { const newEntries = Object.entries(template).map(([key, value]) => ({ key, value, })); handleUpdate(newEntries); }; return (
Usage Details (optional)
Add usage details to test pricing tier matching. Leave empty to match the default tier.
{/* Template Buttons */}
Prefill from template:
{/* Usage Details Table */} {entries.length > 0 ? (
Usage Type
Value
{entries.map((entry, index) => (
handleKeyChange(index, e.target.value)} /> handleValueChange(index, parseFloat(e.target.value) || 0) } />
))}
) : null}
); }