![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/domains/lavocat.ca/public_html/src/hooks/ |
import { useState, useEffect, useRef } from 'react';
interface CompetitionData {
caseId: string;
currentHighestBid: number;
totalBidders: number;
bidCount: number;
lastUpdated: string;
}
interface UseCompetitionUpdatesProps {
caseId: string;
enabled?: boolean;
pollInterval?: number; // in milliseconds
}
export const useCompetitionUpdates = ({
caseId,
enabled = true,
pollInterval = 5000
}: UseCompetitionUpdatesProps) => {
const [data, setData] = useState<CompetitionData | null>(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const intervalRef = useRef<NodeJS.Timeout | null>(null);
const lastDataRef = useRef<string>('');
const fetchCompetitionData = async () => {
if (!enabled || !caseId) return;
try {
setLoading(true);
setError(null);
const response = await fetch(`/api/public/cases/${caseId}/competition/status`);
if (!response.ok) {
throw new Error('Failed to fetch competition data');
}
const competitionData = await response.json();
const dataString = JSON.stringify(competitionData);
// Only update if data has changed
if (dataString !== lastDataRef.current) {
setData(competitionData);
lastDataRef.current = dataString;
}
} catch (err) {
setError(err instanceof Error ? err.message : 'Unknown error');
} finally {
setLoading(false);
}
};
useEffect(() => {
if (!enabled || !caseId) return;
// Initial fetch
fetchCompetitionData();
// Set up polling
intervalRef.current = setInterval(fetchCompetitionData, pollInterval);
return () => {
if (intervalRef.current) {
clearInterval(intervalRef.current);
intervalRef.current = null;
}
};
}, [caseId, enabled, pollInterval]);
const refresh = () => {
fetchCompetitionData();
};
return {
data,
loading,
error,
refresh
};
};