All files / src/hooks usePresentationSections.ts

91.46% Statements 75/82
77.58% Branches 45/58
100% Functions 23/23
91.25% Lines 73/80

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239                                              104x 52x 3x   13x                                                           2x     2x             14x 14x 1x     13x 13x 14x     14x 14x 14x     14x     14x 14x 14x     14x   14x 14x     14x       14x 14x 10x     10x     10x     10x       14x 11x       14x 14x     14x   2x 6x           2x 2x 6x     2x 3x       2x     2x   2x 3x       11x 4x         13x 13x 13x     13x   13x               6x 6x 7x     7x     7x     8x   6x 4x     2x     7x       3x                 7x 7x 7x     7x     7x     7x   7x 5x       2x     6x                    
/**
 * usePresentationSections Hook
 * Story 10.8a: Moderator Presentation Page — Functional
 *
 * Derives the ordered section array from event data.
 * Section types map 1-to-1 to slide components.
 *
 * ACs: #1, #13, #26–28
 */
 
import { useMemo } from 'react';
import type { PresentationEventDetail, PresentationSession } from '@/services/presentationService';
import type { User } from '@/types/user.types';
import type { components } from '@/types/generated/events-core-api.types';
import type { PresentationSettings } from '@/services/presentationService';
 
type TeaserImageItem = components['schemas']['TeaserImageItem'];
 
function insertTeaserImages(
  sections: PresentationSection[],
  images: TeaserImageItem[],
  position: string
): void {
  [...images]
    .filter((img) => (img.presentationPosition ?? 'AFTER_TOPIC_REVEAL') === position)
    .sort((a, b) => a.displayOrder - b.displayOrder)
    .forEach((img) => {
      sections.push({
        type: 'teaser-image',
        key: `teaser-image-${img.id}`,
        imageUrl: img.imageUrl,
      });
    });
}
 
export type SectionType =
  | 'welcome'
  | 'about'
  | 'committee'
  | 'topic-reveal'
  | 'teaser-image'
  | 'agenda-preview'
  | 'session'
  | 'break'
  | 'agenda-recap'
  | 'upcoming-events'
  | 'apero';
 
export interface PresentationSection {
  type: SectionType;
  key: string;
  session?: PresentationSession;
  /** Present only for type === 'teaser-image' (Story 10.22) */
  imageUrl?: string;
}
 
// Session types that become individual speaker slides
const SPEAKER_SESSION_TYPES = new Set(['keynote', 'presentation', 'workshop', 'panel_discussion']);
 
// Session types that are breaks/structural (break flow, not speaker slide)
const BREAK_SESSION_TYPES = new Set(['break', 'lunch', 'aperitif']);
 
export function usePresentationSections(
  event: PresentationEventDetail | null,
  sessions: PresentationSession[],
  globalTeaserImages?: TeaserImageItem[]
): PresentationSection[] {
  return useMemo(() => {
    if (!event) {
      return [];
    }
 
    const sections: PresentationSection[] = [];
    const teaserImages = event.teaserImages ?? [];
    const globalImages = globalTeaserImages ?? [];
 
    // §1 Welcome
    sections.push({ type: 'welcome', key: 'welcome' });
    insertTeaserImages(sections, globalImages, 'AFTER_WELCOME');
    insertTeaserImages(sections, teaserImages, 'AFTER_WELCOME');
 
    // §2 About
    sections.push({ type: 'about', key: 'about' });
 
    // §3 Committee
    sections.push({ type: 'committee', key: 'committee' });
    insertTeaserImages(sections, globalImages, 'AFTER_COMMITTEE');
    insertTeaserImages(sections, teaserImages, 'AFTER_COMMITTEE');
 
    // §4 Topic Reveal
    sections.push({ type: 'topic-reveal', key: 'topic-reveal' });
    // §4.5 Teaser Images at AFTER_TOPIC_REVEAL (Story 10.22 AC5; default position)
    insertTeaserImages(sections, globalImages, 'AFTER_TOPIC_REVEAL');
    insertTeaserImages(sections, teaserImages, 'AFTER_TOPIC_REVEAL');
 
    // §5 Agenda Preview
    sections.push({ type: 'agenda-preview', key: 'agenda-preview' });
 
    // Sort sessions by startTime ascending (nulls last)
    // Defensive: guard against non-array sessions (paginated API response)
    const sessionsArray = Array.isArray(sessions) ? sessions : [];
    const sortedSessions = [...sessionsArray].sort((a, b) => {
      Iif (!a.startTime && !b.startTime) {
        return 0;
      }
      Iif (!a.startTime) {
        return 1;
      }
      Iif (!b.startTime) {
        return -1;
      }
      return new Date(a.startTime).getTime() - new Date(b.startTime).getTime();
    });
 
    // Find the first break/lunch session
    const breakSession = sortedSessions.find(
      (s) => s.sessionType && BREAK_SESSION_TYPES.has(s.sessionType)
    );
 
    // Speaker sessions only — exclude moderation/networking/break/lunch
    const speakerSessions = sortedSessions.filter(
      (s) => s.sessionType && SPEAKER_SESSION_TYPES.has(s.sessionType)
    );
 
    if (breakSession?.startTime) {
      // Pre-break speaker sections
      const preBreak = speakerSessions.filter(
        (s) => s.startTime && new Date(s.startTime) < new Date(breakSession.startTime!)
      );
      // Post-break speaker sections — sessions starting AT OR AFTER break end (>=, not >).
      // Using strictly-greater-than would exclude sessions that start exactly when the
      // break ends, which is the normal case in BATbern schedules (e.g. break 19:30–20:00,
      // next talk at 20:00). Fix: use >= so boundary-equal sessions are included.
      const breakBoundary = breakSession.endTime ?? breakSession.startTime!;
      const postBreak = speakerSessions.filter(
        (s) => s.startTime && new Date(s.startTime) >= new Date(breakBoundary)
      );
 
      preBreak.forEach((s) => {
        sections.push({ type: 'session', key: `session-${s.sessionSlug}`, session: s });
      });
 
      // §N Break (sequential nav)
      sections.push({ type: 'break', key: 'break', session: breakSession });
 
      // §N+1 Agenda Recap
      sections.push({ type: 'agenda-recap', key: 'agenda-recap' });
 
      postBreak.forEach((s) => {
        sections.push({ type: 'session', key: `session-${s.sessionSlug}`, session: s });
      });
    } else {
      // No break — all speaker sessions in sequence
      speakerSessions.forEach((s) => {
        sections.push({ type: 'session', key: `session-${s.sessionSlug}`, session: s });
      });
    }
 
    // Upcoming Events
    sections.push({ type: 'upcoming-events', key: 'upcoming-events' });
    insertTeaserImages(sections, globalImages, 'AFTER_UPCOMING_EVENTS');
    insertTeaserImages(sections, teaserImages, 'AFTER_UPCOMING_EVENTS');
 
    // Apéro
    sections.push({ type: 'apero', key: 'apero' });
 
    return sections;
  }, [event, sessions, globalTeaserImages]);
}
 
/**
 * Returns the slugs of all pre-break speaker sessions (for AgendaRecap greying).
 */
export function getPreBreakSessionSlugs(sessions: PresentationSession[]): string[] {
  const safeArr = Array.isArray(sessions) ? sessions : [];
  const sorted = [...safeArr].sort((a, b) => {
    Iif (!a.startTime) {
      return 1;
    }
    Iif (!b.startTime) {
      return -1;
    }
    return new Date(a.startTime).getTime() - new Date(b.startTime).getTime();
  });
 
  const breakSession = sorted.find((s) => s.sessionType && BREAK_SESSION_TYPES.has(s.sessionType));
 
  if (!breakSession?.startTime) {
    return [];
  }
 
  return sorted
    .filter(
      (s) =>
        SPEAKER_SESSION_TYPES.has(s.sessionType ?? '') &&
        s.startTime &&
        new Date(s.startTime) < new Date(breakSession.startTime!)
    )
    .map((s) => s.sessionSlug);
}
 
/**
 * Returns the first post-break speaker session (for break resume time).
 */
export function getFirstPostBreakSession(
  sessions: PresentationSession[]
): PresentationSession | null {
  const safeArr = Array.isArray(sessions) ? sessions : [];
  const sorted = [...safeArr].sort((a, b) => {
    Iif (!a.startTime) {
      return 1;
    }
    Iif (!b.startTime) {
      return -1;
    }
    return new Date(a.startTime).getTime() - new Date(b.startTime).getTime();
  });
 
  const breakSession = sorted.find((s) => s.sessionType && BREAK_SESSION_TYPES.has(s.sessionType));
 
  if (!breakSession?.endTime) {
    return null;
  }
 
  // Use >= so a session starting exactly at break end time is included (same fix as postBreak filter above).
  return (
    sorted.find(
      (s) =>
        SPEAKER_SESSION_TYPES.has(s.sessionType ?? '') &&
        s.startTime &&
        new Date(s.startTime) >= new Date(breakSession.endTime!)
    ) ?? null
  );
}
 
export type { PresentationEventDetail, PresentationSession, PresentationSettings };
export type { User };
export type { components };