@garagem-ai/site-sdk 0.3.0 → 0.4.1
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 +59 -3
- package/dist/index.d.mts +27 -361
- package/dist/index.d.ts +27 -361
- package/dist/index.js +530 -50
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +493 -46
- package/dist/index.mjs.map +1 -1
- package/dist/react.d.mts +163 -0
- package/dist/react.d.ts +163 -0
- package/dist/react.js +2124 -0
- package/dist/react.js.map +1 -0
- package/dist/react.mjs +2083 -0
- package/dist/react.mjs.map +1 -0
- package/dist/search-engine-D4EZkNrQ.d.mts +468 -0
- package/dist/search-engine-D4EZkNrQ.d.ts +468 -0
- package/package.json +42 -4
- package/src/fetchers.ts +27 -18
- package/src/index.ts +44 -0
- package/src/internal/url.ts +14 -0
- package/src/listing-card-model.ts +90 -0
- package/src/presentation.ts +79 -0
- package/src/react/cn.ts +3 -0
- package/src/react/filter-panel.tsx +248 -0
- package/src/react/icons.tsx +46 -0
- package/src/react/index.ts +26 -0
- package/src/react/listing-card.tsx +86 -0
- package/src/react/search-chrome.tsx +73 -0
- package/src/react/search-page.tsx +465 -0
- package/src/react/search-pagination.tsx +120 -0
- package/src/react/search-query-form.tsx +81 -0
- package/src/react/search.css +717 -0
- package/src/react/styles/index.ts +1 -0
- package/src/react/styles/search-chrome-css.generated.ts +5 -0
- package/src/react/styles/search-chrome-styles.tsx +12 -0
- package/src/react/types.ts +106 -0
- package/src/react/use-public-search.ts +304 -0
- package/src/search-engine.ts +483 -0
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/* Generated by scripts/build-search-chrome-css.mjs — do not edit. */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
/* prettier-ignore */
|
|
4
|
+
export const SEARCH_CHROME_CSS_VERSION = "0.4.1";
|
|
5
|
+
export const SEARCH_CHROME_CSS = ":root{--garagem-listing-card-radius:0.75rem;--garagem-listing-card-gap:1.5rem;--garagem-listing-price-size:1.125rem;--garagem-listing-media-ratio:4 / 3;--garagem-search-control-height:2.75rem;--garagem-search-icon-size:1.25rem;--garagem-search-radius:var(--radius,0.75rem);--garagem-skeleton:var(--muted,#e7e5e4)}[data-slot=\"search-page\"]{margin-inline:auto;max-width:80rem;padding:3rem 1.5rem}[data-slot=\"search-heading\"]{display:flex;min-width:0;align-items:baseline;gap:0.375rem;font-size:1.25rem;font-weight:600}[data-slot=\"search-heading-count\"]{flex-shrink:0}[data-slot=\"search-heading-title\"]{min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;font-size:inherit;font-weight:inherit}[data-slot=\"search-query-sort\"]{display:flex;flex-direction:column;gap:1rem}@media (min-width:1024px){[data-slot=\"search-query-sort\"]{flex-direction:row;align-items:flex-end}}[data-slot=\"search-query\"]{flex:1;min-width:0}[data-slot=\"search-query-input\"],[data-slot=\"search-sort-trigger\"],[data-slot=\"search-toolbar-trigger\"],[data-slot=\"search-filters-trigger\"]{min-height:var(--garagem-search-control-height)}[data-slot=\"search-query-field\"]{position:relative}[data-slot=\"search-query-input\"]{width:100%;border-radius:var(--garagem-search-radius);border:1px solid var(--border,#e4e4e7);background:var(--background,#fff);padding:0.5rem 5.5rem 0.5rem 1rem;outline:none}[data-slot=\"search-query-actions\"]{position:absolute;top:50%;right:0.35rem;display:flex;transform:translateY(-50%);align-items:center;gap:0.15rem}[data-slot=\"search-icon-button\"]{display:inline-flex;height:2.25rem;width:2.25rem;cursor:pointer;align-items:center;justify-content:center;border:0;border-radius:999px;background:var(--primary,#111);color:var(--primary-foreground,#fff)}[data-slot=\"search-icon-button\"][data-kind=\"clear\"]{background:transparent;color:var(--muted-foreground,#737373)}[data-slot=\"search-icon\"]{width:var(--garagem-search-icon-size);height:var(--garagem-search-icon-size);display:block}[data-slot=\"search-sort\"]{display:flex;flex-direction:column;gap:0.25rem}[data-slot=\"search-sort-trigger\"]{appearance:none;cursor:pointer;border-radius:var(--garagem-search-radius);border:1px solid var(--border,#e4e4e7);background:var(--background,#fff);padding:0.5rem 2.25rem 0.5rem 0.75rem;background-image:url(\"data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='none' stroke='%23737373' stroke-width='2' viewBox='0 0 24 24'><path d='m6 9 6 6 6-6'/></svg>\");background-repeat:no-repeat;background-position:right 0.65rem center}[data-slot=\"search-toolbar\"]{display:flex;flex-wrap:wrap;align-items:flex-end;gap:0.5rem}[data-slot=\"search-toolbar-trigger\"],[data-slot=\"search-filters-trigger\"]{cursor:pointer;border-radius:var(--garagem-search-radius);border:1px solid var(--border,#e4e4e7);background:var(--background,#fff);padding:0.5rem 0.75rem;font-size:0.875rem}[data-slot=\"search-popover\"]{position:absolute;z-index:30;margin-top:0.5rem;width:18rem;border-radius:0.75rem;border:1px solid var(--border,#e4e4e7);background:var(--background,#fff);padding:1rem;box-shadow:0 10px 30px rgb(0 0 0 / 0.12);animation:garagem-search-in 140ms ease-out}[data-slot=\"search-grid\"],[data-slot=\"search-skeleton\"]{display:grid;grid-template-columns:1fr;gap:var(--garagem-listing-card-gap)}@media (min-width:640px){[data-slot=\"search-grid\"],[data-slot=\"search-skeleton\"]{grid-template-columns:repeat(2,minmax(0,1fr))}}@media (min-width:1024px){[data-slot=\"search-grid\"],[data-slot=\"search-skeleton\"]{grid-template-columns:repeat(3,minmax(0,1fr))}}[data-slot=\"listing-card\"]{overflow:hidden;border-radius:var(--garagem-listing-card-radius);border:1px solid var(--border,#e4e4e7);background:var(--card,var(--background,#fff))}[data-slot=\"listing-card-price-row\"]{display:inline-flex;align-items:baseline;gap:0.25rem}[data-slot=\"listing-card-price\"]{font-size:var(--garagem-listing-price-size);font-weight:600}[data-slot=\"listing-card-period\"]{font-size:0.75rem;color:var(--muted-foreground,#737373)}[data-slot=\"search-checkbox\"]{appearance:none;width:1.15rem;height:1.15rem;flex-shrink:0;cursor:pointer;border:1px solid var(--primary,#111);border-radius:0.25rem;background:var(--background,#fff)}[data-slot=\"search-checkbox\"]:checked{background-color:var(--primary,#111);background-image:url(\"data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='white' stroke-width='3'><path d='M5 12l5 5L20 7'/></svg>\");background-repeat:no-repeat;background-position:center;background-size:0.8rem}[data-slot=\"search-scroll\"]{max-height:12rem;overflow:auto;scrollbar-width:thin;scrollbar-color:var(--muted-foreground,#a1a1aa) transparent}[data-slot=\"search-range-apply\"]{display:inline-flex;min-height:2.5rem;cursor:pointer;align-items:center;justify-content:center;border:0;border-radius:var(--garagem-search-radius);background:var(--primary,#111);color:var(--primary-foreground,#fff);padding:0 0.75rem;font-size:0.75rem;font-weight:600}[data-slot=\"search-sheet\"]{position:fixed;inset:0;z-index:40}[data-slot=\"search-sheet-overlay\"]{position:absolute;inset:0;border:0;cursor:pointer;background:rgb(0 0 0 / 0.4);animation:garagem-search-fade 160ms ease-out}[data-slot=\"search-sheet-panel\"]{position:absolute;top:0;right:0;display:flex;height:100dvh;width:min(22rem,100%);flex-direction:column;background:var(--background,#fff);animation:garagem-search-sheet 180ms ease-out}[data-slot=\"search-sheet-body\"]{min-height:0;flex:1;overflow:auto;padding:1.5rem 1.5rem 0;scrollbar-width:thin}[data-slot=\"search-sheet-footer\"]{display:flex;gap:0.5rem;border-top:1px solid var(--border,#e4e4e7);padding:1rem 1.5rem}[data-slot=\"search-apply-bar\"]{display:flex;width:100%;gap:0.5rem}[data-slot=\"search-apply-bar\"] > button{min-height:2.5rem;flex:1;cursor:pointer;border-radius:var(--garagem-search-radius);padding:0 1rem}[data-slot=\"search-apply-bar\"] > button[data-kind=\"clear\"]{border:1px solid var(--border,#e4e4e7);background:transparent}[data-slot=\"search-apply-bar\"] > button[data-kind=\"apply\"]{border:0;background:var(--primary,#111);color:var(--primary-foreground,#fff);font-weight:600}[data-slot=\"search-pagination\"]{margin-top:2.5rem;display:flex;justify-content:center}[data-slot=\"search-pagination\"] ul{display:flex;flex-wrap:wrap;justify-content:center;gap:0.25rem;padding:0;margin:0;list-style:none}[data-slot=\"search-pagination-item\"]{display:inline-flex;width:2.25rem;height:2.25rem;align-items:center;justify-content:center;border:1px solid var(--border,#e4e4e7);border-radius:0.375rem;font-size:0.75rem;font-weight:500}@media (min-width:768px){[data-slot=\"search-pagination-item\"]{width:2.5rem;height:2.5rem}}[data-slot=\"search-pagination-item\"] a,[data-slot=\"search-pagination-item\"] span{display:flex;width:100%;height:100%;align-items:center;justify-content:center;color:inherit;text-decoration:none}[data-slot=\"search-pagination-item\"][data-current=\"true\"]{background:var(--primary,#111);color:var(--primary-foreground,#fff)}[data-slot=\"search-pagination-item\"][data-disabled=\"true\"]{cursor:not-allowed;color:var(--muted-foreground,#737373)}[data-slot=\"search-toolbar-item\"]{position:relative}[data-slot=\"search-body\"]{margin-top:2rem;display:flex;flex-direction:column;gap:1.5rem}[data-slot=\"search-label\"]{margin-bottom:0.25rem;display:block;font-size:0.875rem;color:var(--muted-foreground,#737373)}[data-slot=\"search-sheet-title\"]{padding:1.5rem 1.5rem 0;font-size:1.125rem;font-weight:600}[data-slot=\"search-layout\"]{display:grid;gap:2rem}[data-slot=\"search-sidebar\"]{display:none}[data-slot=\"search-layout\"] > div > [data-slot=\"search-filters-trigger\"]{margin-bottom:1rem}@media (min-width:1024px){[data-slot=\"search-layout\"]{grid-template-columns:280px minmax(0,1fr)}[data-slot=\"search-sidebar\"]{display:block;position:sticky;top:1.5rem;align-self:start}[data-slot=\"search-layout\"] > div > [data-slot=\"search-filters-trigger\"]{display:none}}[data-slot=\"listing-card-link\"]{display:block;color:inherit;text-decoration:none;cursor:pointer}[data-slot=\"listing-card-media\"]{position:relative;width:100%;overflow:hidden;background:var(--muted,#f5f5f4);aspect-ratio:var(--garagem-listing-media-ratio)}[data-slot=\"listing-card-media\"] img{height:100%;width:100%;object-fit:cover;transition:transform 0.3s}[data-slot=\"listing-card\"]:hover [data-slot=\"listing-card-media\"] img{transform:scale(1.05)}@media (prefers-reduced-motion:reduce){[data-slot=\"listing-card-media\"] img{transition:none}[data-slot=\"listing-card\"]:hover [data-slot=\"listing-card-media\"] img{transform:none}}[data-slot=\"listing-card-empty\"]{display:flex;height:100%;width:100%;align-items:center;justify-content:center;font-size:0.75rem;color:var(--muted-foreground,#737373)}[data-slot=\"listing-card-cta\"]{margin-top:0.75rem}[data-slot=\"search-filters\"]{display:flex;flex-direction:column;gap:1.25rem}[data-slot=\"search-filter\"]{margin:0;padding:0;border:0;font-size:0.875rem}[data-slot=\"search-filter\"] legend{margin-bottom:0.5rem;padding:0;color:var(--muted-foreground,#737373)}[data-slot=\"search-range-row\"]{display:flex;align-items:flex-end;gap:0.5rem}[data-slot=\"search-range-side\"]{min-width:0;flex:1}[data-slot=\"search-range-side\"] > span:first-child{margin-bottom:0.25rem;display:block;font-size:0.75rem;color:var(--muted-foreground,#737373)}[data-slot=\"search-range-affix\"]{position:relative;display:block}[data-slot=\"search-range-prefix\"]{pointer-events:none;position:absolute;inset-block:0;left:0;display:flex;align-items:center;padding-left:0.75rem;color:var(--muted-foreground,#737373)}[data-slot=\"search-range-input\"]{min-height:2.5rem;width:100%;border-radius:var(--garagem-search-radius);border:1px solid var(--border,#e4e4e7);background:var(--background,#fff);padding:0 0.75rem}[data-slot=\"search-range-affix\"] [data-slot=\"search-range-input\"]{padding-left:2.5rem}[data-slot=\"search-count-group\"]{display:flex;flex-wrap:wrap;gap:0.375rem}[data-slot=\"search-count-choice\"]{min-height:2.5rem;cursor:pointer;border-radius:var(--garagem-search-radius);border:1px solid var(--border,#e4e4e7);background:var(--background,#fff);padding:0 0.75rem;font-size:0.875rem}[data-slot=\"search-count-choice\"][data-selected=\"true\"]{border-color:var(--primary,#111);background:color-mix(in srgb,var(--primary,#111) 10%,transparent)}[data-slot=\"search-enum-list\"],[data-slot=\"search-scroll\"]{display:flex;flex-direction:column;gap:0.375rem}[data-slot=\"search-enum-option\"]{display:flex;cursor:pointer;align-items:center;gap:0.5rem}[data-slot=\"listing-card-badge\"]{position:absolute;left:0.75rem;top:0.75rem;border-radius:999px;background:color-mix(in srgb,var(--background,#fff) 90%,transparent);padding:0.25rem 0.5rem;font-size:0.75rem;font-weight:500}[data-slot=\"listing-card-body\"]{padding:1rem}[data-slot=\"listing-card-title\"]{display:-webkit-box;margin-top:0.25rem;overflow:hidden;-webkit-box-orient:vertical;-webkit-line-clamp:1;font-size:0.875rem;font-weight:500}[data-slot=\"listing-card-location\"]{margin-top:0.125rem;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;font-size:0.75rem;color:var(--muted-foreground,#737373)}[data-slot=\"listing-card-features\"]{display:flex;flex-wrap:wrap;margin-top:0.75rem;gap:0.25rem 1rem;padding:0;list-style:none;font-size:0.75rem;color:var(--muted-foreground,#737373)}[data-slot=\"search-skeleton-block\"]{animation:garagem-search-pulse 1.2s ease-in-out infinite;background:var(--garagem-skeleton);border-radius:0.375rem}[data-slot=\"search-skeleton-card\"]{overflow:hidden;border-radius:var(--garagem-listing-card-radius);border:1px solid var(--border,#e4e4e7)}[data-slot=\"search-skeleton-body\"]{display:flex;flex-direction:column;gap:0.5rem;padding:1rem}[data-slot=\"search-skeleton-block\"][data-kind=\"media\"]{aspect-ratio:var(--garagem-listing-media-ratio)}[data-slot=\"search-skeleton-block\"][data-kind=\"line-lg\"]{height:1.25rem;width:50%}[data-slot=\"search-skeleton-block\"][data-kind=\"line-md\"]{height:1rem;width:75%}[data-slot=\"search-skeleton-block\"][data-kind=\"line-sm\"]{height:0.75rem;width:66.666667%}[data-slot=\"search-skeleton-block\"][data-kind=\"detail-media\"]{max-width:45rem;aspect-ratio:4 / 3}[data-slot=\"search-skeleton-block\"][data-kind=\"detail-title\"]{width:66.666667%;height:2rem;margin-top:1.5rem}@media (prefers-reduced-motion:reduce){[data-slot=\"search-popover\"],[data-slot=\"search-sheet-overlay\"],[data-slot=\"search-sheet-panel\"],[data-slot=\"search-skeleton-block\"]{animation:none}}@keyframes garagem-search-in{from{opacity:0;transform:translateY(-4px) scale(0.98)}to{opacity:1;transform:none}}@keyframes garagem-search-fade{from{opacity:0}to{opacity:1}}@keyframes garagem-search-sheet{from{transform:translateX(100%)}to{transform:none}}@keyframes garagem-search-pulse{50%{opacity:0.55}}";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ReactElement } from "react";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
SEARCH_CHROME_CSS,
|
|
5
|
+
SEARCH_CHROME_CSS_VERSION,
|
|
6
|
+
} from "./search-chrome-css.generated";
|
|
7
|
+
|
|
8
|
+
const STYLE_ID = `garagem-search-chrome-${SEARCH_CHROME_CSS_VERSION}`;
|
|
9
|
+
|
|
10
|
+
export function SearchChromeStyles(): ReactElement {
|
|
11
|
+
return <style id={STYLE_ID}>{SEARCH_CHROME_CSS}</style>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import type { CSSProperties, ReactNode } from "react";
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
AppliedSearchQuery,
|
|
5
|
+
Listing,
|
|
6
|
+
SearchPathFilters,
|
|
7
|
+
SearchUi,
|
|
8
|
+
} from "../types";
|
|
9
|
+
import type { ListingCardModel } from "../listing-card-model";
|
|
10
|
+
import type { LocationNameTables } from "../search-engine";
|
|
11
|
+
|
|
12
|
+
export type SearchPageVariant = "toolbar" | "dialog" | "sidebar";
|
|
13
|
+
|
|
14
|
+
export type SearchPageClassNames = {
|
|
15
|
+
root?: string;
|
|
16
|
+
header?: string;
|
|
17
|
+
heading?: string;
|
|
18
|
+
headingCount?: string;
|
|
19
|
+
headingTitle?: string;
|
|
20
|
+
toolbar?: string;
|
|
21
|
+
toolbarTrigger?: string;
|
|
22
|
+
popover?: string;
|
|
23
|
+
query?: string;
|
|
24
|
+
sort?: string;
|
|
25
|
+
icon?: string;
|
|
26
|
+
sidebar?: string;
|
|
27
|
+
dialog?: string;
|
|
28
|
+
dialogOverlay?: string;
|
|
29
|
+
dialogPanel?: string;
|
|
30
|
+
grid?: string;
|
|
31
|
+
card?: string;
|
|
32
|
+
pagination?: string;
|
|
33
|
+
applyBar?: string;
|
|
34
|
+
skeleton?: string;
|
|
35
|
+
checkbox?: string;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type SearchPageIcons = {
|
|
39
|
+
search?: ReactNode;
|
|
40
|
+
clear?: ReactNode;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export type ListingCardClassNames = {
|
|
44
|
+
root?: string;
|
|
45
|
+
media?: string;
|
|
46
|
+
badge?: string;
|
|
47
|
+
body?: string;
|
|
48
|
+
price?: string;
|
|
49
|
+
title?: string;
|
|
50
|
+
location?: string;
|
|
51
|
+
features?: string;
|
|
52
|
+
cta?: string;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export type ListingCardSlots = {
|
|
56
|
+
media?: ReactNode;
|
|
57
|
+
badge?: ReactNode;
|
|
58
|
+
cta?: ReactNode;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export type ListingCardProps = {
|
|
62
|
+
listing: Listing | ListingCardModel;
|
|
63
|
+
href?: string;
|
|
64
|
+
className?: string;
|
|
65
|
+
classNames?: ListingCardClassNames;
|
|
66
|
+
slots?: ListingCardSlots;
|
|
67
|
+
style?: CSSProperties;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export type SiteIdInput =
|
|
71
|
+
| string
|
|
72
|
+
| Promise<string>
|
|
73
|
+
| (() => string | Promise<string>);
|
|
74
|
+
|
|
75
|
+
export type UsePublicSearchOptions = {
|
|
76
|
+
siteId: SiteIdInput;
|
|
77
|
+
baseUrl?: string;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export type PublicSearchState = {
|
|
81
|
+
schema: SearchUi | null;
|
|
82
|
+
listings: Listing[];
|
|
83
|
+
page: number;
|
|
84
|
+
total: number;
|
|
85
|
+
totalPages: number;
|
|
86
|
+
loading: boolean;
|
|
87
|
+
pathname: string;
|
|
88
|
+
search: string;
|
|
89
|
+
pathFilters: SearchPathFilters;
|
|
90
|
+
applied: AppliedSearchQuery;
|
|
91
|
+
qDraft: string;
|
|
92
|
+
setQDraft: (value: string) => void;
|
|
93
|
+
staged: SearchPathFilters;
|
|
94
|
+
setStaged: (next: SearchPathFilters) => void;
|
|
95
|
+
resetStagedFromUrl: () => void;
|
|
96
|
+
applyFilter: (filterId: string, value: unknown) => void;
|
|
97
|
+
stageFilter: (filterId: string, value: unknown) => void;
|
|
98
|
+
applyStaged: () => void;
|
|
99
|
+
clearStaged: () => void;
|
|
100
|
+
submitQuery: () => void;
|
|
101
|
+
clearQuery: () => void;
|
|
102
|
+
setSort: (sort: string | null) => void;
|
|
103
|
+
hrefForPage: (page: number) => string;
|
|
104
|
+
go: (href: string) => void;
|
|
105
|
+
locationNames: LocationNameTables | null;
|
|
106
|
+
};
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
4
|
+
import { usePathname, useRouter, useSearchParams } from "next/navigation";
|
|
5
|
+
|
|
6
|
+
import { getSearchUi, queryListings } from "../fetchers";
|
|
7
|
+
import { coerceSiteId, normalizeBaseUrl } from "../internal/url";
|
|
8
|
+
import {
|
|
9
|
+
applyPathFilter,
|
|
10
|
+
buildQueryListingsParams,
|
|
11
|
+
canonicalizeSearchHref,
|
|
12
|
+
clearStagedPathFilters,
|
|
13
|
+
hrefForAppliedChange,
|
|
14
|
+
hrefForPathFilters,
|
|
15
|
+
readPublicSearch,
|
|
16
|
+
type LocationNameTables,
|
|
17
|
+
} from "../search-engine";
|
|
18
|
+
import type { SearchPathFilters, SearchUi } from "../types";
|
|
19
|
+
import type {
|
|
20
|
+
PublicSearchState,
|
|
21
|
+
SiteIdInput,
|
|
22
|
+
UsePublicSearchOptions,
|
|
23
|
+
} from "./types";
|
|
24
|
+
|
|
25
|
+
async function resolveSiteIdInput(siteId: SiteIdInput): Promise<string> {
|
|
26
|
+
const value = typeof siteId === "function" ? siteId() : siteId;
|
|
27
|
+
return coerceSiteId(value);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function currentHref(pathname: string, search: string): string {
|
|
31
|
+
return search ? `${pathname}?${search}` : pathname;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function usePublicSearch(
|
|
35
|
+
options: UsePublicSearchOptions
|
|
36
|
+
): PublicSearchState {
|
|
37
|
+
const { siteId, baseUrl } = options;
|
|
38
|
+
const router = useRouter();
|
|
39
|
+
const pathname = usePathname() || "/imoveis";
|
|
40
|
+
const searchParams = useSearchParams();
|
|
41
|
+
const search = searchParams.toString();
|
|
42
|
+
|
|
43
|
+
const siteIdRef = useRef(siteId);
|
|
44
|
+
siteIdRef.current = siteId;
|
|
45
|
+
const siteIdToken = typeof siteId === "string" ? siteId : null;
|
|
46
|
+
|
|
47
|
+
const [catalogId, setCatalogId] = useState<string | null>(null);
|
|
48
|
+
const [schema, setSchema] = useState<SearchUi | null>(null);
|
|
49
|
+
const [listings, setListings] = useState<PublicSearchState["listings"]>([]);
|
|
50
|
+
const [page, setPage] = useState(1);
|
|
51
|
+
const [total, setTotal] = useState(0);
|
|
52
|
+
const [totalPages, setTotalPages] = useState(1);
|
|
53
|
+
const [loading, setLoading] = useState(true);
|
|
54
|
+
const [qDraft, setQDraft] = useState("");
|
|
55
|
+
const [staged, setStaged] = useState<SearchPathFilters>({});
|
|
56
|
+
const [locationNames, setLocationNames] = useState<LocationNameTables | null>(
|
|
57
|
+
null
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
const parsed = useMemo(
|
|
61
|
+
() => readPublicSearch(pathname, search, schema?.dictionary),
|
|
62
|
+
[pathname, search, schema?.dictionary]
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
setQDraft(parsed.applied.query || "");
|
|
67
|
+
}, [parsed.applied.query]);
|
|
68
|
+
|
|
69
|
+
const resetStagedFromUrl = useCallback(() => {
|
|
70
|
+
setStaged({ ...parsed.pathFilters });
|
|
71
|
+
}, [parsed.pathFilters]);
|
|
72
|
+
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
setStaged({ ...parsed.pathFilters });
|
|
75
|
+
}, [parsed.pathFilters]);
|
|
76
|
+
|
|
77
|
+
const locationsRef = useRef<LocationNameTables | null>(null);
|
|
78
|
+
|
|
79
|
+
useEffect(() => {
|
|
80
|
+
if (!schema) return;
|
|
81
|
+
const canonical = canonicalizeSearchHref(
|
|
82
|
+
pathname,
|
|
83
|
+
search,
|
|
84
|
+
schema.dictionary
|
|
85
|
+
);
|
|
86
|
+
if (canonical) router.replace(canonical);
|
|
87
|
+
}, [pathname, search, schema, router]);
|
|
88
|
+
|
|
89
|
+
useEffect(() => {
|
|
90
|
+
let active = true;
|
|
91
|
+
setCatalogId(null);
|
|
92
|
+
setSchema(null);
|
|
93
|
+
setListings([]);
|
|
94
|
+
setLoading(true);
|
|
95
|
+
resolveSiteIdInput(siteIdRef.current)
|
|
96
|
+
.then((id) => {
|
|
97
|
+
if (active) setCatalogId(id);
|
|
98
|
+
})
|
|
99
|
+
.catch(() => {
|
|
100
|
+
if (active) setLoading(false);
|
|
101
|
+
});
|
|
102
|
+
return () => {
|
|
103
|
+
active = false;
|
|
104
|
+
};
|
|
105
|
+
}, [baseUrl, siteIdToken]);
|
|
106
|
+
|
|
107
|
+
useEffect(() => {
|
|
108
|
+
if (!catalogId) return;
|
|
109
|
+
const id = catalogId;
|
|
110
|
+
let active = true;
|
|
111
|
+
locationsRef.current = null;
|
|
112
|
+
setLocationNames(null);
|
|
113
|
+
setSchema(null);
|
|
114
|
+
setListings([]);
|
|
115
|
+
setLoading(true);
|
|
116
|
+
async function loadSchema() {
|
|
117
|
+
try {
|
|
118
|
+
const nextSchema = await getSearchUi({ siteId: id, baseUrl });
|
|
119
|
+
if (!active) return;
|
|
120
|
+
let locations: LocationNameTables | null = null;
|
|
121
|
+
if (nextSchema.locationsPath.startsWith("/")) {
|
|
122
|
+
try {
|
|
123
|
+
const response = await fetch(
|
|
124
|
+
`${normalizeBaseUrl(baseUrl)}${nextSchema.locationsPath}`
|
|
125
|
+
);
|
|
126
|
+
if (response.ok) {
|
|
127
|
+
locations = (await response.json()) as LocationNameTables;
|
|
128
|
+
}
|
|
129
|
+
} catch {
|
|
130
|
+
locations = null;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (!active) return;
|
|
134
|
+
locationsRef.current = locations;
|
|
135
|
+
setLocationNames(locations);
|
|
136
|
+
setSchema(nextSchema);
|
|
137
|
+
} catch {
|
|
138
|
+
if (active) {
|
|
139
|
+
locationsRef.current = null;
|
|
140
|
+
setLocationNames(null);
|
|
141
|
+
setSchema(null);
|
|
142
|
+
setLoading(false);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
loadSchema();
|
|
147
|
+
return () => {
|
|
148
|
+
active = false;
|
|
149
|
+
};
|
|
150
|
+
}, [baseUrl, catalogId]);
|
|
151
|
+
|
|
152
|
+
useEffect(() => {
|
|
153
|
+
if (!schema || !catalogId) return;
|
|
154
|
+
const currentSchema = schema;
|
|
155
|
+
const id = catalogId;
|
|
156
|
+
let active = true;
|
|
157
|
+
async function loadListings() {
|
|
158
|
+
setLoading(true);
|
|
159
|
+
try {
|
|
160
|
+
const { pathFilters, applied } = readPublicSearch(
|
|
161
|
+
pathname,
|
|
162
|
+
search,
|
|
163
|
+
currentSchema.dictionary
|
|
164
|
+
);
|
|
165
|
+
const response = await queryListings({
|
|
166
|
+
siteId: id,
|
|
167
|
+
baseUrl,
|
|
168
|
+
params: buildQueryListingsParams(
|
|
169
|
+
pathFilters,
|
|
170
|
+
applied,
|
|
171
|
+
currentSchema,
|
|
172
|
+
locationsRef.current
|
|
173
|
+
),
|
|
174
|
+
});
|
|
175
|
+
if (!active) return;
|
|
176
|
+
setListings(response.listings || []);
|
|
177
|
+
setPage(response.page || 1);
|
|
178
|
+
setTotal(response.total || 0);
|
|
179
|
+
setTotalPages(response.totalPages || 1);
|
|
180
|
+
} catch {
|
|
181
|
+
if (active) {
|
|
182
|
+
setListings([]);
|
|
183
|
+
setPage(1);
|
|
184
|
+
setTotal(0);
|
|
185
|
+
setTotalPages(1);
|
|
186
|
+
}
|
|
187
|
+
} finally {
|
|
188
|
+
if (active) setLoading(false);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
loadListings();
|
|
192
|
+
return () => {
|
|
193
|
+
active = false;
|
|
194
|
+
};
|
|
195
|
+
}, [pathname, search, baseUrl, schema, catalogId]);
|
|
196
|
+
|
|
197
|
+
const go = useCallback(
|
|
198
|
+
(href: string) => {
|
|
199
|
+
if (href && href !== currentHref(pathname, search)) router.push(href);
|
|
200
|
+
},
|
|
201
|
+
[pathname, search, router]
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
const applyFilter = useCallback(
|
|
205
|
+
(filterId: string, value: unknown) => {
|
|
206
|
+
if (!schema) return;
|
|
207
|
+
go(
|
|
208
|
+
hrefForPathFilters(
|
|
209
|
+
pathname,
|
|
210
|
+
search,
|
|
211
|
+
schema.dictionary,
|
|
212
|
+
applyPathFilter(parsed.pathFilters, filterId, value)
|
|
213
|
+
)
|
|
214
|
+
);
|
|
215
|
+
},
|
|
216
|
+
[go, pathname, search, schema, parsed.pathFilters]
|
|
217
|
+
);
|
|
218
|
+
|
|
219
|
+
const stageFilter = useCallback((filterId: string, value: unknown) => {
|
|
220
|
+
setStaged((current) => applyPathFilter(current, filterId, value));
|
|
221
|
+
}, []);
|
|
222
|
+
|
|
223
|
+
const applyStaged = useCallback(() => {
|
|
224
|
+
if (!schema) return;
|
|
225
|
+
go(hrefForPathFilters(pathname, search, schema.dictionary, staged));
|
|
226
|
+
}, [go, pathname, search, schema, staged]);
|
|
227
|
+
|
|
228
|
+
const clearStaged = useCallback(() => {
|
|
229
|
+
setStaged((current) => clearStagedPathFilters(current));
|
|
230
|
+
}, []);
|
|
231
|
+
|
|
232
|
+
const submitQuery = useCallback(() => {
|
|
233
|
+
if (!schema) return;
|
|
234
|
+
go(
|
|
235
|
+
hrefForAppliedChange(pathname, search, schema.dictionary, {
|
|
236
|
+
type: "query",
|
|
237
|
+
query: qDraft,
|
|
238
|
+
})
|
|
239
|
+
);
|
|
240
|
+
}, [go, pathname, search, schema, qDraft]);
|
|
241
|
+
|
|
242
|
+
const clearQuery = useCallback(() => {
|
|
243
|
+
setQDraft("");
|
|
244
|
+
if (!schema) return;
|
|
245
|
+
go(
|
|
246
|
+
hrefForAppliedChange(pathname, search, schema.dictionary, {
|
|
247
|
+
type: "query",
|
|
248
|
+
query: "",
|
|
249
|
+
})
|
|
250
|
+
);
|
|
251
|
+
}, [go, pathname, search, schema]);
|
|
252
|
+
|
|
253
|
+
const setSort = useCallback(
|
|
254
|
+
(sort: string | null) => {
|
|
255
|
+
if (!schema) return;
|
|
256
|
+
go(
|
|
257
|
+
hrefForAppliedChange(pathname, search, schema.dictionary, {
|
|
258
|
+
type: "sort",
|
|
259
|
+
sort,
|
|
260
|
+
})
|
|
261
|
+
);
|
|
262
|
+
},
|
|
263
|
+
[go, pathname, search, schema]
|
|
264
|
+
);
|
|
265
|
+
|
|
266
|
+
const hrefForPage = useCallback(
|
|
267
|
+
(nextPage: number) => {
|
|
268
|
+
if (!schema) return pathname;
|
|
269
|
+
return hrefForAppliedChange(pathname, search, schema.dictionary, {
|
|
270
|
+
type: "page",
|
|
271
|
+
page: nextPage,
|
|
272
|
+
});
|
|
273
|
+
},
|
|
274
|
+
[pathname, search, schema]
|
|
275
|
+
);
|
|
276
|
+
|
|
277
|
+
return {
|
|
278
|
+
schema,
|
|
279
|
+
listings,
|
|
280
|
+
page,
|
|
281
|
+
total,
|
|
282
|
+
totalPages,
|
|
283
|
+
loading,
|
|
284
|
+
pathname,
|
|
285
|
+
search,
|
|
286
|
+
pathFilters: parsed.pathFilters,
|
|
287
|
+
applied: parsed.applied,
|
|
288
|
+
qDraft,
|
|
289
|
+
setQDraft,
|
|
290
|
+
staged,
|
|
291
|
+
setStaged,
|
|
292
|
+
resetStagedFromUrl,
|
|
293
|
+
applyFilter,
|
|
294
|
+
stageFilter,
|
|
295
|
+
applyStaged,
|
|
296
|
+
clearStaged,
|
|
297
|
+
submitQuery,
|
|
298
|
+
clearQuery,
|
|
299
|
+
setSort,
|
|
300
|
+
hrefForPage,
|
|
301
|
+
go,
|
|
302
|
+
locationNames,
|
|
303
|
+
};
|
|
304
|
+
}
|