@garagem-ai/site-sdk 0.5.3 → 0.6.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.d.mts CHANGED
@@ -1,9 +1,25 @@
1
- import { e as SearchUi, n as Listing, o as SearchPathFilters, A as AppliedSearchQuery, x as LocationNameTables } from './search-engine-CkbO77G8.mjs';
1
+ import { e as SearchUi, n as Listing, x as LocationNameTables, o as SearchPathFilters, A as AppliedSearchQuery } from './search-engine-CtnLzmwW.mjs';
2
2
 
3
3
  type SiteIdInput = string | Promise<string> | (() => string | Promise<string>);
4
+ type PublicSearchSnapshot = {
5
+ schema?: SearchUi | null;
6
+ listings?: Listing[];
7
+ page?: number;
8
+ total?: number;
9
+ totalPages?: number;
10
+ locationNames?: LocationNameTables | null;
11
+ catalogId?: string | null;
12
+ };
4
13
  type UsePublicSearchOptions = {
5
14
  siteId: SiteIdInput;
6
15
  baseUrl?: string;
16
+ /**
17
+ * Query string without `?`, from the server page's `searchParams`.
18
+ * The hook must not call Next's search-params hook: that suspends the
19
+ * tree, and Next leaves the resolved HTML in a hidden node until JS runs.
20
+ */
21
+ search?: string;
22
+ initial?: PublicSearchSnapshot;
7
23
  };
8
24
  type PublicSearchState = {
9
25
  schema: SearchUi | null;
@@ -35,4 +51,4 @@ type PublicSearchState = {
35
51
 
36
52
  declare function usePublicSearch(options: UsePublicSearchOptions): PublicSearchState;
37
53
 
38
- export { type PublicSearchState, type SiteIdInput, type UsePublicSearchOptions, usePublicSearch };
54
+ export { type PublicSearchSnapshot, type PublicSearchState, type SiteIdInput, type UsePublicSearchOptions, usePublicSearch };
package/dist/react.d.ts CHANGED
@@ -1,9 +1,25 @@
1
- import { e as SearchUi, n as Listing, o as SearchPathFilters, A as AppliedSearchQuery, x as LocationNameTables } from './search-engine-CkbO77G8.js';
1
+ import { e as SearchUi, n as Listing, x as LocationNameTables, o as SearchPathFilters, A as AppliedSearchQuery } from './search-engine-CtnLzmwW.js';
2
2
 
3
3
  type SiteIdInput = string | Promise<string> | (() => string | Promise<string>);
4
+ type PublicSearchSnapshot = {
5
+ schema?: SearchUi | null;
6
+ listings?: Listing[];
7
+ page?: number;
8
+ total?: number;
9
+ totalPages?: number;
10
+ locationNames?: LocationNameTables | null;
11
+ catalogId?: string | null;
12
+ };
4
13
  type UsePublicSearchOptions = {
5
14
  siteId: SiteIdInput;
6
15
  baseUrl?: string;
16
+ /**
17
+ * Query string without `?`, from the server page's `searchParams`.
18
+ * The hook must not call Next's search-params hook: that suspends the
19
+ * tree, and Next leaves the resolved HTML in a hidden node until JS runs.
20
+ */
21
+ search?: string;
22
+ initial?: PublicSearchSnapshot;
7
23
  };
8
24
  type PublicSearchState = {
9
25
  schema: SearchUi | null;
@@ -35,4 +51,4 @@ type PublicSearchState = {
35
51
 
36
52
  declare function usePublicSearch(options: UsePublicSearchOptions): PublicSearchState;
37
53
 
38
- export { type PublicSearchState, type SiteIdInput, type UsePublicSearchOptions, usePublicSearch };
54
+ export { type PublicSearchSnapshot, type PublicSearchState, type SiteIdInput, type UsePublicSearchOptions, usePublicSearch };
package/dist/react.js CHANGED
@@ -736,6 +736,71 @@ function clearStagedPathFilters(current, keepFilterIds = []) {
736
736
  return next;
737
737
  }
738
738
 
739
+ // src/react/public-search-snapshot.ts
740
+ function hrefParts(href) {
741
+ const q = href.indexOf("?");
742
+ if (q < 0) return { pathname: href, search: "" };
743
+ return { pathname: href.slice(0, q), search: href.slice(q + 1) };
744
+ }
745
+ function searchHrefKey(pathname, search) {
746
+ const qs = search.replace(/^\?/, "");
747
+ return qs ? `${pathname}?${qs}` : pathname;
748
+ }
749
+ function searchKeysRepresentSameQuery(a, b, dictionary) {
750
+ if (a == null) return false;
751
+ if (a === b) return true;
752
+ const left = hrefParts(a);
753
+ const right = hrefParts(b);
754
+ const leftCanon = canonicalizeSearchHref(left.pathname, left.search, dictionary) ?? searchHrefKey(left.pathname, left.search);
755
+ const rightCanon = canonicalizeSearchHref(right.pathname, right.search, dictionary) ?? searchHrefKey(right.pathname, right.search);
756
+ return leftCanon === rightCanon;
757
+ }
758
+ function hasTrustedSearchSnapshot(initial) {
759
+ return Boolean(initial?.schema || initial?.listings?.length);
760
+ }
761
+ function resolveSeedTotal(initial) {
762
+ if (typeof initial?.total === "number") return initial.total;
763
+ return initial?.listings?.length ?? 0;
764
+ }
765
+ function preferFetchedLocationNames(previous, fetched) {
766
+ return fetched ?? previous;
767
+ }
768
+ function shouldClearPaintedSearchOnSchemaFailure(keepSnapshot) {
769
+ return !keepSnapshot;
770
+ }
771
+ function shouldAdvancePaintedSearchKeyOnCanonicalize() {
772
+ return false;
773
+ }
774
+ function shouldSetLoadingOnSearchKeyChange(options) {
775
+ return !searchKeysRepresentSameQuery(
776
+ options.paintedSearchKey,
777
+ options.searchKey,
778
+ options.dictionary
779
+ );
780
+ }
781
+ function shouldPreservePaintedListingsOnRefetchFailure(options) {
782
+ return searchKeysRepresentSameQuery(
783
+ options.paintedSearchKey,
784
+ options.searchKey,
785
+ options.dictionary
786
+ );
787
+ }
788
+ function initialQueryDraft(pathname, search, dictionary) {
789
+ return readPublicSearch(pathname, search, dictionary).applied.query || "";
790
+ }
791
+ function seedPublicSearchState(initial) {
792
+ return {
793
+ schema: initial?.schema ?? null,
794
+ listings: initial?.listings ?? [],
795
+ page: initial?.page ?? 1,
796
+ total: resolveSeedTotal(initial),
797
+ totalPages: initial?.totalPages ?? 1,
798
+ locationNames: initial?.locationNames ?? null,
799
+ catalogId: initial?.catalogId ?? null,
800
+ loading: !hasTrustedSearchSnapshot(initial)
801
+ };
802
+ }
803
+
739
804
  // src/react/staged-filters-mirror.ts
740
805
  var StagedFiltersMirror = class {
741
806
  current;
@@ -768,26 +833,33 @@ function currentHref(pathname, search) {
768
833
  return search ? `${pathname}?${search}` : pathname;
769
834
  }
770
835
  function usePublicSearch(options) {
771
- const { siteId, baseUrl } = options;
836
+ const { siteId, baseUrl, initial, search: searchFromServer = "" } = options;
772
837
  const router = (0, import_navigation.useRouter)();
773
838
  const pathname = (0, import_navigation.usePathname)() || "/imoveis";
774
- const searchParams = (0, import_navigation.useSearchParams)();
775
- const search = searchParams.toString();
839
+ const search = searchFromServer;
840
+ const searchKey = search ? `${pathname}?${search}` : pathname;
841
+ const [seed] = (0, import_react.useState)(() => seedPublicSearchState(initial));
842
+ const keepSnapshotRef = (0, import_react.useRef)(hasTrustedSearchSnapshot(initial));
843
+ const lastSearchKeyRef = (0, import_react.useRef)(keepSnapshotRef.current ? searchKey : null);
776
844
  const siteIdRef = (0, import_react.useRef)(siteId);
777
845
  siteIdRef.current = siteId;
778
846
  const siteIdToken = typeof siteId === "string" ? siteId : null;
779
- const [catalogId, setCatalogId] = (0, import_react.useState)(null);
780
- const [schema, setSchema] = (0, import_react.useState)(null);
781
- const [listings, setListings] = (0, import_react.useState)([]);
782
- const [page, setPage] = (0, import_react.useState)(1);
783
- const [total, setTotal] = (0, import_react.useState)(0);
784
- const [totalPages, setTotalPages] = (0, import_react.useState)(1);
785
- const [loading, setLoading] = (0, import_react.useState)(true);
786
- const [qDraft, setQDraft] = (0, import_react.useState)("");
847
+ const [catalogId, setCatalogId] = (0, import_react.useState)(seed.catalogId);
848
+ const [schema, setSchema] = (0, import_react.useState)(seed.schema);
849
+ const [listings, setListings] = (0, import_react.useState)(
850
+ seed.listings
851
+ );
852
+ const [page, setPage] = (0, import_react.useState)(seed.page);
853
+ const [total, setTotal] = (0, import_react.useState)(seed.total);
854
+ const [totalPages, setTotalPages] = (0, import_react.useState)(seed.totalPages);
855
+ const [loading, setLoading] = (0, import_react.useState)(seed.loading);
856
+ const [qDraft, setQDraft] = (0, import_react.useState)(
857
+ () => initialQueryDraft(pathname, search, initial?.schema?.dictionary)
858
+ );
787
859
  const stagedMirrorRef = (0, import_react.useRef)(new StagedFiltersMirror());
788
860
  const [staged, setStagedState] = (0, import_react.useState)({});
789
861
  const [locationNames, setLocationNames] = (0, import_react.useState)(
790
- null
862
+ seed.locationNames
791
863
  );
792
864
  const replaceStaged = (0, import_react.useCallback)((next) => {
793
865
  stagedMirrorRef.current.replace(next);
@@ -806,7 +878,7 @@ function usePublicSearch(options) {
806
878
  (0, import_react.useEffect)(() => {
807
879
  replaceStaged({ ...parsed.pathFilters });
808
880
  }, [parsed.pathFilters, replaceStaged]);
809
- const locationsRef = (0, import_react.useRef)(null);
881
+ const locationsRef = (0, import_react.useRef)(seed.locationNames);
810
882
  (0, import_react.useEffect)(() => {
811
883
  if (!schema) return;
812
884
  const canonical = canonicalizeSearchHref(
@@ -814,14 +886,20 @@ function usePublicSearch(options) {
814
886
  search,
815
887
  schema.dictionary
816
888
  );
817
- if (canonical) router.replace(canonical);
889
+ if (!canonical) return;
890
+ if (shouldAdvancePaintedSearchKeyOnCanonicalize()) {
891
+ lastSearchKeyRef.current = canonical;
892
+ }
893
+ router.replace(canonical);
818
894
  }, [pathname, search, schema, router]);
819
895
  (0, import_react.useEffect)(() => {
820
896
  let active = true;
821
- setCatalogId(null);
822
- setSchema(null);
823
- setListings([]);
824
- setLoading(true);
897
+ if (!keepSnapshotRef.current) {
898
+ setCatalogId(null);
899
+ setSchema(null);
900
+ setListings([]);
901
+ setLoading(true);
902
+ }
825
903
  resolveSiteIdInput(siteIdRef.current).then((id) => {
826
904
  if (active) setCatalogId(id);
827
905
  }).catch(() => {
@@ -835,11 +913,13 @@ function usePublicSearch(options) {
835
913
  if (!catalogId) return;
836
914
  const id = catalogId;
837
915
  let active = true;
838
- locationsRef.current = null;
839
- setLocationNames(null);
840
- setSchema(null);
841
- setListings([]);
842
- setLoading(true);
916
+ if (!keepSnapshotRef.current) {
917
+ locationsRef.current = null;
918
+ setLocationNames(null);
919
+ setSchema(null);
920
+ setListings([]);
921
+ setLoading(true);
922
+ }
843
923
  async function loadSchema() {
844
924
  try {
845
925
  const nextSchema = await getSearchUi({ siteId: id, baseUrl });
@@ -858,14 +938,22 @@ function usePublicSearch(options) {
858
938
  }
859
939
  }
860
940
  if (!active) return;
861
- locationsRef.current = locations;
862
- setLocationNames(locations);
941
+ const nextLocations = preferFetchedLocationNames(
942
+ locationsRef.current,
943
+ locations
944
+ );
945
+ locationsRef.current = nextLocations;
946
+ setLocationNames(nextLocations);
863
947
  setSchema(nextSchema);
948
+ keepSnapshotRef.current = false;
864
949
  } catch {
865
950
  if (active) {
866
- locationsRef.current = null;
867
- setLocationNames(null);
868
- setSchema(null);
951
+ if (shouldClearPaintedSearchOnSchemaFailure(keepSnapshotRef.current)) {
952
+ keepSnapshotRef.current = false;
953
+ locationsRef.current = null;
954
+ setLocationNames(null);
955
+ setSchema(null);
956
+ }
869
957
  setLoading(false);
870
958
  }
871
959
  }
@@ -881,7 +969,13 @@ function usePublicSearch(options) {
881
969
  const id = catalogId;
882
970
  let active = true;
883
971
  async function loadListings() {
884
- setLoading(true);
972
+ if (shouldSetLoadingOnSearchKeyChange({
973
+ paintedSearchKey: lastSearchKeyRef.current,
974
+ searchKey,
975
+ dictionary: currentSchema.dictionary
976
+ })) {
977
+ setLoading(true);
978
+ }
885
979
  try {
886
980
  const { pathFilters, applied } = readPublicSearch(
887
981
  pathname,
@@ -903,15 +997,24 @@ function usePublicSearch(options) {
903
997
  setPage(response.page || 1);
904
998
  setTotal(response.total || 0);
905
999
  setTotalPages(response.totalPages || 1);
1000
+ lastSearchKeyRef.current = searchKey;
1001
+ keepSnapshotRef.current = false;
906
1002
  } catch {
907
- if (active) {
1003
+ if (active && !shouldPreservePaintedListingsOnRefetchFailure({
1004
+ paintedSearchKey: lastSearchKeyRef.current,
1005
+ searchKey,
1006
+ dictionary: currentSchema.dictionary
1007
+ })) {
908
1008
  setListings([]);
909
1009
  setPage(1);
910
1010
  setTotal(0);
911
1011
  setTotalPages(1);
1012
+ keepSnapshotRef.current = false;
912
1013
  }
913
1014
  } finally {
914
- if (active) setLoading(false);
1015
+ if (active) {
1016
+ setLoading(false);
1017
+ }
915
1018
  }
916
1019
  }
917
1020
  loadListings();