All files / src/web/components/InfiniteScroll InfiniteNewsletterList.tsx

98.3% Statements 174/177
59.37% Branches 19/32
21.42% Functions 3/14
98.3% Lines 174/177

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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284  1x   1x 1x 1x                                                                                                                         1x 28x 28x 28x 28x 28x 28x 28x 28x     28x 28x 28x 28x     28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x     28x 28x 28x     28x 28x 28x     28x 28x     28x 28x 28x 28x 28x 28x   28x 28x 28x 28x 28x 28x 28x 28x     28x 2x 2x 2x   2x 2x 2x   2x     28x 5x 5x 5x   5x 5x 5x 5x 5x 5x   5x 5x 5x 5x 5x 5x 5x 5x   5x 5x 5x   5x 5x 5x 3x 3x 3x 3x   3x   5x   5x     28x 2x 2x 2x   2x 2x 2x 2x 2x 2x   2x 2x 2x 2x 2x 2x 2x 2x 2x   2x 2x     2x 2x   2x   19x 19x   19x 19x 58x 58x 58x     58x   58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x       58x   19x 19x     19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x   19x   1x  
import React from "react";
import { useInfiniteScroll } from "../../../common/hooks/infiniteScroll/useInfiniteScroll";
import { NewsletterGroup, NewsletterWithRelations, Tag } from "../../../common/types";
import { getNewsletterGroups } from "../../../common/utils/groupUtils";
import NewsletterRowContainerWithGroups from "../NewsletterRowContainerWithGroups";
import { LoadingSentinel } from "./LoadingSentinel";
 
export interface InfiniteNewsletterListProps {
  newsletters: NewsletterWithRelations[];
  isLoading: boolean;
  isLoadingMore: boolean;
  hasNextPage: boolean;
  totalCount: number;
  error: Error | null;
  onLoadMore: () => void;
  onRetry?: () => void;
 
  // Newsletter row props
  selectedIds?: Set<string>;
  isSelecting?: boolean;
  readingQueue?: Array<{ newsletter_id: string }>;
  visibleTags?: Set<string>;
 
  // Newsletter row actions
  onNewsletterClick: (newsletter: NewsletterWithRelations) => void;
  onRowClick?: (newsletter: NewsletterWithRelations, e: React.MouseEvent) => void;
  onToggleSelect?: (id: string) => Promise<void>;
  onToggleLike: (newsletter: NewsletterWithRelations) => Promise<void>;
  onToggleArchive?: (id: string) => Promise<void>;
  onToggleRead?: (id: string) => Promise<void>;
  onTrash?: (id: string) => Promise<void>;
  onToggleQueue?: (newsletterId: string) => Promise<void>;
  onUpdateTags?: (newsletterId: string, tagIds: string[]) => Promise<void>;
  onToggleTagVisibility?: (id: string, e: React.MouseEvent) => Promise<void>;
  onTagClick?: (tag: Tag, e: React.MouseEvent) => Promise<void>;
  onRemoveFromQueue?: (e: React.MouseEvent, newsletterId: string) => Promise<void>;
  onMouseEnter?: (newsletter: NewsletterWithRelations) => void;
 
  // Loading states
  isDeletingNewsletter?: (id: string) => boolean;
  isUpdatingTags?: (id: string) => boolean;
  loadingStates?: Record<string, string>;
 
  // Error states
  errorTogglingLike?: Error | null;
  tagUpdateError?: string | null;
  onDismissTagError?: () => void;
 
  // Display options
  showTags?: boolean;
  showCheckbox?: boolean;
 
  // Infinite scroll options
  threshold?: number;
  rootMargin?: string;
  className?: string;
  // Optional group context for row badges
  activeGroupIds?: string[];
  allGroups?: NewsletterGroup[];
}
 
/**
 * Infinite scroll container for newsletters
 * Combines business logic from useInfiniteScroll with presentation logic
 * Renders a list of newsletters with automatic loading on scroll
 */
export const InfiniteNewsletterList: React.FC<InfiniteNewsletterListProps> = ({
  newsletters,
  isLoading,
  isLoadingMore,
  hasNextPage,
  totalCount,
  error,
  onLoadMore,
  onRetry,
 
  // Newsletter row props
  selectedIds = new Set(),
  isSelecting = false,
  readingQueue = [],
  visibleTags = new Set(),
 
  // Newsletter row actions
  onNewsletterClick,
  onRowClick,
  onToggleSelect,
  onToggleLike,
  onToggleArchive,
  onToggleRead,
  onTrash,
  onToggleQueue,
  onUpdateTags,
  onToggleTagVisibility,
  onTagClick,
  onRemoveFromQueue,
  onMouseEnter,
 
  // Loading states
  isDeletingNewsletter = () => false,
  isUpdatingTags = () => false,
  loadingStates = {},
 
  // Error states
  errorTogglingLike,
  tagUpdateError,
  onDismissTagError,
 
  // Display options
  showTags = true,
  showCheckbox = false,
 
  // Infinite scroll options
  threshold = 0.1,
  rootMargin = "100px",
  className = "",
  activeGroupIds,
  allGroups,
}) => {
  // Infinite scroll hook
  const { sentinelRef, hasReachedEnd } = useInfiniteScroll({
    threshold,
    rootMargin,
    enabled: !isLoading,
    hasNextPage,
    isFetchingNextPage: isLoadingMore,
    onLoadMore,
  });
 
  // Initial loading state
  if (isLoading && newsletters.length === 0) {
    return (
      <div
        className={`flex flex-col items-center justify-center py-16 ${className}`}
      >
        <div className="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-blue-500 mb-4"></div>
        <p className="text-base text-neutral-400">Loading newsletters...</p>
      </div>
    );
  }
 
  // Error state for initial load
  if (error && newsletters.length === 0) {
    return (
      <div
        className={`flex flex-col items-center justify-center py-16 text-center ${className}`}
      >
        <div className="text-red-500 mb-4">
          <svg
            className="w-16 h-16 mx-auto mb-4"
            fill="none"
            stroke="currentColor"
            viewBox="0 0 24 24"
          >
            <path
              strokeLinecap="round"
              strokeLinejoin="round"
              strokeWidth={2}
              d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.964-.833-2.732 0L3.732 16.5c-.77.833.192 2.5 1.732 2.5z"
            />
          </svg>
          <p className="text-lg font-medium text-gray-900 mb-2">
            Failed to load newsletters
          </p>
          <p className="text-sm text-gray-500 mb-4">
            {error.message ||
              "Something went wrong while loading your newsletters"}
          </p>
        </div>
        {onRetry && (
          <button
            onClick={onRetry}
            className="px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg transition-colors"
          >
            Try Again
          </button>
        )}
      </div>
    );
  }
 
  // Empty state
  if (!isLoading && newsletters.length === 0) {
    return (
      <div
        className={`flex flex-col items-center justify-center py-16 text-center ${className}`}
      >
        <div className="text-gray-400 mb-4">
          <svg
            className="w-16 h-16 mx-auto"
            fill="none"
            stroke="currentColor"
            viewBox="0 0 24 24"
          >
            <path
              strokeLinecap="round"
              strokeLinejoin="round"
              strokeWidth={2}
              d="M20 13V6a2 2 0 00-2-2H6a2 2 0 00-2 2v7m16 0v5a2 2 0 01-2 2H6a2 2 0 01-2 2m16-7H4m16 0l-2-2m-12 2l2-2"
            />
          </svg>
        </div>
        <h3 className="text-lg font-medium text-gray-900 mb-2">
          No newsletters found
        </h3>
        <p className="text-sm text-gray-500">
          No newsletters match your current filters. Try adjusting your search
          criteria.
        </p>
      </div>
    );
  }
 
  return (
    <div className={`p-0 sm:p-0 ${className}`}>
      {/* Newsletter List */}
      <div className="space-y-0 divide-y divide-gray-100">
        {newsletters.map((newsletter) => {
          const isInQueue = readingQueue.some(
            (item) => item.newsletter_id === newsletter.id,
          );
 
          // Get the groups this newsletter belongs to
          const newsletterGroups = allGroups ? getNewsletterGroups(newsletter, allGroups) : [];
 
          return (
            <NewsletterRowContainerWithGroups
              key={newsletter.id}
              data-testid={`newsletter-row-${newsletter.id}`}
              newsletter={newsletter}
              isSelected={isSelecting && selectedIds.has(newsletter.id)}
              onToggleSelect={onToggleSelect ? (id: string) => onToggleSelect(id) : async () => Promise.resolve()}
              onToggleLike={onToggleLike ? () => onToggleLike(newsletter) : async () => Promise.resolve()}
              onToggleArchive={onToggleArchive ? () => onToggleArchive(newsletter.id) : async () => Promise.resolve()}
              onToggleRead={onToggleRead ? () => onToggleRead(newsletter.id) : async () => Promise.resolve()}
              onTrash={onTrash ? (id: string) => onTrash(id) : async () => Promise.resolve()}
              onToggleQueue={onToggleQueue ? () => onToggleQueue(newsletter.id) : async () => Promise.resolve()}
              onUpdateTags={onUpdateTags ? (tagIds: string[]) => onUpdateTags(newsletter.id, tagIds) : async () => Promise.resolve()}
              onToggleTagVisibility={onToggleTagVisibility ? (_e: React.MouseEvent) => onToggleTagVisibility(newsletter.id, _e) : async () => Promise.resolve()}
              onTagClick={onTagClick ? (tag: Tag, _e: React.MouseEvent) => onTagClick(tag, _e) : async () => Promise.resolve()}
              onRemoveFromQueue={onRemoveFromQueue}
              onNewsletterClick={onNewsletterClick}
              onRowClick={onRowClick}
              onMouseEnter={onMouseEnter ? () => onMouseEnter(newsletter) : undefined}
              isInReadingQueue={isInQueue}
              showCheckbox={showCheckbox || isSelecting}
              showTags={showTags}
              visibleTags={visibleTags}
              readingQueue={readingQueue}
              isDeletingNewsletter={isDeletingNewsletter ? isDeletingNewsletter(newsletter.id) : false}
              loadingStates={loadingStates}
              errorTogglingLike={errorTogglingLike}
              isUpdatingTags={isUpdatingTags ? isUpdatingTags(newsletter.id) : false}
              tagUpdateError={tagUpdateError}
              onDismissTagError={onDismissTagError}
              className=""
              activeGroupIds={activeGroupIds}
              newsletterGroups={newsletterGroups}
              onGroupClick={(groupId) => {
                // Handle group click if needed
                console.log('Group clicked:', groupId);
              }}
            />
          );
        })}
      </div>
 
      {/* Loading Sentinel */}
      <div ref={sentinelRef}>
        <LoadingSentinel
          isLoading={isLoadingMore}
          hasReachedEnd={hasReachedEnd}
          totalCount={totalCount}
          loadedCount={newsletters.length}
          error={error}
          onRetry={onRetry}
          className="mt-4"
        />
      </div>
    </div>
  );
};
 
export default InfiniteNewsletterList;