All checks were successful
- Corrige erro gramatical: "Dispositivo desativada" -> "Dispositivo desativado" - Adiciona botao "Verificar novamente" na tela de desativacao - Adiciona callback onReactivated no MachineStateMonitor - Corrige fundo escuro para cobrir toda a tela quando desativado - Corrige acentuacoes faltantes no historico de automacoes 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
103 lines
3.6 KiB
TypeScript
103 lines
3.6 KiB
TypeScript
/**
|
|
* MachineStateMonitor - Componente para monitorar o estado da máquina em tempo real
|
|
*
|
|
* Este componente usa uma subscription Convex para detectar mudanças no estado da máquina:
|
|
* - Quando isActive muda para false: máquina foi desativada
|
|
* - Quando hasValidToken muda para false: máquina foi resetada (tokens revogados)
|
|
*
|
|
* O componente não renderiza nada, apenas monitora e chama callbacks quando detecta mudanças.
|
|
*/
|
|
|
|
import { useEffect, useRef } from "react"
|
|
import { useQuery, ConvexProvider } from "convex/react"
|
|
import type { ConvexReactClient } from "convex/react"
|
|
import { api } from "../convex/_generated/api"
|
|
import type { Id } from "../convex/_generated/dataModel"
|
|
|
|
type MachineStateMonitorProps = {
|
|
machineId: string
|
|
onDeactivated?: () => void
|
|
onTokenRevoked?: () => void
|
|
onReactivated?: () => void
|
|
}
|
|
|
|
function MachineStateMonitorInner({ machineId, onDeactivated, onTokenRevoked, onReactivated }: MachineStateMonitorProps) {
|
|
const machineState = useQuery(api.machines.getMachineState, {
|
|
machineId: machineId as Id<"machines">,
|
|
})
|
|
|
|
// Refs para rastrear o estado anterior e evitar chamadas duplicadas
|
|
const previousIsActive = useRef<boolean | null>(null)
|
|
const previousHasValidToken = useRef<boolean | null>(null)
|
|
const initialLoadDone = useRef(false)
|
|
|
|
useEffect(() => {
|
|
if (!machineState) return
|
|
|
|
// Na primeira carga, verifica estado inicial E armazena valores
|
|
if (!initialLoadDone.current) {
|
|
console.log("[MachineStateMonitor] Carga inicial", {
|
|
isActive: machineState.isActive,
|
|
hasValidToken: machineState.hasValidToken,
|
|
found: machineState.found,
|
|
})
|
|
|
|
// Se já estiver desativado na carga inicial, chama callback
|
|
if (machineState.isActive === false) {
|
|
console.log("[MachineStateMonitor] Máquina já estava desativada")
|
|
onDeactivated?.()
|
|
}
|
|
|
|
// Se token já estiver inválido na carga inicial, chama callback
|
|
if (machineState.hasValidToken === false) {
|
|
console.log("[MachineStateMonitor] Token já estava revogado")
|
|
onTokenRevoked?.()
|
|
}
|
|
|
|
previousIsActive.current = machineState.isActive
|
|
previousHasValidToken.current = machineState.hasValidToken
|
|
initialLoadDone.current = true
|
|
return
|
|
}
|
|
|
|
// Detecta mudança de ativo para inativo
|
|
if (previousIsActive.current === true && machineState.isActive === false) {
|
|
console.log("[MachineStateMonitor] Máquina foi desativada")
|
|
onDeactivated?.()
|
|
}
|
|
|
|
// Detecta mudança de inativo para ativo (reativação)
|
|
if (previousIsActive.current === false && machineState.isActive === true) {
|
|
console.log("[MachineStateMonitor] Máquina foi reativada")
|
|
onReactivated?.()
|
|
}
|
|
|
|
// Detecta mudança de token válido para inválido
|
|
if (previousHasValidToken.current === true && machineState.hasValidToken === false) {
|
|
console.log("[MachineStateMonitor] Token foi revogado (reset)")
|
|
onTokenRevoked?.()
|
|
}
|
|
|
|
// Atualiza refs
|
|
previousIsActive.current = machineState.isActive
|
|
previousHasValidToken.current = machineState.hasValidToken
|
|
}, [machineState, onDeactivated, onTokenRevoked, onReactivated])
|
|
|
|
// Este componente nao renderiza nada
|
|
return null
|
|
}
|
|
|
|
type MachineStateMonitorWithClientProps = MachineStateMonitorProps & {
|
|
client: ConvexReactClient
|
|
}
|
|
|
|
/**
|
|
* Wrapper que recebe o cliente Convex e envolve o monitor com o provider
|
|
*/
|
|
export function MachineStateMonitor({ client, ...props }: MachineStateMonitorWithClientProps) {
|
|
return (
|
|
<ConvexProvider client={client}>
|
|
<MachineStateMonitorInner {...props} />
|
|
</ConvexProvider>
|
|
)
|
|
}
|