Spaces:
Sleeping
Sleeping
Download apps/web/src/components/ui/CalComBooking.tsx from Benaah/MicProtocol: direct link, hf CLI and curl.
- Browser
- Download file 10.9 kB
-
https://huggingface.co/spaces/Benaah/MicProtocol/resolve/main/apps/web/src/components/ui/CalComBooking.tsx
- Command line
-
hf download hf://spaces/Benaah/MicProtocol/apps/web/src/components/ui/CalComBooking.tsx
-
curl -L -o CalComBooking.tsx https://huggingface.co/spaces/Benaah/MicProtocol/resolve/main/apps/web/src/components/ui/CalComBooking.tsx
10.9 kB
| "use client"; | |
| import React, { useState, useCallback, useEffect, useRef } from "react"; | |
| import { trpc } from "@/lib/trpc"; | |
| import { useToast } from "@/lib/toast"; | |
| import { io, Socket } from "socket.io-client"; | |
| import { Calendar, Clock, User, Mail, CheckCircle, X, Plus } from "lucide-react"; | |
| interface CalComEventType { | |
| id: number; | |
| title: string; | |
| slug: string; | |
| length: number; | |
| description?: string; | |
| active?: boolean; | |
| } | |
| interface CalComBookingProps { | |
| className?: string; | |
| style?: React.CSSProperties; | |
| } | |
| export function CalComBooking({ className, style }: CalComBookingProps) { | |
| const { data: workspaces } = trpc.workspace.list.useQuery(undefined, { | |
| retry: false, | |
| refetchOnWindowFocus: false, | |
| }); | |
| const workspace = workspaces?.[0] as any; | |
| const workspaceId = workspace?.id; | |
| const toast = useToast(); | |
| const [selectedEventType, setSelectedEventType] = useState<CalComEventType | null>(null); | |
| const [startTime, setStartTime] = useState(""); | |
| const [attendeeName, setAttendeeName] = useState(""); | |
| const [attendeeEmail, setAttendeeEmail] = useState(""); | |
| const [attendeeTimeZone, setAttendeeTimeZone] = useState(Intl.DateTimeFormat().resolvedOptions().timeZone); | |
| const [meetingUrl, setMeetingUrl] = useState(""); | |
| const [bookingPending, setBookingPending] = useState(false); | |
| const [view, setView] = useState<"event-types" | "booking-form">("event-types"); | |
| const socketRef = useRef<Socket | null>(null); | |
| const eventTypesQuery = trpc.integrations.listCalComEventTypes.useQuery( | |
| { workspaceId: workspaceId! }, | |
| { enabled: !!workspaceId, retry: false }, | |
| ); | |
| const createBooking = trpc.integrations.createCalComBooking.useMutation({ | |
| onSuccess: () => { | |
| toast.success("Booking created successfully!"); | |
| setView("event-types"); | |
| setSelectedEventType(null); | |
| setStartTime(""); | |
| setAttendeeName(""); | |
| setAttendeeEmail(""); | |
| setMeetingUrl(""); | |
| }, | |
| onError: (err) => { | |
| toast.error(err.message || "Failed to create booking"); | |
| }, | |
| onSettled: () => { | |
| setBookingPending(false); | |
| }, | |
| }); | |
| // WebSocket connection for real-time Cal.com booking events | |
| useEffect(() => { | |
| if (!workspaceId) return; | |
| const apiUrl = process.env.NEXT_PUBLIC_API_URL || "http://localhost:4000"; | |
| const token = localStorage.getItem("auth_token"); | |
| if (!token) return; | |
| const socket = io(apiUrl, { | |
| transports: ["websocket", "polling"], | |
| auth: { token }, | |
| }); | |
| socketRef.current = socket; | |
| socket.on("connect", () => { | |
| // Join workspace room to receive Cal.com booking events | |
| socket.emit("join:workspace", { workspaceId }); | |
| }); | |
| socket.on("calcom:booking:created", ({ booking }: { booking: unknown }) => { | |
| toast.success("Cal.com booking created successfully"); | |
| }); | |
| socket.on("calcom:booking:cancelled", ({ bookingId }: { bookingId: string }) => { | |
| toast.error("Cal.com booking was cancelled"); | |
| }); | |
| socket.on("calcom:booking:rescheduled", ({ bookingId }: { bookingId: string }) => { | |
| toast.warning("Cal.com booking was rescheduled"); | |
| }); | |
| return () => { | |
| socket.close(); | |
| socketRef.current = null; | |
| }; | |
| }, [workspaceId, toast]); | |
| const handleCreateBooking = () => { | |
| if (!workspaceId) { | |
| toast.error("No active workspace found."); | |
| return; | |
| } | |
| if (!selectedEventType) { | |
| toast.error("Please select an event type."); | |
| return; | |
| } | |
| if (!startTime) { | |
| toast.error("Please select a date and time."); | |
| return; | |
| } | |
| if (!attendeeName || !attendeeEmail) { | |
| toast.error("Please enter your name and email."); | |
| return; | |
| } | |
| setBookingPending(true); | |
| createBooking.mutate({ | |
| workspaceId, | |
| eventTypeId: selectedEventType.id, | |
| start: startTime, | |
| attendee: { | |
| email: attendeeEmail, | |
| name: attendeeName, | |
| timeZone: attendeeTimeZone, | |
| }, | |
| meetingUrl: meetingUrl || undefined, | |
| }); | |
| }; | |
| const isLoading = !eventTypesQuery.data; | |
| const eventTypes = eventTypesQuery.data ?? []; | |
| if (isLoading) { | |
| return ( | |
| <div className="rounded-xl border border-border-color bg-bg-elevated p-6" style={{ borderColor: "var(--border-color)" }}> | |
| <p className="text-text-secondary text-sm">Loading event types...</p> | |
| </div> | |
| ); | |
| } | |
| if (view === "booking-form" && selectedEventType) { | |
| return ( | |
| <div | |
| className={["rounded-xl border border-border-color bg-bg-elevated p-6 shadow-md", className].filter(Boolean).join(" ")} | |
| style={{ borderColor: "var(--border-color)", ...style }} | |
| > | |
| <div className="flex items-center justify-between mb-4"> | |
| <button | |
| onClick={() => setView("event-types")} | |
| className="flex items-center gap-1.5 text-text-tertiary hover:text-text-primary transition-colors" | |
| > | |
| <X size={14} /> Back | |
| </button> | |
| <div className="flex items-center gap-2"> | |
| <Calendar size={14} className="text-text-tertiary" /> | |
| <span className="text-text-primary font-medium text-sm">{selectedEventType.title}</span> | |
| </div> | |
| </div> | |
| <p className="text-text-secondary text-sm mb-6"> | |
| {selectedEventType.description || `A ${selectedEventType.length} minute session`} | |
| </p> | |
| <div className="space-y-4"> | |
| <div> | |
| <label className="text-text-tertiary text-xs uppercase tracking-wider font-medium mb-2 block"> | |
| Date & Time | |
| </label> | |
| <input | |
| type="datetime-local" | |
| value={startTime} | |
| onChange={(e) => setStartTime(e.target.value)} | |
| className="input w-full" | |
| /> | |
| </div> | |
| <div> | |
| <label className="text-text-tertiary text-xs uppercase tracking-wider font-medium mb-2 block"> | |
| Your Name | |
| </label> | |
| <input | |
| type="text" | |
| value={attendeeName} | |
| onChange={(e) => setAttendeeName(e.target.value)} | |
| placeholder="e.g. Jane Doe" | |
| className="input w-full" | |
| /> | |
| </div> | |
| <div> | |
| <label className="text-text-tertiary text-xs uppercase tracking-wider font-medium mb-2 block"> | |
| Email Address | |
| </label> | |
| <input | |
| type="email" | |
| value={attendeeEmail} | |
| onChange={(e) => setAttendeeEmail(e.target.value)} | |
| placeholder="e.g. jane@example.com" | |
| className="input w-full" | |
| /> | |
| </div> | |
| <div> | |
| <label className="text-text-tertiary text-xs uppercase tracking-wider font-medium mb-2 block"> | |
| Time Zone | |
| </label> | |
| <input | |
| type="text" | |
| value={attendeeTimeZone} | |
| onChange={(e) => setAttendeeTimeZone(e.target.value)} | |
| placeholder="e.g. Africa/Nairobi" | |
| className="input w-full" | |
| /> | |
| </div> | |
| <div> | |
| <label className="text-text-tertiary text-xs uppercase tracking-wider font-medium mb-2 block"> | |
| Meeting URL (optional) | |
| </label> | |
| <input | |
| type="url" | |
| value={meetingUrl} | |
| onChange={(e) => setMeetingUrl(e.target.value)} | |
| placeholder="e.g. https://meet.example.com/room" | |
| className="input w-full" | |
| /> | |
| </div> | |
| <button | |
| onClick={handleCreateBooking} | |
| disabled={bookingPending || createBooking.isPending} | |
| className="btn btn-primary w-full py-3 disabled:opacity-50" | |
| > | |
| {bookingPending || createBooking.isPending ? ( | |
| <> | |
| <Plus size={14} className="animate-spin" /> | |
| Creating booking... | |
| </> | |
| ) : ( | |
| `Confirm ${selectedEventType.length}-min booking` | |
| )} | |
| </button> | |
| </div> | |
| </div> | |
| ); | |
| } | |
| return ( | |
| <div | |
| className={["rounded-xl border border-border-color bg-bg-elevated p-6", className].filter(Boolean).join(" ")} | |
| style={{ borderColor: "var(--border-color)", ...style }} | |
| > | |
| <div className="flex items-center justify-between mb-6"> | |
| <div> | |
| <h3 className="text-text-primary font-semibold text-lg">Schedule with Cal.com</h3> | |
| <p className="text-text-secondary text-sm mt-1"> | |
| Choose an event type to book a time | |
| </p> | |
| </div> | |
| <div className="w-10 h-10 rounded-md bg-bg-tertiary flex items-center justify-center border border-border-color-subtle"> | |
| <Calendar size={16} className="text-text-tertiary" /> | |
| </div> | |
| </div> | |
| {eventTypes.length === 0 ? ( | |
| <div className="text-center py-8"> | |
| <p className="text-text-tertiary text-sm"> | |
| No event types available. Please connect and configure Cal.com in Settings > Integrations. | |
| </p> | |
| </div> | |
| ) : ( | |
| <div className="grid grid-cols-1 sm:grid-cols-2 gap-4"> | |
| {eventTypes.map((et) => ( | |
| <div | |
| key={et.id} | |
| onClick={() => { | |
| setSelectedEventType(et); | |
| setView("booking-form"); | |
| }} | |
| className="card cursor-pointer hover:border-primary transition-colors" | |
| style={{ borderColor: "var(--border-color)" }} | |
| > | |
| <div className="flex items-start justify-between gap-3"> | |
| <div className="flex-1"> | |
| <div className="flex items-center gap-2 mb-2"> | |
| <span className="text-text-tertiary text-xs uppercase tracking-wider font-medium"> | |
| {et.length} min | |
| </span> | |
| {et.active !== false && ( | |
| <span className="w-2 h-2 rounded-full bg-color-accent" /> | |
| )} | |
| </div> | |
| <h4 className="text-text-primary font-semibold text-sm">{et.title}</h4> | |
| {et.description && ( | |
| <p className="text-text-secondary text-xs mt-1 line-clamp-2">{et.description}</p> | |
| )} | |
| </div> | |
| <div className="w-8 h-8 rounded-full bg-bg-tertiary flex items-center justify-center text-text-tertiary"> | |
| <Plus size={12} /> | |
| </div> | |
| </div> | |
| </div> | |
| ))} | |
| </div> | |
| )} | |
| {!workspaceId && ( | |
| <div className="mt-4 p-3 rounded-md bg-bg-tertiary border border-border-color-subtle"> | |
| <p className="text-text-tertiary text-xs"> | |
| No active workspace found. Please select a workspace to use Cal.com booking. | |
| </p> | |
| </div> | |
| )} | |
| </div> | |
| ); | |
| } | |