All files / src/components/organizer/SpeakerStatus getPrimaryAction.ts

70% Statements 21/30
94.73% Branches 18/19
33.33% Functions 4/12
68.96% Lines 20/29

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                                                                                          56x 63x   63x 56x 56x                                                                     77x   77x   14x                 8x                 15x 15x             15x                     9x                   18x                   1x                         8x 8x 2x             6x                   4x                        
/**
 * Primary-action helper for the speaker kanban card (Story 11.D.2).
 *
 * Maps a speaker's current workflow state to the single most-likely next action,
 * surfaced as a full-width button along the bottom edge of the card. The mapping is
 * the source of truth from `docs/plans/speaker-workflow-refactor.md` §8.2.
 *
 * Two return shapes:
 *   - `kind: 'button'`  → render an MUI `<Button>` (the common case).
 *   - `kind: 'chip'`    → render an MUI `<Chip>` (only QUALITY_REVIEWED with assigned slot).
 *
 * The helper is pure — no React, no hooks — so it can be unit-tested directly.
 */
import type { TFunction } from 'i18next';
import type { SpeakerPoolEntry, SpeakerWorkflowState } from '@/types/speakerPool.types';
 
export interface PrimaryActionCallbacks {
  onLogOutreach: (speaker: SpeakerPoolEntry) => void;
  onPromoteSpeaker: (speaker: SpeakerPoolEntry) => void;
  onSendInvitation: (speaker: SpeakerPoolEntry) => void;
  /** Opens drawer for the ACCEPTED card. Story 11.D.4 — drawer's Content sub-tab. */
  onEnterContent: (speaker: SpeakerPoolEntry) => void;
  /** Opens drawer for the CONTENT_SUBMITTED card. Story 11.D.4 — drawer's Quality Review sub-view. */
  onReviewContent: (speaker: SpeakerPoolEntry) => void;
  onSpeakerClick: (speaker: SpeakerPoolEntry) => void;
  onAssignSessionSlot: (speaker: SpeakerPoolEntry) => void;
}
 
export interface SlotCapacityState {
  reached: boolean;
  invited: number;
  accepted: number;
  slots: number;
}
 
/**
 * Computes slot-capacity from the speaker pool + the event's max slots.
 * Post-acceptance states (CONTENT_SUBMITTED, QUALITY_REVIEWED) still occupy a slot
 * per ADR-009. Pure function — kanban + drawer call this to share the same gate
 * (Story 11.D.4 review patch — drawer must honor the same gate as the kanban).
 */
export function computeSlotCapacity(
  speakers: ReadonlyArray<SpeakerPoolEntry>,
  maxSlots: number | undefined
): SlotCapacityState {
  const acceptedCount = speakers.filter((s) =>
    ['ACCEPTED', 'CONTENT_SUBMITTED', 'QUALITY_REVIEWED'].includes(s.status)
  ).length;
  const invitedCount = speakers.filter((s) => s.status === 'INVITED').length;
  const max = maxSlots ?? 0;
  return {
    reached: max > 0 && acceptedCount + invitedCount >= max,
    invited: invitedCount,
    accepted: acceptedCount,
    slots: max,
  };
}
 
export type PrimaryAction =
  | {
      kind: 'button';
      label: string;
      onClick: () => void;
      disabled: boolean;
      tooltip?: string;
      testIdSuffix: string;
    }
  | {
      kind: 'chip';
      label: string;
      tooltip?: string;
      testIdSuffix: string;
    }
  | { kind: 'none' };
 
/**
 * Returns the primary action for a card based on its current workflow state.
 * Returns `{ kind: 'none' }` for unknown states (defensive — should not happen in practice).
 */
export function getPrimaryAction(
  speaker: SpeakerPoolEntry,
  callbacks: PrimaryActionCallbacks,
  slotCapacity: SlotCapacityState,
  t: TFunction
): PrimaryAction {
  const status = speaker.status as SpeakerWorkflowState;
 
  switch (status) {
    case 'IDENTIFIED':
      return {
        kind: 'button',
        label: t('organizer:speakerCard.primaryAction.logOutreach'),
        onClick: () => callbacks.onLogOutreach(speaker),
        disabled: false,
        testIdSuffix: 'log-outreach',
      };
 
    case 'CONTACTED':
      return {
        kind: 'button',
        label: t('organizer:speakerCard.primaryAction.promoteToSpeaker'),
        onClick: () => callbacks.onPromoteSpeaker(speaker),
        disabled: false,
        testIdSuffix: 'promote-to-speaker',
      };
 
    case 'READY': {
      const disabled = slotCapacity.reached;
      const tooltip = disabled
        ? t('organizer:speakerCard.slotCapacityTooltip', {
            invited: slotCapacity.invited,
            accepted: slotCapacity.accepted,
            slots: slotCapacity.slots,
          })
        : undefined;
      return {
        kind: 'button',
        label: t('organizer:speakerCard.primaryAction.sendInvitation'),
        onClick: () => callbacks.onSendInvitation(speaker),
        disabled,
        tooltip,
        testIdSuffix: 'send-invitation',
      };
    }
 
    case 'INVITED':
      return {
        kind: 'button',
        label: t('organizer:speakerCard.primaryAction.viewResponseStatus'),
        onClick: () => callbacks.onSpeakerClick(speaker),
        disabled: false,
        testIdSuffix: 'view-response-status',
      };
 
    case 'ACCEPTED':
      // Story 11.D.4 — opens drawer pre-positioned at the on-behalf content form.
      return {
        kind: 'button',
        label: t('organizer:speakerCard.primaryAction.enterContent'),
        onClick: () => callbacks.onEnterContent(speaker),
        disabled: false,
        testIdSuffix: 'enter-content',
      };
 
    case 'CONTENT_SUBMITTED':
      // Story 11.D.4 — opens drawer pre-positioned at the Quality Review sub-view.
      return {
        kind: 'button',
        label: t('organizer:speakerCard.primaryAction.reviewContent'),
        onClick: () => callbacks.onReviewContent(speaker),
        disabled: false,
        testIdSuffix: 'review-content',
      };
 
    case 'QUALITY_REVIEWED': {
      // `isSlotAssigned` is a derived flag computed at read time (Story 11.B.3) —
      // strict-true check avoids treating `undefined` (e.g. stale snapshot before the
      // derivation ran) as "no slot". Fall back to `sessionId != null` as a defensive
      // secondary signal that matches the existing session-lookup pattern.
      const hasAssignedSlot = speaker.isSlotAssigned === true || speaker.sessionId != null;
      if (hasAssignedSlot) {
        return {
          kind: 'chip',
          label: t('organizer:speakerCard.publishable'),
          tooltip: t('organizer:speakerCard.publishableTooltip'),
          testIdSuffix: 'publishable',
        };
      }
      return {
        kind: 'button',
        label: t('organizer:speakerCard.primaryAction.assignSessionSlot'),
        onClick: () => callbacks.onAssignSessionSlot(speaker),
        disabled: false,
        testIdSuffix: 'assign-session-slot',
      };
    }
 
    case 'DECLINED':
      return {
        kind: 'button',
        label: t('organizer:speakerCard.primaryAction.viewDetails'),
        onClick: () => callbacks.onSpeakerClick(speaker),
        disabled: false,
        testIdSuffix: 'view-details',
      };
 
    default:
      return { kind: 'none' };
  }
}