@garagem-ai/site-sdk 0.6.1 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +260 -28
- package/dist/index.d.mts +488 -150
- package/dist/index.d.ts +488 -150
- package/dist/index.js +882 -36
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +857 -36
- package/dist/index.mjs.map +1 -1
- package/dist/react.d.mts +46 -2
- package/dist/react.d.ts +46 -2
- package/dist/react.js +334 -14
- package/dist/react.js.map +1 -1
- package/dist/react.mjs +332 -14
- package/dist/react.mjs.map +1 -1
- package/dist/{search-engine-CtnLzmwW.d.mts → types-photo-search-CmGRj6my.d.mts} +229 -1
- package/dist/{search-engine-CtnLzmwW.d.ts → types-photo-search-CmGRj6my.d.ts} +229 -1
- package/package.json +2 -1
- package/src/client.ts +23 -0
- package/src/fetchers-market.ts +216 -0
- package/src/fetchers-photo-search.ts +101 -0
- package/src/fetchers-photos.ts +98 -0
- package/src/fetchers.ts +92 -29
- package/src/index.ts +92 -2
- package/src/internal/sdk-http.ts +122 -0
- package/src/leads.ts +34 -3
- package/src/listing-photos.ts +312 -0
- package/src/market-labels.ts +51 -0
- package/src/photo-search-cover.ts +90 -0
- package/src/react/index.ts +8 -0
- package/src/react/types.ts +5 -0
- package/src/react/use-public-photo-search.ts +165 -0
- package/src/react/use-public-search.ts +23 -5
- package/src/resolve-site-id.ts +3 -0
- package/src/studio-contract.ts +178 -3
- package/src/types-market.ts +138 -0
- package/src/types-photo-search.ts +55 -0
- package/src/types-photos.ts +46 -0
- package/src/types.ts +18 -0
package/dist/react.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { m as SearchUi, e as Listing, U as LocationNameTables, S as SearchPathFilters, A as AppliedSearchQuery, t as PhotoSearchItem, u as ListingWithMatchedPhoto, F as FetchLike } from './types-photo-search-CmGRj6my.mjs';
|
|
2
2
|
|
|
3
3
|
type SiteIdInput = string | Promise<string> | (() => string | Promise<string>);
|
|
4
4
|
type PublicSearchSnapshot = {
|
|
@@ -13,6 +13,11 @@ type PublicSearchSnapshot = {
|
|
|
13
13
|
type UsePublicSearchOptions = {
|
|
14
14
|
siteId: SiteIdInput;
|
|
15
15
|
baseUrl?: string;
|
|
16
|
+
/**
|
|
17
|
+
* Site publishable key (`pk_live_…`). Sent on schema, location, and
|
|
18
|
+
* listing requests. Not a secret. Omit it to stay on the anonymous budget.
|
|
19
|
+
*/
|
|
20
|
+
publishableKey?: string;
|
|
16
21
|
/**
|
|
17
22
|
* Query string without `?`, from the server page's `searchParams`.
|
|
18
23
|
* The hook must not call Next's search-params hook: that suspends the
|
|
@@ -51,4 +56,43 @@ type PublicSearchState = {
|
|
|
51
56
|
|
|
52
57
|
declare function usePublicSearch(options: UsePublicSearchOptions): PublicSearchState;
|
|
53
58
|
|
|
54
|
-
|
|
59
|
+
/** Same debounce as the CRM "Foto que combina com a busca" evidence lane. */
|
|
60
|
+
declare const PHOTO_SEARCH_DEBOUNCE_MS = 400;
|
|
61
|
+
type UsePublicPhotoSearchOptions = {
|
|
62
|
+
siteId: SiteIdInput;
|
|
63
|
+
baseUrl?: string;
|
|
64
|
+
/**
|
|
65
|
+
* The text to search by (usually the main search box). The hook trims it,
|
|
66
|
+
* debounces requests and ignores responses that arrive out of order.
|
|
67
|
+
*/
|
|
68
|
+
query: string;
|
|
69
|
+
/** Listings per page (API default 12, cap 24). */
|
|
70
|
+
limit?: number;
|
|
71
|
+
/** 1-based page. */
|
|
72
|
+
page?: number;
|
|
73
|
+
/** `false` pauses the hook and clears its results (e.g. a hidden panel). */
|
|
74
|
+
enabled?: boolean;
|
|
75
|
+
/** Milliseconds to wait after the last `query` change. Default 400. */
|
|
76
|
+
debounceMs?: number;
|
|
77
|
+
/** Override the fetch implementation (testing, retries). */
|
|
78
|
+
fetch?: FetchLike;
|
|
79
|
+
};
|
|
80
|
+
type PublicPhotoSearchState = {
|
|
81
|
+
/** `false` once the API says the tenant has no photo search: hide the UI. */
|
|
82
|
+
available: boolean;
|
|
83
|
+
/** Nearest-first matches, each with its card and matched photo. */
|
|
84
|
+
items: PhotoSearchItem[];
|
|
85
|
+
/** `items` as cards with the matched photo already applied as cover. */
|
|
86
|
+
listings: ListingWithMatchedPhoto[];
|
|
87
|
+
total: number;
|
|
88
|
+
page: number;
|
|
89
|
+
limit: number;
|
|
90
|
+
/** A request is in flight (or scheduled by the debounce). */
|
|
91
|
+
loading: boolean;
|
|
92
|
+
error: Error | null;
|
|
93
|
+
/** The trimmed query the current `items` answer (`""` before the first result). */
|
|
94
|
+
query: string;
|
|
95
|
+
};
|
|
96
|
+
declare function usePublicPhotoSearch(options: UsePublicPhotoSearchOptions): PublicPhotoSearchState;
|
|
97
|
+
|
|
98
|
+
export { PHOTO_SEARCH_DEBOUNCE_MS, type PublicPhotoSearchState, type PublicSearchSnapshot, type PublicSearchState, type SiteIdInput, type UsePublicPhotoSearchOptions, type UsePublicSearchOptions, usePublicPhotoSearch, usePublicSearch };
|
package/dist/react.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { m as SearchUi, e as Listing, U as LocationNameTables, S as SearchPathFilters, A as AppliedSearchQuery, t as PhotoSearchItem, u as ListingWithMatchedPhoto, F as FetchLike } from './types-photo-search-CmGRj6my.js';
|
|
2
2
|
|
|
3
3
|
type SiteIdInput = string | Promise<string> | (() => string | Promise<string>);
|
|
4
4
|
type PublicSearchSnapshot = {
|
|
@@ -13,6 +13,11 @@ type PublicSearchSnapshot = {
|
|
|
13
13
|
type UsePublicSearchOptions = {
|
|
14
14
|
siteId: SiteIdInput;
|
|
15
15
|
baseUrl?: string;
|
|
16
|
+
/**
|
|
17
|
+
* Site publishable key (`pk_live_…`). Sent on schema, location, and
|
|
18
|
+
* listing requests. Not a secret. Omit it to stay on the anonymous budget.
|
|
19
|
+
*/
|
|
20
|
+
publishableKey?: string;
|
|
16
21
|
/**
|
|
17
22
|
* Query string without `?`, from the server page's `searchParams`.
|
|
18
23
|
* The hook must not call Next's search-params hook: that suspends the
|
|
@@ -51,4 +56,43 @@ type PublicSearchState = {
|
|
|
51
56
|
|
|
52
57
|
declare function usePublicSearch(options: UsePublicSearchOptions): PublicSearchState;
|
|
53
58
|
|
|
54
|
-
|
|
59
|
+
/** Same debounce as the CRM "Foto que combina com a busca" evidence lane. */
|
|
60
|
+
declare const PHOTO_SEARCH_DEBOUNCE_MS = 400;
|
|
61
|
+
type UsePublicPhotoSearchOptions = {
|
|
62
|
+
siteId: SiteIdInput;
|
|
63
|
+
baseUrl?: string;
|
|
64
|
+
/**
|
|
65
|
+
* The text to search by (usually the main search box). The hook trims it,
|
|
66
|
+
* debounces requests and ignores responses that arrive out of order.
|
|
67
|
+
*/
|
|
68
|
+
query: string;
|
|
69
|
+
/** Listings per page (API default 12, cap 24). */
|
|
70
|
+
limit?: number;
|
|
71
|
+
/** 1-based page. */
|
|
72
|
+
page?: number;
|
|
73
|
+
/** `false` pauses the hook and clears its results (e.g. a hidden panel). */
|
|
74
|
+
enabled?: boolean;
|
|
75
|
+
/** Milliseconds to wait after the last `query` change. Default 400. */
|
|
76
|
+
debounceMs?: number;
|
|
77
|
+
/** Override the fetch implementation (testing, retries). */
|
|
78
|
+
fetch?: FetchLike;
|
|
79
|
+
};
|
|
80
|
+
type PublicPhotoSearchState = {
|
|
81
|
+
/** `false` once the API says the tenant has no photo search: hide the UI. */
|
|
82
|
+
available: boolean;
|
|
83
|
+
/** Nearest-first matches, each with its card and matched photo. */
|
|
84
|
+
items: PhotoSearchItem[];
|
|
85
|
+
/** `items` as cards with the matched photo already applied as cover. */
|
|
86
|
+
listings: ListingWithMatchedPhoto[];
|
|
87
|
+
total: number;
|
|
88
|
+
page: number;
|
|
89
|
+
limit: number;
|
|
90
|
+
/** A request is in flight (or scheduled by the debounce). */
|
|
91
|
+
loading: boolean;
|
|
92
|
+
error: Error | null;
|
|
93
|
+
/** The trimmed query the current `items` answer (`""` before the first result). */
|
|
94
|
+
query: string;
|
|
95
|
+
};
|
|
96
|
+
declare function usePublicPhotoSearch(options: UsePublicPhotoSearchOptions): PublicPhotoSearchState;
|
|
97
|
+
|
|
98
|
+
export { PHOTO_SEARCH_DEBOUNCE_MS, type PublicPhotoSearchState, type PublicSearchSnapshot, type PublicSearchState, type SiteIdInput, type UsePublicPhotoSearchOptions, type UsePublicSearchOptions, usePublicPhotoSearch, usePublicSearch };
|
package/dist/react.js
CHANGED
|
@@ -21,6 +21,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
// src/react/index.ts
|
|
22
22
|
var react_exports = {};
|
|
23
23
|
__export(react_exports, {
|
|
24
|
+
PHOTO_SEARCH_DEBOUNCE_MS: () => PHOTO_SEARCH_DEBOUNCE_MS,
|
|
25
|
+
usePublicPhotoSearch: () => usePublicPhotoSearch,
|
|
24
26
|
usePublicSearch: () => usePublicSearch
|
|
25
27
|
});
|
|
26
28
|
module.exports = __toCommonJS(react_exports);
|
|
@@ -29,6 +31,90 @@ module.exports = __toCommonJS(react_exports);
|
|
|
29
31
|
var import_react = require("react");
|
|
30
32
|
var import_navigation = require("next/navigation");
|
|
31
33
|
|
|
34
|
+
// src/internal/sdk-http.ts
|
|
35
|
+
var PUBLISHABLE_KEY_HEADER = "X-Garagem-Publishable-Key";
|
|
36
|
+
function publishableKeyHeaders(publishableKey) {
|
|
37
|
+
const key = publishableKey?.trim();
|
|
38
|
+
if (!key) return {};
|
|
39
|
+
return { [PUBLISHABLE_KEY_HEADER]: key };
|
|
40
|
+
}
|
|
41
|
+
function withPublishableKeyHeader(init, publishableKey) {
|
|
42
|
+
const extra = publishableKeyHeaders(publishableKey);
|
|
43
|
+
if (Object.keys(extra).length === 0) return init;
|
|
44
|
+
const current = init.headers;
|
|
45
|
+
const base = plainHeaders(current);
|
|
46
|
+
return { ...init, headers: { ...base, ...extra } };
|
|
47
|
+
}
|
|
48
|
+
function plainHeaders(headers) {
|
|
49
|
+
const out = {};
|
|
50
|
+
if (!headers) return out;
|
|
51
|
+
if (typeof Headers !== "undefined" && headers instanceof Headers) {
|
|
52
|
+
headers.forEach((value, name) => {
|
|
53
|
+
out[name] = value;
|
|
54
|
+
});
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
if (Array.isArray(headers)) {
|
|
58
|
+
for (const pair of headers) {
|
|
59
|
+
out[String(pair[0])] = String(pair[1]);
|
|
60
|
+
}
|
|
61
|
+
return out;
|
|
62
|
+
}
|
|
63
|
+
for (const [name, value] of Object.entries(
|
|
64
|
+
headers
|
|
65
|
+
)) {
|
|
66
|
+
if (typeof value === "string") out[name] = value;
|
|
67
|
+
}
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
function finiteNonNegative(value) {
|
|
71
|
+
if (typeof value === "number" && Number.isFinite(value) && value >= 0) {
|
|
72
|
+
return value;
|
|
73
|
+
}
|
|
74
|
+
if (typeof value === "string" && value.trim()) {
|
|
75
|
+
const parsed = Number(value);
|
|
76
|
+
if (Number.isFinite(parsed) && parsed >= 0) return parsed;
|
|
77
|
+
}
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
function parseSdkErrorBody(text) {
|
|
81
|
+
const raw = text ? text.slice(0, 200) : "";
|
|
82
|
+
if (!text) {
|
|
83
|
+
return { reason: null, retryAfterSeconds: null, summary: "" };
|
|
84
|
+
}
|
|
85
|
+
try {
|
|
86
|
+
const body = JSON.parse(text);
|
|
87
|
+
if (!body || typeof body !== "object") {
|
|
88
|
+
return { reason: null, retryAfterSeconds: null, summary: raw };
|
|
89
|
+
}
|
|
90
|
+
const record = body;
|
|
91
|
+
const reason = typeof record.reason === "string" && record.reason ? record.reason : null;
|
|
92
|
+
const retryAfterSeconds = finiteNonNegative(
|
|
93
|
+
record.retryAfterSeconds ?? record.retryAfter
|
|
94
|
+
);
|
|
95
|
+
const errorText = typeof record.error === "string" && record.error ? record.error : raw;
|
|
96
|
+
return {
|
|
97
|
+
reason,
|
|
98
|
+
retryAfterSeconds,
|
|
99
|
+
summary: errorText.slice(0, 200)
|
|
100
|
+
};
|
|
101
|
+
} catch {
|
|
102
|
+
return { reason: null, retryAfterSeconds: null, summary: raw };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
function retryAfterFromResponse(response, bodySeconds, nowMs = Date.now()) {
|
|
106
|
+
const header = response.headers.get("Retry-After")?.trim();
|
|
107
|
+
if (header) {
|
|
108
|
+
const delta = finiteNonNegative(header);
|
|
109
|
+
if (delta != null) return delta;
|
|
110
|
+
const at = Date.parse(header);
|
|
111
|
+
if (Number.isFinite(at)) {
|
|
112
|
+
return Math.max(0, Math.ceil((at - nowMs) / 1e3));
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return bodySeconds;
|
|
116
|
+
}
|
|
117
|
+
|
|
32
118
|
// src/internal/url.ts
|
|
33
119
|
var DEFAULT_BASE_URL = "https://app.garagem.ai";
|
|
34
120
|
function normalizeBaseUrl(baseUrl) {
|
|
@@ -47,27 +133,50 @@ async function coerceSiteId(siteId) {
|
|
|
47
133
|
}
|
|
48
134
|
|
|
49
135
|
// src/fetchers.ts
|
|
50
|
-
async function callJson(url, init, fetchImpl) {
|
|
51
|
-
const response = await fetchImpl(
|
|
136
|
+
async function callJson(url, init, fetchImpl, publishableKey) {
|
|
137
|
+
const response = await fetchImpl(
|
|
138
|
+
url,
|
|
139
|
+
withPublishableKeyHeader(init, publishableKey)
|
|
140
|
+
);
|
|
52
141
|
if (!response.ok) {
|
|
53
|
-
|
|
54
|
-
throw new GaragemSdkError(
|
|
55
|
-
`Garagem SDK: request failed (${response.status} ${response.statusText}) for ${url}${text ? ` \u2014 ${text.slice(0, 200)}` : ""}`,
|
|
56
|
-
{ status: response.status, url }
|
|
57
|
-
);
|
|
142
|
+
throw await readGaragemSdkError(response, url);
|
|
58
143
|
}
|
|
59
144
|
return await response.json();
|
|
60
145
|
}
|
|
61
146
|
var GaragemSdkError = class extends Error {
|
|
62
147
|
status;
|
|
63
148
|
url;
|
|
149
|
+
/** Machine reason from the JSON body (`invalid_publishable_key`, `rate_limited`, …). */
|
|
150
|
+
reason;
|
|
151
|
+
/**
|
|
152
|
+
* Seconds to wait before retrying. Set on HTTP 429 from `Retry-After`
|
|
153
|
+
* or the body's `retryAfterSeconds`. `null` when the response has neither.
|
|
154
|
+
*/
|
|
155
|
+
retryAfter;
|
|
64
156
|
constructor(message, info) {
|
|
65
157
|
super(message);
|
|
66
158
|
this.name = "GaragemSdkError";
|
|
67
159
|
this.status = info.status;
|
|
68
160
|
this.url = info.url;
|
|
161
|
+
this.reason = info.reason ?? null;
|
|
162
|
+
this.retryAfter = info.retryAfter ?? null;
|
|
69
163
|
}
|
|
70
164
|
};
|
|
165
|
+
async function readGaragemSdkError(response, url) {
|
|
166
|
+
const text = await response.text().catch(() => "");
|
|
167
|
+
const parsed = parseSdkErrorBody(text);
|
|
168
|
+
const retryAfter = response.status === 429 ? retryAfterFromResponse(response, parsed.retryAfterSeconds) : parsed.retryAfterSeconds;
|
|
169
|
+
const detail = parsed.summary;
|
|
170
|
+
return new GaragemSdkError(
|
|
171
|
+
`Garagem SDK: request failed (${response.status} ${response.statusText}) for ${url}${detail ? ` \u2014 ${detail}` : ""}`,
|
|
172
|
+
{
|
|
173
|
+
status: response.status,
|
|
174
|
+
url,
|
|
175
|
+
reason: parsed.reason,
|
|
176
|
+
retryAfter
|
|
177
|
+
}
|
|
178
|
+
);
|
|
179
|
+
}
|
|
71
180
|
async function getSearchUi(options) {
|
|
72
181
|
const { baseUrl, fetch: fetchImpl, next } = options;
|
|
73
182
|
const siteId = await coerceSiteId(options.siteId);
|
|
@@ -76,7 +185,8 @@ async function getSearchUi(options) {
|
|
|
76
185
|
return callJson(
|
|
77
186
|
url,
|
|
78
187
|
{ method: "GET", headers: { Accept: "application/json" }, next },
|
|
79
|
-
fetchImpl ?? fetch
|
|
188
|
+
fetchImpl ?? fetch,
|
|
189
|
+
options.publishableKey
|
|
80
190
|
);
|
|
81
191
|
}
|
|
82
192
|
async function queryListings(options) {
|
|
@@ -95,7 +205,8 @@ async function queryListings(options) {
|
|
|
95
205
|
body: JSON.stringify(params ?? {}),
|
|
96
206
|
next
|
|
97
207
|
},
|
|
98
|
-
fetchImpl ?? fetch
|
|
208
|
+
fetchImpl ?? fetch,
|
|
209
|
+
options.publishableKey
|
|
99
210
|
);
|
|
100
211
|
}
|
|
101
212
|
|
|
@@ -833,7 +944,13 @@ function currentHref(pathname, search) {
|
|
|
833
944
|
return search ? `${pathname}?${search}` : pathname;
|
|
834
945
|
}
|
|
835
946
|
function usePublicSearch(options) {
|
|
836
|
-
const {
|
|
947
|
+
const {
|
|
948
|
+
siteId,
|
|
949
|
+
baseUrl,
|
|
950
|
+
publishableKey,
|
|
951
|
+
initial,
|
|
952
|
+
search: searchFromServer = ""
|
|
953
|
+
} = options;
|
|
837
954
|
const router = (0, import_navigation.useRouter)();
|
|
838
955
|
const pathname = (0, import_navigation.usePathname)() || "/imoveis";
|
|
839
956
|
const search = searchFromServer;
|
|
@@ -922,13 +1039,23 @@ function usePublicSearch(options) {
|
|
|
922
1039
|
}
|
|
923
1040
|
async function loadSchema() {
|
|
924
1041
|
try {
|
|
925
|
-
const nextSchema = await getSearchUi({
|
|
1042
|
+
const nextSchema = await getSearchUi({
|
|
1043
|
+
siteId: id,
|
|
1044
|
+
baseUrl,
|
|
1045
|
+
publishableKey
|
|
1046
|
+
});
|
|
926
1047
|
if (!active) return;
|
|
927
1048
|
let locations = null;
|
|
928
1049
|
if (nextSchema.locationsPath.startsWith("/")) {
|
|
929
1050
|
try {
|
|
930
1051
|
const response = await fetch(
|
|
931
|
-
`${normalizeBaseUrl(baseUrl)}${nextSchema.locationsPath}
|
|
1052
|
+
`${normalizeBaseUrl(baseUrl)}${nextSchema.locationsPath}`,
|
|
1053
|
+
{
|
|
1054
|
+
headers: {
|
|
1055
|
+
Accept: "application/json",
|
|
1056
|
+
...publishableKeyHeaders(publishableKey)
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
932
1059
|
);
|
|
933
1060
|
if (response.ok) {
|
|
934
1061
|
locations = await response.json();
|
|
@@ -962,7 +1089,7 @@ function usePublicSearch(options) {
|
|
|
962
1089
|
return () => {
|
|
963
1090
|
active = false;
|
|
964
1091
|
};
|
|
965
|
-
}, [baseUrl, catalogId]);
|
|
1092
|
+
}, [baseUrl, catalogId, publishableKey]);
|
|
966
1093
|
(0, import_react.useEffect)(() => {
|
|
967
1094
|
if (!schema || !catalogId) return;
|
|
968
1095
|
const currentSchema = schema;
|
|
@@ -985,6 +1112,7 @@ function usePublicSearch(options) {
|
|
|
985
1112
|
const response = await queryListings({
|
|
986
1113
|
siteId: id,
|
|
987
1114
|
baseUrl,
|
|
1115
|
+
publishableKey,
|
|
988
1116
|
params: buildQueryListingsParams(
|
|
989
1117
|
pathFilters,
|
|
990
1118
|
applied,
|
|
@@ -1021,7 +1149,7 @@ function usePublicSearch(options) {
|
|
|
1021
1149
|
return () => {
|
|
1022
1150
|
active = false;
|
|
1023
1151
|
};
|
|
1024
|
-
}, [pathname, search, baseUrl, schema, catalogId]);
|
|
1152
|
+
}, [pathname, search, baseUrl, schema, catalogId, publishableKey]);
|
|
1025
1153
|
const go = (0, import_react.useCallback)(
|
|
1026
1154
|
(href) => {
|
|
1027
1155
|
if (href && href !== currentHref(pathname, search)) router.push(href);
|
|
@@ -1134,8 +1262,200 @@ function usePublicSearch(options) {
|
|
|
1134
1262
|
locationNames
|
|
1135
1263
|
};
|
|
1136
1264
|
}
|
|
1265
|
+
|
|
1266
|
+
// src/react/use-public-photo-search.ts
|
|
1267
|
+
var import_react2 = require("react");
|
|
1268
|
+
|
|
1269
|
+
// src/fetchers-photo-search.ts
|
|
1270
|
+
var PHOTO_SEARCH_MIN_QUERY_LENGTH = 3;
|
|
1271
|
+
var EMPTY_RESULT = {
|
|
1272
|
+
available: true,
|
|
1273
|
+
items: [],
|
|
1274
|
+
total: 0,
|
|
1275
|
+
page: 1,
|
|
1276
|
+
limit: 12
|
|
1277
|
+
};
|
|
1278
|
+
function positiveInt(value) {
|
|
1279
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value < 1) {
|
|
1280
|
+
return null;
|
|
1281
|
+
}
|
|
1282
|
+
return Math.floor(value);
|
|
1283
|
+
}
|
|
1284
|
+
function photoSearchUrl(siteId, params, baseUrl) {
|
|
1285
|
+
const search = new URLSearchParams();
|
|
1286
|
+
search.set("q", params.q.trim());
|
|
1287
|
+
const limit = positiveInt(params.limit);
|
|
1288
|
+
if (limit != null) search.set("limit", String(limit));
|
|
1289
|
+
const page = positiveInt(params.page);
|
|
1290
|
+
if (page != null) search.set("page", String(page));
|
|
1291
|
+
return `${normalizeBaseUrl(baseUrl)}/api/sites/${encodeURIComponent(
|
|
1292
|
+
siteId
|
|
1293
|
+
)}/search/photos?${search.toString()}`;
|
|
1294
|
+
}
|
|
1295
|
+
async function searchListingPhotos(siteId, params, options = {}) {
|
|
1296
|
+
const q = (params?.q ?? "").trim();
|
|
1297
|
+
const limit = positiveInt(params?.limit) ?? EMPTY_RESULT.limit;
|
|
1298
|
+
if (q.length < PHOTO_SEARCH_MIN_QUERY_LENGTH) {
|
|
1299
|
+
return { ...EMPTY_RESULT, limit, page: positiveInt(params?.page) ?? 1 };
|
|
1300
|
+
}
|
|
1301
|
+
const resolvedSiteId = await coerceSiteId(siteId);
|
|
1302
|
+
const url = photoSearchUrl(resolvedSiteId, { ...params, q }, options.baseUrl);
|
|
1303
|
+
const data = await callJson(
|
|
1304
|
+
url,
|
|
1305
|
+
{
|
|
1306
|
+
method: "GET",
|
|
1307
|
+
headers: { Accept: "application/json" },
|
|
1308
|
+
next: options.next
|
|
1309
|
+
},
|
|
1310
|
+
options.fetch ?? fetch
|
|
1311
|
+
);
|
|
1312
|
+
const items = Array.isArray(data.items) ? data.items : [];
|
|
1313
|
+
return {
|
|
1314
|
+
available: data.available !== false,
|
|
1315
|
+
items,
|
|
1316
|
+
total: typeof data.total === "number" ? data.total : items.length,
|
|
1317
|
+
page: typeof data.page === "number" ? data.page : positiveInt(params.page) ?? 1,
|
|
1318
|
+
limit: typeof data.limit === "number" ? data.limit : limit
|
|
1319
|
+
};
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1322
|
+
// src/photo-search-cover.ts
|
|
1323
|
+
var MATCHED_PHOTO_COVER_MAX_DISTANCE = 0.4;
|
|
1324
|
+
function resolveMatchedCoverPhoto(photo, maxDistance = MATCHED_PHOTO_COVER_MAX_DISTANCE) {
|
|
1325
|
+
if (!photo || !photo.src) return null;
|
|
1326
|
+
if (photo.distance == null) return photo;
|
|
1327
|
+
if (!Number.isFinite(photo.distance)) return null;
|
|
1328
|
+
return photo.distance <= maxDistance ? photo : null;
|
|
1329
|
+
}
|
|
1330
|
+
function itemsOf(photoResults) {
|
|
1331
|
+
if (!photoResults) return [];
|
|
1332
|
+
return Array.isArray(photoResults) ? photoResults : photoResults.items ?? [];
|
|
1333
|
+
}
|
|
1334
|
+
function applyMatchedPhotoCover(cards, photoResults, options = {}) {
|
|
1335
|
+
const maxDistance = options.maxDistance ?? MATCHED_PHOTO_COVER_MAX_DISTANCE;
|
|
1336
|
+
const photoByListingId = /* @__PURE__ */ new Map();
|
|
1337
|
+
for (const item of itemsOf(photoResults)) {
|
|
1338
|
+
const id = item?.listing?.id;
|
|
1339
|
+
if (!id || photoByListingId.has(id)) continue;
|
|
1340
|
+
const photo = resolveMatchedCoverPhoto(item.matchedPhoto, maxDistance);
|
|
1341
|
+
if (photo) photoByListingId.set(id, photo);
|
|
1342
|
+
}
|
|
1343
|
+
return cards.map((card) => {
|
|
1344
|
+
const photo = photoByListingId.get(card.id) ?? null;
|
|
1345
|
+
if (!photo) return { ...card, matchedPhoto: null };
|
|
1346
|
+
return {
|
|
1347
|
+
...card,
|
|
1348
|
+
coverImage: {
|
|
1349
|
+
src: photo.src,
|
|
1350
|
+
alt: photo.alt || card.coverImage?.alt || card.title
|
|
1351
|
+
},
|
|
1352
|
+
matchedPhoto: photo
|
|
1353
|
+
};
|
|
1354
|
+
});
|
|
1355
|
+
}
|
|
1356
|
+
|
|
1357
|
+
// src/react/use-public-photo-search.ts
|
|
1358
|
+
var PHOTO_SEARCH_DEBOUNCE_MS = 400;
|
|
1359
|
+
var EMPTY = {
|
|
1360
|
+
available: true,
|
|
1361
|
+
items: [],
|
|
1362
|
+
total: 0,
|
|
1363
|
+
page: 1,
|
|
1364
|
+
limit: 12
|
|
1365
|
+
};
|
|
1366
|
+
async function resolveSiteIdInput2(siteId) {
|
|
1367
|
+
const value = typeof siteId === "function" ? siteId() : siteId;
|
|
1368
|
+
return coerceSiteId(value);
|
|
1369
|
+
}
|
|
1370
|
+
function usePublicPhotoSearch(options) {
|
|
1371
|
+
const {
|
|
1372
|
+
siteId,
|
|
1373
|
+
baseUrl,
|
|
1374
|
+
limit,
|
|
1375
|
+
page,
|
|
1376
|
+
enabled = true,
|
|
1377
|
+
debounceMs = PHOTO_SEARCH_DEBOUNCE_MS,
|
|
1378
|
+
fetch: fetchImpl
|
|
1379
|
+
} = options;
|
|
1380
|
+
const query = (options.query ?? "").trim();
|
|
1381
|
+
const searchable = enabled && query.length >= PHOTO_SEARCH_MIN_QUERY_LENGTH;
|
|
1382
|
+
const siteIdRef = (0, import_react2.useRef)(siteId);
|
|
1383
|
+
siteIdRef.current = siteId;
|
|
1384
|
+
const siteIdToken = typeof siteId === "string" ? siteId : null;
|
|
1385
|
+
const fetchRef = (0, import_react2.useRef)(fetchImpl);
|
|
1386
|
+
fetchRef.current = fetchImpl;
|
|
1387
|
+
const requestRef = (0, import_react2.useRef)(0);
|
|
1388
|
+
const [result, setResult] = (0, import_react2.useState)(EMPTY);
|
|
1389
|
+
const [answered, setAnswered] = (0, import_react2.useState)("");
|
|
1390
|
+
const [loading, setLoading] = (0, import_react2.useState)(searchable);
|
|
1391
|
+
const [error, setError] = (0, import_react2.useState)(null);
|
|
1392
|
+
(0, import_react2.useEffect)(() => {
|
|
1393
|
+
const requestId = ++requestRef.current;
|
|
1394
|
+
if (!searchable) {
|
|
1395
|
+
setResult(EMPTY);
|
|
1396
|
+
setAnswered("");
|
|
1397
|
+
setLoading(false);
|
|
1398
|
+
setError(null);
|
|
1399
|
+
return;
|
|
1400
|
+
}
|
|
1401
|
+
setLoading(true);
|
|
1402
|
+
let active = true;
|
|
1403
|
+
let timer = null;
|
|
1404
|
+
const run = () => {
|
|
1405
|
+
void (async () => {
|
|
1406
|
+
try {
|
|
1407
|
+
const id = await resolveSiteIdInput2(siteIdRef.current);
|
|
1408
|
+
const next = await searchListingPhotos(
|
|
1409
|
+
id,
|
|
1410
|
+
{ q: query, limit, page },
|
|
1411
|
+
{ baseUrl, fetch: fetchRef.current }
|
|
1412
|
+
);
|
|
1413
|
+
if (!active || requestId !== requestRef.current) return;
|
|
1414
|
+
setResult(next);
|
|
1415
|
+
setAnswered(query);
|
|
1416
|
+
setError(null);
|
|
1417
|
+
} catch (caught) {
|
|
1418
|
+
if (!active || requestId !== requestRef.current) return;
|
|
1419
|
+
setResult(EMPTY);
|
|
1420
|
+
setAnswered(query);
|
|
1421
|
+
setError(
|
|
1422
|
+
caught instanceof Error ? caught : new Error(String(caught))
|
|
1423
|
+
);
|
|
1424
|
+
} finally {
|
|
1425
|
+
if (active && requestId === requestRef.current) setLoading(false);
|
|
1426
|
+
}
|
|
1427
|
+
})();
|
|
1428
|
+
};
|
|
1429
|
+
if (debounceMs <= 0) run();
|
|
1430
|
+
else timer = setTimeout(run, debounceMs);
|
|
1431
|
+
return () => {
|
|
1432
|
+
active = false;
|
|
1433
|
+
if (timer != null) clearTimeout(timer);
|
|
1434
|
+
};
|
|
1435
|
+
}, [searchable, query, limit, page, baseUrl, siteIdToken, debounceMs]);
|
|
1436
|
+
const listings = (0, import_react2.useMemo)(
|
|
1437
|
+
() => applyMatchedPhotoCover(
|
|
1438
|
+
result.items.map((item) => item.listing),
|
|
1439
|
+
result.items
|
|
1440
|
+
),
|
|
1441
|
+
[result.items]
|
|
1442
|
+
);
|
|
1443
|
+
return {
|
|
1444
|
+
available: result.available,
|
|
1445
|
+
items: result.items,
|
|
1446
|
+
listings,
|
|
1447
|
+
total: result.total,
|
|
1448
|
+
page: result.page,
|
|
1449
|
+
limit: result.limit,
|
|
1450
|
+
loading,
|
|
1451
|
+
error,
|
|
1452
|
+
query: answered
|
|
1453
|
+
};
|
|
1454
|
+
}
|
|
1137
1455
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1138
1456
|
0 && (module.exports = {
|
|
1457
|
+
PHOTO_SEARCH_DEBOUNCE_MS,
|
|
1458
|
+
usePublicPhotoSearch,
|
|
1139
1459
|
usePublicSearch
|
|
1140
1460
|
});
|
|
1141
1461
|
//# sourceMappingURL=react.js.map
|