@garagem-ai/site-sdk 0.6.0 → 0.7.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 +91 -0
- package/dist/index.d.mts +330 -166
- package/dist/index.d.ts +330 -166
- package/dist/index.js +365 -24
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +350 -24
- package/dist/index.mjs.map +1 -1
- package/dist/react.d.mts +41 -2
- package/dist/react.d.ts +41 -2
- package/dist/react.js +194 -0
- package/dist/react.js.map +1 -1
- package/dist/react.mjs +192 -0
- package/dist/react.mjs.map +1 -1
- package/dist/{search-engine-CtnLzmwW.d.mts → types-photo-search-OeONNDxl.d.mts} +204 -1
- package/dist/{search-engine-CtnLzmwW.d.ts → types-photo-search-OeONNDxl.d.ts} +204 -1
- package/package.json +2 -1
- package/src/fetchers-market.ts +216 -0
- package/src/fetchers-photo-search.ts +101 -0
- package/src/index.ts +61 -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/use-public-photo-search.ts +165 -0
- package/src/studio-contract.ts +125 -23
- package/src/types-market.ts +138 -0
- package/src/types-photo-search.ts +55 -0
- package/src/types.ts +7 -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-OeONNDxl.mjs';
|
|
2
2
|
|
|
3
3
|
type SiteIdInput = string | Promise<string> | (() => string | Promise<string>);
|
|
4
4
|
type PublicSearchSnapshot = {
|
|
@@ -51,4 +51,43 @@ type PublicSearchState = {
|
|
|
51
51
|
|
|
52
52
|
declare function usePublicSearch(options: UsePublicSearchOptions): PublicSearchState;
|
|
53
53
|
|
|
54
|
-
|
|
54
|
+
/** Same debounce as the CRM "Foto que combina com a busca" evidence lane. */
|
|
55
|
+
declare const PHOTO_SEARCH_DEBOUNCE_MS = 400;
|
|
56
|
+
type UsePublicPhotoSearchOptions = {
|
|
57
|
+
siteId: SiteIdInput;
|
|
58
|
+
baseUrl?: string;
|
|
59
|
+
/**
|
|
60
|
+
* The text to search by (usually the main search box). The hook trims it,
|
|
61
|
+
* debounces requests and ignores responses that arrive out of order.
|
|
62
|
+
*/
|
|
63
|
+
query: string;
|
|
64
|
+
/** Listings per page (API default 12, cap 24). */
|
|
65
|
+
limit?: number;
|
|
66
|
+
/** 1-based page. */
|
|
67
|
+
page?: number;
|
|
68
|
+
/** `false` pauses the hook and clears its results (e.g. a hidden panel). */
|
|
69
|
+
enabled?: boolean;
|
|
70
|
+
/** Milliseconds to wait after the last `query` change. Default 400. */
|
|
71
|
+
debounceMs?: number;
|
|
72
|
+
/** Override the fetch implementation (testing, retries). */
|
|
73
|
+
fetch?: FetchLike;
|
|
74
|
+
};
|
|
75
|
+
type PublicPhotoSearchState = {
|
|
76
|
+
/** `false` once the API says the tenant has no photo search: hide the UI. */
|
|
77
|
+
available: boolean;
|
|
78
|
+
/** Nearest-first matches, each with its card and matched photo. */
|
|
79
|
+
items: PhotoSearchItem[];
|
|
80
|
+
/** `items` as cards with the matched photo already applied as cover. */
|
|
81
|
+
listings: ListingWithMatchedPhoto[];
|
|
82
|
+
total: number;
|
|
83
|
+
page: number;
|
|
84
|
+
limit: number;
|
|
85
|
+
/** A request is in flight (or scheduled by the debounce). */
|
|
86
|
+
loading: boolean;
|
|
87
|
+
error: Error | null;
|
|
88
|
+
/** The trimmed query the current `items` answer (`""` before the first result). */
|
|
89
|
+
query: string;
|
|
90
|
+
};
|
|
91
|
+
declare function usePublicPhotoSearch(options: UsePublicPhotoSearchOptions): PublicPhotoSearchState;
|
|
92
|
+
|
|
93
|
+
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-OeONNDxl.js';
|
|
2
2
|
|
|
3
3
|
type SiteIdInput = string | Promise<string> | (() => string | Promise<string>);
|
|
4
4
|
type PublicSearchSnapshot = {
|
|
@@ -51,4 +51,43 @@ type PublicSearchState = {
|
|
|
51
51
|
|
|
52
52
|
declare function usePublicSearch(options: UsePublicSearchOptions): PublicSearchState;
|
|
53
53
|
|
|
54
|
-
|
|
54
|
+
/** Same debounce as the CRM "Foto que combina com a busca" evidence lane. */
|
|
55
|
+
declare const PHOTO_SEARCH_DEBOUNCE_MS = 400;
|
|
56
|
+
type UsePublicPhotoSearchOptions = {
|
|
57
|
+
siteId: SiteIdInput;
|
|
58
|
+
baseUrl?: string;
|
|
59
|
+
/**
|
|
60
|
+
* The text to search by (usually the main search box). The hook trims it,
|
|
61
|
+
* debounces requests and ignores responses that arrive out of order.
|
|
62
|
+
*/
|
|
63
|
+
query: string;
|
|
64
|
+
/** Listings per page (API default 12, cap 24). */
|
|
65
|
+
limit?: number;
|
|
66
|
+
/** 1-based page. */
|
|
67
|
+
page?: number;
|
|
68
|
+
/** `false` pauses the hook and clears its results (e.g. a hidden panel). */
|
|
69
|
+
enabled?: boolean;
|
|
70
|
+
/** Milliseconds to wait after the last `query` change. Default 400. */
|
|
71
|
+
debounceMs?: number;
|
|
72
|
+
/** Override the fetch implementation (testing, retries). */
|
|
73
|
+
fetch?: FetchLike;
|
|
74
|
+
};
|
|
75
|
+
type PublicPhotoSearchState = {
|
|
76
|
+
/** `false` once the API says the tenant has no photo search: hide the UI. */
|
|
77
|
+
available: boolean;
|
|
78
|
+
/** Nearest-first matches, each with its card and matched photo. */
|
|
79
|
+
items: PhotoSearchItem[];
|
|
80
|
+
/** `items` as cards with the matched photo already applied as cover. */
|
|
81
|
+
listings: ListingWithMatchedPhoto[];
|
|
82
|
+
total: number;
|
|
83
|
+
page: number;
|
|
84
|
+
limit: number;
|
|
85
|
+
/** A request is in flight (or scheduled by the debounce). */
|
|
86
|
+
loading: boolean;
|
|
87
|
+
error: Error | null;
|
|
88
|
+
/** The trimmed query the current `items` answer (`""` before the first result). */
|
|
89
|
+
query: string;
|
|
90
|
+
};
|
|
91
|
+
declare function usePublicPhotoSearch(options: UsePublicPhotoSearchOptions): PublicPhotoSearchState;
|
|
92
|
+
|
|
93
|
+
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);
|
|
@@ -1134,8 +1136,200 @@ function usePublicSearch(options) {
|
|
|
1134
1136
|
locationNames
|
|
1135
1137
|
};
|
|
1136
1138
|
}
|
|
1139
|
+
|
|
1140
|
+
// src/react/use-public-photo-search.ts
|
|
1141
|
+
var import_react2 = require("react");
|
|
1142
|
+
|
|
1143
|
+
// src/fetchers-photo-search.ts
|
|
1144
|
+
var PHOTO_SEARCH_MIN_QUERY_LENGTH = 3;
|
|
1145
|
+
var EMPTY_RESULT = {
|
|
1146
|
+
available: true,
|
|
1147
|
+
items: [],
|
|
1148
|
+
total: 0,
|
|
1149
|
+
page: 1,
|
|
1150
|
+
limit: 12
|
|
1151
|
+
};
|
|
1152
|
+
function positiveInt(value) {
|
|
1153
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value < 1) {
|
|
1154
|
+
return null;
|
|
1155
|
+
}
|
|
1156
|
+
return Math.floor(value);
|
|
1157
|
+
}
|
|
1158
|
+
function photoSearchUrl(siteId, params, baseUrl) {
|
|
1159
|
+
const search = new URLSearchParams();
|
|
1160
|
+
search.set("q", params.q.trim());
|
|
1161
|
+
const limit = positiveInt(params.limit);
|
|
1162
|
+
if (limit != null) search.set("limit", String(limit));
|
|
1163
|
+
const page = positiveInt(params.page);
|
|
1164
|
+
if (page != null) search.set("page", String(page));
|
|
1165
|
+
return `${normalizeBaseUrl(baseUrl)}/api/sites/${encodeURIComponent(
|
|
1166
|
+
siteId
|
|
1167
|
+
)}/search/photos?${search.toString()}`;
|
|
1168
|
+
}
|
|
1169
|
+
async function searchListingPhotos(siteId, params, options = {}) {
|
|
1170
|
+
const q = (params?.q ?? "").trim();
|
|
1171
|
+
const limit = positiveInt(params?.limit) ?? EMPTY_RESULT.limit;
|
|
1172
|
+
if (q.length < PHOTO_SEARCH_MIN_QUERY_LENGTH) {
|
|
1173
|
+
return { ...EMPTY_RESULT, limit, page: positiveInt(params?.page) ?? 1 };
|
|
1174
|
+
}
|
|
1175
|
+
const resolvedSiteId = await coerceSiteId(siteId);
|
|
1176
|
+
const url = photoSearchUrl(resolvedSiteId, { ...params, q }, options.baseUrl);
|
|
1177
|
+
const data = await callJson(
|
|
1178
|
+
url,
|
|
1179
|
+
{
|
|
1180
|
+
method: "GET",
|
|
1181
|
+
headers: { Accept: "application/json" },
|
|
1182
|
+
next: options.next
|
|
1183
|
+
},
|
|
1184
|
+
options.fetch ?? fetch
|
|
1185
|
+
);
|
|
1186
|
+
const items = Array.isArray(data.items) ? data.items : [];
|
|
1187
|
+
return {
|
|
1188
|
+
available: data.available !== false,
|
|
1189
|
+
items,
|
|
1190
|
+
total: typeof data.total === "number" ? data.total : items.length,
|
|
1191
|
+
page: typeof data.page === "number" ? data.page : positiveInt(params.page) ?? 1,
|
|
1192
|
+
limit: typeof data.limit === "number" ? data.limit : limit
|
|
1193
|
+
};
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
// src/photo-search-cover.ts
|
|
1197
|
+
var MATCHED_PHOTO_COVER_MAX_DISTANCE = 0.4;
|
|
1198
|
+
function resolveMatchedCoverPhoto(photo, maxDistance = MATCHED_PHOTO_COVER_MAX_DISTANCE) {
|
|
1199
|
+
if (!photo || !photo.src) return null;
|
|
1200
|
+
if (photo.distance == null) return photo;
|
|
1201
|
+
if (!Number.isFinite(photo.distance)) return null;
|
|
1202
|
+
return photo.distance <= maxDistance ? photo : null;
|
|
1203
|
+
}
|
|
1204
|
+
function itemsOf(photoResults) {
|
|
1205
|
+
if (!photoResults) return [];
|
|
1206
|
+
return Array.isArray(photoResults) ? photoResults : photoResults.items ?? [];
|
|
1207
|
+
}
|
|
1208
|
+
function applyMatchedPhotoCover(cards, photoResults, options = {}) {
|
|
1209
|
+
const maxDistance = options.maxDistance ?? MATCHED_PHOTO_COVER_MAX_DISTANCE;
|
|
1210
|
+
const photoByListingId = /* @__PURE__ */ new Map();
|
|
1211
|
+
for (const item of itemsOf(photoResults)) {
|
|
1212
|
+
const id = item?.listing?.id;
|
|
1213
|
+
if (!id || photoByListingId.has(id)) continue;
|
|
1214
|
+
const photo = resolveMatchedCoverPhoto(item.matchedPhoto, maxDistance);
|
|
1215
|
+
if (photo) photoByListingId.set(id, photo);
|
|
1216
|
+
}
|
|
1217
|
+
return cards.map((card) => {
|
|
1218
|
+
const photo = photoByListingId.get(card.id) ?? null;
|
|
1219
|
+
if (!photo) return { ...card, matchedPhoto: null };
|
|
1220
|
+
return {
|
|
1221
|
+
...card,
|
|
1222
|
+
coverImage: {
|
|
1223
|
+
src: photo.src,
|
|
1224
|
+
alt: photo.alt || card.coverImage?.alt || card.title
|
|
1225
|
+
},
|
|
1226
|
+
matchedPhoto: photo
|
|
1227
|
+
};
|
|
1228
|
+
});
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1231
|
+
// src/react/use-public-photo-search.ts
|
|
1232
|
+
var PHOTO_SEARCH_DEBOUNCE_MS = 400;
|
|
1233
|
+
var EMPTY = {
|
|
1234
|
+
available: true,
|
|
1235
|
+
items: [],
|
|
1236
|
+
total: 0,
|
|
1237
|
+
page: 1,
|
|
1238
|
+
limit: 12
|
|
1239
|
+
};
|
|
1240
|
+
async function resolveSiteIdInput2(siteId) {
|
|
1241
|
+
const value = typeof siteId === "function" ? siteId() : siteId;
|
|
1242
|
+
return coerceSiteId(value);
|
|
1243
|
+
}
|
|
1244
|
+
function usePublicPhotoSearch(options) {
|
|
1245
|
+
const {
|
|
1246
|
+
siteId,
|
|
1247
|
+
baseUrl,
|
|
1248
|
+
limit,
|
|
1249
|
+
page,
|
|
1250
|
+
enabled = true,
|
|
1251
|
+
debounceMs = PHOTO_SEARCH_DEBOUNCE_MS,
|
|
1252
|
+
fetch: fetchImpl
|
|
1253
|
+
} = options;
|
|
1254
|
+
const query = (options.query ?? "").trim();
|
|
1255
|
+
const searchable = enabled && query.length >= PHOTO_SEARCH_MIN_QUERY_LENGTH;
|
|
1256
|
+
const siteIdRef = (0, import_react2.useRef)(siteId);
|
|
1257
|
+
siteIdRef.current = siteId;
|
|
1258
|
+
const siteIdToken = typeof siteId === "string" ? siteId : null;
|
|
1259
|
+
const fetchRef = (0, import_react2.useRef)(fetchImpl);
|
|
1260
|
+
fetchRef.current = fetchImpl;
|
|
1261
|
+
const requestRef = (0, import_react2.useRef)(0);
|
|
1262
|
+
const [result, setResult] = (0, import_react2.useState)(EMPTY);
|
|
1263
|
+
const [answered, setAnswered] = (0, import_react2.useState)("");
|
|
1264
|
+
const [loading, setLoading] = (0, import_react2.useState)(searchable);
|
|
1265
|
+
const [error, setError] = (0, import_react2.useState)(null);
|
|
1266
|
+
(0, import_react2.useEffect)(() => {
|
|
1267
|
+
const requestId = ++requestRef.current;
|
|
1268
|
+
if (!searchable) {
|
|
1269
|
+
setResult(EMPTY);
|
|
1270
|
+
setAnswered("");
|
|
1271
|
+
setLoading(false);
|
|
1272
|
+
setError(null);
|
|
1273
|
+
return;
|
|
1274
|
+
}
|
|
1275
|
+
setLoading(true);
|
|
1276
|
+
let active = true;
|
|
1277
|
+
let timer = null;
|
|
1278
|
+
const run = () => {
|
|
1279
|
+
void (async () => {
|
|
1280
|
+
try {
|
|
1281
|
+
const id = await resolveSiteIdInput2(siteIdRef.current);
|
|
1282
|
+
const next = await searchListingPhotos(
|
|
1283
|
+
id,
|
|
1284
|
+
{ q: query, limit, page },
|
|
1285
|
+
{ baseUrl, fetch: fetchRef.current }
|
|
1286
|
+
);
|
|
1287
|
+
if (!active || requestId !== requestRef.current) return;
|
|
1288
|
+
setResult(next);
|
|
1289
|
+
setAnswered(query);
|
|
1290
|
+
setError(null);
|
|
1291
|
+
} catch (caught) {
|
|
1292
|
+
if (!active || requestId !== requestRef.current) return;
|
|
1293
|
+
setResult(EMPTY);
|
|
1294
|
+
setAnswered(query);
|
|
1295
|
+
setError(
|
|
1296
|
+
caught instanceof Error ? caught : new Error(String(caught))
|
|
1297
|
+
);
|
|
1298
|
+
} finally {
|
|
1299
|
+
if (active && requestId === requestRef.current) setLoading(false);
|
|
1300
|
+
}
|
|
1301
|
+
})();
|
|
1302
|
+
};
|
|
1303
|
+
if (debounceMs <= 0) run();
|
|
1304
|
+
else timer = setTimeout(run, debounceMs);
|
|
1305
|
+
return () => {
|
|
1306
|
+
active = false;
|
|
1307
|
+
if (timer != null) clearTimeout(timer);
|
|
1308
|
+
};
|
|
1309
|
+
}, [searchable, query, limit, page, baseUrl, siteIdToken, debounceMs]);
|
|
1310
|
+
const listings = (0, import_react2.useMemo)(
|
|
1311
|
+
() => applyMatchedPhotoCover(
|
|
1312
|
+
result.items.map((item) => item.listing),
|
|
1313
|
+
result.items
|
|
1314
|
+
),
|
|
1315
|
+
[result.items]
|
|
1316
|
+
);
|
|
1317
|
+
return {
|
|
1318
|
+
available: result.available,
|
|
1319
|
+
items: result.items,
|
|
1320
|
+
listings,
|
|
1321
|
+
total: result.total,
|
|
1322
|
+
page: result.page,
|
|
1323
|
+
limit: result.limit,
|
|
1324
|
+
loading,
|
|
1325
|
+
error,
|
|
1326
|
+
query: answered
|
|
1327
|
+
};
|
|
1328
|
+
}
|
|
1137
1329
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1138
1330
|
0 && (module.exports = {
|
|
1331
|
+
PHOTO_SEARCH_DEBOUNCE_MS,
|
|
1332
|
+
usePublicPhotoSearch,
|
|
1139
1333
|
usePublicSearch
|
|
1140
1334
|
});
|
|
1141
1335
|
//# sourceMappingURL=react.js.map
|