All files / src/hooks/usePublishing usePublishing.ts

96.42% Statements 27/28
92.85% Branches 13/14
93.75% Functions 15/16
96.42% Lines 27/28

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                                                                                                          4x 39x     39x 39x     39x   22x         39x             39x 4x     3x         39x 2x   2x         39x 2x     2x         39x             2x       39x 2x         39x                     39x           4x 2x               2x         2x 2x          
/**
 * usePublishing Hook (Story 5.7 - Task 5b GREEN Phase)
 *
 * State management hook for progressive publishing functionality
 * Features:
 * - Publish/unpublish phases (topic → speakers → agenda)
 * - Publishing preview
 * - Auto-publish scheduling
 * - Real-time updates via React Query
 */
 
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { publishingService } from '@/services/publishingService/publishingService';
import type {
  PublishingPhase,
  PublishPreviewResponse,
  PublishingStatusResponse,
  AutoPublishScheduleRequest,
  PublishValidationError,
} from '@/types/event.types';
 
export interface UsePublishingReturn {
  // Publishing status (real-time validation data)
  publishingStatus: PublishingStatusResponse | undefined;
  isLoadingStatus: boolean;
 
  // Publish/unpublish mutations
  publishPhase: (phase: PublishingPhase) => void;
  unpublishPhase: (phase: PublishingPhase) => void;
  isPublishing: boolean;
  isUnpublishing: boolean;
  publishError: Error | null;
  validationErrors: Array<{ field: string; message: string; requirement: string }>;
 
  // Preview
  preview: PublishPreviewResponse | null;
  fetchPreview: (phase: PublishingPhase) => void;
  isLoadingPreview: boolean;
  previewError: Error | null;
 
  // Auto-publish scheduling
  scheduleAutoPublish: (phase: PublishingPhase, options: AutoPublishScheduleRequest) => void;
  cancelAutoPublish: (phase: PublishingPhase) => void;
  isScheduling: boolean;
  isCancelling: boolean;
}
 
/**
 * Hook for managing progressive publishing state and operations
 *
 * @param eventCode - Event code (e.g., "BATbern142")
 * @returns Publishing state and operations
 */
export const usePublishing = (eventCode: string): UsePublishingReturn => {
  const queryClient = useQueryClient();
 
  // Query keys
  const statusKey = ['publishing', 'status', eventCode];
  const previewKey = ['publishing', 'preview', eventCode];
 
  // Publishing status query (auto-fetched)
  const { data: publishingStatus, isLoading: isLoadingStatus } = useQuery({
    queryKey: statusKey,
    queryFn: () => publishingService.getPublishingStatus(eventCode),
    staleTime: 10000, // 10 seconds - validation can change frequently
  });
 
  // Preview query (manually triggered via fetchPreview)
  const { data: preview } = useQuery({
    queryKey: previewKey,
    queryFn: () => null as PublishPreviewResponse | null, // Placeholder, actual call via mutation
    enabled: false, // Don't auto-fetch, controlled by fetchPreview
  });
 
  // Publish phase mutation
  const publishPhaseMutation = useMutation({
    mutationFn: (phase: PublishingPhase) => publishingService.publishPhase(eventCode, phase),
    onSuccess: () => {
      // Invalidate status, version history and change log to refetch
      queryClient.invalidateQueries({ queryKey: statusKey });
    },
  });
 
  // Unpublish phase mutation
  const unpublishPhaseMutation = useMutation({
    mutationFn: (phase: PublishingPhase) => publishingService.unpublishPhase(eventCode, phase),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: statusKey });
    },
  });
 
  // Fetch preview mutation (manual trigger)
  const fetchPreviewMutation = useMutation({
    mutationFn: (phase: PublishingPhase) => publishingService.getPublishPreview(eventCode, phase),
    onSuccess: (data) => {
      // Update preview query data manually
      queryClient.setQueryData(previewKey, data);
    },
  });
 
  // Schedule auto-publish mutation
  const scheduleAutoPublishMutation = useMutation({
    mutationFn: ({
      phase,
      options,
    }: {
      phase: PublishingPhase;
      options: AutoPublishScheduleRequest;
    }) => publishingService.scheduleAutoPublish(eventCode, phase, options),
  });
 
  // Cancel auto-publish mutation
  const cancelAutoPublishMutation = useMutation({
    mutationFn: (phase: PublishingPhase) => publishingService.cancelAutoPublish(eventCode, phase),
  });
 
  // Extract validation errors from publish error (422 response)
  const validationErrors: Array<{ field: string; message: string; requirement: string }> =
    publishPhaseMutation.error &&
    typeof publishPhaseMutation.error === 'object' &&
    'response' in publishPhaseMutation.error &&
    publishPhaseMutation.error.response &&
    typeof publishPhaseMutation.error.response === 'object' &&
    'status' in publishPhaseMutation.error.response &&
    publishPhaseMutation.error.response.status === 422 &&
    'data' in publishPhaseMutation.error.response
      ? (publishPhaseMutation.error.response.data as PublishValidationError)?.validationErrors || []
      : [];
 
  return {
    // Publishing status
    publishingStatus,
    isLoadingStatus,
 
    // Publish/unpublish
    publishPhase: (phase) => publishPhaseMutation.mutate(phase),
    unpublishPhase: (phase) => unpublishPhaseMutation.mutate(phase),
    isPublishing: publishPhaseMutation.isPending,
    isUnpublishing: unpublishPhaseMutation.isPending,
    publishError: publishPhaseMutation.error,
    validationErrors,
 
    // Preview
    preview: preview || null,
    fetchPreview: (phase) => fetchPreviewMutation.mutate(phase),
    isLoadingPreview: fetchPreviewMutation.isPending,
    previewError: fetchPreviewMutation.error,
 
    // Auto-publish scheduling
    scheduleAutoPublish: (phase, options) => scheduleAutoPublishMutation.mutate({ phase, options }),
    cancelAutoPublish: (phase) => cancelAutoPublishMutation.mutate(phase),
    isScheduling: scheduleAutoPublishMutation.isPending,
    isCancelling: cancelAutoPublishMutation.isPending,
  };
};