Skip to content

Commit 234ee3a

Browse files
authored
Merge pull request #21 from BuilderIO/ai_main_f031f053e868
Update from the Builder.io agent
2 parents 7696aea + 45d0ce0 commit 234ee3a

File tree

11 files changed

+1456
-286
lines changed

11 files changed

+1456
-286
lines changed
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
import { useState, useMemo } from "react";
2+
import { format, parseISO, parse, isValid } from "date-fns";
3+
import {
4+
Calendar,
5+
CalendarDays,
6+
CalendarRange,
7+
Clock,
8+
Plus,
9+
Zap,
10+
ArrowRight,
11+
} from "lucide-react";
12+
import {
13+
CommandDialog,
14+
CommandInput,
15+
CommandList,
16+
CommandEmpty,
17+
CommandGroup,
18+
CommandItem,
19+
CommandShortcut,
20+
CommandSeparator,
21+
} from "@/components/ui/command";
22+
import { cn } from "@/lib/utils";
23+
import type { CalendarEvent } from "@shared/api";
24+
25+
type ViewMode = "month" | "week" | "day";
26+
27+
interface CommandPaletteProps {
28+
open: boolean;
29+
onClose: () => void;
30+
events: CalendarEvent[];
31+
onGoToDate: (date: Date) => void;
32+
onEventClick: (event: CalendarEvent) => void;
33+
onCreateEvent: () => void;
34+
onViewChange: (view: ViewMode) => void;
35+
onToday: () => void;
36+
}
37+
38+
const DATE_FORMATS = [
39+
"MM/dd/yyyy",
40+
"MM/dd",
41+
"MMMM d",
42+
"MMM d",
43+
"yyyy-MM-dd",
44+
"M/d",
45+
"MMMM d, yyyy",
46+
];
47+
48+
export function CommandPalette({
49+
open,
50+
onClose,
51+
events,
52+
onGoToDate,
53+
onEventClick,
54+
onCreateEvent,
55+
onViewChange,
56+
onToday,
57+
}: CommandPaletteProps) {
58+
const [query, setQuery] = useState("");
59+
60+
const parsedDate = useMemo(() => {
61+
if (!query.trim()) return null;
62+
for (const fmt of DATE_FORMATS) {
63+
try {
64+
const d = parse(query.trim(), fmt, new Date());
65+
if (isValid(d) && d.getFullYear() > 1970) return d;
66+
} catch {
67+
// continue
68+
}
69+
}
70+
return null;
71+
}, [query]);
72+
73+
const matchingEvents = useMemo(() => {
74+
if (!query.trim()) return [];
75+
const q = query.toLowerCase();
76+
return events
77+
.filter((e) => e.title.toLowerCase().includes(q))
78+
.sort((a, b) => new Date(a.start).getTime() - new Date(b.start).getTime())
79+
.slice(0, 6);
80+
}, [query, events]);
81+
82+
function run(fn: () => void) {
83+
fn();
84+
setQuery("");
85+
onClose();
86+
}
87+
88+
function handleOpenChange(open: boolean) {
89+
if (!open) {
90+
setQuery("");
91+
onClose();
92+
}
93+
}
94+
95+
return (
96+
<CommandDialog open={open} onOpenChange={handleOpenChange}>
97+
<CommandInput
98+
placeholder="Search events, go to date, or run a command…"
99+
value={query}
100+
onValueChange={setQuery}
101+
/>
102+
<CommandList>
103+
<CommandEmpty>
104+
No results. Try typing a date like "Jan 15" or an event name.
105+
</CommandEmpty>
106+
107+
{parsedDate && (
108+
<CommandGroup heading="Jump to">
109+
<CommandItem onSelect={() => run(() => onGoToDate(parsedDate))}>
110+
<Calendar className="mr-2 h-4 w-4" />
111+
Go to {format(parsedDate, "MMMM d, yyyy")}
112+
<CommandShortcut>
113+
<ArrowRight className="h-3 w-3" />
114+
</CommandShortcut>
115+
</CommandItem>
116+
</CommandGroup>
117+
)}
118+
119+
{matchingEvents.length > 0 && (
120+
<CommandGroup heading="Events">
121+
{matchingEvents.map((event) => (
122+
<CommandItem
123+
key={event.id}
124+
onSelect={() => run(() => onEventClick(event))}
125+
>
126+
<span
127+
className={cn(
128+
"mr-2 h-2 w-2 shrink-0 rounded-full",
129+
event.color
130+
? ""
131+
: event.source === "google"
132+
? "bg-emerald-500"
133+
: "bg-primary",
134+
)}
135+
style={event.color ? { background: event.color } : undefined}
136+
/>
137+
<span className="flex-1 truncate">{event.title}</span>
138+
<span className="ml-2 text-xs text-muted-foreground">
139+
{format(parseISO(event.start), "MMM d")}
140+
</span>
141+
</CommandItem>
142+
))}
143+
</CommandGroup>
144+
)}
145+
146+
<CommandSeparator />
147+
148+
<CommandGroup heading="Actions">
149+
<CommandItem onSelect={() => run(onCreateEvent)}>
150+
<Plus className="mr-2 h-4 w-4" />
151+
Create new event
152+
<CommandShortcut>C</CommandShortcut>
153+
</CommandItem>
154+
<CommandItem onSelect={() => run(onToday)}>
155+
<Zap className="mr-2 h-4 w-4" />
156+
Go to today
157+
<CommandShortcut>T</CommandShortcut>
158+
</CommandItem>
159+
</CommandGroup>
160+
161+
<CommandSeparator />
162+
163+
<CommandGroup heading="Views">
164+
<CommandItem onSelect={() => run(() => onViewChange("month"))}>
165+
<CalendarDays className="mr-2 h-4 w-4" />
166+
Month view
167+
<CommandShortcut>M</CommandShortcut>
168+
</CommandItem>
169+
<CommandItem onSelect={() => run(() => onViewChange("week"))}>
170+
<CalendarRange className="mr-2 h-4 w-4" />
171+
Week view
172+
<CommandShortcut>W</CommandShortcut>
173+
</CommandItem>
174+
<CommandItem onSelect={() => run(() => onViewChange("day"))}>
175+
<Clock className="mr-2 h-4 w-4" />
176+
Day view
177+
<CommandShortcut>D</CommandShortcut>
178+
</CommandItem>
179+
</CommandGroup>
180+
</CommandList>
181+
</CommandDialog>
182+
);
183+
}

templates/calendar/client/components/calendar/CreateEventDialog.tsx

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState } from "react";
1+
import { useState, useEffect, useRef } from "react";
22
import { format } from "date-fns";
33
import {
44
Dialog,
@@ -38,6 +38,20 @@ export function CreateEventDialog({
3838
const [allDay, setAllDay] = useState(false);
3939

4040
const createEvent = useCreateEvent();
41+
const formRef = useRef<HTMLFormElement>(null);
42+
43+
// ⌘+Enter to submit
44+
useEffect(() => {
45+
if (!open) return;
46+
function onKey(e: KeyboardEvent) {
47+
if ((e.metaKey || e.ctrlKey) && e.key === "Enter") {
48+
e.preventDefault();
49+
formRef.current?.requestSubmit();
50+
}
51+
}
52+
window.addEventListener("keydown", onKey);
53+
return () => window.removeEventListener("keydown", onKey);
54+
}, [open]);
4155

4256
function resetForm() {
4357
setTitle("");
@@ -91,7 +105,7 @@ export function CreateEventDialog({
91105
<DialogTitle>New Event</DialogTitle>
92106
</DialogHeader>
93107

94-
<form onSubmit={handleSubmit} className="space-y-4">
108+
<form ref={formRef} onSubmit={handleSubmit} className="space-y-4">
95109
<div className="space-y-2">
96110
<Label htmlFor="event-title">Title</Label>
97111
<Input
@@ -162,12 +176,19 @@ export function CreateEventDialog({
162176
/>
163177
</div>
164178

165-
<DialogFooter>
166-
<Button type="button" variant="ghost" onClick={onClose}>
179+
<DialogFooter className="items-center gap-2">
180+
<p className="mr-auto text-xs text-muted-foreground/60">
181+
Press{" "}
182+
<kbd className="rounded border border-border bg-muted px-1 font-mono text-[10px]">
183+
⌘↵
184+
</kbd>{" "}
185+
to save
186+
</p>
187+
<Button type="button" variant="ghost" size="sm" onClick={onClose}>
167188
Cancel
168189
</Button>
169-
<Button type="submit" disabled={createEvent.isPending}>
170-
{createEvent.isPending ? "Creating..." : "Create Event"}
190+
<Button type="submit" size="sm" disabled={createEvent.isPending}>
191+
{createEvent.isPending ? "Creating" : "Create Event"}
171192
</Button>
172193
</DialogFooter>
173194
</form>

0 commit comments

Comments
 (0)