@umituz/web-dashboard 8.4.0 → 8.5.0

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,210 @@
1
+ import type { CalendarItem } from '../types/view.types';
2
+
3
+ export const DAY_MINUTES = 24 * 60;
4
+ export const DEFAULT_DAY_START_MIN = 7 * 60;
5
+ export const DEFAULT_DAY_END_MIN = 21 * 60;
6
+ export const DEFAULT_MAX_PER_DAY = 3;
7
+ export const SLOT_QUANTUM_MIN = 30;
8
+
9
+ function pad(value: number): string {
10
+ return String(value).padStart(2, '0');
11
+ }
12
+
13
+ export function parseDayKey(dayKey: string): Date {
14
+ const [year, month, day] = dayKey.split('-').map(Number);
15
+ return new Date(year ?? 1970, (month ?? 1) - 1, day ?? 1);
16
+ }
17
+
18
+ export function toDayKey(date: Date): string {
19
+ return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
20
+ }
21
+
22
+ export function todayKey(): string {
23
+ return toDayKey(new Date());
24
+ }
25
+
26
+ export function addDaysKey(dayKey: string, days: number): string {
27
+ const date = parseDayKey(dayKey);
28
+ date.setDate(date.getDate() + days);
29
+ return toDayKey(date);
30
+ }
31
+
32
+ export function addMonthsKey(dayKey: string, months: number): string {
33
+ const date = parseDayKey(dayKey);
34
+ const dayOfMonth = date.getDate();
35
+ date.setDate(1);
36
+ date.setMonth(date.getMonth() + months);
37
+ const lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
38
+ date.setDate(Math.min(dayOfMonth, lastDay));
39
+ return toDayKey(date);
40
+ }
41
+
42
+ export function startOfWeekKey(dayKey: string, weekStartsOn: 0 | 1 = 1): string {
43
+ const back = (parseDayKey(dayKey).getDay() + 7 - weekStartsOn) % 7;
44
+ return addDaysKey(dayKey, -back);
45
+ }
46
+
47
+ export function weekDays(anchorKey: string, weekStartsOn: 0 | 1 = 1): string[] {
48
+ const start = startOfWeekKey(anchorKey, weekStartsOn);
49
+ return Array.from({ length: 7 }, (_, index) => addDaysKey(start, index));
50
+ }
51
+
52
+ export function monthMatrix(anchorKey: string, weekStartsOn: 0 | 1 = 1): string[] {
53
+ const firstOfMonth = `${anchorKey.slice(0, 7)}-01`;
54
+ const start = startOfWeekKey(firstOfMonth, weekStartsOn);
55
+ return Array.from({ length: 42 }, (_, index) => addDaysKey(start, index));
56
+ }
57
+
58
+ export function sameMonth(firstKey: string, secondKey: string): boolean {
59
+ return firstKey.slice(0, 7) === secondKey.slice(0, 7);
60
+ }
61
+
62
+ export function formatWeekdayShort(dayKey: string, locale = 'en-US'): string {
63
+ return parseDayKey(dayKey).toLocaleDateString(locale, { weekday: 'short' });
64
+ }
65
+
66
+ export function formatDayNumber(dayKey: string): string {
67
+ return String(Number(dayKey.slice(8)));
68
+ }
69
+
70
+ export function formatMonthTitle(anchorKey: string, locale = 'en-US'): string {
71
+ return parseDayKey(anchorKey).toLocaleDateString(locale, { month: 'long', year: 'numeric' });
72
+ }
73
+
74
+ export function formatWeekTitle(anchorKey: string, locale = 'en-US'): string {
75
+ const days = weekDays(anchorKey);
76
+ const first = parseDayKey(days[0] ?? anchorKey);
77
+ const last = parseDayKey(days[6] ?? anchorKey);
78
+ const left = first.toLocaleDateString(locale, { month: 'short', day: 'numeric' });
79
+ const rightOptions: Intl.DateTimeFormatOptions =
80
+ first.getMonth() === last.getMonth()
81
+ ? { day: 'numeric', year: 'numeric' }
82
+ : { month: 'short', day: 'numeric', year: 'numeric' };
83
+ return `${left} – ${last.toLocaleDateString(locale, rightOptions)}`;
84
+ }
85
+
86
+ export function formatDayTitle(dayKey: string, locale = 'en-US'): string {
87
+ return parseDayKey(dayKey).toLocaleDateString(locale, {
88
+ weekday: 'long',
89
+ month: 'long',
90
+ day: 'numeric',
91
+ year: 'numeric',
92
+ });
93
+ }
94
+
95
+ export function minutesToLabel(minutes: number): string {
96
+ const normalized = ((Math.round(minutes) % DAY_MINUTES) + DAY_MINUTES) % DAY_MINUTES;
97
+ return `${pad(Math.floor(normalized / 60))}:${pad(normalized % 60)}`;
98
+ }
99
+
100
+ export interface CalendarSpan {
101
+ startMin: number;
102
+ endMin: number;
103
+ }
104
+
105
+ export function itemSpan(item: CalendarItem): CalendarSpan {
106
+ const startMin = item.startMin ?? 0;
107
+ const endMin = item.endMin ?? Math.min(startMin + 60, DAY_MINUTES);
108
+ return { startMin, endMin: Math.max(endMin, startMin) };
109
+ }
110
+
111
+ export function spansOverlap(first: CalendarSpan, second: CalendarSpan): boolean {
112
+ return first.startMin < second.endMin && second.startMin < first.endMin;
113
+ }
114
+
115
+ export function findConflicts<T extends CalendarItem>(items: readonly T[]): T[] {
116
+ const sorted = [...items]
117
+ .map((item) => ({ item, span: itemSpan(item) }))
118
+ .sort((first, second) => first.span.startMin - second.span.startMin);
119
+ const conflicted = new Set<string>();
120
+ for (let index = 0; index < sorted.length; index += 1) {
121
+ for (let other = index + 1; other < sorted.length; other += 1) {
122
+ if (sorted[other]!.span.startMin >= sorted[index]!.span.endMin) break;
123
+ conflicted.add(sorted[index]!.item.id);
124
+ conflicted.add(sorted[other]!.item.id);
125
+ }
126
+ }
127
+ return items.filter((item) => conflicted.has(item.id));
128
+ }
129
+
130
+ export interface PositionedItem<T extends CalendarItem> {
131
+ item: T;
132
+ column: number;
133
+ columns: number;
134
+ }
135
+
136
+ export function layoutColumns<T extends CalendarItem>(items: readonly T[]): PositionedItem<T>[] {
137
+ const sorted = [...items].sort((first, second) => {
138
+ const firstSpan = itemSpan(first);
139
+ const secondSpan = itemSpan(second);
140
+ return firstSpan.startMin === secondSpan.startMin
141
+ ? secondSpan.endMin - firstSpan.endMin
142
+ : firstSpan.startMin - secondSpan.startMin;
143
+ });
144
+
145
+ const placed: PositionedItem<T>[] = [];
146
+ let cluster: PositionedItem<T>[] = [];
147
+ let clusterEnd = -1;
148
+
149
+ const flush = () => {
150
+ for (const entry of cluster) entry.columns = cluster.length;
151
+ placed.push(...cluster);
152
+ cluster = [];
153
+ clusterEnd = -1;
154
+ };
155
+
156
+ for (const item of sorted) {
157
+ const span = itemSpan(item);
158
+ if (cluster.length > 0 && span.startMin >= clusterEnd) flush();
159
+ clusterEnd = Math.max(clusterEnd, span.endMin);
160
+ const column = cluster.findIndex((entry) => itemSpan(entry.item).endMin <= span.startMin);
161
+ cluster.push({ item, column: column === -1 ? cluster.length : column, columns: 1 });
162
+ }
163
+ flush();
164
+ return placed;
165
+ }
166
+
167
+ export function eventGeometry(
168
+ item: CalendarItem,
169
+ dayStartMin: number,
170
+ dayEndMin: number,
171
+ ): { top: number; height: number } {
172
+ const span = itemSpan(item);
173
+ const total = Math.max(dayEndMin - dayStartMin, 1);
174
+ const start = (span.startMin - dayStartMin) / total;
175
+ const end = Math.min((span.endMin - dayStartMin) / total, 1);
176
+ const clampedStart = Math.max(start, 0);
177
+ return { top: clampedStart * 100, height: Math.max(end - clampedStart, 0.012) * 100 };
178
+ }
179
+
180
+ export interface DaySlot {
181
+ startMin: number;
182
+ endMin: number;
183
+ taken: boolean;
184
+ }
185
+
186
+ export function generateDaySlots(
187
+ options: { startMin: number; endMin: number; durationMin: number; stepMin?: number },
188
+ booked: readonly CalendarSpan[] = [],
189
+ ): DaySlot[] {
190
+ const step = options.stepMin ?? options.durationMin;
191
+ const slots: DaySlot[] = [];
192
+ for (let start = options.startMin; start + options.durationMin <= options.endMin; start += step) {
193
+ const slot = { startMin: start, endMin: start + options.durationMin };
194
+ slots.push({ ...slot, taken: booked.some((span) => spansOverlap(slot, span)) });
195
+ }
196
+ return slots;
197
+ }
198
+
199
+ export function groupByDayKey<T extends CalendarItem>(items: readonly T[]): Map<string, T[]> {
200
+ const map = new Map<string, T[]>();
201
+ for (const item of items) {
202
+ const bucket = map.get(item.dayKey);
203
+ if (bucket) bucket.push(item);
204
+ else map.set(item.dayKey, [item]);
205
+ }
206
+ for (const bucket of map.values()) {
207
+ bucket.sort((first, second) => itemSpan(first).startMin - itemSpan(second).startMin);
208
+ }
209
+ return map;
210
+ }