// ----------------------------------------------------------------------------- // Author: Simon-Pierre Boucher // Contact: contact@spboucher.ai // Project: Groupe Ka / Ka Maps (Home-Ka integration) // kamaps/adapter.ts : Home-Ka → MapProperty adapter. Viewport-driven data via // /api/listings.geojson?bbox=… (cancellable requests, never a storm). // ----------------------------------------------------------------------------- import type { BoundsQuery, BoundsQueryResult, KaAppSource, KaDataAdapter, MapProperty, } from "@groupe-ka/ka-maps"; import { bboxToString } from "@groupe-ka/ka-maps"; import type { ListingFilters } from "../api"; /** ka-maps ships a closed KaAppSource union (pre-Home-Ka) — cast our id. */ export const HOMEKA_APP_SOURCE = "home-ka" as KaAppSource; interface HomeKaFeatureProps { uid: string; title: string | null; address: string | null; price: number | null; price_label: string | null; property_type: string | null; bedrooms: number | null; bathrooms: number | null; source: string; city: string | null; state: string | null; image: string | null; } interface HomeKaFC { type: "FeatureCollection"; features: { geometry: { coordinates: [number, number] }; properties: HomeKaFeatureProps; }[]; totalGeocoded?: number; totalMatching?: number; } function toMapProperty( coords: [number, number], p: HomeKaFeatureProps, ): MapProperty { return { id: p.uid, appSource: HOMEKA_APP_SOURCE, longitude: coords[0], latitude: coords[1], kind: "listing", listingType: "sale", price: p.price ?? undefined, propertyType: p.property_type ?? undefined, bedrooms: p.bedrooms ?? undefined, bathrooms: p.bathrooms ?? undefined, address: p.address ?? p.title ?? undefined, city: p.city ?? undefined, region: p.state ?? undefined, thumbnailUrl: p.image ?? undefined, originalUrl: `/property/${encodeURIComponent(p.uid)}`, extra: { title: p.title, priceLabel: p.price_label, source: p.source, state: p.state, }, }; } /** Viewport→data adapter for the Home-Ka map. */ export const homeKaMapAdapter: KaDataAdapter = { id: "home-ka-listings", appSource: HOMEKA_APP_SOURCE, async fetchInBounds(query: BoundsQuery): Promise { const params = new URLSearchParams(); const filters = (query.filters ?? {}) as Partial; for (const [k, v] of Object.entries(filters)) { if (v && k !== "sort") params.set(k, String(v)); } params.set("bbox", bboxToString(query.bbox)); params.set("limit", "3000"); const res = await fetch(`/api/listings.geojson?${params}`, { signal: query.signal, }); if (!res.ok) throw new Error(`Map: API ${res.status}`); const data = (await res.json()) as HomeKaFC; return { properties: data.features.map((f) => toMapProperty(f.geometry.coordinates, f.properties), ), totalCount: data.totalMatching, }; }, };