@garagem-ai/site-sdk 0.4.2 → 0.4.4

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,236 @@
1
+ /**
2
+ * Visit-lead schedule: hours in, slots out. Pure — no fetch, no React.
3
+ * Calendar dates are civil dates in `timeZone` (America/Sao_Paulo), never
4
+ * UTC midnight from `new Date("YYYY-MM-DD")`.
5
+ */
6
+
7
+ export const VISIT_TIME_ZONE = "America/Sao_Paulo" as const;
8
+
9
+ export type HHmm = string;
10
+
11
+ export type VisitHours = {
12
+ start: HHmm;
13
+ end: HHmm;
14
+ };
15
+
16
+ export type VisitScheduleConfig = {
17
+ timeZone: typeof VISIT_TIME_ZONE;
18
+ saturday: boolean;
19
+ sunday: boolean;
20
+ weekday: VisitHours;
21
+ weekend: VisitHours | null;
22
+ minHoursAhead: number;
23
+ };
24
+
25
+ export type VisitSchedule = VisitScheduleConfig & {
26
+ disabledDays: number[];
27
+ weekdayTimeSlots: HHmm[];
28
+ weekendTimeSlots: HHmm[];
29
+ timeSlots: HHmm[];
30
+ };
31
+
32
+ const HHMM_RE = /^([01]\d|2[0-3]):([0-5]\d)$/;
33
+ const YMD_RE = /^(\d{4})-(\d{2})-(\d{2})$/;
34
+ const ISO_INSTANT_RE =
35
+ /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/;
36
+
37
+ const WEEKDAY_SHORT: Record<string, number> = {
38
+ Sun: 0,
39
+ Mon: 1,
40
+ Tue: 2,
41
+ Wed: 3,
42
+ Thu: 4,
43
+ Fri: 5,
44
+ Sat: 6,
45
+ };
46
+
47
+ function pad2(value: number): string {
48
+ return String(value).padStart(2, "0");
49
+ }
50
+
51
+ function parseHHmm(value: string): { hour: number; minute: number } | null {
52
+ const match = HHMM_RE.exec(value);
53
+ if (!match) return null;
54
+ return { hour: Number(match[1]), minute: Number(match[2]) };
55
+ }
56
+
57
+ function minutesOfDay(hhmm: HHmm): number | null {
58
+ const parsed = parseHHmm(hhmm);
59
+ if (!parsed) return null;
60
+ return parsed.hour * 60 + parsed.minute;
61
+ }
62
+
63
+ export function expandVisitTimeSlots(hours: VisitHours): HHmm[] {
64
+ const start = minutesOfDay(hours.start);
65
+ const end = minutesOfDay(hours.end);
66
+ if (start == null || end == null || start > end) return [];
67
+ const slots: HHmm[] = [];
68
+ for (let minutes = start; minutes <= end; minutes += 60) {
69
+ const hour = Math.floor(minutes / 60);
70
+ const minute = minutes % 60;
71
+ slots.push(`${pad2(hour)}:${pad2(minute)}`);
72
+ }
73
+ return slots;
74
+ }
75
+
76
+ function unionSorted(left: HHmm[], right: HHmm[]): HHmm[] {
77
+ return Array.from(new Set([...left, ...right])).sort();
78
+ }
79
+
80
+ export function expandVisitSchedule(
81
+ config: VisitScheduleConfig
82
+ ): VisitSchedule {
83
+ const weekendHours =
84
+ config.weekend ??
85
+ (config.saturday || config.sunday ? config.weekday : null);
86
+ const weekdayTimeSlots = expandVisitTimeSlots(config.weekday);
87
+ const weekendTimeSlots = weekendHours
88
+ ? expandVisitTimeSlots(weekendHours)
89
+ : [];
90
+ const disabledDays: number[] = [];
91
+ if (!config.sunday) disabledDays.push(0);
92
+ if (!config.saturday) disabledDays.push(6);
93
+ return {
94
+ ...config,
95
+ weekend: weekendHours,
96
+ disabledDays,
97
+ weekdayTimeSlots,
98
+ weekendTimeSlots,
99
+ timeSlots: unionSorted(weekdayTimeSlots, weekendTimeSlots),
100
+ };
101
+ }
102
+
103
+ export const STUDIO_DEFAULT_VISIT_SCHEDULE: VisitSchedule = expandVisitSchedule(
104
+ {
105
+ timeZone: VISIT_TIME_ZONE,
106
+ saturday: false,
107
+ sunday: false,
108
+ weekday: { start: "06:00", end: "22:00" },
109
+ weekend: null,
110
+ minHoursAhead: 2,
111
+ }
112
+ );
113
+
114
+ function zonedParts(
115
+ date: Date,
116
+ timeZone: string
117
+ ): {
118
+ year: number;
119
+ month: number;
120
+ day: number;
121
+ hour: number;
122
+ minute: number;
123
+ weekday: number;
124
+ } {
125
+ const parts = new Intl.DateTimeFormat("en-US", {
126
+ timeZone,
127
+ weekday: "short",
128
+ year: "numeric",
129
+ month: "2-digit",
130
+ day: "2-digit",
131
+ hour: "2-digit",
132
+ minute: "2-digit",
133
+ hourCycle: "h23",
134
+ }).formatToParts(date);
135
+ const get = (type: Intl.DateTimeFormatPartTypes): string =>
136
+ parts.find((part) => part.type === type)?.value ?? "";
137
+ const weekday = WEEKDAY_SHORT[get("weekday")] ?? 0;
138
+ return {
139
+ year: Number(get("year")),
140
+ month: Number(get("month")),
141
+ day: Number(get("day")),
142
+ hour: Number(get("hour")),
143
+ minute: Number(get("minute")),
144
+ weekday,
145
+ };
146
+ }
147
+
148
+ function calendarYmd(date: Date, timeZone: string): string {
149
+ const parts = zonedParts(date, timeZone);
150
+ return `${parts.year}-${pad2(parts.month)}-${pad2(parts.day)}`;
151
+ }
152
+
153
+ function parseYmd(
154
+ value: string
155
+ ): { year: number; month: number; day: number } | null {
156
+ const match = YMD_RE.exec(value);
157
+ if (!match) return null;
158
+ const year = Number(match[1]);
159
+ const month = Number(match[2]);
160
+ const day = Number(match[3]);
161
+ const utc = new Date(Date.UTC(year, month - 1, day));
162
+ if (
163
+ utc.getUTCFullYear() !== year ||
164
+ utc.getUTCMonth() !== month - 1 ||
165
+ utc.getUTCDate() !== day
166
+ ) {
167
+ return null;
168
+ }
169
+ return { year, month, day };
170
+ }
171
+
172
+ /** Normalize a civil date or ISO instant to YYYY-MM-DD in `timeZone`. */
173
+ export function normalizeVisitDate(
174
+ date: Date | string,
175
+ timeZone: string = VISIT_TIME_ZONE
176
+ ): string | null {
177
+ if (typeof date === "string") {
178
+ if (parseYmd(date)) return date;
179
+ if (!ISO_INSTANT_RE.test(date)) return null;
180
+ const instant = new Date(date);
181
+ return Number.isFinite(instant.getTime())
182
+ ? calendarYmd(instant, timeZone)
183
+ : null;
184
+ }
185
+ if (date instanceof Date && Number.isFinite(date.getTime())) {
186
+ return calendarYmd(date, timeZone);
187
+ }
188
+ return null;
189
+ }
190
+
191
+ function weekdayOfYmd(ymd: string, timeZone: string): number | null {
192
+ const parsed = parseYmd(ymd);
193
+ if (!parsed) return null;
194
+ // 15:00 UTC is noon in America/Sao_Paulo (UTC−3), same civil day.
195
+ const utcNoonish = new Date(
196
+ Date.UTC(parsed.year, parsed.month - 1, parsed.day, 15, 0, 0)
197
+ );
198
+ return zonedParts(utcNoonish, timeZone).weekday;
199
+ }
200
+
201
+ export function slotsForDate(
202
+ schedule: VisitSchedule,
203
+ date: Date | string,
204
+ now: Date = new Date()
205
+ ): HHmm[] {
206
+ const timeZone = schedule.timeZone;
207
+ const ymd = normalizeVisitDate(date, timeZone);
208
+ if (!ymd) return [];
209
+ const weekday = weekdayOfYmd(ymd, timeZone);
210
+ if (weekday == null) return [];
211
+ if (weekday === 0 && !schedule.sunday) return [];
212
+ if (weekday === 6 && !schedule.saturday) return [];
213
+ const base =
214
+ weekday === 0 || weekday === 6
215
+ ? schedule.weekendTimeSlots
216
+ : schedule.weekdayTimeSlots;
217
+ const today = calendarYmd(now, timeZone);
218
+ if (ymd < today) return [];
219
+ if (ymd !== today) return [...base];
220
+ const nowParts = zonedParts(now, timeZone);
221
+ const cutoff =
222
+ nowParts.hour * 60 + nowParts.minute + schedule.minHoursAhead * 60;
223
+ return base.filter((slot) => {
224
+ const minutes = minutesOfDay(slot);
225
+ return minutes != null && minutes >= cutoff;
226
+ });
227
+ }
228
+
229
+ export function isVisitSlotAllowed(
230
+ schedule: VisitSchedule,
231
+ visitDate: Date | string,
232
+ visitTime: HHmm,
233
+ now: Date = new Date()
234
+ ): boolean {
235
+ return slotsForDate(schedule, visitDate, now).includes(visitTime);
236
+ }
@@ -15,10 +15,10 @@ export const STUDIO_SDK_CONTRACT = {
15
15
  /**
16
16
  * Exact pin for generated Studio sites. Must be ≥ `selfInjectSince` so
17
17
  * prompts that skip the layout chrome tag match the tarball v0 installs.
18
- * This PR’s changeset publishes that tarball as 0.4.2 — do not generate
18
+ * This PR’s changeset publishes that tarball as 0.4.3 — do not generate
19
19
  * against this floor until Version Packages is on npm.
20
20
  */
21
- minNpmVersion: "0.4.2",
21
+ minNpmVersion: "0.4.3",
22
22
  /** First release where ListingCard and ResultsSkeleton inject the sheet. */
23
23
  selfInjectSince: "0.4.2",
24
24
  chromeStrategy: "self-inject" as const,
@@ -40,6 +40,17 @@ export const STUDIO_SDK_CONTRACT = {
40
40
  SearchChromeStyles: "0.4.1",
41
41
  } as const,
42
42
  forbiddenImports: ["@garagem-ai/site-sdk/react/search.css"] as const,
43
+ leadStrategy: "visit-default" as const,
44
+ leadExports: [
45
+ "getLeadUi",
46
+ "submitVisitLead",
47
+ "submitContactLead",
48
+ "slotsForDate",
49
+ "expandVisitSchedule",
50
+ "expandVisitTimeSlots",
51
+ "isVisitSlotAllowed",
52
+ "STUDIO_DEFAULT_VISIT_SCHEDULE",
53
+ ] as const,
43
54
  } as const;
44
55
 
45
56
  export type StudioSdkContract = typeof STUDIO_SDK_CONTRACT;
@@ -47,6 +58,14 @@ export type StudioSdkContract = typeof STUDIO_SDK_CONTRACT;
47
58
  const REACT_SDK_NAMED_IMPORT_RE =
48
59
  /(?:^|[\n;])\s*import\s+(?!type\b)(?:type\s+)?\{([^}]+)\}\s+from\s+['"]@garagem-ai\/site-sdk\/react['"]/g;
49
60
 
61
+ /** Showcase POST route — not GET …/leads/ui (getLeadUi schema). */
62
+ export const SHOWCASE_LEADS_POST_URL_RE =
63
+ /\/api\/sites\/[^\s"']*\/leads(?!\/ui)/;
64
+
65
+ export function hasShowcaseLeadsPostUrl(text: string): boolean {
66
+ return SHOWCASE_LEADS_POST_URL_RE.test(text);
67
+ }
68
+
50
69
  export function coerceNpmVersion(raw: unknown): string | null {
51
70
  if (typeof raw !== "string") return null;
52
71
  const match = raw.trim().match(/(\d+)\.(\d+)\.(\d+)/);
@@ -107,6 +126,11 @@ export function studioSdkPinInstruction(): string {
107
126
  return `In \`package.json\` set \`"@garagem-ai/site-sdk": "${version}"\` (exact). Import only these \`${STUDIO_SDK_PACKAGE}/react\` names: ${names}.`;
108
127
  }
109
128
 
129
+ export function studioLeadInstruction(): string {
130
+ const names = STUDIO_SDK_CONTRACT.leadExports.join(", ");
131
+ return `Listing-detail visit/contact leads: import ${names} from \`${STUDIO_SDK_PACKAGE}\` (the main package, not \`/react\`). Call \`submitVisitLead\` / \`submitContactLead\` — never concatenate a leads webhook URL and never POST the Showcase demo leads route.`;
132
+ }
133
+
110
134
  export function studioChromeInstruction(): string {
111
135
  const injectors =
112
136
  compareSemver(
@@ -134,5 +158,11 @@ export function studioCodegenContractViolations(text: string): string[] {
134
158
  violations.push(name);
135
159
  }
136
160
  }
161
+ if (hasShowcaseLeadsPostUrl(text)) {
162
+ violations.push("showcase-leads-url");
163
+ }
164
+ if (/\btype\s*=\s*["']date["']/.test(text)) {
165
+ violations.push("native-date-input");
166
+ }
137
167
  return violations;
138
168
  }