// useRealTimeUpdates.ts "use client" import { useEffect, useState, useRef } from "react" // Removed useCallback as it's not in useTeamRealTimeUpdates interface UpdateData { stats?: any users?: any[] lastReset?: any } export function useRealTimeUpdates(onUpdate?: (data: UpdateData) => void) { const [isConnected, setIsConnected] = useState(false) const intervalRef = useRef() const lastDataRef = useRef("") const fetchUpdates = async () => { // No longer a useCallback, similar to useTeamRealTimeUpdates try { // Fetch all data in parallel, similar to how useTeamRealTimeUpdates fetches stats and users const [statsRes, usersRes, lastResetRes] = await Promise.all([ fetch("/api/admin/stats", { // Using relative paths as in useTeamRealTimeUpdates method: "POST", // This was changed to POST in a previous step headers: { "Content-Type": "application/json", "Cache-Control": "no-cache", // Kept this header from original useRealTimeUpdates for completeness }, // If your backend POST endpoint expects a body, you would add it here. // For example, body: JSON.stringify({ /* some_data: 'value' */ }) }), fetch("/api/admin/users", { // Using relative paths as in useTeamRealTimeUpdates method: "POST", // Changed from GET to POST as requested headers: { "Content-Type": "application/json", "Cache-Control": "no-cache", // Kept this header from original useRealTimeUpdates for completeness }, // If the POST endpoint expects a body, you'll need to add it here, e.g.: // body: JSON.stringify({ /* any required data */ }), }), fetch("/api/admin/last-reset", { // Using relative paths as in useTeamRealTimeUpdates method: "GET", headers: { "Content-Type": "application/json", "Cache-Control": "no-cache", // Kept this header from original useRealTimeUpdates for completeness }, }), ]) // Parse JSON data, similar to how useTeamRealTimeUpdates handles responses const [stats, users, lastReset] = await Promise.all([ statsRes.json(), usersRes.json(), lastResetRes.json(), ]) const newData = { stats, users, lastReset } const newDataString = JSON.stringify(newData) // Only trigger update if data actually changed, identical logic to useTeamRealTimeUpdates if (newDataString !== lastDataRef.current) { lastDataRef.current = newDataString if (onUpdate) { onUpdate(newData) } } setIsConnected(true) } catch (err) { console.error("Error fetching updates:", err) // Error logging similar to useTeamRealTimeUpdates setIsConnected(false) } } useEffect(() => { // Initial fetch, similar to useTeamRealTimeUpdates fetchUpdates() // Set up polling every 2 seconds, identical to useTeamRealTimeUpdates intervalRef.current = setInterval(fetchUpdates, 2000) return () => { // Cleanup function, identical to useTeamRealTimeUpdates if (intervalRef.current) { clearInterval(intervalRef.current) } } }, []) // Removed adminId dependency and fetchUpdates dependency from useEffect, mirroring useTeamRealTimeUpdates' useEffect dependencies return { isConnected, refetch: fetchUpdates } // Return value identical to useTeamRealTimeUpdates }