admin(machines): add resilient fallback for details page
- Keep Convex useQuery for machines.getById - Add HTTP fallback via /api/admin/machines/[id]/details if query stays loading (>1.2s) - Helps when websocket/convex-react doesn’t initialize and avoids permanent skeleton
This commit is contained in:
parent
e0f65cc774
commit
4cfbd22cf2
2 changed files with 54 additions and 5 deletions
|
|
@ -1,6 +1,6 @@
|
|||
"use client"
|
||||
|
||||
import { useMemo } from "react"
|
||||
import { useEffect, useMemo, useRef, useState } from "react"
|
||||
import { useQuery } from "convex/react"
|
||||
import { api } from "@/convex/_generated/api"
|
||||
import {
|
||||
|
|
@ -20,11 +20,37 @@ export function AdminMachineDetailsClient({ tenantId, machineId }: { tenantId: s
|
|||
| Record<string, unknown>
|
||||
| null
|
||||
| undefined
|
||||
|
||||
// Fallback via HTTP in caso de o Convex React demorar/ficar preso em loading
|
||||
const [fallback, setFallback] = useState<Record<string, unknown> | null>(null)
|
||||
const timer = useRef<ReturnType<typeof setTimeout> | null>(null)
|
||||
const shouldLoad = single === undefined && !fallback && Boolean(machineId)
|
||||
|
||||
useEffect(() => {
|
||||
if (!shouldLoad) return
|
||||
timer.current = setTimeout(async () => {
|
||||
try {
|
||||
const res = await fetch(`/api/admin/machines/${machineId}/details`, { credentials: "include" })
|
||||
if (res.ok) {
|
||||
const data = (await res.json()) as Record<string, unknown>
|
||||
setFallback(data)
|
||||
}
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}, 1200)
|
||||
return () => {
|
||||
if (timer.current) clearTimeout(timer.current)
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [shouldLoad, machineId])
|
||||
|
||||
const machine: MachinesQueryItem | null = useMemo(() => {
|
||||
if (single === undefined || single === null) return single as null
|
||||
return normalizeMachineItem(single)
|
||||
}, [single])
|
||||
const isLoading = single === undefined
|
||||
const source = single ?? fallback
|
||||
if (source === undefined || source === null) return source as null
|
||||
return normalizeMachineItem(source)
|
||||
}, [single, fallback])
|
||||
const isLoading = single === undefined && !fallback
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue