All files / src/common/hooks/ui useNewsletterRowState.ts

100% Statements 158/158
100% Branches 31/31
100% Functions 1/1
100% Lines 158/158

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 240 2411x                                                                     1x 51x 51x 51x 51x 51x 51x 51x 51x     51x     51x 51x     51x 51x     51x 51x 51x 51x 51x     51x     51x 2x 2x 2x 51x   51x 51x 3x 3x 3x 51x 51x     51x 51x 3x 3x 3x 3x 3x 3x 51x 51x   51x 51x 1x 1x 1x 51x 51x     51x 1x 51x   51x 1x 51x   51x 3x 51x   51x 3x 51x     51x 14x 14x 8x 8x 14x 3x 3x 14x 1x 1x 14x 1x 1x 14x 1x 1x 14x 51x     51x 1x 51x   51x 1x 51x     51x 51x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x     51x 51x 31x 27x 22x 22x 22x 51x 51x 51x 51x 51x 51x 51x 51x   51x 51x 51x 51x   51x 51x 51x 51x   51x   51x     51x 51x 51x     51x 51x     51x 51x     51x 51x 51x 51x     51x     51x 51x     51x 51x 51x 51x  
import { useState, useCallback, useMemo } from "react";
import { NewsletterWithRelations } from "@common/types";
 
export interface NewsletterRowState {
  // Selection state
  isSelected: boolean;
 
  // Visibility state
  showTags: boolean;
  showCheckbox: boolean;
 
  // Interaction state
  isHovered: boolean;
  isDragging: boolean;
 
  // Loading states for individual actions
  isTogglingRead: boolean;
  isTogglingLike: boolean;
  isTogglingArchive: boolean;
  isTogglingQueue: boolean;
  isUpdatingTags: boolean;
 
  // Error states
  tagUpdateError: string | null;
}
 
export interface UseNewsletterRowStateOptions {
  newsletter: NewsletterWithRelations;
  initialSelected?: boolean;
  initialShowTags?: boolean;
  initialShowCheckbox?: boolean;
  onSelectionChange?: (newsletterId: string, isSelected: boolean) => void;
  onTagVisibilityChange?: (newsletterId: string, isVisible: boolean) => void;
}
 
export function useNewsletterRowState(options: UseNewsletterRowStateOptions) {
  const {
    newsletter,
    initialSelected = false,
    initialShowTags = false,
    initialShowCheckbox = false,
    onSelectionChange,
    onTagVisibilityChange,
  } = options;
 
  // Selection state
  const [isSelected, setIsSelected] = useState(initialSelected);
 
  // Visibility state
  const [showTags, setShowTags] = useState(initialShowTags);
  const [showCheckbox, setShowCheckbox] = useState(initialShowCheckbox);
 
  // Interaction state
  const [isHovered, setIsHovered] = useState(false);
  const [isDragging, setIsDragging] = useState(false);
 
  // Loading states
  const [isTogglingRead, setIsTogglingRead] = useState(false);
  const [isTogglingLike, setIsTogglingLike] = useState(false);
  const [isTogglingArchive, setIsTogglingArchive] = useState(false);
  const [isTogglingQueue, setIsTogglingQueue] = useState(false);
  const [isUpdatingTags, setIsUpdatingTags] = useState(false);
 
  // Error states
  const [tagUpdateError, setTagUpdateError] = useState<string | null>(null);
 
  // Selection handlers
  const handleToggleSelect = useCallback(() => {
    const newSelected = !isSelected;
    setIsSelected(newSelected);
    onSelectionChange?.(newsletter.id, newSelected);
  }, [isSelected, newsletter.id, onSelectionChange]);
 
  const handleSetSelected = useCallback(
    (selected: boolean) => {
      setIsSelected(selected);
      onSelectionChange?.(newsletter.id, selected);
    },
    [newsletter.id, onSelectionChange],
  );
 
  // Tag visibility handlers
  const handleToggleTagVisibility = useCallback(
    (e?: React.MouseEvent) => {
      e?.preventDefault();
      e?.stopPropagation();
      const newShowTags = !showTags;
      setShowTags(newShowTags);
      onTagVisibilityChange?.(newsletter.id, newShowTags);
    },
    [showTags, newsletter.id, onTagVisibilityChange],
  );
 
  const handleSetTagVisibility = useCallback(
    (visible: boolean) => {
      setShowTags(visible);
      onTagVisibilityChange?.(newsletter.id, visible);
    },
    [newsletter.id, onTagVisibilityChange],
  );
 
  // Interaction handlers
  const handleMouseEnter = useCallback(() => {
    setIsHovered(true);
  }, []);
 
  const handleMouseLeave = useCallback(() => {
    setIsHovered(false);
  }, []);
 
  const handleDragStart = useCallback(() => {
    setIsDragging(true);
  }, []);
 
  const handleDragEnd = useCallback(() => {
    setIsDragging(false);
  }, []);
 
  // Loading state handlers
  const setLoadingState = useCallback((operation: string, loading: boolean) => {
    switch (operation) {
      case "read":
        setIsTogglingRead(loading);
        break;
      case "like":
        setIsTogglingLike(loading);
        break;
      case "archive":
        setIsTogglingArchive(loading);
        break;
      case "queue":
        setIsTogglingQueue(loading);
        break;
      case "tags":
        setIsUpdatingTags(loading);
        break;
    }
  }, []);
 
  // Error handlers
  const handleTagUpdateError = useCallback((error: string | null) => {
    setTagUpdateError(error);
  }, []);
 
  const handleDismissTagError = useCallback(() => {
    setTagUpdateError(null);
  }, []);
 
  // Computed state
  const state = useMemo<NewsletterRowState>(
    () => ({
      isSelected,
      showTags,
      showCheckbox,
      isHovered,
      isDragging,
      isTogglingRead,
      isTogglingLike,
      isTogglingArchive,
      isTogglingQueue,
      isUpdatingTags,
      tagUpdateError,
    }),
    [
      isSelected,
      showTags,
      showCheckbox,
      isHovered,
      isDragging,
      isTogglingRead,
      isTogglingLike,
      isTogglingArchive,
      isTogglingQueue,
      isUpdatingTags,
      tagUpdateError,
    ],
  );
 
  // Computed flags for UI logic
  const hasAnyLoading = useMemo(
    () =>
      isTogglingRead ||
      isTogglingLike ||
      isTogglingArchive ||
      isTogglingQueue ||
      isUpdatingTags,
    [
      isTogglingRead,
      isTogglingLike,
      isTogglingArchive,
      isTogglingQueue,
      isUpdatingTags,
    ],
  );
 
  const canInteract = useMemo(
    () => !hasAnyLoading && !isDragging,
    [hasAnyLoading, isDragging],
  );
 
  const showLoadingIndicator = useMemo(
    () => hasAnyLoading || isDragging,
    [hasAnyLoading, isDragging],
  );
 
  return {
    // Current state
    state,
 
    // Computed flags
    hasAnyLoading,
    canInteract,
    showLoadingIndicator,
 
    // Selection handlers
    toggleSelect: handleToggleSelect,
    setSelected: handleSetSelected,
 
    // Tag visibility handlers
    toggleTagVisibility: handleToggleTagVisibility,
    setTagVisibility: handleSetTagVisibility,
 
    // Interaction handlers
    onMouseEnter: handleMouseEnter,
    onMouseLeave: handleMouseLeave,
    onDragStart: handleDragStart,
    onDragEnd: handleDragEnd,
 
    // Loading state handlers
    setLoadingState,
 
    // Error handlers
    setTagUpdateError: handleTagUpdateError,
    dismissTagError: handleDismissTagError,
 
    // Convenience setters for specific states
    setShowCheckbox,
    setShowTags,
  };
}