| 1 | import { AuthDiagnosticsReadScope } from "@t3tools/contracts"; |
| 2 | import { AuthEnvironmentMaintainScope } from "@t3tools/contracts"; |
| 3 | import type { |
| 4 | EnvironmentId, |
| 5 | ResourceTelemetryHistoryInput, |
| 6 | ResourceTelemetrySnapshot, |
| 7 | } from "@t3tools/contracts"; |
| 8 | import * as Cause from "effect/Cause"; |
| 9 | import { useCallback } from "react"; |
| 10 | |
| 11 | import { usePrimaryEnvironment } from "../state/environments"; |
| 12 | import { useEnvironmentQuery } from "../state/query"; |
| 13 | import { serverEnvironment } from "../state/server"; |
| 14 | import { readEnvironmentScope, useEnvironmentScope } from "../state/session"; |
| 15 | import { useAtomCommand } from "../state/use-atom-command"; |
| 16 | |
| 17 | export interface ResourceTelemetryState { |
| 18 | readonly data: ResourceTelemetrySnapshot | null; |
| 19 | readonly error: string | null; |
| 20 | readonly isPending: boolean; |
| 21 | readonly refresh: () => void; |
| 22 | readonly retry: () => Promise<ResourceTelemetrySnapshot>; |
| 23 | } |
| 24 | |
| 25 | export function useResourceTelemetry( |
| 26 | targetEnvironmentId?: EnvironmentId | null, |
| 27 | ): ResourceTelemetryState { |
| 28 | const primaryEnvironment = usePrimaryEnvironment(); |
| 29 | const environmentId = |
| 30 | targetEnvironmentId === undefined |
| 31 | ? (primaryEnvironment?.environmentId ?? null) |
| 32 | : targetEnvironmentId; |
| 33 | const canReadDiagnostics = useEnvironmentScope(environmentId, AuthDiagnosticsReadScope); |
| 34 | const query = useEnvironmentQuery( |
| 35 | environmentId === null || !canReadDiagnostics |
| 36 | ? null |
| 37 | : serverEnvironment.resourceTelemetry({ environmentId, input: {} }), |
| 38 | ); |
| 39 | const retryCommand = useAtomCommand(serverEnvironment.retryResourceTelemetry, { |
| 40 | reportFailure: false, |
| 41 | }); |
| 42 | const retry = useCallback(async () => { |
| 43 | if (environmentId === null) { |
| 44 | throw new Error("No environment is selected."); |
| 45 | } |
| 46 | if ( |
| 47 | !readEnvironmentScope(environmentId, AuthEnvironmentMaintainScope) || |
| 48 | !readEnvironmentScope(environmentId, AuthDiagnosticsReadScope) |
| 49 | ) { |
| 50 | throw new Error("This connection cannot restart the resource monitor."); |
| 51 | } |
| 52 | const result = await retryCommand({ environmentId, input: {} }); |
| 53 | if (result._tag === "Failure") { |
| 54 | throw Cause.squash(result.cause); |
| 55 | } |
| 56 | return result.value.snapshot; |
| 57 | }, [environmentId, retryCommand]); |
| 58 | |
| 59 | return { ...query, retry }; |
| 60 | } |
| 61 | |
| 62 | export function useResourceTelemetryHistory( |
| 63 | input: ResourceTelemetryHistoryInput, |
| 64 | targetEnvironmentId?: EnvironmentId | null, |
| 65 | ) { |
| 66 | const primaryEnvironment = usePrimaryEnvironment(); |
| 67 | const environmentId = |
| 68 | targetEnvironmentId === undefined |
| 69 | ? (primaryEnvironment?.environmentId ?? null) |
| 70 | : targetEnvironmentId; |
| 71 | const canReadDiagnostics = useEnvironmentScope(environmentId, AuthDiagnosticsReadScope); |
| 72 | return useEnvironmentQuery( |
| 73 | environmentId === null || !canReadDiagnostics |
| 74 | ? null |
| 75 | : serverEnvironment.resourceTelemetryHistory({ environmentId, input }), |
| 76 | ); |
| 77 | } |