Skip to content

Commit 5fb5ff6

Browse files
committed
refactor(calendar): use built-in DateFnsAdapter from @taskgenius/calendar
Remove redundant custom date adapter implementations in favor of the DateFnsAdapter exported directly from @taskgenius/calendar library. - Delete local DateFnsAdapter utility class - Remove inline NormalizedDateFnsAdapter from calendar component - Import DateFnsAdapter from @taskgenius/calendar package
1 parent 82a2020 commit 5fb5ff6

File tree

2 files changed

+3
-476
lines changed

2 files changed

+3
-476
lines changed

src/components/features/calendar/index.ts

Lines changed: 3 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
import {
2626
Calendar,
2727
type DateAdapter,
28+
DateFnsAdapter,
2829
hideWeekends,
2930
workingHours,
3031
onlyDays,
@@ -82,171 +83,6 @@ export interface CalendarEvent extends Task {
8283
badge?: boolean;
8384
}
8485

85-
class NormalizedDateFnsAdapter implements DateAdapter<Date> {
86-
constructor(private readonly fns: typeof dateFns) {}
87-
88-
private normalizeFormat(formatStr?: string) {
89-
if (!formatStr) return formatStr;
90-
// Align legacy moment-style tokens with date-fns v4 requirements
91-
return formatStr
92-
.replace(/Y{2,4}/g, (token) => token.toLowerCase())
93-
.replace(/D{1,2}/g, (token) => token.toLowerCase());
94-
}
95-
96-
create(date?: string | Date) {
97-
if (!date) return new Date();
98-
if (date instanceof Date) return new Date(date);
99-
return this.fns.parseISO(date);
100-
}
101-
102-
parse(dateStr: string, formatStr?: string) {
103-
const normalizedFormat = this.normalizeFormat(formatStr);
104-
if (!normalizedFormat) {
105-
return this.fns.parseISO(dateStr);
106-
}
107-
return this.fns.parse(dateStr, normalizedFormat, new Date());
108-
}
109-
110-
format(date: Date, formatStr: string) {
111-
return this.fns.format(
112-
date,
113-
this.normalizeFormat(formatStr) ?? formatStr,
114-
);
115-
}
116-
117-
year(date: Date) {
118-
return this.fns.getYear(date);
119-
}
120-
121-
month(date: Date) {
122-
return this.fns.getMonth(date);
123-
}
124-
125-
date(date: Date) {
126-
return this.fns.getDate(date);
127-
}
128-
129-
day(date: Date) {
130-
return this.fns.getDay(date);
131-
}
132-
133-
hour(date: Date) {
134-
return this.fns.getHours(date);
135-
}
136-
137-
minute(date: Date) {
138-
return this.fns.getMinutes(date);
139-
}
140-
141-
setHour(date: Date, hour: number) {
142-
return this.fns.setHours(date, hour);
143-
}
144-
145-
setMinute(date: Date, minute: number) {
146-
return this.fns.setMinutes(date, minute);
147-
}
148-
149-
add(date: Date, amount: number, unit: string) {
150-
switch (unit) {
151-
case "year":
152-
return this.fns.addYears(date, amount);
153-
case "month":
154-
return this.fns.addMonths(date, amount);
155-
case "week":
156-
return this.fns.addWeeks(date, amount);
157-
case "day":
158-
return this.fns.addDays(date, amount);
159-
case "hour":
160-
return this.fns.addHours(date, amount);
161-
case "minute":
162-
return this.fns.addMinutes(date, amount);
163-
default:
164-
return date;
165-
}
166-
}
167-
168-
diff(date1: Date, date2: Date, unit: string) {
169-
switch (unit) {
170-
case "year":
171-
return this.fns.differenceInYears(date1, date2);
172-
case "month":
173-
return this.fns.differenceInMonths(date1, date2);
174-
case "week":
175-
return this.fns.differenceInWeeks(date1, date2);
176-
case "day":
177-
return this.fns.differenceInDays(date1, date2);
178-
case "hour":
179-
return this.fns.differenceInHours(date1, date2);
180-
case "minute":
181-
return this.fns.differenceInMinutes(date1, date2);
182-
default:
183-
return 0;
184-
}
185-
}
186-
187-
startOf(date: Date, unit: string) {
188-
switch (unit) {
189-
case "year":
190-
return this.fns.startOfYear(date);
191-
case "month":
192-
return this.fns.startOfMonth(date);
193-
case "week":
194-
return this.fns.startOfWeek(date);
195-
case "day":
196-
return this.fns.startOfDay(date);
197-
case "hour":
198-
return this.fns.startOfHour(date);
199-
case "minute":
200-
return this.fns.startOfMinute(date);
201-
default:
202-
return date;
203-
}
204-
}
205-
206-
endOf(date: Date, unit: string) {
207-
switch (unit) {
208-
case "year":
209-
return this.fns.endOfYear(date);
210-
case "month":
211-
return this.fns.endOfMonth(date);
212-
case "week":
213-
return this.fns.endOfWeek(date);
214-
case "day":
215-
return this.fns.endOfDay(date);
216-
case "hour":
217-
return this.fns.endOfHour(date);
218-
case "minute":
219-
return this.fns.endOfMinute(date);
220-
default:
221-
return date;
222-
}
223-
}
224-
225-
isBefore(date1: Date, date2: Date, unit?: string) {
226-
if (!unit) return this.fns.isBefore(date1, date2);
227-
return this.fns.isBefore(
228-
this.startOf(date1, unit),
229-
this.startOf(date2, unit),
230-
);
231-
}
232-
233-
isAfter(date1: Date, date2: Date, unit?: string) {
234-
if (!unit) return this.fns.isAfter(date1, date2);
235-
return this.fns.isAfter(
236-
this.startOf(date1, unit),
237-
this.startOf(date2, unit),
238-
);
239-
}
240-
241-
isSame(date1: Date, date2: Date, unit?: string) {
242-
if (!unit) return this.fns.isEqual(date1, date2);
243-
return this.fns.isEqual(
244-
this.startOf(date1, unit),
245-
this.startOf(date2, unit),
246-
);
247-
}
248-
}
249-
25086
/**
25187
* Main CalendarComponent class
25288
*/
@@ -886,7 +722,7 @@ export class CalendarComponent extends Component {
886722
},
887723
// Use custom view registry for agenda/year views
888724
viewRegistry: isAgendaOrYear ? this.viewRegistry : undefined,
889-
dateAdapter: new NormalizedDateFnsAdapter(dateFns),
725+
dateAdapter: new DateFnsAdapter(dateFns),
890726
events: this.convertTasksToTGEvents(),
891727
showEventCounts: cc.showEventCounts ?? true,
892728
dateFormats: {
@@ -974,7 +810,7 @@ export class CalendarComponent extends Component {
974810
)
975811
: undefined,
976812
},
977-
dateAdapter: new NormalizedDateFnsAdapter(dateFns),
813+
dateAdapter: new DateFnsAdapter(dateFns),
978814
events: this.convertTasksToTGEvents(),
979815
showEventCounts: true,
980816
dateFormats: {

0 commit comments

Comments
 (0)