From 728cc78b2258dfb76cf7c93c6a87647142f66272 Mon Sep 17 00:00:00 2001 From: Harshit-Mishra2212 Date: Fri, 29 May 2026 18:27:06 +0530 Subject: [PATCH] refactor: extract duplicated CSV fetch logic into shared loadCSV function --- src/hooks/useCSVData.ts | 91 +++++++++++------------------------------ 1 file changed, 24 insertions(+), 67 deletions(-) diff --git a/src/hooks/useCSVData.ts b/src/hooks/useCSVData.ts index 3c8d97e..1db2a69 100644 --- a/src/hooks/useCSVData.ts +++ b/src/hooks/useCSVData.ts @@ -1,94 +1,47 @@ -import { useState, useEffect } from 'react'; +import { useState, useEffect, useCallback } from 'react'; import Papa, { ParseResult, ParseConfig } from 'papaparse'; import { EngagementData } from '@/types/dashboard'; +const CSV_URL = '/data/weekly-engagement-data.csv'; + export function useCSVData() { const [data, setData] = useState([]); const [isLoading, setIsLoading] = useState(true); const [isError, setIsError] = useState(false); const [timestamp, setTimestamp] = useState(0); - useEffect(() => { - async function fetchCSV() { - try { - console.log('Fetching CSV data...'); - const response = await fetch('/data/weekly-engagement-data.csv', { - method: 'GET', - headers: { - 'Accept': 'text/csv' - } - }); - if (!response.ok) { - throw new Error('Failed to fetch CSV: ' + response.statusText); - } - - const csvText = await response.text(); - console.log('CSV data received, starting parsing...'); - - const parseConfig: ParseConfig = { - header: true, - dynamicTyping: true, - skipEmptyLines: true, - complete: (results: ParseResult) => { - console.log('CSV parsing complete:', { - rows: results.data.length, - fields: results.meta.fields, - errors: results.errors - }); - - if (results.errors.length > 0) { - console.error('CSV parsing errors:', results.errors); - } - - setData(results.data); - setIsLoading(false); - setTimestamp(Date.now()); - }, - error: (error: Error) => { - console.error('CSV parsing error:', error); - setIsError(true); - setIsLoading(false); - } - } as ParseConfig; - - Papa.parse(csvText, parseConfig); - } catch (error) { - console.error('Error loading CSV:', error); - setIsError(true); - setIsLoading(false); - } - } - - fetchCSV(); - }, []); - - const mutate = async () => { + const loadCSV = useCallback(async () => { setIsLoading(true); setIsError(false); try { - console.log('Manually refreshing CSV data...'); - const response = await fetch('/data/weekly-engagement-data.csv', { + console.log('Fetching CSV data...'); + const response = await fetch(CSV_URL, { method: 'GET', - headers: { - 'Accept': 'text/csv' - } + headers: { 'Accept': 'text/csv' } }); + if (!response.ok) { throw new Error('Failed to fetch CSV: ' + response.statusText); } + const csvText = await response.text(); - console.log('CSV refresh: data received, starting parsing...'); + console.log('CSV data received, starting parsing...'); const parseConfig: ParseConfig = { header: true, dynamicTyping: true, skipEmptyLines: true, complete: (results: ParseResult) => { - console.log('CSV refresh parsing complete:', { + console.log('CSV parsing complete:', { rows: results.data.length, fields: results.meta.fields, errors: results.errors }); + + if (results.errors.length > 0) { + console.error('CSV parsing errors:', results.errors); + } + setData(results.data); setIsLoading(false); setTimestamp(Date.now()); @@ -102,17 +55,21 @@ export function useCSVData() { Papa.parse(csvText, parseConfig); } catch (error) { - console.error('Error refreshing CSV:', error); + console.error('Error loading CSV:', error); setIsError(true); setIsLoading(false); } - }; + }, []); + + useEffect(() => { + loadCSV(); + }, [loadCSV]); return { data, isLoading, isError, - mutate, + mutate: loadCSV, timestamp }; -} +} \ No newline at end of file