@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.mjs CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  // src/react/use-public-search.ts
4
4
  import { useCallback, useEffect, useMemo, useRef, useState } from "react";
5
- import { usePathname, useRouter, useSearchParams } from "next/navigation";
5
+ import { usePathname, useRouter } from "next/navigation";
6
6
 
7
7
  // src/internal/url.ts
8
8
  var DEFAULT_BASE_URL = "https://app.garagem.ai";
@@ -711,6 +711,71 @@ function clearStagedPathFilters(current, keepFilterIds = []) {
711
711
  return next;
712
712
  }
713
713
 
714
+ // src/react/public-search-snapshot.ts
715
+ function hrefParts(href) {
716
+ const q = href.indexOf("?");
717
+ if (q < 0) return { pathname: href, search: "" };
718
+ return { pathname: href.slice(0, q), search: href.slice(q + 1) };
719
+ }
720
+ function searchHrefKey(pathname, search) {
721
+ const qs = search.replace(/^\?/, "");
722
+ return qs ? `${pathname}?${qs}` : pathname;
723
+ }
724
+ function searchKeysRepresentSameQuery(a, b, dictionary) {
725
+ if (a == null) return false;
726
+ if (a === b) return true;
727
+ const left = hrefParts(a);
728
+ const right = hrefParts(b);
729
+ const leftCanon = canonicalizeSearchHref(left.pathname, left.search, dictionary) ?? searchHrefKey(left.pathname, left.search);
730
+ const rightCanon = canonicalizeSearchHref(right.pathname, right.search, dictionary) ?? searchHrefKey(right.pathname, right.search);
731
+ return leftCanon === rightCanon;
732
+ }
733
+ function hasTrustedSearchSnapshot(initial) {
734
+ return Boolean(initial?.schema || initial?.listings?.length);
735
+ }
736
+ function resolveSeedTotal(initial) {
737
+ if (typeof initial?.total === "number") return initial.total;
738
+ return initial?.listings?.length ?? 0;
739
+ }
740
+ function preferFetchedLocationNames(previous, fetched) {
741
+ return fetched ?? previous;
742
+ }
743
+ function shouldClearPaintedSearchOnSchemaFailure(keepSnapshot) {
744
+ return !keepSnapshot;
745
+ }
746
+ function shouldAdvancePaintedSearchKeyOnCanonicalize() {
747
+ return false;
748
+ }
749
+ function shouldSetLoadingOnSearchKeyChange(options) {
750
+ return !searchKeysRepresentSameQuery(
751
+ options.paintedSearchKey,
752
+ options.searchKey,
753
+ options.dictionary
754
+ );
755
+ }
756
+ function shouldPreservePaintedListingsOnRefetchFailure(options) {
757
+ return searchKeysRepresentSameQuery(
758
+ options.paintedSearchKey,
759
+ options.searchKey,
760
+ options.dictionary
761
+ );
762
+ }
763
+ function initialQueryDraft(pathname, search, dictionary) {
764
+ return readPublicSearch(pathname, search, dictionary).applied.query || "";
765
+ }
766
+ function seedPublicSearchState(initial) {
767
+ return {
768
+ schema: initial?.schema ?? null,
769
+ listings: initial?.listings ?? [],
770
+ page: initial?.page ?? 1,
771
+ total: resolveSeedTotal(initial),
772
+ totalPages: initial?.totalPages ?? 1,
773
+ locationNames: initial?.locationNames ?? null,
774
+ catalogId: initial?.catalogId ?? null,
775
+ loading: !hasTrustedSearchSnapshot(initial)
776
+ };
777
+ }
778
+
714
779
  // src/react/staged-filters-mirror.ts
715
780
  var StagedFiltersMirror = class {
716
781
  current;
@@ -743,26 +808,33 @@ function currentHref(pathname, search) {
743
808
  return search ? `${pathname}?${search}` : pathname;
744
809
  }
745
810
  function usePublicSearch(options) {
746
- const { siteId, baseUrl } = options;
811
+ const { siteId, baseUrl, initial, search: searchFromServer = "" } = options;
747
812
  const router = useRouter();
748
813
  const pathname = usePathname() || "/imoveis";
749
- const searchParams = useSearchParams();
750
- const search = searchParams.toString();
814
+ const search = searchFromServer;
815
+ const searchKey = search ? `${pathname}?${search}` : pathname;
816
+ const [seed] = useState(() => seedPublicSearchState(initial));
817
+ const keepSnapshotRef = useRef(hasTrustedSearchSnapshot(initial));
818
+ const lastSearchKeyRef = useRef(keepSnapshotRef.current ? searchKey : null);
751
819
  const siteIdRef = useRef(siteId);
752
820
  siteIdRef.current = siteId;
753
821
  const siteIdToken = typeof siteId === "string" ? siteId : null;
754
- const [catalogId, setCatalogId] = useState(null);
755
- const [schema, setSchema] = useState(null);
756
- const [listings, setListings] = useState([]);
757
- const [page, setPage] = useState(1);
758
- const [total, setTotal] = useState(0);
759
- const [totalPages, setTotalPages] = useState(1);
760
- const [loading, setLoading] = useState(true);
761
- const [qDraft, setQDraft] = useState("");
822
+ const [catalogId, setCatalogId] = useState(seed.catalogId);
823
+ const [schema, setSchema] = useState(seed.schema);
824
+ const [listings, setListings] = useState(
825
+ seed.listings
826
+ );
827
+ const [page, setPage] = useState(seed.page);
828
+ const [total, setTotal] = useState(seed.total);
829
+ const [totalPages, setTotalPages] = useState(seed.totalPages);
830
+ const [loading, setLoading] = useState(seed.loading);
831
+ const [qDraft, setQDraft] = useState(
832
+ () => initialQueryDraft(pathname, search, initial?.schema?.dictionary)
833
+ );
762
834
  const stagedMirrorRef = useRef(new StagedFiltersMirror());
763
835
  const [staged, setStagedState] = useState({});
764
836
  const [locationNames, setLocationNames] = useState(
765
- null
837
+ seed.locationNames
766
838
  );
767
839
  const replaceStaged = useCallback((next) => {
768
840
  stagedMirrorRef.current.replace(next);
@@ -781,7 +853,7 @@ function usePublicSearch(options) {
781
853
  useEffect(() => {
782
854
  replaceStaged({ ...parsed.pathFilters });
783
855
  }, [parsed.pathFilters, replaceStaged]);
784
- const locationsRef = useRef(null);
856
+ const locationsRef = useRef(seed.locationNames);
785
857
  useEffect(() => {
786
858
  if (!schema) return;
787
859
  const canonical = canonicalizeSearchHref(
@@ -789,14 +861,20 @@ function usePublicSearch(options) {
789
861
  search,
790
862
  schema.dictionary
791
863
  );
792
- if (canonical) router.replace(canonical);
864
+ if (!canonical) return;
865
+ if (shouldAdvancePaintedSearchKeyOnCanonicalize()) {
866
+ lastSearchKeyRef.current = canonical;
867
+ }
868
+ router.replace(canonical);
793
869
  }, [pathname, search, schema, router]);
794
870
  useEffect(() => {
795
871
  let active = true;
796
- setCatalogId(null);
797
- setSchema(null);
798
- setListings([]);
799
- setLoading(true);
872
+ if (!keepSnapshotRef.current) {
873
+ setCatalogId(null);
874
+ setSchema(null);
875
+ setListings([]);
876
+ setLoading(true);
877
+ }
800
878
  resolveSiteIdInput(siteIdRef.current).then((id) => {
801
879
  if (active) setCatalogId(id);
802
880
  }).catch(() => {
@@ -810,11 +888,13 @@ function usePublicSearch(options) {
810
888
  if (!catalogId) return;
811
889
  const id = catalogId;
812
890
  let active = true;
813
- locationsRef.current = null;
814
- setLocationNames(null);
815
- setSchema(null);
816
- setListings([]);
817
- setLoading(true);
891
+ if (!keepSnapshotRef.current) {
892
+ locationsRef.current = null;
893
+ setLocationNames(null);
894
+ setSchema(null);
895
+ setListings([]);
896
+ setLoading(true);
897
+ }
818
898
  async function loadSchema() {
819
899
  try {
820
900
  const nextSchema = await getSearchUi({ siteId: id, baseUrl });
@@ -833,14 +913,22 @@ function usePublicSearch(options) {
833
913
  }
834
914
  }
835
915
  if (!active) return;
836
- locationsRef.current = locations;
837
- setLocationNames(locations);
916
+ const nextLocations = preferFetchedLocationNames(
917
+ locationsRef.current,
918
+ locations
919
+ );
920
+ locationsRef.current = nextLocations;
921
+ setLocationNames(nextLocations);
838
922
  setSchema(nextSchema);
923
+ keepSnapshotRef.current = false;
839
924
  } catch {
840
925
  if (active) {
841
- locationsRef.current = null;
842
- setLocationNames(null);
843
- setSchema(null);
926
+ if (shouldClearPaintedSearchOnSchemaFailure(keepSnapshotRef.current)) {
927
+ keepSnapshotRef.current = false;
928
+ locationsRef.current = null;
929
+ setLocationNames(null);
930
+ setSchema(null);
931
+ }
844
932
  setLoading(false);
845
933
  }
846
934
  }
@@ -856,7 +944,13 @@ function usePublicSearch(options) {
856
944
  const id = catalogId;
857
945
  let active = true;
858
946
  async function loadListings() {
859
- setLoading(true);
947
+ if (shouldSetLoadingOnSearchKeyChange({
948
+ paintedSearchKey: lastSearchKeyRef.current,
949
+ searchKey,
950
+ dictionary: currentSchema.dictionary
951
+ })) {
952
+ setLoading(true);
953
+ }
860
954
  try {
861
955
  const { pathFilters, applied } = readPublicSearch(
862
956
  pathname,
@@ -878,15 +972,24 @@ function usePublicSearch(options) {
878
972
  setPage(response.page || 1);
879
973
  setTotal(response.total || 0);
880
974
  setTotalPages(response.totalPages || 1);
975
+ lastSearchKeyRef.current = searchKey;
976
+ keepSnapshotRef.current = false;
881
977
  } catch {
882
- if (active) {
978
+ if (active && !shouldPreservePaintedListingsOnRefetchFailure({
979
+ paintedSearchKey: lastSearchKeyRef.current,
980
+ searchKey,
981
+ dictionary: currentSchema.dictionary
982
+ })) {
883
983
  setListings([]);
884
984
  setPage(1);
885
985
  setTotal(0);
886
986
  setTotalPages(1);
987
+ keepSnapshotRef.current = false;
887
988
  }
888
989
  } finally {
889
- if (active) setLoading(false);
990
+ if (active) {
991
+ setLoading(false);
992
+ }
890
993
  }
891
994
  }
892
995
  loadListings();