Updated to v1.0.4, fixed admin global page and overall speed
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
// useRealTimeUpdates.ts
|
||||
"use client"
|
||||
|
||||
import { useEffect, useState, useRef } from "react"
|
||||
import { useEffect, useState, useRef } from "react" // Removed useCallback as it's not in useTeamRealTimeUpdates
|
||||
|
||||
interface UpdateData {
|
||||
stats?: any
|
||||
@@ -13,21 +14,48 @@ export function useRealTimeUpdates(onUpdate?: (data: UpdateData) => void) {
|
||||
const intervalRef = useRef<NodeJS.Timeout>()
|
||||
const lastDataRef = useRef<string>("")
|
||||
|
||||
const fetchUpdates = async () => {
|
||||
const fetchUpdates = async () => { // No longer a useCallback, similar to useTeamRealTimeUpdates
|
||||
try {
|
||||
// Fetch all data in parallel
|
||||
const [statsRes, usersRes, resetRes] = await Promise.all([
|
||||
fetch("/api/admin/stats"),
|
||||
fetch("/api/admin/users"),
|
||||
fetch("/api/admin/last-reset"),
|
||||
// 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
|
||||
},
|
||||
}),
|
||||
])
|
||||
|
||||
const [stats, users, lastReset] = await Promise.all([statsRes.json(), usersRes.json(), resetRes.json()])
|
||||
// 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
|
||||
// Only trigger update if data actually changed, identical logic to useTeamRealTimeUpdates
|
||||
if (newDataString !== lastDataRef.current) {
|
||||
lastDataRef.current = newDataString
|
||||
if (onUpdate) {
|
||||
@@ -37,24 +65,25 @@ export function useRealTimeUpdates(onUpdate?: (data: UpdateData) => void) {
|
||||
|
||||
setIsConnected(true)
|
||||
} catch (err) {
|
||||
console.error("Error fetching updates:", err)
|
||||
console.error("Error fetching updates:", err) // Error logging similar to useTeamRealTimeUpdates
|
||||
setIsConnected(false)
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
// Initial fetch
|
||||
// Initial fetch, similar to useTeamRealTimeUpdates
|
||||
fetchUpdates()
|
||||
|
||||
// Set up polling every 2 seconds
|
||||
// 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 { isConnected, refetch: fetchUpdates } // Return value identical to useTeamRealTimeUpdates
|
||||
}
|
||||
Reference in New Issue
Block a user