import { useState } from "preact/hooks"; import AddLeaseDialog from "./AddLeaseDialog.tsx"; import { Lease, Property } from "../../db/interfaces.ts"; export function PopAddLeaseDialog(leases: Lease[], properties: Property[]) { const [popup, setPopup] = useState< { propertyId: string | null; popupKey: number; resolve: (value: boolean) => void; } | null >(null); const [selectedPropertyId, setSelectedPropertyId] = useState(""); const [showPropertySelection, setShowPropertySelection] = useState(false); function getPropertyInfo(propertyId: string) { const property = properties.find((p) => p.id === propertyId); return property ? { title: property.title, address: property.address_1, price: property.price, } : { title: "", address: "", price: undefined }; } // Show the popup and return a Promise for the answer const popAddLease = ( propertyId?: string, popupKey?: number, ): Promise => { return new Promise((resolve) => { if (propertyId) { setPopup({ propertyId, popupKey: popupKey || 0, resolve }); } else { setShowPropertySelection(true); setPopup({ propertyId: null, popupKey: popupKey || 0, resolve }); } }); }; // Render the popup if needed const popupAddLeaseDialog = popup && popup.propertyId ? (() => { const { title, address, price } = getPropertyInfo(popup.propertyId); return ( { popup.resolve(false); setPopup(null); setShowPropertySelection(false); setSelectedPropertyId(""); }} /> ); })() : null; // Render property selection dialog if no propertyId provided const propertySelectionDialog = showPropertySelection ? (() => { return (

Select Property

); })() : null; return [popAddLease, popupAddLeaseDialog || propertySelectionDialog] as const; }