Auth: poll machine session to reflect deactivation in real time; Desktop: refresh deactivation screen to match design system

This commit is contained in:
Esdras Renan 2025-10-19 02:13:39 -03:00
parent 01461d031b
commit 77f48652cd
2 changed files with 75 additions and 24 deletions

View file

@ -217,6 +217,57 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
}
}, [session?.user])
// Poll machine session periodically to reflect admin changes (e.g., deactivation)
useEffect(() => {
const shouldPoll = Boolean(session?.user?.role === "machine") || Boolean(machineContext)
if (!shouldPoll) return
let cancelled = false
const tick = async () => {
try {
const response = await fetch("/api/machines/session", { credentials: "include" })
if (!response.ok) return
const data = await response.json()
if (cancelled) return
const mc = data?.machine
if (mc && typeof mc === "object") {
setMachineContext((prev) => {
// only update when something changes to avoid re-renders
const next = {
machineId: mc.id,
tenantId: mc.tenantId,
persona: mc.persona ?? null,
assignedUserId: mc.assignedUserId ?? null,
assignedUserEmail: mc.assignedUserEmail ?? null,
assignedUserName: mc.assignedUserName ?? null,
assignedUserRole: mc.assignedUserRole ?? null,
companyId: mc.companyId ?? null,
isActive: (mc.isActive ?? true) as boolean,
} as MachineContext
return JSON.stringify(prev) === JSON.stringify(next) ? prev : next
})
}
} catch {
/* ignore transient errors */
}
}
const id = setInterval(tick, 15000)
// Also refresh when tab gains focus
const onFocus = () => tick()
if (typeof window !== "undefined") {
window.addEventListener("focus", onFocus)
document.addEventListener("visibilitychange", onFocus)
}
tick()
return () => {
cancelled = true
clearInterval(id)
if (typeof window !== "undefined") {
window.removeEventListener("focus", onFocus)
document.removeEventListener("visibilitychange", onFocus)
}
}
}, [session?.user?.role, machineContext])
useEffect(() => {
if (!session?.user || session.user.role === "machine" || convexUserId) return