@garagem-ai/site-sdk 0.6.1 → 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/dist/react.mjs CHANGED
@@ -1109,7 +1109,199 @@ function usePublicSearch(options) {
1109
1109
  locationNames
1110
1110
  };
1111
1111
  }
1112
+
1113
+ // src/react/use-public-photo-search.ts
1114
+ import { useEffect as useEffect2, useMemo as useMemo2, useRef as useRef2, useState as useState2 } from "react";
1115
+
1116
+ // src/fetchers-photo-search.ts
1117
+ var PHOTO_SEARCH_MIN_QUERY_LENGTH = 3;
1118
+ var EMPTY_RESULT = {
1119
+ available: true,
1120
+ items: [],
1121
+ total: 0,
1122
+ page: 1,
1123
+ limit: 12
1124
+ };
1125
+ function positiveInt(value) {
1126
+ if (typeof value !== "number" || !Number.isFinite(value) || value < 1) {
1127
+ return null;
1128
+ }
1129
+ return Math.floor(value);
1130
+ }
1131
+ function photoSearchUrl(siteId, params, baseUrl) {
1132
+ const search = new URLSearchParams();
1133
+ search.set("q", params.q.trim());
1134
+ const limit = positiveInt(params.limit);
1135
+ if (limit != null) search.set("limit", String(limit));
1136
+ const page = positiveInt(params.page);
1137
+ if (page != null) search.set("page", String(page));
1138
+ return `${normalizeBaseUrl(baseUrl)}/api/sites/${encodeURIComponent(
1139
+ siteId
1140
+ )}/search/photos?${search.toString()}`;
1141
+ }
1142
+ async function searchListingPhotos(siteId, params, options = {}) {
1143
+ const q = (params?.q ?? "").trim();
1144
+ const limit = positiveInt(params?.limit) ?? EMPTY_RESULT.limit;
1145
+ if (q.length < PHOTO_SEARCH_MIN_QUERY_LENGTH) {
1146
+ return { ...EMPTY_RESULT, limit, page: positiveInt(params?.page) ?? 1 };
1147
+ }
1148
+ const resolvedSiteId = await coerceSiteId(siteId);
1149
+ const url = photoSearchUrl(resolvedSiteId, { ...params, q }, options.baseUrl);
1150
+ const data = await callJson(
1151
+ url,
1152
+ {
1153
+ method: "GET",
1154
+ headers: { Accept: "application/json" },
1155
+ next: options.next
1156
+ },
1157
+ options.fetch ?? fetch
1158
+ );
1159
+ const items = Array.isArray(data.items) ? data.items : [];
1160
+ return {
1161
+ available: data.available !== false,
1162
+ items,
1163
+ total: typeof data.total === "number" ? data.total : items.length,
1164
+ page: typeof data.page === "number" ? data.page : positiveInt(params.page) ?? 1,
1165
+ limit: typeof data.limit === "number" ? data.limit : limit
1166
+ };
1167
+ }
1168
+
1169
+ // src/photo-search-cover.ts
1170
+ var MATCHED_PHOTO_COVER_MAX_DISTANCE = 0.4;
1171
+ function resolveMatchedCoverPhoto(photo, maxDistance = MATCHED_PHOTO_COVER_MAX_DISTANCE) {
1172
+ if (!photo || !photo.src) return null;
1173
+ if (photo.distance == null) return photo;
1174
+ if (!Number.isFinite(photo.distance)) return null;
1175
+ return photo.distance <= maxDistance ? photo : null;
1176
+ }
1177
+ function itemsOf(photoResults) {
1178
+ if (!photoResults) return [];
1179
+ return Array.isArray(photoResults) ? photoResults : photoResults.items ?? [];
1180
+ }
1181
+ function applyMatchedPhotoCover(cards, photoResults, options = {}) {
1182
+ const maxDistance = options.maxDistance ?? MATCHED_PHOTO_COVER_MAX_DISTANCE;
1183
+ const photoByListingId = /* @__PURE__ */ new Map();
1184
+ for (const item of itemsOf(photoResults)) {
1185
+ const id = item?.listing?.id;
1186
+ if (!id || photoByListingId.has(id)) continue;
1187
+ const photo = resolveMatchedCoverPhoto(item.matchedPhoto, maxDistance);
1188
+ if (photo) photoByListingId.set(id, photo);
1189
+ }
1190
+ return cards.map((card) => {
1191
+ const photo = photoByListingId.get(card.id) ?? null;
1192
+ if (!photo) return { ...card, matchedPhoto: null };
1193
+ return {
1194
+ ...card,
1195
+ coverImage: {
1196
+ src: photo.src,
1197
+ alt: photo.alt || card.coverImage?.alt || card.title
1198
+ },
1199
+ matchedPhoto: photo
1200
+ };
1201
+ });
1202
+ }
1203
+
1204
+ // src/react/use-public-photo-search.ts
1205
+ var PHOTO_SEARCH_DEBOUNCE_MS = 400;
1206
+ var EMPTY = {
1207
+ available: true,
1208
+ items: [],
1209
+ total: 0,
1210
+ page: 1,
1211
+ limit: 12
1212
+ };
1213
+ async function resolveSiteIdInput2(siteId) {
1214
+ const value = typeof siteId === "function" ? siteId() : siteId;
1215
+ return coerceSiteId(value);
1216
+ }
1217
+ function usePublicPhotoSearch(options) {
1218
+ const {
1219
+ siteId,
1220
+ baseUrl,
1221
+ limit,
1222
+ page,
1223
+ enabled = true,
1224
+ debounceMs = PHOTO_SEARCH_DEBOUNCE_MS,
1225
+ fetch: fetchImpl
1226
+ } = options;
1227
+ const query = (options.query ?? "").trim();
1228
+ const searchable = enabled && query.length >= PHOTO_SEARCH_MIN_QUERY_LENGTH;
1229
+ const siteIdRef = useRef2(siteId);
1230
+ siteIdRef.current = siteId;
1231
+ const siteIdToken = typeof siteId === "string" ? siteId : null;
1232
+ const fetchRef = useRef2(fetchImpl);
1233
+ fetchRef.current = fetchImpl;
1234
+ const requestRef = useRef2(0);
1235
+ const [result, setResult] = useState2(EMPTY);
1236
+ const [answered, setAnswered] = useState2("");
1237
+ const [loading, setLoading] = useState2(searchable);
1238
+ const [error, setError] = useState2(null);
1239
+ useEffect2(() => {
1240
+ const requestId = ++requestRef.current;
1241
+ if (!searchable) {
1242
+ setResult(EMPTY);
1243
+ setAnswered("");
1244
+ setLoading(false);
1245
+ setError(null);
1246
+ return;
1247
+ }
1248
+ setLoading(true);
1249
+ let active = true;
1250
+ let timer = null;
1251
+ const run = () => {
1252
+ void (async () => {
1253
+ try {
1254
+ const id = await resolveSiteIdInput2(siteIdRef.current);
1255
+ const next = await searchListingPhotos(
1256
+ id,
1257
+ { q: query, limit, page },
1258
+ { baseUrl, fetch: fetchRef.current }
1259
+ );
1260
+ if (!active || requestId !== requestRef.current) return;
1261
+ setResult(next);
1262
+ setAnswered(query);
1263
+ setError(null);
1264
+ } catch (caught) {
1265
+ if (!active || requestId !== requestRef.current) return;
1266
+ setResult(EMPTY);
1267
+ setAnswered(query);
1268
+ setError(
1269
+ caught instanceof Error ? caught : new Error(String(caught))
1270
+ );
1271
+ } finally {
1272
+ if (active && requestId === requestRef.current) setLoading(false);
1273
+ }
1274
+ })();
1275
+ };
1276
+ if (debounceMs <= 0) run();
1277
+ else timer = setTimeout(run, debounceMs);
1278
+ return () => {
1279
+ active = false;
1280
+ if (timer != null) clearTimeout(timer);
1281
+ };
1282
+ }, [searchable, query, limit, page, baseUrl, siteIdToken, debounceMs]);
1283
+ const listings = useMemo2(
1284
+ () => applyMatchedPhotoCover(
1285
+ result.items.map((item) => item.listing),
1286
+ result.items
1287
+ ),
1288
+ [result.items]
1289
+ );
1290
+ return {
1291
+ available: result.available,
1292
+ items: result.items,
1293
+ listings,
1294
+ total: result.total,
1295
+ page: result.page,
1296
+ limit: result.limit,
1297
+ loading,
1298
+ error,
1299
+ query: answered
1300
+ };
1301
+ }
1112
1302
  export {
1303
+ PHOTO_SEARCH_DEBOUNCE_MS,
1304
+ usePublicPhotoSearch,
1113
1305
  usePublicSearch
1114
1306
  };
1115
1307
  //# sourceMappingURL=react.mjs.map