|
@@ -1,22 +1,47 @@
|
|
|
import Head from 'next/head'
|
|
import Head from 'next/head'
|
|
|
import Link from 'next/link'
|
|
import Link from 'next/link'
|
|
|
import { useRouter } from 'next/router'
|
|
import { useRouter } from 'next/router'
|
|
|
-import { useEffect, useState } from 'react'
|
|
|
|
|
|
|
+import { useEffect, useState, useCallback } from 'react'
|
|
|
import { useDashboardStats } from '../hooks/useDashboardStats'
|
|
import { useDashboardStats } from '../hooks/useDashboardStats'
|
|
|
import { useDevices } from '../hooks/useDevices'
|
|
import { useDevices } from '../hooks/useDevices'
|
|
|
import { StatsCard } from '../components/StatsCard'
|
|
import { StatsCard } from '../components/StatsCard'
|
|
|
import { DeviceCard } from '../components/DeviceCard'
|
|
import { DeviceCard } from '../components/DeviceCard'
|
|
|
import { ProtectedRoute } from '../components/ProtectedRoute'
|
|
import { ProtectedRoute } from '../components/ProtectedRoute'
|
|
|
import { getStoredUser, authApi } from '../lib/auth'
|
|
import { getStoredUser, authApi } from '../lib/auth'
|
|
|
|
|
+import { useSocket } from '../hooks/useSocket'
|
|
|
import type { AuthUser } from '../lib/auth'
|
|
import type { AuthUser } from '../lib/auth'
|
|
|
|
|
|
|
|
function DashboardContent() {
|
|
function DashboardContent() {
|
|
|
const router = useRouter()
|
|
const router = useRouter()
|
|
|
- const { data: stats, isLoading: statsLoading } = useDashboardStats()
|
|
|
|
|
- const { data: devices = [], isLoading: devicesLoading } = useDevices()
|
|
|
|
|
|
|
+ const { data: stats, isLoading: statsLoading, refetch: refetchStats } = useDashboardStats()
|
|
|
|
|
+ const { data: devices = [], isLoading: devicesLoading, refetch: refetchDevices } = useDevices()
|
|
|
const [user, setUser] = useState<AuthUser | null>(null)
|
|
const [user, setUser] = useState<AuthUser | null>(null)
|
|
|
|
|
|
|
|
- useEffect(() => { setUser(getStoredUser()) }, [])
|
|
|
|
|
|
|
+ // Collect unique project IDs from devices for WS subscription
|
|
|
|
|
+ const projectIds = [...new Set(devices.map((d: any) => d.projectId).filter(Boolean))] as string[]
|
|
|
|
|
+
|
|
|
|
|
+ // Subscribe to realtime events
|
|
|
|
|
+ const onHeartbeat = useCallback((data: { deviceId: string; status: string; storageFreeGb: number }) => {
|
|
|
|
|
+ refetchDevices()
|
|
|
|
|
+ refetchStats()
|
|
|
|
|
+ }, [refetchDevices, refetchStats])
|
|
|
|
|
+
|
|
|
|
|
+ const onStatusChanged = useCallback((data: { deviceId: string; status: string }) => {
|
|
|
|
|
+ refetchDevices()
|
|
|
|
|
+ refetchStats()
|
|
|
|
|
+ }, [refetchDevices, refetchStats])
|
|
|
|
|
+
|
|
|
|
|
+ const { subscribe } = useSocket(projectIds)
|
|
|
|
|
+
|
|
|
|
|
+ useEffect(() => {
|
|
|
|
|
+ const unsubHb = subscribe('device.heartbeat', onHeartbeat)
|
|
|
|
|
+ const unsubSc = subscribe('device.status.changed', onStatusChanged)
|
|
|
|
|
+ return () => { unsubHb(); unsubSc() }
|
|
|
|
|
+ }, [subscribe, onHeartbeat, onStatusChanged])
|
|
|
|
|
+
|
|
|
|
|
+ useEffect(() => {
|
|
|
|
|
+ setUser(getStoredUser())
|
|
|
|
|
+ }, [])
|
|
|
|
|
|
|
|
async function handleLogout() {
|
|
async function handleLogout() {
|
|
|
try { await authApi.logout() } catch {}
|
|
try { await authApi.logout() } catch {}
|
|
@@ -33,7 +58,14 @@ function DashboardContent() {
|
|
|
{/* Header */}
|
|
{/* Header */}
|
|
|
<header className="bg-white border-b border-gray-200 px-6 py-3">
|
|
<header className="bg-white border-b border-gray-200 px-6 py-3">
|
|
|
<div className="flex items-center justify-between">
|
|
<div className="flex items-center justify-between">
|
|
|
- <h1 className="text-lg font-bold text-gray-900">Construction Timelapse</h1>
|
|
|
|
|
|
|
+ <div className="flex items-center gap-3">
|
|
|
|
|
+ <h1 className="text-lg font-bold text-gray-900">Construction Timelapse</h1>
|
|
|
|
|
+ {/* Live indicator */}
|
|
|
|
|
+ <span className="flex items-center gap-1.5 rounded-full bg-emerald-50 px-2 py-0.5 text-xs font-medium text-emerald-600">
|
|
|
|
|
+ <span className="h-1.5 w-1.5 rounded-full bg-emerald-500" />
|
|
|
|
|
+ Live
|
|
|
|
|
+ </span>
|
|
|
|
|
+ </div>
|
|
|
<div className="flex items-center gap-4">
|
|
<div className="flex items-center gap-4">
|
|
|
<nav className="flex gap-4 text-sm text-gray-500">
|
|
<nav className="flex gap-4 text-sm text-gray-500">
|
|
|
<Link href="/" className="text-blue-600 font-medium">Dashboard</Link>
|
|
<Link href="/" className="text-blue-600 font-medium">Dashboard</Link>
|
|
@@ -98,7 +130,7 @@ function DashboardContent() {
|
|
|
</div>
|
|
</div>
|
|
|
) : (
|
|
) : (
|
|
|
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
|
|
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
|
|
|
- {devices.slice(0, 6).map((d) => (
|
|
|
|
|
|
|
+ {devices.slice(0, 6).map((d: any) => (
|
|
|
<DeviceCard
|
|
<DeviceCard
|
|
|
key={d.id}
|
|
key={d.id}
|
|
|
id={d.id}
|
|
id={d.id}
|