File size: 14,325 Bytes
5a81b95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/**
 * CalendarWidget - Multi-Calendar Unified View
 * Integrates Google Calendar, Outlook, Apple Calendar
 * 
 * Features:
 * - Auto-discovery of calendar sources
 * - Unified timeline view
 * - Quick meeting join
 * - Event creation
 * - Broadcasting calendar events to other widgets
 */

import React, { useEffect, useState } from 'react';
import { Calendar, Video, Users, Clock, Plus, ChevronLeft, ChevronRight } from 'lucide-react';
import { useLiveData } from '@/hooks/useLiveData';
import { useWidgetCommunication } from '@/contexts/WidgetContext';
import { cn } from '@/lib/utils';

interface CalendarEvent {
    id: string;
    title: string;
    start: string;
    end: string;
    attendees?: string[];
    location?: string;
    meetingLink?: string;
    calendar: string;
    color?: string;
}

interface CalendarWidgetProps {
    widgetId: string;
}

export default function CalendarWidget({ widgetId }: CalendarWidgetProps) {
    const [selectedDate, setSelectedDate] = useState(new Date());
    const [viewMode, setViewMode] = useState<'day' | 'week' | 'month'>('day');

    // AUTO-DISCOVERY: Connect to calendar sources
    const {
        data: calendarData,
        connected,
        recommendedSources,
        connectionStatus,
        refetch,
    } = useLiveData({
        widgetId,
        widgetType: 'calendar',
        requiredSources: ['google-calendar', 'outlook-calendar'],
        optionalSources: ['apple-calendar', 'notion-calendar'],
        autoConnect: true,
        pollInterval: 60000, // Refresh every minute
    });

    // INTER-WIDGET COMMUNICATION
    const { broadcastEvent, subscribeToEvent } = useWidgetCommunication(widgetId);

    // Broadcast upcoming meeting
    useEffect(() => {
        if (calendarData?.events) {
            const now = new Date();
            const upcomingMeeting = calendarData.events.find((event: CalendarEvent) => {
                const start = new Date(event.start);
                const diff = start.getTime() - now.getTime();
                return diff > 0 && diff < 5 * 60 * 1000; // 5 minutes before
            });

            if (upcomingMeeting) {
                broadcastEvent({
                    type: 'calendar.meeting.upcoming',
                    data: {
                        meetingId: upcomingMeeting.id,
                        title: upcomingMeeting.title,
                        start: upcomingMeeting.start,
                        attendees: upcomingMeeting.attendees,
                        meetingLink: upcomingMeeting.meetingLink,
                    },
                });
            }
        }
    }, [calendarData]);

    // Listen to task completion events to update calendar
    useEffect(() => {
        const unsub = subscribeToEvent('task.completed', (event) => {
            // Remove related calendar block if task is done
            if (event.data.calendarBlockId) {
                // TODO: Remove calendar block
            }
        });

        return () => unsub();
    }, []);

    const handleJoinMeeting = (meeting: CalendarEvent) => {
        if (meeting.meetingLink) {
            window.open(meeting.meetingLink, '_blank');

            broadcastEvent({
                type: 'calendar.meeting.joined',
                data: {
                    meetingId: meeting.id,
                    title: meeting.title,
                },
            });
        }
    };

    const todayEvents = calendarData?.events?.filter((event: CalendarEvent) => {
        const eventDate = new Date(event.start);
        return eventDate.toDateString() === selectedDate.toDateString();
    }) || [];

    const nextMeeting = todayEvents.filter((event: CalendarEvent) => {
        return new Date(event.start) > new Date();
    })[0];

    return (
        <div className="h-full flex flex-col bg-gradient-to-br from-blue-500/10 to-purple-500/10 backdrop-blur-sm border border-blue-500/30 rounded-lg overflow-hidden">
            {/* Header */}
            <div className="p-4 border-b border-blue-500/20 bg-gradient-to-r from-blue-500/10 to-transparent">
                <div className="flex items-center justify-between mb-3">
                    <div className="flex items-center gap-2">
                        <Calendar className="w-5 h-5 text-blue-400" />
                        <span className="font-display text-sm uppercase tracking-wider text-blue-400">
                            Calendar
                        </span>
                    </div>

                    {/* View Mode Toggle */}
                    <div className="flex gap-1 bg-black/20 rounded p-1">
                        {(['day', 'week', 'month'] as const).map((mode) => (
                            <button
                                key={mode}
                                onClick={() => setViewMode(mode)}
                                className={cn(
                                    "px-2 py-1 rounded text-xs transition-all",
                                    viewMode === mode
                                        ? "bg-blue-500 text-white"
                                        : "text-blue-300 hover:bg-blue-500/20"
                                )}
                            >
                                {mode}
                            </button>
                        ))}
                    </div>
                </div>

                {/* Date Navigator */}
                <div className="flex items-center justify-between">
                    <button
                        onClick={() => {
                            const newDate = new Date(selectedDate);
                            newDate.setDate(newDate.getDate() - 1);
                            setSelectedDate(newDate);
                        }}
                        className="p-1 hover:bg-blue-500/20 rounded transition-colors"
                    >
                        <ChevronLeft className="w-4 h-4 text-blue-300" />
                    </button>

                    <div className="text-center">
                        <div className="text-lg font-semibold text-white">
                            {selectedDate.toLocaleDateString('en-US', { weekday: 'short', month: 'short', day: 'numeric' })}
                        </div>
                        <div className="text-xs text-blue-300">
                            {todayEvents.length} events
                        </div>
                    </div>

                    <button
                        onClick={() => {
                            const newDate = new Date(selectedDate);
                            newDate.setDate(newDate.getDate() + 1);
                            setSelectedDate(newDate);
                        }}
                        className="p-1 hover:bg-blue-500/20 rounded transition-colors"
                    >
                        <ChevronRight className="w-4 h-4 text-blue-300" />
                    </button>
                </div>
            </div>

            {/* Connection Status */}
            {!connected && (
                <div className="p-3 bg-orange-500/10 border-b border-orange-500/30">
                    <div className="flex items-center gap-2 text-xs text-orange-400">
                        <Clock className="w-4 h-4" />
                        <span>Connecting to calendar sources...</span>
                    </div>
                </div>
            )}

            {/* Next Meeting Highlight */}
            {nextMeeting && (
                <div className="p-3 bg-green-500/10 border-b border-green-500/30">
                    <div className="flex items-center justify-between">
                        <div>
                            <div className="text-xs text-green-400 mb-1">Up Next</div>
                            <div className="text-sm font-medium text-white">{nextMeeting.title}</div>
                            <div className="text-xs text-green-300">
                                {new Date(nextMeeting.start).toLocaleTimeString('en-US', {
                                    hour: 'numeric',
                                    minute: '2-digit'
                                })}
                            </div>
                        </div>
                        {nextMeeting.meetingLink && (
                            <button
                                onClick={() => handleJoinMeeting(nextMeeting)}
                                className="flex items-center gap-2 px-3 py-2 bg-green-500 text-white rounded text-xs hover:bg-green-600 transition-colors"
                            >
                                <Video className="w-3 h-3" />
                                Join
                            </button>
                        )}
                    </div>
                </div>
            )}

            {/* Events List */}
            <div className="flex-1 overflow-y-auto p-3 space-y-2">
                {connected && todayEvents.length === 0 && (
                    <div className="text-center py-8">
                        <Calendar className="w-8 h-8 text-blue-400/30 mx-auto mb-2" />
                        <p className="text-sm text-blue-300/50">No events for this day</p>
                    </div>
                )}

                {todayEvents.map((event: CalendarEvent) => {
                    const isPast = new Date(event.end) < new Date();
                    const isNow = new Date(event.start) <= new Date() && new Date(event.end) >= new Date();

                    return (
                        <div
                            key={event.id}
                            className={cn(
                                "p-3 rounded-lg border transition-all",
                                isNow && "bg-green-500/20 border-green-500/50",
                                !isNow && !isPast && "bg-blue-500/10 border-blue-500/30 hover:bg-blue-500/20",
                                isPast && "bg-gray-500/10 border-gray-500/20 opacity-50"
                            )}
                        >
                            <div className="flex items-start justify-between gap-2">
                                <div className="flex-1">
                                    <div className="flex items-center gap-2 mb-1">
                                        <div
                                            className="w-2 h-2 rounded-full"
                                            style={{ backgroundColor: event.color || '#3b82f6' }}
                                        />
                                        <span className="text-sm font-medium text-white">
                                            {event.title}
                                        </span>
                                    </div>

                                    <div className="flex items-center gap-3 text-xs text-blue-300">
                                        <div className="flex items-center gap-1">
                                            <Clock className="w-3 h-3" />
                                            {new Date(event.start).toLocaleTimeString('en-US', {
                                                hour: 'numeric',
                                                minute: '2-digit'
                                            })}
                                        </div>

                                        {event.attendees && event.attendees.length > 0 && (
                                            <div className="flex items-center gap-1">
                                                <Users className="w-3 h-3" />
                                                {event.attendees.length}
                                            </div>
                                        )}
                                    </div>

                                    {event.location && (
                                        <div className="text-xs text-blue-300/70 mt-1">
                                            📍 {event.location}
                                        </div>
                                    )}
                                </div>

                                {event.meetingLink && !isPast && (
                                    <button
                                        onClick={() => handleJoinMeeting(event)}
                                        className="p-1.5 bg-blue-500/20 hover:bg-blue-500/30 rounded transition-colors"
                                    >
                                        <Video className="w-4 h-4 text-blue-400" />
                                    </button>
                                )}
                            </div>
                        </div>
                    );
                })}
            </div>

            {/* Source Recommendations */}
            {recommendedSources.length > 0 && (
                <div className="p-3 border-t border-blue-500/20 bg-orange-500/10">
                    <div className="text-xs text-orange-400 mb-2">
                        Missing Calendar Sources:
                    </div>
                    {recommendedSources.map((source) => (
                        <div key={source.id} className="flex items-center justify-between mb-1">
                            <span className="text-xs text-orange-300">{source.name}</span>
                            <button
                                onClick={() => {
                                    // Enable source
                                    refetch();
                                }}
                                className="px-2 py-1 bg-orange-500 text-white rounded text-xs"
                            >
                                Enable
                            </button>
                        </div>
                    ))}
                </div>
            )}

            {/* Quick Actions */}
            <div className="p-3 border-t border-blue-500/20 bg-black/20">
                <button className="w-full flex items-center justify-center gap-2 px-3 py-2 bg-blue-500 hover:bg-blue-600 text-white rounded transition-colors">
                    <Plus className="w-4 h-4" />
                    <span className="text-sm">New Event</span>
                </button>
            </div>
        </div>
    );
}