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