@garagem-ai/site-sdk 0.4.2 → 0.4.3

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,226 @@
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
+
35
+ const WEEKDAY_SHORT: Record<string, number> = {
36
+ Sun: 0,
37
+ Mon: 1,
38
+ Tue: 2,
39
+ Wed: 3,
40
+ Thu: 4,
41
+ Fri: 5,
42
+ Sat: 6,
43
+ };
44
+
45
+ function pad2(value: number): string {
46
+ return String(value).padStart(2, "0");
47
+ }
48
+
49
+ function parseHHmm(value: string): { hour: number; minute: number } | null {
50
+ const match = HHMM_RE.exec(value);
51
+ if (!match) return null;
52
+ return { hour: Number(match[1]), minute: Number(match[2]) };
53
+ }
54
+
55
+ function minutesOfDay(hhmm: HHmm): number | null {
56
+ const parsed = parseHHmm(hhmm);
57
+ if (!parsed) return null;
58
+ return parsed.hour * 60 + parsed.minute;
59
+ }
60
+
61
+ export function expandVisitTimeSlots(hours: VisitHours): HHmm[] {
62
+ const start = minutesOfDay(hours.start);
63
+ const end = minutesOfDay(hours.end);
64
+ if (start == null || end == null || start > end) return [];
65
+ const slots: HHmm[] = [];
66
+ for (let minutes = start; minutes <= end; minutes += 60) {
67
+ const hour = Math.floor(minutes / 60);
68
+ const minute = minutes % 60;
69
+ slots.push(`${pad2(hour)}:${pad2(minute)}`);
70
+ }
71
+ return slots;
72
+ }
73
+
74
+ function unionSorted(left: HHmm[], right: HHmm[]): HHmm[] {
75
+ return Array.from(new Set([...left, ...right])).sort();
76
+ }
77
+
78
+ export function expandVisitSchedule(
79
+ config: VisitScheduleConfig
80
+ ): VisitSchedule {
81
+ const weekendHours =
82
+ config.weekend ??
83
+ (config.saturday || config.sunday ? config.weekday : null);
84
+ const weekdayTimeSlots = expandVisitTimeSlots(config.weekday);
85
+ const weekendTimeSlots = weekendHours
86
+ ? expandVisitTimeSlots(weekendHours)
87
+ : [];
88
+ const disabledDays: number[] = [];
89
+ if (!config.sunday) disabledDays.push(0);
90
+ if (!config.saturday) disabledDays.push(6);
91
+ return {
92
+ ...config,
93
+ weekend: weekendHours,
94
+ disabledDays,
95
+ weekdayTimeSlots,
96
+ weekendTimeSlots,
97
+ timeSlots: unionSorted(weekdayTimeSlots, weekendTimeSlots),
98
+ };
99
+ }
100
+
101
+ export const STUDIO_DEFAULT_VISIT_SCHEDULE: VisitSchedule = expandVisitSchedule(
102
+ {
103
+ timeZone: VISIT_TIME_ZONE,
104
+ saturday: false,
105
+ sunday: false,
106
+ weekday: { start: "06:00", end: "22:00" },
107
+ weekend: null,
108
+ minHoursAhead: 2,
109
+ }
110
+ );
111
+
112
+ function zonedParts(
113
+ date: Date,
114
+ timeZone: string
115
+ ): {
116
+ year: number;
117
+ month: number;
118
+ day: number;
119
+ hour: number;
120
+ minute: number;
121
+ weekday: number;
122
+ } {
123
+ const parts = new Intl.DateTimeFormat("en-US", {
124
+ timeZone,
125
+ weekday: "short",
126
+ year: "numeric",
127
+ month: "2-digit",
128
+ day: "2-digit",
129
+ hour: "2-digit",
130
+ minute: "2-digit",
131
+ hourCycle: "h23",
132
+ }).formatToParts(date);
133
+ const get = (type: Intl.DateTimeFormatPartTypes): string =>
134
+ parts.find((part) => part.type === type)?.value ?? "";
135
+ const weekday = WEEKDAY_SHORT[get("weekday")] ?? 0;
136
+ return {
137
+ year: Number(get("year")),
138
+ month: Number(get("month")),
139
+ day: Number(get("day")),
140
+ hour: Number(get("hour")),
141
+ minute: Number(get("minute")),
142
+ weekday,
143
+ };
144
+ }
145
+
146
+ function calendarYmd(date: Date, timeZone: string): string {
147
+ const parts = zonedParts(date, timeZone);
148
+ return `${parts.year}-${pad2(parts.month)}-${pad2(parts.day)}`;
149
+ }
150
+
151
+ function parseYmd(
152
+ value: string
153
+ ): { year: number; month: number; day: number } | null {
154
+ const match = YMD_RE.exec(value);
155
+ if (!match) return null;
156
+ const year = Number(match[1]);
157
+ const month = Number(match[2]);
158
+ const day = Number(match[3]);
159
+ const utc = new Date(Date.UTC(year, month - 1, day));
160
+ if (
161
+ utc.getUTCFullYear() !== year ||
162
+ utc.getUTCMonth() !== month - 1 ||
163
+ utc.getUTCDate() !== day
164
+ ) {
165
+ return null;
166
+ }
167
+ return { year, month, day };
168
+ }
169
+
170
+ /** Civil YYYY-MM-DD in `timeZone`. String dates must already be YMD. */
171
+ function toCalendarYmd(date: Date | string, timeZone: string): string | null {
172
+ if (typeof date === "string") {
173
+ return parseYmd(date) ? date : null;
174
+ }
175
+ if (date instanceof Date && Number.isFinite(date.getTime())) {
176
+ return calendarYmd(date, timeZone);
177
+ }
178
+ return null;
179
+ }
180
+
181
+ function weekdayOfYmd(ymd: string, timeZone: string): number | null {
182
+ const parsed = parseYmd(ymd);
183
+ if (!parsed) return null;
184
+ // 15:00 UTC is noon in America/Sao_Paulo (UTC−3), same civil day.
185
+ const utcNoonish = new Date(
186
+ Date.UTC(parsed.year, parsed.month - 1, parsed.day, 15, 0, 0)
187
+ );
188
+ return zonedParts(utcNoonish, timeZone).weekday;
189
+ }
190
+
191
+ export function slotsForDate(
192
+ schedule: VisitSchedule,
193
+ date: Date | string,
194
+ now: Date = new Date()
195
+ ): HHmm[] {
196
+ const timeZone = schedule.timeZone;
197
+ const ymd = toCalendarYmd(date, timeZone);
198
+ if (!ymd) return [];
199
+ const weekday = weekdayOfYmd(ymd, timeZone);
200
+ if (weekday == null) return [];
201
+ if (weekday === 0 && !schedule.sunday) return [];
202
+ if (weekday === 6 && !schedule.saturday) return [];
203
+ const base =
204
+ weekday === 0 || weekday === 6
205
+ ? schedule.weekendTimeSlots
206
+ : schedule.weekdayTimeSlots;
207
+ const today = calendarYmd(now, timeZone);
208
+ if (ymd < today) return [];
209
+ if (ymd !== today) return [...base];
210
+ const nowParts = zonedParts(now, timeZone);
211
+ const cutoff =
212
+ nowParts.hour * 60 + nowParts.minute + schedule.minHoursAhead * 60;
213
+ return base.filter((slot) => {
214
+ const minutes = minutesOfDay(slot);
215
+ return minutes != null && minutes >= cutoff;
216
+ });
217
+ }
218
+
219
+ export function isVisitSlotAllowed(
220
+ schedule: VisitSchedule,
221
+ visitDate: string,
222
+ visitTime: HHmm,
223
+ now: Date = new Date()
224
+ ): boolean {
225
+ return slotsForDate(schedule, visitDate, now).includes(visitTime);
226
+ }
@@ -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
  }