Deletion fix

This commit is contained in:
Milan Paunovic
2024-12-24 23:19:23 +01:00
parent 609d01b81c
commit 7d3e39b77d
23 changed files with 312 additions and 638 deletions

View File

@ -29,6 +29,21 @@ interface FormattedEvent {
color: string;
}
const createEventHash = (event: FormattedEvent): string => {
const startTime = 'date' in event.start ? event.start.date : event.start.dateTime;
const endTime = 'date' in event.end ? event.end.date : event.end.dateTime;
const str = `${startTime}-${endTime}-${event.title}`;
let hash = 0;
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash;
}
return hash.toString(36);
};
// Precompute time constants
const DAY_IN_MS = 24 * 60 * 60 * 1000;
const PERIOD_IN_MS = 5 * DAY_IN_MS;
@ -59,75 +74,70 @@ const getValidDate = (date: any, timestamp?: EventTimestamp): Date | null => {
};
// Batch process events
const processEvents = async (
events: Event[],
selectedDate: Date,
selectedUser: { uid: string } | null
): Promise<FormattedEvent[]> => {
// Early return if no events
if (!events.length) return [];
// Pre-calculate constants
const currentRangeKey = getDateRangeKey(selectedDate.getTime());
const isTablet = Device.deviceType === DeviceType.TABLET;
const userId = selectedUser?.uid;
// Process in chunks to avoid blocking the main thread
const uniqueEvents = new Map<string, FormattedEvent>();
const processedHashes = new Set<string>();
const CHUNK_SIZE = 100;
const results: FormattedEvent[] = [];
for (let i = 0; i < events.length; i += CHUNK_SIZE) {
const chunk = events.slice(i, i + CHUNK_SIZE);
// Process chunk and await to give UI thread a chance to breathe
await new Promise(resolve => setTimeout(resolve, 0));
for (const event of chunk) {
try {
// Quick user filter
if (isTablet && userId &&
!event.attendees?.includes(userId) &&
event.creatorId !== userId) {
continue;
}
// Validate dates first
const startDate = getValidDate(event.start, event.startDate);
if (!startDate) continue;
const rangeKey = getDateRangeKey(startDate.getTime());
// Skip events outside our range
if (Math.abs(rangeKey - currentRangeKey) > 1) continue;
const endDate = getValidDate(event.end, event.endDate);
if (!endDate) continue;
if (event.allDay) {
const dateStr = format(startDate, 'yyyy-MM-dd');
const endDateStr = format(endDate, 'yyyy-MM-dd');
const formattedEvent = event.allDay ? {
id: event.id,
title: event.title,
start: { date: format(startDate, 'yyyy-MM-dd') },
end: { date: format(endDate, 'yyyy-MM-dd') },
color: event.eventColor
} : {
id: event.id,
title: event.title,
start: {
dateTime: startDate.toISOString(),
timeZone: TIME_ZONE
},
end: {
dateTime: endDate.toISOString(),
timeZone: TIME_ZONE
},
color: event.eventColor
};
results.push({
id: event.id,
title: event.title,
start: { date: dateStr },
end: { date: endDateStr },
color: event.eventColor
});
} else {
results.push({
id: event.id,
title: event.title,
start: {
dateTime: startDate.toISOString(),
timeZone: TIME_ZONE
},
end: {
dateTime: endDate.toISOString(),
timeZone: TIME_ZONE
},
color: event.eventColor
});
const hash = createEventHash(formattedEvent);
if (!processedHashes.has(hash)) {
processedHashes.add(hash);
uniqueEvents.set(event.id, formattedEvent);
}
} catch (error) {
console.error('Error processing event:', event.id, error);
continue;
@ -135,7 +145,7 @@ const processEvents = async (
}
}
return results;
return Array.from(uniqueEvents.values());
};
export const useFormattedEvents = (