Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

resourceTelemetryState.ts

83 lines2.9 KB

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>;
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 return { ...query, retry };
65​
66export function useResourceTelemetryHistory(
67 input: ResourceTelemetryHistoryInput,
68 targetEnvironmentId?: EnvironmentId | null,
69) {
70 const primaryEnvironment = usePrimaryEnvironment();
71 const environmentId =
72 targetEnvironmentId === undefined
73 ? (primaryEnvironment?.environmentId ?? null)
74 : targetEnvironmentId;
75 const canReadDiagnostics = useEnvironmentScope(environmentId, AuthDiagnosticsReadScope);
76 const resolve = useMessages();
77 return useEnvironmentQuery(
78 environmentId === null || !canReadDiagnostics
79 ? null
80 : serverEnvironment.resourceTelemetryHistory({ environmentId, input }),
81 resolve,
82 );