@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 83 lines 2.89 KB
Raw
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 { msg, useMessages } from "gt-react";
10
11 import { m } from "../i18n/messages";
12 import { usePrimaryEnvironment } from "../state/environments";
13 import { useEnvironmentQuery } from "../state/query";
14 import { serverEnvironment } from "../state/server";
15 import { readEnvironmentScope, useEnvironmentScope } from "../state/session";
16 import { useAtomCommand } from "../state/use-atom-command";
17
18 export 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
26 export 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 };
64 }
65
66 export 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 );
83 }