generated from plutack/nextjsauth-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseDashboardData.tsx
39 lines (32 loc) · 1.05 KB
/
useDashboardData.tsx
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
import { useState, useEffect } from 'react';
import { useSocket } from '@/components/layout/socket-provider';
import { DashboardData } from "@/types/dashboard";
export const useDashboardData = () => {
const [data, setData] = useState<DashboardData | null>(null);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<Error | null>(null);
const { socket, isConnected } = useSocket();
useEffect(() => {
if (isConnected && socket) {
// Request initial data
socket.emit('getDashboardData');
// Listen for dashboard data updates
socket.on('dashboardData', (newData: DashboardData) => {
setData(newData);
setIsLoading(false);
});
// Listen for errors
socket.on('dashboardError', (err: string) => {
setError(new Error(err));
setIsLoading(false);
});
}
return () => {
if (socket) {
socket.off('dashboardData');
socket.off('dashboardError');
}
};
}, [socket, isConnected]);
return { data, isLoading, error };
};