@garagem-ai/site-sdk 0.2.0 → 0.4.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.
@@ -0,0 +1,251 @@
1
+ "use client";
2
+
3
+ import type { ReactElement } from "react";
4
+
5
+ import { formatGroupedInteger } from "../presentation";
6
+ import {
7
+ countFacetChoices,
8
+ pathFilterHasOption,
9
+ pathFilterValue,
10
+ } from "../search-engine";
11
+ import type { SearchPathFilters, SearchUiFilter } from "../types";
12
+ import { cn } from "./cn";
13
+
14
+ export type FilterPanelProps = {
15
+ filters: SearchUiFilter[];
16
+ pathFilters: SearchPathFilters;
17
+ onChange: (filterId: string, value: unknown) => void;
18
+ rangeDraft: Record<string, { min: string; max: string }>;
19
+ onRangePatch: (filterId: string, side: "min" | "max", value: string) => void;
20
+ onRangeCommit: (filterId: string) => void;
21
+ };
22
+
23
+ function RangeFields({
24
+ filter,
25
+ rangeDraft,
26
+ onRangePatch,
27
+ onRangeCommit,
28
+ }: {
29
+ filter: SearchUiFilter;
30
+ rangeDraft: FilterPanelProps["rangeDraft"];
31
+ onRangePatch: FilterPanelProps["onRangePatch"];
32
+ onRangeCommit: FilterPanelProps["onRangeCommit"];
33
+ }): ReactElement {
34
+ const draft = rangeDraft[filter.id] || { min: "", max: "" };
35
+ const isPrice = filter.id === "price";
36
+ const patch = (side: "min" | "max", raw: string) => {
37
+ onRangePatch(filter.id, side, isPrice ? formatGroupedInteger(raw) : raw);
38
+ };
39
+ const inputClass =
40
+ "min-h-10 w-full rounded-lg border border-border bg-background px-3";
41
+ return (
42
+ <fieldset className="text-sm">
43
+ <legend className="mb-1 text-muted-foreground">{filter.label}</legend>
44
+ <div className="flex gap-2">
45
+ <RangeSide
46
+ id={`${filter.id}-min`}
47
+ label="Mín"
48
+ isPrice={isPrice}
49
+ value={draft.min}
50
+ inputClass={inputClass}
51
+ onPatch={(raw) => patch("min", raw)}
52
+ onCommit={() => onRangeCommit(filter.id)}
53
+ />
54
+ <RangeSide
55
+ id={`${filter.id}-max`}
56
+ label="Máx"
57
+ isPrice={isPrice}
58
+ value={draft.max}
59
+ inputClass={inputClass}
60
+ onPatch={(raw) => patch("max", raw)}
61
+ onCommit={() => onRangeCommit(filter.id)}
62
+ />
63
+ </div>
64
+ </fieldset>
65
+ );
66
+ }
67
+
68
+ function RangeSide({
69
+ id,
70
+ label,
71
+ isPrice,
72
+ value,
73
+ inputClass,
74
+ onPatch,
75
+ onCommit,
76
+ }: {
77
+ id: string;
78
+ label: string;
79
+ isPrice: boolean;
80
+ value: string;
81
+ inputClass: string;
82
+ onPatch: (raw: string) => void;
83
+ onCommit: () => void;
84
+ }): ReactElement {
85
+ const input = (
86
+ <input
87
+ id={id}
88
+ type={isPrice ? "text" : "number"}
89
+ inputMode={isPrice ? "numeric" : "decimal"}
90
+ min={isPrice ? undefined : 0}
91
+ step={isPrice ? undefined : 0.01}
92
+ value={value}
93
+ placeholder={isPrice ? "0" : undefined}
94
+ onChange={(event) => onPatch(event.target.value)}
95
+ onBlur={onCommit}
96
+ onKeyDown={(event) => {
97
+ if (event.key === "Enter") onCommit();
98
+ }}
99
+ className={isPrice ? `${inputClass} pl-10` : inputClass}
100
+ />
101
+ );
102
+ return (
103
+ <label className="min-w-0 flex-1" htmlFor={id}>
104
+ <span className="mb-1 block text-xs text-muted-foreground">{label}</span>
105
+ {isPrice ? (
106
+ <span className="relative block">
107
+ <span className="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3 text-muted-foreground">
108
+ R$
109
+ </span>
110
+ {input}
111
+ </span>
112
+ ) : (
113
+ input
114
+ )}
115
+ </label>
116
+ );
117
+ }
118
+
119
+ function CountFields({
120
+ filter,
121
+ pathFilters,
122
+ onChange,
123
+ }: {
124
+ filter: SearchUiFilter;
125
+ pathFilters: SearchPathFilters;
126
+ onChange: FilterPanelProps["onChange"];
127
+ }): ReactElement {
128
+ const selected = pathFilterValue(pathFilters, filter.id);
129
+ const choices = countFacetChoices(filter.options);
130
+ return (
131
+ <fieldset className="text-sm">
132
+ <legend className="mb-2 text-muted-foreground">{filter.label}</legend>
133
+ <div className="flex flex-wrap gap-1.5" role="group">
134
+ {choices.map((choice) => (
135
+ <button
136
+ key={choice.id}
137
+ type="button"
138
+ onClick={() => onChange(filter.id, Number(choice.id))}
139
+ className={cn(
140
+ "min-h-10 cursor-pointer rounded-lg border px-3 text-sm hover:bg-muted",
141
+ selected === choice.id && "border-primary bg-primary/10"
142
+ )}
143
+ >
144
+ {choice.label}
145
+ </button>
146
+ ))}
147
+ </div>
148
+ </fieldset>
149
+ );
150
+ }
151
+
152
+ function EnumFields({
153
+ filter,
154
+ pathFilters,
155
+ onChange,
156
+ }: {
157
+ filter: SearchUiFilter;
158
+ pathFilters: SearchPathFilters;
159
+ onChange: FilterPanelProps["onChange"];
160
+ }): ReactElement {
161
+ const options = filter.options || [];
162
+ const scroll = options.length > 8;
163
+ return (
164
+ <fieldset className="text-sm">
165
+ <legend className="mb-2 text-muted-foreground">{filter.label}</legend>
166
+ <div
167
+ className={cn(
168
+ "flex flex-col gap-1.5",
169
+ scroll && "max-h-48 overflow-y-auto"
170
+ )}
171
+ >
172
+ {options.map((option) => (
173
+ <label
174
+ key={option.id}
175
+ className="flex cursor-pointer items-center gap-2"
176
+ >
177
+ <input
178
+ type="checkbox"
179
+ checked={pathFilterHasOption(pathFilters, filter.id, option.id)}
180
+ onChange={() => onChange(filter.id, option.id)}
181
+ className="cursor-pointer"
182
+ />
183
+ <span>{option.label}</span>
184
+ </label>
185
+ ))}
186
+ </div>
187
+ </fieldset>
188
+ );
189
+ }
190
+
191
+ export function FilterPanel({
192
+ filters,
193
+ pathFilters,
194
+ onChange,
195
+ rangeDraft,
196
+ onRangePatch,
197
+ onRangeCommit,
198
+ }: FilterPanelProps): ReactElement {
199
+ return (
200
+ <div className="flex flex-col gap-5" data-slot="search-filters">
201
+ {filters.map((filter) => {
202
+ if (filter.type === "range") {
203
+ return (
204
+ <RangeFields
205
+ key={filter.id}
206
+ filter={filter}
207
+ rangeDraft={rangeDraft}
208
+ onRangePatch={onRangePatch}
209
+ onRangeCommit={onRangeCommit}
210
+ />
211
+ );
212
+ }
213
+ if (filter.type === "count") {
214
+ return (
215
+ <CountFields
216
+ key={filter.id}
217
+ filter={filter}
218
+ pathFilters={pathFilters}
219
+ onChange={onChange}
220
+ />
221
+ );
222
+ }
223
+ return (
224
+ <EnumFields
225
+ key={filter.id}
226
+ filter={filter}
227
+ pathFilters={pathFilters}
228
+ onChange={onChange}
229
+ />
230
+ );
231
+ })}
232
+ </div>
233
+ );
234
+ }
235
+
236
+ export function rangeDraftFromPath(
237
+ pathFilters: SearchPathFilters
238
+ ): Record<string, { min: string; max: string }> {
239
+ const price = pathFilters.price || {};
240
+ const area = pathFilters.area || {};
241
+ return {
242
+ price: {
243
+ min: price.min != null ? formatGroupedInteger(price.min) : "",
244
+ max: price.max != null ? formatGroupedInteger(price.max) : "",
245
+ },
246
+ property_area_total: {
247
+ min: area.min != null ? String(area.min) : "",
248
+ max: area.max != null ? String(area.max) : "",
249
+ },
250
+ };
251
+ }
@@ -0,0 +1,20 @@
1
+ "use client";
2
+
3
+ export { ListingCard } from "./listing-card";
4
+ export { SearchPage } from "./search-page";
5
+ export { SearchPagination } from "./search-pagination";
6
+ export { SearchQueryForm } from "./search-query-form";
7
+ export { FilterPanel, rangeDraftFromPath } from "./filter-panel";
8
+ export { ResultsSkeleton, SearchSortSelect } from "./search-chrome";
9
+ export { usePublicSearch } from "./use-public-search";
10
+ export type {
11
+ ListingCardClassNames,
12
+ ListingCardProps,
13
+ ListingCardSlots,
14
+ PublicSearchState,
15
+ SearchPageClassNames,
16
+ SearchPageVariant,
17
+ SiteIdInput,
18
+ UsePublicSearchOptions,
19
+ } from "./types";
20
+ export type { SearchPageProps } from "./search-page";
@@ -0,0 +1,143 @@
1
+ "use client";
2
+
3
+ import type { ReactElement } from "react";
4
+ import Link from "next/link";
5
+
6
+ import { formatAreaM2 } from "../presentation";
7
+ import { toListingCardModel } from "../listing-card-model";
8
+ import type { Listing } from "../types";
9
+ import { cn } from "./cn";
10
+ import type { ListingCardProps } from "./types";
11
+
12
+ function isListing(value: ListingCardProps["listing"]): value is Listing {
13
+ return (
14
+ "priceVisibility" in value && "businessType" in value && "title" in value
15
+ );
16
+ }
17
+
18
+ export function ListingCard({
19
+ listing,
20
+ href,
21
+ className,
22
+ classNames,
23
+ slots,
24
+ style,
25
+ }: ListingCardProps): ReactElement {
26
+ const model = isListing(listing) ? toListingCardModel(listing) : listing;
27
+ const target = href ?? model.href;
28
+ const areaLabel = formatAreaM2(model.areaM2);
29
+
30
+ return (
31
+ <article
32
+ data-slot="listing-card"
33
+ className={cn(
34
+ "group overflow-hidden rounded-[var(--garagem-listing-card-radius,0.75rem)] border border-border bg-background",
35
+ className,
36
+ classNames?.root
37
+ )}
38
+ style={style}
39
+ >
40
+ <Link
41
+ href={target}
42
+ className="block cursor-pointer"
43
+ data-slot="listing-card-link"
44
+ >
45
+ <div
46
+ data-slot="listing-card-media"
47
+ className={cn(
48
+ "relative w-full overflow-hidden bg-muted",
49
+ classNames?.media
50
+ )}
51
+ style={{ aspectRatio: "var(--garagem-listing-media-ratio, 4 / 3)" }}
52
+ >
53
+ {slots?.media ??
54
+ (model.coverSrc ? (
55
+ <img
56
+ src={model.coverSrc}
57
+ alt={model.coverAlt}
58
+ className="h-full w-full object-cover transition-transform duration-300 group-hover:scale-105"
59
+ />
60
+ ) : (
61
+ <div className="flex h-full w-full items-center justify-center text-xs text-muted-foreground">
62
+ Sem foto
63
+ </div>
64
+ ))}
65
+ {slots?.badge ?? (
66
+ <span
67
+ data-slot="listing-card-badge"
68
+ className={cn(
69
+ "absolute left-3 top-3 rounded-full bg-background/90 px-2 py-1 text-xs font-medium",
70
+ classNames?.badge
71
+ )}
72
+ >
73
+ {model.businessLabel}
74
+ </span>
75
+ )}
76
+ </div>
77
+ <div
78
+ data-slot="listing-card-body"
79
+ className={cn("p-4", classNames?.body)}
80
+ >
81
+ <div className="flex items-baseline justify-between gap-2">
82
+ <p
83
+ data-slot="listing-card-price"
84
+ className={cn("font-semibold tracking-tight", classNames?.price)}
85
+ style={{
86
+ fontSize: "var(--garagem-listing-price-size, 1.125rem)",
87
+ }}
88
+ >
89
+ {model.priceLabel}
90
+ </p>
91
+ {model.pricePeriod ? (
92
+ <span className="text-xs text-muted-foreground">
93
+ {model.pricePeriod}
94
+ </span>
95
+ ) : null}
96
+ </div>
97
+ <h3
98
+ data-slot="listing-card-title"
99
+ className={cn(
100
+ "mt-1 line-clamp-1 text-sm font-medium",
101
+ classNames?.title
102
+ )}
103
+ >
104
+ {model.title}
105
+ </h3>
106
+ <p
107
+ data-slot="listing-card-location"
108
+ className={cn(
109
+ "mt-0.5 line-clamp-1 text-xs text-muted-foreground",
110
+ classNames?.location
111
+ )}
112
+ >
113
+ {model.locationLabel || model.propertyType || ""}
114
+ </p>
115
+ <ul
116
+ data-slot="listing-card-features"
117
+ className={cn(
118
+ "mt-3 flex flex-wrap items-center gap-x-4 gap-y-1 text-xs text-muted-foreground",
119
+ classNames?.features
120
+ )}
121
+ >
122
+ {model.bedrooms != null ? <li>{model.bedrooms} quartos</li> : null}
123
+ {model.bathrooms != null ? (
124
+ <li>{model.bathrooms} banheiros</li>
125
+ ) : null}
126
+ {model.parkingSpaces != null ? (
127
+ <li>{model.parkingSpaces} vagas</li>
128
+ ) : null}
129
+ {areaLabel ? <li>{areaLabel}</li> : null}
130
+ </ul>
131
+ {slots?.cta ? (
132
+ <div
133
+ data-slot="listing-card-cta"
134
+ className={cn("mt-3", classNames?.cta)}
135
+ >
136
+ {slots.cta}
137
+ </div>
138
+ ) : null}
139
+ </div>
140
+ </Link>
141
+ </article>
142
+ );
143
+ }
@@ -0,0 +1,72 @@
1
+ "use client";
2
+
3
+ import type { ReactElement } from "react";
4
+
5
+ import { searchSortOptions } from "../search-engine";
6
+ import { cn } from "./cn";
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 className={cn("text-sm", className)}>
24
+ <span className="mb-1 block text-muted-foreground">Ordenar</span>
25
+ <select
26
+ id="garagem-search-sort"
27
+ value={value}
28
+ onChange={(event) => onChange(event.target.value || null)}
29
+ className="min-h-10 cursor-pointer rounded-lg border border-border bg-background px-3"
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
48
+ data-slot="search-skeleton"
49
+ className={cn(
50
+ "grid grid-cols-1 gap-[var(--garagem-listing-card-gap,1.5rem)] sm:grid-cols-2 lg:grid-cols-3",
51
+ className
52
+ )}
53
+ >
54
+ {Array.from({ length: 6 }, (_, i) => (
55
+ <div
56
+ key={i}
57
+ className="overflow-hidden rounded-[var(--garagem-listing-card-radius,0.75rem)] border border-border"
58
+ >
59
+ <div
60
+ className="animate-pulse bg-muted"
61
+ style={{ aspectRatio: "4 / 3" }}
62
+ />
63
+ <div className="space-y-2 p-4">
64
+ <div className="h-5 w-1/2 animate-pulse rounded bg-muted" />
65
+ <div className="h-4 w-3/4 animate-pulse rounded bg-muted" />
66
+ <div className="h-3 w-2/3 animate-pulse rounded bg-muted" />
67
+ </div>
68
+ </div>
69
+ ))}
70
+ </div>
71
+ );
72
+ }