perf(quick-wins): batch collaborator resolution, debounce SSE refresh, loading states

- Eliminate N+1 on resolveCollaborator: add batchResolveCollaborators() in
  auth.ts (2 DB queries max regardless of session count), update all 4
  workshop services to use post-batch mapping
- Debounce router.refresh() in useLive.ts (300ms) to group simultaneous
  SSE events and avoid cascade re-renders
- Call cleanupOldEvents fire-and-forget in createEvent to purge old SSE
  events inline without blocking the response
- Add loading.tsx skeletons on /sessions and /users matching actual page
  layout (PageHeader + content structure)
- Lazy-load ShareModal via next/dynamic in BaseSessionLiveWrapper to reduce
  initial JS bundle

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-10 08:07:22 +01:00
parent 2d266f89f9
commit a8c05aa841
10 changed files with 196 additions and 33 deletions

View File

@@ -1,5 +1,5 @@
import { prisma } from '@/services/database';
import { resolveCollaborator } from '@/services/auth';
import { resolveCollaborator, batchResolveCollaborators } from '@/services/auth';
import { getTeamMemberIdsForAdminTeams } from '@/services/teams';
import { createSessionPermissionChecks } from '@/services/session-permissions';
import { createShareAndEventHandlers } from '@/services/session-share-events';
@@ -21,7 +21,7 @@ const motivatorInclude = {
// ============================================
export async function getMotivatorSessionsByUserId(userId: string) {
return mergeSessionsByUserId(
const sessions = await mergeSessionsByUserId(
(uid) =>
prisma.movingMotivatorsSession.findMany({
where: { userId: uid },
@@ -33,14 +33,18 @@ export async function getMotivatorSessionsByUserId(userId: string) {
where: { userId: uid },
include: { session: { include: motivatorInclude } },
}),
userId,
(s) => resolveCollaborator(s.participant).then((r) => ({ resolvedParticipant: r }))
userId
);
const resolved = await batchResolveCollaborators(sessions.map((s) => s.participant));
return sessions.map((s) => ({
...s,
resolvedParticipant: resolved.get(s.participant.trim()) ?? { raw: s.participant, matchedUser: null },
}));
}
/** Sessions owned by team members (where user is team admin) that are NOT shared with the user. */
export async function getTeamCollaboratorSessionsForAdmin(userId: string) {
return fetchTeamCollaboratorSessions(
const sessions = await fetchTeamCollaboratorSessions(
(teamMemberIds, uid) =>
prisma.movingMotivatorsSession.findMany({
where: { userId: { in: teamMemberIds }, shares: { none: { userId: uid } } },
@@ -48,9 +52,13 @@ export async function getTeamCollaboratorSessionsForAdmin(userId: string) {
orderBy: { updatedAt: 'desc' },
}),
getTeamMemberIdsForAdminTeams,
userId,
(s) => resolveCollaborator(s.participant).then((r) => ({ resolvedParticipant: r }))
userId
);
const resolved = await batchResolveCollaborators(sessions.map((s) => s.participant));
return sessions.map((s) => ({
...s,
resolvedParticipant: resolved.get(s.participant.trim()) ?? { raw: s.participant, matchedUser: null },
}));
}
const motivatorByIdInclude = {