Log machine context errors in portal
This commit is contained in:
parent
545d5bea4b
commit
0fb95147f4
3 changed files with 82 additions and 5 deletions
|
|
@ -37,6 +37,12 @@ type MachineContext = {
|
|||
companyId: string | null
|
||||
}
|
||||
|
||||
type MachineContextError = {
|
||||
status: number
|
||||
message: string
|
||||
details?: Record<string, unknown> | null
|
||||
}
|
||||
|
||||
const authClient = createAuthClient({
|
||||
plugins: [customSessionClient<AppAuth>()],
|
||||
fetchOptions: {
|
||||
|
|
@ -53,6 +59,8 @@ type AuthContextValue = {
|
|||
isStaff: boolean
|
||||
isCustomer: boolean
|
||||
machineContext: MachineContext | null
|
||||
machineContextLoading: boolean
|
||||
machineContextError: MachineContextError | null
|
||||
}
|
||||
|
||||
const AuthContext = createContext<AuthContextValue>({
|
||||
|
|
@ -64,6 +72,8 @@ const AuthContext = createContext<AuthContextValue>({
|
|||
isStaff: false,
|
||||
isCustomer: false,
|
||||
machineContext: null,
|
||||
machineContextLoading: false,
|
||||
machineContextError: null,
|
||||
})
|
||||
|
||||
export function useAuth() {
|
||||
|
|
@ -77,6 +87,8 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
|||
const ensureUser = useMutation(api.users.ensureUser)
|
||||
const [convexUserId, setConvexUserId] = useState<string | null>(null)
|
||||
const [machineContext, setMachineContext] = useState<MachineContext | null>(null)
|
||||
const [machineContextLoading, setMachineContextLoading] = useState(false)
|
||||
const [machineContextError, setMachineContextError] = useState<MachineContextError | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
if (!session?.user || session.user.role === "machine") {
|
||||
|
|
@ -87,16 +99,37 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
|||
useEffect(() => {
|
||||
if (!session?.user || session.user.role !== "machine") {
|
||||
setMachineContext(null)
|
||||
setMachineContextError(null)
|
||||
setMachineContextLoading(false)
|
||||
return
|
||||
}
|
||||
|
||||
let cancelled = false
|
||||
setMachineContextLoading(true)
|
||||
setMachineContextError(null)
|
||||
;(async () => {
|
||||
try {
|
||||
const response = await fetch("/api/machines/session", { credentials: "include" })
|
||||
if (!response.ok) {
|
||||
let payload: Record<string, unknown> | null = null
|
||||
try {
|
||||
const parsed = await response.clone().json()
|
||||
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
||||
payload = parsed as Record<string, unknown>
|
||||
}
|
||||
} catch {
|
||||
payload = null
|
||||
}
|
||||
const fallbackMessage = "Falha ao carregar o contexto da m<>quina."
|
||||
const message =
|
||||
(payload && typeof payload.error === "string" && payload.error.trim()) || fallbackMessage
|
||||
if (!cancelled) {
|
||||
setMachineContext(null)
|
||||
setMachineContextError({
|
||||
status: response.status,
|
||||
message,
|
||||
details: payload,
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
@ -122,11 +155,21 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
|||
assignedUserRole: machine.assignedUserRole ?? null,
|
||||
companyId: machine.companyId ?? null,
|
||||
})
|
||||
setMachineContextError(null)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to load machine context", error)
|
||||
if (!cancelled) {
|
||||
setMachineContext(null)
|
||||
setMachineContextError({
|
||||
status: 0,
|
||||
message: "Erro ao carregar o contexto da m<>quina.",
|
||||
details: error instanceof Error ? { message: error.message } : null,
|
||||
})
|
||||
}
|
||||
} finally {
|
||||
if (!cancelled) {
|
||||
setMachineContextLoading(false)
|
||||
}
|
||||
}
|
||||
})()
|
||||
|
|
@ -184,8 +227,10 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
|||
isStaff: isStaff(normalizedRole),
|
||||
isCustomer: normalizedRole === "collaborator",
|
||||
machineContext,
|
||||
machineContextLoading,
|
||||
machineContextError,
|
||||
}),
|
||||
[session, isPending, effectiveConvexUserId, normalizedRole, machineContext]
|
||||
[session, isPending, effectiveConvexUserId, normalizedRole, machineContext, machineContextLoading, machineContextError]
|
||||
)
|
||||
|
||||
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue