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
| 1 | import { useAtomValue } from "@effect/atom-react"; |
| 2 | import { |
| 3 | AuthDiagnosticsReadScope, |
| 4 | USAGE_CONTRACT_VERSION, |
| 5 | type EnvironmentId, |
| 6 | type UsageSummary, |
| 7 | type UsageSummaryInput, |
| 8 | } from "@t3tools/contracts"; |
| 9 | import { refreshUsage, usageSummaryInput } from "@t3tools/client-runtime/state/usage"; |
| 10 | import { resolveUsageAccess } from "@t3tools/client-runtime/state/usage-access"; |
| 11 | import { mergeUsage, type EnvironmentUsage, type MergedUsage } from "@t3tools/shared/usageMerge"; |
| 12 | import { AsyncResult, Atom } from "effect/reactivity"; |
| 13 | import { msg, useMessages } from "gt-react-native"; |
| 14 | import { useCallback, useMemo } from "react"; |
| 15 | |
| 16 | import { keepMarked } from "../i18n/messages"; |
| 17 | import { appAtomRegistry } from "./atom-registry"; |
| 18 | import { environmentPresentations } from "./presentation"; |
| 19 | import { serverEnvironment } from "./server"; |
| 20 | import { environmentSession, readEnvironmentScope } from "./session"; |
| 21 | |
| 22 | export interface EnvironmentUsageStatus { |
| 23 | readonly environmentId: EnvironmentId; |
| 24 | readonly label: string; |
| 25 | readonly isPending: boolean; |
| 26 | readonly isConnected: boolean; |
| 27 | readonly canReadDiagnostics: boolean; |
| 28 | readonly error: string | null; |
| 29 | readonly summary: UsageSummary | null; |
| 30 | } |
| 31 | |
| 32 | const usageByWindowAtom = Atom.family((windowKey: string) => |
| 33 | Atom.make((get): readonly EnvironmentUsageStatus[] => { |
| 34 | const input = JSON.parse(windowKey) as UsageSummaryInput; |
| 35 | const presentations = get(environmentPresentations.presentationsAtom); |
| 36 | |
| 37 | const statuses: EnvironmentUsageStatus[] = []; |
| 38 | for (const [environmentId, presentation] of presentations) { |
| 39 | const sessionResult = get(environmentSession.sessionStateAtom(environmentId)); |
| 40 | const access = resolveUsageAccess( |
| 41 | { |
| 42 | connectionPhase: presentation.connection.phase, |
| 43 | session: AsyncResult.getOrElse(sessionResult, () => null), |
| 44 | hasSessionError: sessionResult._tag === "Failure", |
| 45 | }, |
| 46 | keepMarked, |
| 47 | ); |
| 48 | if (!access.canReadDiagnostics) { |
| 49 | statuses.push({ |
| 50 | environmentId, |
| 51 | label: presentation.entry.target.label, |
| 52 | isConnected: presentation.connection.phase === "connected", |
| 53 | ...access, |
| 54 | summary: null, |
| 55 | }); |
| 56 | continue; |
| 57 | } |
| 58 | const result = get( |
| 59 | serverEnvironment.usageSummary({ |
| 60 | environmentId, |
| 61 | input: usageSummaryInput( |
| 62 | input, |
| 63 | get(serverEnvironment.usageModelFilterAtom(environmentId)), |
| 64 | ), |
| 65 | }), |
| 66 | ); |
| 67 | const summary = AsyncResult.getOrElse(result, () => null); |
| 68 | statuses.push({ |
| 69 | environmentId, |
| 70 | label: presentation.entry.target.label, |
| 71 | isPending: result.waiting, |
| 72 | isConnected: presentation.connection.phase === "connected", |
| 73 | canReadDiagnostics: true, |
| 74 | error: result._tag === "Failure" ? msg("This environment could not report usage.") : null, |
| 75 | summary, |
| 76 | }); |
| 77 | } |
| 78 | return statuses; |
| 79 | }).pipe(Atom.withLabel(`mobile-usage:window:${windowKey}`)), |
| 80 | ); |
| 81 | |
| 82 | /** Every environment usage can come from, without asking any of them for usage. */ |
| 83 | const usageEnvironmentsAtom = Atom.make((get): readonly EnvironmentUsageStatus[] => |
| 84 | [...get(environmentPresentations.presentationsAtom)].map(([environmentId, presentation]) => ({ |
| 85 | environmentId, |
| 86 | label: presentation.entry.target.label, |
| 87 | isPending: true, |
| 88 | isConnected: presentation.connection.phase === "connected", |
| 89 | canReadDiagnostics: false, |
| 90 | error: null, |
| 91 | summary: null, |
| 92 | })), |
| 93 | ).pipe(Atom.withLabel("mobile-usage:environments")); |
| 94 | |
| 95 | export interface UsageView { |
| 96 | readonly merged: MergedUsage; |
| 97 | readonly environments: readonly EnvironmentUsageStatus[]; |
| 98 | readonly selectedEnvironments: readonly EnvironmentUsageStatus[]; |
| 99 | |
| 100 | readonly isPending: boolean; |
| 101 | |
| 102 | readonly isPartial: boolean; |
| 103 | readonly refresh: (input?: UsageSummaryInput) => Promise<void>; |
| 104 | } |
| 105 | |
| 106 | export function useUsage( |
| 107 | input: UsageSummaryInput, |
| 108 | selectedEnvironmentIds: ReadonlySet<EnvironmentId> | null = null, |
| 109 | /** Off, it lists the environments without loading their usage. */ |
| 110 | load = true, |
| 111 | ): UsageView { |
| 112 | const windowKey = useMemo( |
| 113 | () => |
| 114 | JSON.stringify({ |
| 115 | sinceDay: input.sinceDay, |
| 116 | untilDay: input.untilDay, |
| 117 | timeZone: input.timeZone, |
| 118 | resolution: input.resolution, |
| 119 | sinceTime: input.sinceTime, |
| 120 | untilTime: input.untilTime, |
| 121 | }), |
| 122 | [ |
| 123 | input.sinceDay, |
| 124 | input.untilDay, |
| 125 | input.timeZone, |
| 126 | input.resolution, |
| 127 | input.sinceTime, |
| 128 | input.untilTime, |
| 129 | ], |
| 130 | ); |
| 131 | const messages = useMessages(); |
| 132 | const markedEnvironments = useAtomValue( |
| 133 | load ? usageByWindowAtom(windowKey) : usageEnvironmentsAtom, |
| 134 | ); |
| 135 | const environments = useMemo( |
| 136 | () => |
| 137 | markedEnvironments.map((environment) => |
| 138 | environment.error === null |
| 139 | ? environment |
| 140 | : { ...environment, error: messages(environment.error) }, |
| 141 | ), |
| 142 | [markedEnvironments, messages], |
| 143 | ); |
| 144 | const selectedEnvironments = useMemo( |
| 145 | () => |
| 146 | selectedEnvironmentIds === null |
| 147 | ? environments |
| 148 | : environments.filter(({ environmentId }) => selectedEnvironmentIds.has(environmentId)), |
| 149 | [environments, selectedEnvironmentIds], |
| 150 | ); |
| 151 | |
| 152 | const refresh = useCallback( |
| 153 | (nextInput?: UsageSummaryInput) => |
| 154 | refreshUsage({ |
| 155 | registry: appAtomRegistry, |
| 156 | server: serverEnvironment, |
| 157 | presentations: environmentPresentations, |
| 158 | |
| 159 | environmentIds: selectedEnvironments |
| 160 | .filter( |
| 161 | (environment) => |
| 162 | environment.canReadDiagnostics && |
| 163 | readEnvironmentScope(environment.environmentId, AuthDiagnosticsReadScope), |
| 164 | ) |
| 165 | .map(({ environmentId }) => environmentId), |
| 166 | input: nextInput ?? (JSON.parse(windowKey) as UsageSummaryInput), |
| 167 | }), |
| 168 | [selectedEnvironments, windowKey], |
| 169 | ); |
| 170 | |
| 171 | const merged = useMemo(() => { |
| 172 | const answered: EnvironmentUsage[] = selectedEnvironments.flatMap((environment) => |
| 173 | environment.summary === null |
| 174 | ? [] |
| 175 | : [ |
| 176 | { |
| 177 | environmentId: environment.environmentId, |
| 178 | label: environment.label, |
| 179 | summary: environment.summary, |
| 180 | }, |
| 181 | ], |
| 182 | ); |
| 183 | return mergeUsage(answered, USAGE_CONTRACT_VERSION); |
| 184 | }, [selectedEnvironments]); |
| 185 | |
| 186 | const answeredCount = selectedEnvironments.filter( |
| 187 | (environment) => environment.summary !== null, |
| 188 | ).length; |
| 189 | const stillReporting = selectedEnvironments.filter( |
| 190 | (environment) => environment.summary === null && environment.error === null, |
| 191 | ).length; |
| 192 | |
| 193 | return { |
| 194 | merged, |
| 195 | environments, |
| 196 | selectedEnvironments, |
| 197 | isPending: answeredCount === 0 && stillReporting > 0, |
| 198 | isPartial: answeredCount > 0 && stillReporting > 0, |
| 199 | refresh, |
| 200 | }; |
| 201 | } |