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(""); 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) => { setPopup({ propertyId, popupKey, resolve }); }); }; // Render the popup if needed const popupAddLeaseDialog = popup ? (() => { const { title, address, price } = getPropertyInfo(popup.propertyId || ""); return ( { popup.resolve(false); setPopup(null); }} /> ); })() : null; // Render property selection dialog if no propertyId provided const propertySelectionDialog = popup && !popup.propertyId ? (() => { return (

Select Property

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