Agenda Sync refactor

This commit is contained in:
alma 2026-01-14 14:24:54 +01:00
parent de8cbc5824
commit b728e3e3f4
2 changed files with 41 additions and 28 deletions

View File

@ -198,30 +198,7 @@ export default async function CalendarPage() {
}
});
// If no calendars exist at all, create a default one
if (calendars.length === 0) {
const defaultCalendars = [
{
name: "Privée",
color: "#4F46E5",
description: "Your default calendar"
}
];
calendars = await Promise.all(
defaultCalendars.map(async (cal) => {
return prisma.calendar.create({
data: {
...cal,
userId: session?.user?.id || '',
},
include: {
events: true
}
});
})
);
}
// No default calendar creation - only synced calendars from courrier
const now = new Date();
const nextWeek = add(now, { days: 7 });

View File

@ -688,7 +688,34 @@ function EventPreview({ event, calendar }: { event: Event; calendar: Calendar })
}
export function CalendarClient({ initialCalendars, userId, userProfile }: CalendarClientProps) {
const [calendars, setCalendars] = useState(initialCalendars.map(cal => ({
// Sort calendars: synced (courrier) first, then groups, then missions
const sortCalendars = (cals: typeof initialCalendars) => {
return [...cals].sort((a, b) => {
const aIsSynced = a.syncConfig?.syncEnabled && a.syncConfig?.mailCredential;
const bIsSynced = b.syncConfig?.syncEnabled && b.syncConfig?.mailCredential;
const aIsGroup = a.name?.startsWith("Groupe:");
const bIsGroup = b.name?.startsWith("Groupe:");
const aIsMission = a.name?.startsWith("Mission:");
const bIsMission = b.name?.startsWith("Mission:");
// Synced calendars first
if (aIsSynced && !bIsSynced) return -1;
if (!aIsSynced && bIsSynced) return 1;
// If both synced or both not synced, check groups
if (aIsGroup && !bIsGroup && !bIsSynced) return -1;
if (!aIsGroup && bIsGroup && !aIsSynced) return 1;
// If both groups or both not groups, check missions
if (aIsMission && !bIsMission && !bIsGroup && !bIsSynced) return -1;
if (!aIsMission && bIsMission && !aIsGroup && !aIsSynced) return 1;
// Same type, sort by name
return (a.name || '').localeCompare(b.name || '');
});
};
const [calendars, setCalendars] = useState(sortCalendars(initialCalendars).map(cal => ({
...cal,
events: cal.events || []
})));
@ -790,7 +817,10 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
}));
console.log("Setting calendars with processed events:", processedCalendars);
setCalendars(processedCalendars);
setCalendars(sortCalendars(processedCalendars as typeof initialCalendars).map(cal => ({
...cal,
events: cal.events || []
})));
// Update statistics and upcoming events
updateStatistics();
@ -999,7 +1029,10 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
}
return cal;
});
setCalendars(updatedCalendars);
setCalendars(sortCalendars(updatedCalendars as typeof initialCalendars).map(cal => ({
...cal,
events: cal.events || []
})));
// Fetch fresh data to ensure all calendars are up to date
await fetchCalendars();
@ -1034,7 +1067,10 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend
...cal,
events: cal.events.filter(e => e.id !== selectedEvent.id)
}));
setCalendars(updatedCalendars);
setCalendars(sortCalendars(updatedCalendars as typeof initialCalendars).map(cal => ({
...cal,
events: cal.events || []
})));
// Close modal and reset form
setIsEventModalOpen(false);