File size: 10,893 Bytes
87a665c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<script lang="ts">
	import { createEventDispatcher, getContext } from 'svelte';
	import type { CalendarEventModel, CalendarModel } from '$lib/apis/calendar';
	import CalendarEventChip from './CalendarEventChip.svelte';

	const i18n = getContext('i18n');
	const dispatch = createEventDispatcher();

	export let events: CalendarEventModel[] = [];
	export let calendars: CalendarModel[] = [];
	export let visibleCalendarIds: Set<string> = new Set();
	export let view: 'month' | 'week' | 'day' = 'month';
	export let currentDate: Date = new Date();

	const NS = 1_000_000;
	const DAY_NAMES = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];

	$: calColorMap = calendars.reduce(
		(acc, c) => ({ ...acc, [c.id]: c.color }),
		{} as Record<string, string | null>
	);
	$: filteredEvents = events.filter((e) => visibleCalendarIds.has(e.calendar_id));

	// Pre-group events by day key so the template reactively updates when events change
	$: eventsByDay = (() => {
		const map: Record<string, CalendarEventModel[]> = {};
		for (const e of filteredEvents) {
			const startMs = e.start_at / NS;
			const endMs = (e.end_at || e.start_at) / NS;
			// Get local midnight for event start/end
			const startDate = new Date(startMs);
			const endDate = new Date(endMs);
			const d = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate());
			const last = new Date(endDate.getFullYear(), endDate.getMonth(), endDate.getDate()).getTime();
			while (d.getTime() <= last) {
				const key = d.getTime().toString();
				(map[key] ??= []).push(e);
				d.setDate(d.getDate() + 1);
			}
		}
		return map;
	})();

	$: monthStart = new Date(currentDate.getFullYear(), currentDate.getMonth(), 1);
	$: calendarStart = (() => {
		const d = new Date(monthStart);
		d.setDate(d.getDate() - d.getDay());
		return d;
	})();

	$: monthDays = (() => {
		const days: Date[] = [];
		const d = new Date(calendarStart);
		for (let i = 0; i < 42; i++) {
			days.push(new Date(d));
			d.setDate(d.getDate() + 1);
		}
		return days;
	})();

	$: weekStart = (() => {
		const d = new Date(currentDate);
		d.setDate(d.getDate() - d.getDay());
		d.setHours(0, 0, 0, 0);
		return d;
	})();

	$: weekDays = (() => {
		const days: Date[] = [];
		const d = new Date(weekStart);
		for (let i = 0; i < 7; i++) {
			days.push(new Date(d));
			d.setDate(d.getDate() + 1);
		}
		return days;
	})();

	$: hours = Array.from({ length: 24 }, (_, i) => i);

	function isToday(d: Date): boolean {
		return d.toDateString() === new Date().toDateString();
	}

	function isCurrentMonth(d: Date): boolean {
		return d.getMonth() === currentDate.getMonth();
	}

	function getEventsForDay(day: Date): CalendarEventModel[] {
		const dayStartMs = new Date(day.getFullYear(), day.getMonth(), day.getDate()).getTime();
		const dayEndMs = dayStartMs + 86_400_000;
		return filteredEvents.filter((e) => {
			const startMs = e.start_at / NS;
			const endMs = (e.end_at || e.start_at) / NS;
			return startMs < dayEndMs && endMs >= dayStartMs;
		});
	}

	function getEventsForHour(
		day: Date,
		hour: number,
		eventsList: CalendarEventModel[] = filteredEvents
	): CalendarEventModel[] {
		const hourStartMs = new Date(day.getFullYear(), day.getMonth(), day.getDate(), hour).getTime();
		const hourEndMs = hourStartMs + 3_600_000;
		return eventsList.filter((e) => {
			const startMs = e.start_at / NS;
			return startMs >= hourStartMs && startMs < hourEndMs;
		});
	}

	function formatHour(h: number): string {
		if (h === 0) return '12 AM';
		if (h < 12) return `${h} AM`;
		if (h === 12) return '12 PM';
		return `${h - 12} PM`;
	}

	function handleDayClick(day: Date) {
		currentDate = day;
		const ms = new Date(day.getFullYear(), day.getMonth(), day.getDate(), 9).getTime();
		dispatch('createEvent', { start_at: ms * NS });
	}

	function goToDayView(day: Date) {
		currentDate = day;
		view = 'day';
		dispatch('viewChange', view);
		dispatch('navigate', { date: currentDate });
	}

	function handleHourClick(day: Date, hour: number) {
		currentDate = day;
		const ms = new Date(day.getFullYear(), day.getMonth(), day.getDate(), hour).getTime();
		dispatch('createEvent', { start_at: ms * NS });
	}

	function handleEventClick(event: CalendarEventModel) {
		dispatch('eventClick', event);
	}
</script>

<div class="flex flex-col h-full w-full min-h-0 min-w-0">
	<!-- Month View -->
	{#if view === 'month'}
		<div class="flex-1 flex flex-col min-h-0 px-3 pb-3">
			<div class="grid grid-cols-7">
				{#each DAY_NAMES as day}
					<div class="px-2 py-1.5 text-xs text-gray-400 dark:text-gray-500 text-left truncate">
						{$i18n.t(day)}
					</div>
				{/each}
			</div>

			<div
				class="flex-1 grid grid-cols-7 auto-rows-fr min-h-0 rounded-2xl overflow-hidden bg-white dark:bg-gray-900 border border-gray-100/30 dark:border-gray-850/30"
			>
				{#each monthDays as day, i}
					{@const dayKey = new Date(day.getFullYear(), day.getMonth(), day.getDate())
						.getTime()
						.toString()}
					{@const dayEvents = eventsByDay[dayKey] || []}
					{@const col = i % 7}
					{@const row = Math.floor(i / 7)}
					<button
						class="p-1 min-h-0 text-left overflow-hidden transition cursor-pointer flex flex-col
							{isCurrentMonth(day) ? '' : 'opacity-40'}
							hover:bg-gray-50/80 dark:hover:bg-gray-850/30
							{col > 0 ? 'border-l border-gray-100/20 dark:border-gray-850/20' : ''}
							{row > 0 ? 'border-t border-gray-100/20 dark:border-gray-850/20' : ''}"
						on:click={() => handleDayClick(day)}
					>
						<div class="flex justify-start px-0.5 mb-0.5">
							<span
								class="text-xs w-6 h-6 flex items-center justify-center rounded-full
								{isToday(day) ? 'bg-blue-500 text-white' : 'text-gray-500 dark:text-gray-400'}"
							>
								{day.getDate()}
							</span>
						</div>
						<div class="flex flex-col gap-0 flex-1 overflow-hidden">
							{#each dayEvents.slice(0, 3) as evt (evt.instance_id || evt.id)}
								<CalendarEventChip
									event={evt}
									calendarColor={calColorMap[evt.calendar_id]}
									on:click={() => handleEventClick(evt)}
								/>
							{/each}
							{#if dayEvents.length > 3}
								<!-- svelte-ignore a11y-click-events-have-key-events --><!-- svelte-ignore a11y-no-static-element-interactions -->
								<div
									class="text-[10px] text-gray-400 dark:text-gray-500 px-1 mt-auto hover:text-gray-700 dark:hover:text-gray-200 text-left w-full truncate z-10"
									on:click|stopPropagation={() => goToDayView(day)}
								>
									+{dayEvents.length - 3} more
								</div>
							{/if}
						</div>
					</button>
				{/each}
			</div>
		</div>

		<!-- Week View -->
	{:else if view === 'week'}
		<div class="flex-1 flex flex-col min-h-0 px-3 pb-3">
			<div
				class="flex-1 rounded-2xl bg-white dark:bg-gray-900 border border-gray-100/30 dark:border-gray-850/30 overflow-hidden relative"
			>
				<div class="absolute inset-0 overflow-x-auto flex flex-col">
					<div class="min-w-[700px] flex flex-col flex-1">
						<div
							class="grid grid-cols-[52px_repeat(7,1fr)] shrink-0 border-b border-gray-100/30 dark:border-gray-850/30"
						>
							<div></div>
							{#each weekDays as day}
								<div
									class="text-center py-2.5 {day.getDay() > 0
										? 'border-l border-gray-100/20 dark:border-gray-850/20'
										: ''}"
								>
									<div class="text-[11px] text-gray-400 dark:text-gray-500">
										{DAY_NAMES[day.getDay()]}
									</div>
									<div
										class="text-sm mt-0.5 w-7 h-7 flex items-center justify-center mx-auto rounded-full {isToday(
											day
										)
											? 'bg-blue-500 text-white'
											: ''}"
									>
										{day.getDate()}
									</div>
								</div>
							{/each}
						</div>

						<div class="flex-1 overflow-y-auto">
							{#each hours as hour}
								<div
									class="grid grid-cols-[52px_repeat(7,1fr)] min-h-[52px] {hour > 0
										? 'border-t border-gray-100/15 dark:border-gray-850/15'
										: ''}"
								>
									<div
										class="text-[10px] text-gray-400 dark:text-gray-500 text-right pr-2 select-none -mt-1.5 z-10"
									>
										{hour > 0 ? formatHour(hour) : ''}
									</div>
									{#each weekDays as day}
										{@const hourEvents = getEventsForHour(day, hour, filteredEvents)}
										<button
											class="px-0.5 py-0.5 {day.getDay() > 0
												? 'border-l border-gray-100/15 dark:border-gray-850/15'
												: ''} hover:bg-gray-50/50 dark:hover:bg-gray-850/20 transition cursor-pointer min-w-0 flex flex-col"
											on:click={() => handleHourClick(day, hour)}
										>
											<div class="flex flex-col gap-0.5 w-full min-h-0">
												{#each hourEvents.slice(0, 3) as evt (evt.instance_id || evt.id)}
													<CalendarEventChip
														event={evt}
														calendarColor={calColorMap[evt.calendar_id]}
														on:click={() => handleEventClick(evt)}
													/>
												{/each}
												{#if hourEvents.length > 3}
													<!-- svelte-ignore a11y-click-events-have-key-events --><!-- svelte-ignore a11y-no-static-element-interactions -->
													<div
														class="text-[10px] text-gray-400 dark:text-gray-500 px-1 mt-auto hover:text-gray-700 dark:hover:text-gray-200 text-left w-full truncate z-10"
														on:click|stopPropagation={() => goToDayView(day)}
													>
														+{hourEvents.length - 3} more
													</div>
												{/if}
											</div>
										</button>
									{/each}
								</div>
							{/each}
						</div>
					</div>
				</div>
			</div>
		</div>

		<!-- Day View -->
	{:else}
		<div class="flex-1 flex flex-col min-h-0 px-3 pb-3">
			<div
				class="flex-1 rounded-2xl overflow-hidden bg-white dark:bg-gray-900 border border-gray-100/30 dark:border-gray-850/30 overflow-y-auto"
			>
				{#each hours as hour}
					{@const hourEvents = getEventsForHour(currentDate, hour, filteredEvents)}
					<div
						class="flex min-h-[52px] {hour > 0
							? 'border-t border-gray-100/15 dark:border-gray-850/15'
							: ''}"
					>
						<div
							class="w-14 shrink-0 text-[10px] text-gray-400 dark:text-gray-500 text-right pr-3 mt-1 select-none"
						>
							{formatHour(hour)}
						</div>
						<button
							class="flex-1 border-l border-gray-100/15 dark:border-gray-850/15 px-1.5 py-0.5
								hover:bg-gray-50/50 dark:hover:bg-gray-850/20 transition cursor-pointer flex flex-col text-left justify-start"
							on:click={() => handleHourClick(currentDate, hour)}
						>
							<div class="flex flex-col gap-0.5 w-full">
								{#each hourEvents as evt (evt.instance_id || evt.id)}
									<CalendarEventChip
										event={evt}
										calendarColor={calColorMap[evt.calendar_id]}
										on:click={() => handleEventClick(evt)}
									/>
								{/each}
							</div>
						</button>
					</div>
				{/each}
			</div>
		</div>
	{/if}
</div>