@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.
@@ -0,0 +1,73 @@
1
+ "use client";
2
+
3
+ import type { ReactElement } from "react";
4
+
5
+ import { searchSortOptions } from "../search-engine";
6
+ import { SearchChromeStyles } from "./styles";
7
+
8
+ export type SearchSortSelectProps = {
9
+ value: string;
10
+ onChange: (sort: string | null) => void;
11
+ sorts?: Array<{ id: string; label: string }>;
12
+ className?: string;
13
+ };
14
+
15
+ export function SearchSortSelect({
16
+ value,
17
+ onChange,
18
+ sorts,
19
+ className,
20
+ }: SearchSortSelectProps): ReactElement {
21
+ const options = searchSortOptions(sorts);
22
+ return (
23
+ <label data-slot="search-sort" className={className}>
24
+ <span data-slot="search-label">Ordenar</span>
25
+ <select
26
+ id="garagem-search-sort"
27
+ data-slot="search-sort-trigger"
28
+ value={value}
29
+ onChange={(event) => onChange(event.target.value || null)}
30
+ >
31
+ {options.map((sort) => (
32
+ <option key={sort.id || "relevance"} value={sort.id}>
33
+ {sort.label}
34
+ </option>
35
+ ))}
36
+ </select>
37
+ </label>
38
+ );
39
+ }
40
+
41
+ export function ResultsSkeleton({
42
+ className,
43
+ }: {
44
+ className?: string;
45
+ }): ReactElement {
46
+ return (
47
+ <div data-slot="search-skeleton" className={className}>
48
+ {Array.from({ length: 6 }, (_, i) => (
49
+ <div key={i} data-slot="search-skeleton-card">
50
+ <div data-slot="search-skeleton-block" data-kind="media" />
51
+ <div data-slot="search-skeleton-body">
52
+ <div data-slot="search-skeleton-block" data-kind="line-lg" />
53
+ <div data-slot="search-skeleton-block" data-kind="line-md" />
54
+ <div data-slot="search-skeleton-block" data-kind="line-sm" />
55
+ </div>
56
+ </div>
57
+ ))}
58
+ </div>
59
+ );
60
+ }
61
+
62
+ export function SearchPageFallback({
63
+ className,
64
+ }: {
65
+ className?: string;
66
+ }): ReactElement {
67
+ return (
68
+ <main data-slot="search-page">
69
+ <SearchChromeStyles />
70
+ <ResultsSkeleton className={className} />
71
+ </main>
72
+ );
73
+ }
@@ -0,0 +1,465 @@
1
+ "use client";
2
+
3
+ import {
4
+ useCallback,
5
+ useEffect,
6
+ useRef,
7
+ useState,
8
+ type ReactElement,
9
+ type ReactNode,
10
+ } from "react";
11
+
12
+ import {
13
+ clearStagedPathFilters,
14
+ searchResultsHeading,
15
+ toolbarFilterIds,
16
+ visibleSearchFilters,
17
+ } from "../search-engine";
18
+ import type { SearchUiFilter } from "../types";
19
+ import { cn } from "./cn";
20
+ import { FilterPanel, rangeDraftFromPath } from "./filter-panel";
21
+ import { ListingCard } from "./listing-card";
22
+ import { ResultsSkeleton, SearchSortSelect } from "./search-chrome";
23
+ import { SearchPagination } from "./search-pagination";
24
+ import { SearchQueryForm } from "./search-query-form";
25
+ import type {
26
+ SearchPageClassNames,
27
+ SearchPageIcons,
28
+ SearchPageVariant,
29
+ SiteIdInput,
30
+ } from "./types";
31
+ import { SearchChromeStyles } from "./styles";
32
+ import { usePublicSearch } from "./use-public-search";
33
+
34
+ export type SearchPageProps = {
35
+ siteId: SiteIdInput;
36
+ baseUrl?: string;
37
+ variant?: SearchPageVariant;
38
+ className?: string;
39
+ classNames?: SearchPageClassNames;
40
+ heading?: ReactNode;
41
+ icons?: SearchPageIcons;
42
+ listingCard?: typeof ListingCard;
43
+ };
44
+
45
+ function ToolbarPopover({
46
+ id,
47
+ label,
48
+ open,
49
+ onOpenChange,
50
+ triggerClassName,
51
+ popoverClassName,
52
+ children,
53
+ }: {
54
+ id: string;
55
+ label: string;
56
+ open: boolean;
57
+ onOpenChange: (id: string | null) => void;
58
+ triggerClassName?: string;
59
+ popoverClassName?: string;
60
+ children: ReactNode;
61
+ }): ReactElement {
62
+ const rootRef = useRef<HTMLDivElement>(null);
63
+
64
+ useEffect(() => {
65
+ if (!open) return;
66
+ function onPointer(event: MouseEvent) {
67
+ if (!rootRef.current?.contains(event.target as Node)) {
68
+ onOpenChange(null);
69
+ }
70
+ }
71
+ function onKey(event: KeyboardEvent) {
72
+ if (event.key === "Escape") onOpenChange(null);
73
+ }
74
+ document.addEventListener("mousedown", onPointer);
75
+ document.addEventListener("keydown", onKey);
76
+ return () => {
77
+ document.removeEventListener("mousedown", onPointer);
78
+ document.removeEventListener("keydown", onKey);
79
+ };
80
+ }, [open, onOpenChange]);
81
+
82
+ return (
83
+ <div data-slot="search-toolbar-item" ref={rootRef}>
84
+ <button
85
+ type="button"
86
+ data-slot="search-toolbar-trigger"
87
+ className={triggerClassName}
88
+ aria-expanded={open}
89
+ onClick={() => onOpenChange(open ? null : id)}
90
+ >
91
+ {label}
92
+ </button>
93
+ {open ? (
94
+ <div
95
+ data-slot="search-popover"
96
+ className={popoverClassName}
97
+ role="dialog"
98
+ >
99
+ {children}
100
+ </div>
101
+ ) : null}
102
+ </div>
103
+ );
104
+ }
105
+
106
+ function FiltersTrigger({
107
+ onClick,
108
+ className,
109
+ }: {
110
+ onClick: () => void;
111
+ className?: string;
112
+ }): ReactElement {
113
+ return (
114
+ <button
115
+ type="button"
116
+ title="Filtros"
117
+ data-slot="search-filters-trigger"
118
+ onClick={onClick}
119
+ className={className}
120
+ >
121
+ Filtros
122
+ </button>
123
+ );
124
+ }
125
+
126
+ function ApplyBar({
127
+ onClear,
128
+ onApply,
129
+ className,
130
+ }: {
131
+ onClear: () => void;
132
+ onApply: () => void;
133
+ className?: string;
134
+ }): ReactElement {
135
+ return (
136
+ <div data-slot="search-apply-bar" className={className}>
137
+ <button type="button" data-kind="clear" onClick={onClear}>
138
+ Limpar
139
+ </button>
140
+ <button type="button" data-kind="apply" onClick={onApply}>
141
+ Ver resultados
142
+ </button>
143
+ </div>
144
+ );
145
+ }
146
+
147
+ function FilterSheet({
148
+ open,
149
+ title,
150
+ onClose,
151
+ overlayClassName,
152
+ panelClassName,
153
+ children,
154
+ footer,
155
+ }: {
156
+ open: boolean;
157
+ title: string;
158
+ onClose: () => void;
159
+ overlayClassName?: string;
160
+ panelClassName?: string;
161
+ children: ReactNode;
162
+ footer: ReactNode;
163
+ }): ReactElement | null {
164
+ const onCloseRef = useRef(onClose);
165
+ onCloseRef.current = onClose;
166
+
167
+ useEffect(() => {
168
+ if (!open) return;
169
+ function onKey(event: KeyboardEvent) {
170
+ if (event.key === "Escape") onCloseRef.current();
171
+ }
172
+ document.addEventListener("keydown", onKey);
173
+ const previous = document.body.style.overflow;
174
+ document.body.style.overflow = "hidden";
175
+ return () => {
176
+ document.removeEventListener("keydown", onKey);
177
+ document.body.style.overflow = previous;
178
+ };
179
+ }, [open]);
180
+
181
+ if (!open) return null;
182
+ return (
183
+ <div data-slot="search-sheet">
184
+ <button
185
+ type="button"
186
+ aria-label="Fechar filtros"
187
+ data-slot="search-sheet-overlay"
188
+ className={overlayClassName}
189
+ onClick={onClose}
190
+ />
191
+ <div
192
+ data-slot="search-sheet-panel"
193
+ className={panelClassName}
194
+ role="dialog"
195
+ aria-modal="true"
196
+ aria-label={title}
197
+ >
198
+ <p data-slot="search-sheet-title">{title}</p>
199
+ <div data-slot="search-sheet-body">{children}</div>
200
+ <div data-slot="search-sheet-footer">{footer}</div>
201
+ </div>
202
+ </div>
203
+ );
204
+ }
205
+
206
+ function SearchHeading({
207
+ total,
208
+ title,
209
+ classNames,
210
+ loading,
211
+ }: {
212
+ total: number;
213
+ title: string;
214
+ classNames?: SearchPageClassNames;
215
+ loading?: boolean;
216
+ }): ReactElement {
217
+ return (
218
+ <div data-slot="search-heading" className={classNames?.heading}>
219
+ {!loading ? (
220
+ <span
221
+ data-slot="search-heading-count"
222
+ className={classNames?.headingCount}
223
+ >
224
+ {total.toLocaleString("pt-BR")}
225
+ </span>
226
+ ) : null}
227
+ <h1 data-slot="search-heading-title" className={classNames?.headingTitle}>
228
+ {title}
229
+ </h1>
230
+ </div>
231
+ );
232
+ }
233
+
234
+ export function SearchPage({
235
+ siteId,
236
+ baseUrl,
237
+ variant = "toolbar",
238
+ className,
239
+ classNames,
240
+ heading,
241
+ icons,
242
+ listingCard: Card = ListingCard,
243
+ }: SearchPageProps): ReactElement {
244
+ const search = usePublicSearch({ siteId, baseUrl });
245
+ const [sheetOpen, setSheetOpen] = useState(false);
246
+ const [openToolbar, setOpenToolbar] = useState<string | null>(null);
247
+ const [rangeDraft, setRangeDraft] = useState(() =>
248
+ rangeDraftFromPath(search.pathFilters)
249
+ );
250
+
251
+ useEffect(() => {
252
+ setRangeDraft(rangeDraftFromPath(search.pathFilters));
253
+ }, [search.pathFilters]);
254
+
255
+ const filters = visibleSearchFilters(search.schema);
256
+ const toolbarIds = new Set(toolbarFilterIds(search.schema));
257
+ const toolbarFilters = filters.filter((filter) => toolbarIds.has(filter.id));
258
+ const extraFilters = filters.filter((filter) => !toolbarIds.has(filter.id));
259
+ const headingTitle = searchResultsHeading(
260
+ search.pathFilters,
261
+ search.schema?.dictionary,
262
+ search.locationNames
263
+ );
264
+
265
+ function patchRange(filterId: string, side: "min" | "max", value: string) {
266
+ setRangeDraft((prev) => ({
267
+ ...prev,
268
+ [filterId]: {
269
+ min: prev[filterId]?.min ?? "",
270
+ max: prev[filterId]?.max ?? "",
271
+ [side]: value,
272
+ },
273
+ }));
274
+ }
275
+
276
+ function commitRange(filterId: string, immediate: boolean) {
277
+ const draft = rangeDraft[filterId] || { min: "", max: "" };
278
+ if (immediate) search.applyFilter(filterId, draft);
279
+ else search.stageFilter(filterId, draft);
280
+ }
281
+
282
+ function openStagedFilters() {
283
+ search.resetStagedFromUrl();
284
+ setRangeDraft(rangeDraftFromPath(search.pathFilters));
285
+ setOpenToolbar(null);
286
+ setSheetOpen(true);
287
+ }
288
+
289
+ const closeSheet = useCallback(() => {
290
+ setSheetOpen(false);
291
+ }, []);
292
+
293
+ function clearStagedFilters() {
294
+ const keepIds = variant === "toolbar" ? [...toolbarIds] : [];
295
+ const source = variant === "toolbar" ? search.pathFilters : search.staged;
296
+ const next = clearStagedPathFilters(source, keepIds);
297
+ search.setStaged(next);
298
+ setRangeDraft((prev) => {
299
+ const cleared = rangeDraftFromPath(next);
300
+ if (variant !== "toolbar") return cleared;
301
+ const kept = { ...cleared };
302
+ for (const id of keepIds) {
303
+ if (prev[id]) kept[id] = prev[id];
304
+ }
305
+ return kept;
306
+ });
307
+ }
308
+
309
+ function applyStagedAndClose() {
310
+ search.applyStaged();
311
+ closeSheet();
312
+ }
313
+
314
+ const results: ReactNode = search.loading ? (
315
+ <ResultsSkeleton className={classNames?.skeleton ?? classNames?.grid} />
316
+ ) : (
317
+ <>
318
+ <div data-slot="search-grid" className={classNames?.grid}>
319
+ {search.listings.map((listing) => (
320
+ <Card
321
+ key={listing.id}
322
+ listing={listing}
323
+ className={classNames?.card}
324
+ />
325
+ ))}
326
+ </div>
327
+ <SearchPagination
328
+ page={search.page}
329
+ totalPages={search.totalPages}
330
+ hrefForPage={search.hrefForPage}
331
+ onNavigate={search.go}
332
+ className={classNames?.pagination}
333
+ />
334
+ </>
335
+ );
336
+
337
+ const queryAndSort = (
338
+ <div data-slot="search-query-sort">
339
+ <SearchQueryForm
340
+ value={search.qDraft}
341
+ onChange={search.setQDraft}
342
+ onSubmit={search.submitQuery}
343
+ onClear={search.clearQuery}
344
+ className={classNames?.query}
345
+ iconClassName={classNames?.icon}
346
+ searchIcon={icons?.search}
347
+ clearIcon={icons?.clear}
348
+ />
349
+ <SearchSortSelect
350
+ value={search.applied.sort || ""}
351
+ onChange={search.setSort}
352
+ sorts={search.schema?.sorts}
353
+ className={classNames?.sort}
354
+ />
355
+ </div>
356
+ );
357
+
358
+ const header = heading ?? (
359
+ <SearchHeading
360
+ total={search.total}
361
+ title={headingTitle}
362
+ classNames={classNames}
363
+ loading={search.loading}
364
+ />
365
+ );
366
+
367
+ const stagedPanel = (items: SearchUiFilter[]) => (
368
+ <FilterPanel
369
+ filters={items}
370
+ pathFilters={search.staged}
371
+ onChange={search.stageFilter}
372
+ rangeDraft={rangeDraft}
373
+ onRangePatch={patchRange}
374
+ onRangeCommit={(id) => commitRange(id, false)}
375
+ />
376
+ );
377
+
378
+ const sheet = (
379
+ <FilterSheet
380
+ open={sheetOpen}
381
+ title="Filtros"
382
+ onClose={closeSheet}
383
+ overlayClassName={classNames?.dialogOverlay}
384
+ panelClassName={cn(classNames?.dialog, classNames?.dialogPanel)}
385
+ footer={
386
+ <ApplyBar
387
+ onClear={clearStagedFilters}
388
+ onApply={applyStagedAndClose}
389
+ className={classNames?.applyBar}
390
+ />
391
+ }
392
+ >
393
+ {stagedPanel(variant === "toolbar" ? extraFilters : filters)}
394
+ </FilterSheet>
395
+ );
396
+
397
+ return (
398
+ <main
399
+ data-slot="search-page"
400
+ data-variant={variant}
401
+ className={cn(className, classNames?.root)}
402
+ >
403
+ <SearchChromeStyles />
404
+ <div className={classNames?.header}>{header}</div>
405
+ <div data-slot="search-body">
406
+ {queryAndSort}
407
+ {variant === "toolbar" ? (
408
+ <div data-slot="search-toolbar" className={classNames?.toolbar}>
409
+ {toolbarFilters.map((filter) => (
410
+ <ToolbarPopover
411
+ key={filter.id}
412
+ id={filter.id}
413
+ label={filter.label}
414
+ open={openToolbar === filter.id}
415
+ onOpenChange={setOpenToolbar}
416
+ triggerClassName={classNames?.toolbarTrigger}
417
+ popoverClassName={classNames?.popover}
418
+ >
419
+ <FilterPanel
420
+ filters={[filter]}
421
+ pathFilters={search.pathFilters}
422
+ onChange={(id, value) => {
423
+ search.applyFilter(id, value);
424
+ if (filter.type !== "range") setOpenToolbar(null);
425
+ }}
426
+ rangeDraft={rangeDraft}
427
+ onRangePatch={patchRange}
428
+ onRangeCommit={(id, source) => {
429
+ commitRange(id, true);
430
+ if (source === "apply") setOpenToolbar(null);
431
+ }}
432
+ />
433
+ </ToolbarPopover>
434
+ ))}
435
+ {extraFilters.length > 0 ? (
436
+ <FiltersTrigger onClick={openStagedFilters} />
437
+ ) : null}
438
+ </div>
439
+ ) : null}
440
+ {variant === "dialog" ? (
441
+ <FiltersTrigger onClick={openStagedFilters} />
442
+ ) : null}
443
+ {variant === "sidebar" ? (
444
+ <div data-slot="search-layout">
445
+ <aside data-slot="search-sidebar" className={classNames?.sidebar}>
446
+ {stagedPanel(filters)}
447
+ <ApplyBar
448
+ onClear={clearStagedFilters}
449
+ onApply={search.applyStaged}
450
+ className={classNames?.applyBar}
451
+ />
452
+ </aside>
453
+ <div>
454
+ <FiltersTrigger onClick={openStagedFilters} />
455
+ {results}
456
+ </div>
457
+ </div>
458
+ ) : (
459
+ results
460
+ )}
461
+ </div>
462
+ {sheet}
463
+ </main>
464
+ );
465
+ }
@@ -0,0 +1,120 @@
1
+ "use client";
2
+
3
+ import type { MouseEvent, ReactElement } from "react";
4
+ import Link from "next/link";
5
+
6
+ import { hrefWithExplicitPage, paginationPages } from "../search-engine";
7
+
8
+ export type SearchPaginationProps = {
9
+ page: number;
10
+ totalPages: number;
11
+ hrefForPage: (page: number) => string;
12
+ onNavigate?: (href: string) => void;
13
+ className?: string;
14
+ };
15
+
16
+ function NavItem({
17
+ href,
18
+ label,
19
+ disabled,
20
+ current,
21
+ onNavigate,
22
+ }: {
23
+ href?: string;
24
+ label: string;
25
+ disabled?: boolean;
26
+ current?: boolean;
27
+ onNavigate?: (href: string) => void;
28
+ }): ReactElement {
29
+ if (disabled || !href || current) {
30
+ return (
31
+ <li
32
+ data-slot="search-pagination-item"
33
+ data-current={current ? "true" : undefined}
34
+ data-disabled={disabled ? "true" : undefined}
35
+ >
36
+ <span>{label}</span>
37
+ </li>
38
+ );
39
+ }
40
+
41
+ function handleClick(event: MouseEvent<HTMLAnchorElement>) {
42
+ if (!onNavigate || !href) return;
43
+ if (
44
+ event.metaKey ||
45
+ event.ctrlKey ||
46
+ event.shiftKey ||
47
+ event.altKey ||
48
+ event.button !== 0
49
+ ) {
50
+ return;
51
+ }
52
+ event.preventDefault();
53
+ onNavigate(href);
54
+ }
55
+
56
+ return (
57
+ <li data-slot="search-pagination-item">
58
+ <Link href={href} onClick={handleClick} aria-label={label}>
59
+ {label}
60
+ </Link>
61
+ </li>
62
+ );
63
+ }
64
+
65
+ export function SearchPagination({
66
+ page,
67
+ totalPages,
68
+ hrefForPage,
69
+ onNavigate,
70
+ className,
71
+ }: SearchPaginationProps): ReactElement | null {
72
+ if (totalPages <= 1) return null;
73
+ const pages = paginationPages(page, totalPages, 2);
74
+ return (
75
+ <nav
76
+ data-slot="search-pagination"
77
+ className={className}
78
+ aria-label="Paginação"
79
+ >
80
+ <ul>
81
+ <NavItem
82
+ href={hrefWithExplicitPage(hrefForPage(1), 1)}
83
+ label="‹‹"
84
+ disabled={page <= 1}
85
+ onNavigate={onNavigate}
86
+ />
87
+ <NavItem
88
+ href={hrefWithExplicitPage(
89
+ hrefForPage(Math.max(1, page - 1)),
90
+ Math.max(1, page - 1)
91
+ )}
92
+ label="‹"
93
+ disabled={page <= 1}
94
+ onNavigate={onNavigate}
95
+ />
96
+ {pages.map((n) => (
97
+ <NavItem
98
+ key={n}
99
+ href={hrefWithExplicitPage(hrefForPage(n), n)}
100
+ label={String(n)}
101
+ current={n === page}
102
+ onNavigate={onNavigate}
103
+ />
104
+ ))}
105
+ <NavItem
106
+ href={hrefForPage(Math.min(totalPages, page + 1))}
107
+ label="›"
108
+ disabled={page >= totalPages}
109
+ onNavigate={onNavigate}
110
+ />
111
+ <NavItem
112
+ href={hrefForPage(totalPages)}
113
+ label="››"
114
+ disabled={page >= totalPages}
115
+ onNavigate={onNavigate}
116
+ />
117
+ </ul>
118
+ </nav>
119
+ );
120
+ }