All files / src/common/hooks useNewsletterDetail.ts

79.91% Statements 195/244
66.66% Branches 28/42
54.54% Functions 6/11
79.91% Lines 195/244

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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 3461x 1x   1x             1x 1x 1x 1x                                                 1x 11x 11x 11x 11x 11x 11x     11x 7x 11x   11x 11x 11x 11x 11x 11x 11x 11x     11x 7x 11x     11x 4x       4x       4x 4x   4x 1x 1x   2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 11x     11x 11x 11x 11x 11x 11x 11x 11x 11x   11x 25x   25x   25x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 24x 25x 11x   1x   1x   1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x   11x   2x 2x 2x   2x 11x 11x     11x 1x   1x 1x     1x   1x 1x 1x 1x 1x             1x 1x 1x 1x     1x 1x 1x 1x 1x 1x         1x 1x 1x 1x 1x     1x 1x 1x 1x 1x             1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x             1x 1x 1x 1x     1x 1x 1x                     11x     11x                           11x   11x 11x 11x 11x 11x 11x 11x 11x 11x 11x           1x 3x 3x 3x     3x 3x 3x   3x 3x 3x   3x     3x 3x     3x 3x 1x 1x 3x 1x 1x   2x 2x 2x 2x             2x 2x   3x 3x 3x 3x             3x 3x 3x   3x 3x  
import { AuthContext } from '@common/contexts/AuthContext';
import { NewsletterService } from '@common/services';
import { NewsletterWithRelations } from '@common/types';
import {
  getCacheManagerSafe,
  getQueriesData,
  getQueryData,
  getQueryState,
  prefetchQuery,
} from '@common/utils/cacheUtils';
import { useLogger } from '@common/utils/logger/useLogger';
import { queryKeyFactory } from '@common/utils/queryKeyFactory';
import { useQuery } from '@tanstack/react-query';
import { useCallback, useContext, useMemo } from 'react';
 
export interface UseNewsletterDetailOptions {
  enabled?: boolean;
  staleTime?: number;
  cacheTime?: number;
  refetchOnWindowFocus?: boolean;
  prefetchTags?: boolean;
  prefetchSource?: boolean;
}
 
export interface UseNewsletterDetailResult {
  newsletter: NewsletterWithRelations | undefined;
  isLoading: boolean;
  isError: boolean;
  error: Error | null;
  isFetching: boolean;
  refetch: () => void;
  prefetchRelated: () => Promise<void>;
}
 
/**
 * Custom hook for fetching and caching newsletter details with optimized caching
 * and prefetching capabilities for improved performance
 */
export const useNewsletterDetail = (
  newsletterId: string,
  options: UseNewsletterDetailOptions = {}
) => {
  const auth = useContext(AuthContext);
  const user = auth?.user;
  const log = useLogger();
 
  // Initialize cache manager safely
  const cacheManager = useMemo(() => {
    return getCacheManagerSafe();
  }, []);
 
  const {
    enabled = true,
    staleTime = 5 * 60 * 1000, // 5 minutes
    cacheTime = 30 * 60 * 1000, // 30 minutes
    refetchOnWindowFocus = false,
    prefetchTags = true,
    prefetchSource = true,
  } = options;
 
  // Initialize newsletter service
  const newsletterService = useMemo(() => {
    return new NewsletterService();
  }, []);
 
  // Fetch newsletter detail using the service
  const fetchNewsletterDetail = useCallback(async (): Promise<NewsletterWithRelations> => {
    if (!user) {
      throw new Error('User not authenticated');
    }
 
    if (!newsletterId) {
      throw new Error('Newsletter ID is required');
    }
 
    try {
      const newsletter = await newsletterService.getNewsletter(newsletterId);
 
      if (!newsletter) {
        throw new Error('Newsletter not found');
      }
 
      return newsletter;
    } catch (error) {
      log.error(
        'Failed to fetch newsletter detail',
        {
          action: 'fetch_newsletter_detail',
          metadata: { newsletterId, userId: user.id },
        },
        error instanceof Error ? error : new Error(String(error))
      );
      throw error;
    }
  }, [user, newsletterId, log, newsletterService]);
 
  // Main query for newsletter detail
  const query = useQuery({
    queryKey: queryKeyFactory.newsletters.detail(newsletterId),
    queryFn: fetchNewsletterDetail,
    enabled: enabled && !!user && !!newsletterId,
    staleTime,
    gcTime: cacheTime,
    refetchOnWindowFocus,
    refetchOnMount: false, // Prevent cascade refetching
    refetchOnReconnect: false, // Prevent refetch on reconnect
    // Optimistic updates - try to get data from newsletter lists first
    initialData: () => {
      const listsData = getQueriesData<
        NewsletterWithRelations[] | { data: NewsletterWithRelations[] }
      >(queryKeyFactory.newsletters.lists());
 
      for (const [, data] of listsData) {
        if (data) {
          // Handle both array and paginated response formats
          const newsletters = Array.isArray(data) ? data : data.data;
          if (Array.isArray(newsletters)) {
            const found = newsletters.find((n: NewsletterWithRelations) => n.id === newsletterId);
            if (found) {
              return found;
            }
          }
        }
      }
      return undefined;
    },
    initialDataUpdatedAt: () => {
      // Get the timestamp of when the list data was last updated
      const listsData = getQueriesData<
        NewsletterWithRelations[] | { data: NewsletterWithRelations[] }
      >(queryKeyFactory.newsletters.lists());
 
      let latestUpdate = 0;
      for (const [queryKey, data] of listsData) {
        if (data) {
          // Handle both array and paginated response formats
          const newsletters = Array.isArray(data) ? data : data.data;
          if (Array.isArray(newsletters)) {
            const found = newsletters.find((n: NewsletterWithRelations) => n.id === newsletterId);
            if (found) {
              const state = getQueryState(queryKey);
              latestUpdate = Math.max(latestUpdate, state?.dataUpdatedAt || 0);
            }
          }
        }
      }
 
      return latestUpdate || 0;
    },
    // Retry configuration
    retry: (failureCount, error: Error) => {
      // Don't retry on 404 or authentication errors
      if (error?.message?.includes('not found') || error?.message?.includes('authenticated')) {
        return false;
      }
      return failureCount < 3;
    },
    retryDelay: (attemptIndex) => Math.min(1000 * 2 ** attemptIndex, 30000),
  });
 
  // Prefetch related data for better performance
  const prefetchRelated = useCallback(async (): Promise<void> => {
    if (!query.data || !user) return;
 
    const newsletter = query.data;
    const prefetchPromises: Promise<unknown>[] = [];
 
    // Prefetch tags if enabled and newsletter has tags
    if (prefetchTags && newsletter.tags && newsletter.tags.length > 0) {
      // Prefetch individual tag details
      newsletter.tags.forEach((tag: { id: string }) => {
        prefetchPromises.push(
          prefetchQuery(
            [...queryKeyFactory.newsletters.tag(tag.id)],
            async () => {
              // Use the service to fetch tag details
              const { tagApi } = await import('@common/api/tagApi');
              const tagData = await tagApi.getById(tag.id);
              if (!tagData) throw new Error('Tag not found');
              return tagData;
            },
            { staleTime: 10 * 60 * 1000 } // 10 minutes
          )
        );
      });
 
      // Prefetch newsletters with same tags
      const tagIds = newsletter.tags.map((t: { id: string }) => t.id);
      if (tagIds.length > 0) {
        prefetchPromises.push(
          prefetchQuery(
            [...queryKeyFactory.newsletters.list({ tagIds })],
            async () => {
              // Use the service to fetch newsletters by tags
              const newsletters = await newsletterService.getNewsletters({ tagIds });
              return newsletters.data || [];
            },
            { staleTime: 2 * 60 * 1000 } // 2 minutes
          )
        );
      }
    }
 
    // Prefetch source if enabled and newsletter has a source
    if (prefetchSource && newsletter.source) {
      prefetchPromises.push(
        prefetchQuery(
          [...queryKeyFactory.newsletters.source(newsletter.source.id)],
          async () => {
            // Use the service to fetch source details
            const { newsletterSourceApi } = await import('@common/api/newsletterSourceApi');
            const sourceData = await newsletterSourceApi.getById(newsletter.source!.id);
            if (!sourceData) throw new Error('Source not found');
            return sourceData;
          },
          { staleTime: 15 * 60 * 1000 } // 15 minutes
        )
      );
 
      // Prefetch other newsletters from same source
      prefetchPromises.push(
        prefetchQuery(
          [
            ...queryKeyFactory.newsletters.list({
              sourceId: newsletter.source.id,
            }),
          ],
          async () => {
            // Use the service to fetch newsletters by source
            const newsletters = await newsletterService.getNewsletters({
              sourceIds: [newsletter.source!.id]
            });
            return newsletters.data || [];
          },
          { staleTime: 2 * 60 * 1000 } // 2 minutes
        )
      );
    }
 
    // Execute all prefetch operations
    try {
      await Promise.allSettled(prefetchPromises);
    } catch (_) {
      log.warn('Some prefetch operations failed', {
        action: 'prefetch_operations',
        metadata: {
          newsletterId,
          prefetchTags,
          prefetchSource,
        },
      });
      // Don't throw - prefetching failures shouldn't break the main functionality
    }
  }, [query.data, user, prefetchTags, prefetchSource, log, newsletterId, newsletterService]);
 
  // Enhanced refetch that also updates cache manager
  const refetch = useCallback(() => {
    // Refetch the main query
    const refetchPromise = query.refetch();
 
    // Update cache manager performance metrics
    if (cacheManager && user) {
      // Note: warmCache method not available in SimpleCacheManager
      // This is for future enhancement
      refetchPromise.then(() => {
        // Could add cache warming functionality here in the future
      });
    }
 
    return refetchPromise;
  }, [query, user, cacheManager]);
 
  return {
    newsletter: query.data,
    isLoading: query.isLoading,
    isError: query.isError,
    error: query.error as Error | null,
    isFetching: query.isFetching,
    refetch,
    prefetchRelated,
  };
};
 
/**
 * Hook for prefetching newsletter details without subscribing to the query
 * Useful for hover states and anticipatory loading
 */
export const usePrefetchNewsletterDetail = () => {
  const auth = useContext(AuthContext);
  const user = auth?.user;
  const log = useLogger();
 
  // Initialize newsletter service
  const newsletterService = useMemo(() => {
    return new NewsletterService();
  }, []);
 
  const prefetchNewsletter = useCallback(
    async (newsletterId: string, options: { priority?: boolean } = {}) => {
      if (!user || !newsletterId) return;
 
      const { priority = false } = options;
 
      // Check if already cached and fresh
      const existingData = getQueryData(queryKeyFactory.newsletters.detail(newsletterId));
      const queryState = getQueryState(queryKeyFactory.newsletters.detail(newsletterId));
 
      // If we have fresh data, no need to prefetch
      if (
        existingData &&
        queryState?.dataUpdatedAt &&
        Date.now() - queryState.dataUpdatedAt < 5 * 60 * 1000
      ) {
        return;
      }
 
      try {
        await prefetchQuery(
          queryKeyFactory.newsletters.detail(newsletterId),
          async () => {
            const newsletter = await newsletterService.getNewsletter(newsletterId);
            if (!newsletter) {
              throw new Error('Newsletter not found');
            }
            return newsletter;
          },
          {
            staleTime: 5 * 60 * 1000, // 5 minutes
            // Higher priority prefetches get longer cache time
            gcTime: priority ? 60 * 60 * 1000 : 30 * 60 * 1000,
          }
        );
      } catch (_) {
        log.warn('Failed to prefetch newsletter', {
          action: 'prefetch_newsletter',
          metadata: { newsletterId },
        });
        // Don't throw - prefetch failures shouldn't break the app
      }
    },
    [user, log, newsletterService]
  );
 
  return { prefetchNewsletter };
};