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

ThreadHostPressureIcon.tsx

79 lines2.8 KB

1import { useAtomValue } from "@effect/atom-react";
2import {
3 type EnvironmentId,
4 type ThreadId,
5 hostPressureCulprit,
6 hostPressureRanking,
7} from "@t3tools/contracts";
8import { useGT } from "gt-react-native";
9import { Pressable } from "react-native";
10​
11import { SymbolView } from "../../components/ui/AppSymbol";
12import * as Dialog from "../../components/ui/Dialog";
13import { environmentPresentations } from "../../state/presentation";
14import { environmentServerConfigsAtom } from "../../state/server";
15import { hostPressureAgentUsage } from "../home/hostPressureAgentUsage";
16​
17export interface ThreadHostPressureNote {
18 readonly title: string;
19 readonly detail: string;
21​
22/** Set while the thread's agent is among the heaviest on a connected machine short on memory or CPU. */
23export function useThreadHostPressureNote(
24 environmentId: EnvironmentId,
25 threadId: ThreadId,
26): ThreadHostPressureNote | null {
27 const gt = useGT();
28 // Selects the server's own agent object and primitives, so only culprit rows re-render.
29 const agent = useAtomValue(environmentServerConfigsAtom, (configs) =>
30 hostPressureCulprit(configs.get(environmentId)?.hostPressure, threadId),
31 );
32 const rankedBy = useAtomValue(environmentServerConfigsAtom, (configs) => {
33 const pressure = configs.get(environmentId)?.hostPressure;
34 return pressure === undefined ? "memory" : hostPressureRanking(pressure);
35 });
36 const connected = useAtomValue(
37 environmentPresentations.presentationsAtom,
38 (presentations) => presentations.get(environmentId)?.connection.phase === "connected",
39 );
40 if (agent === null || !connected) return null;
41 const usage = hostPressureAgentUsage(gt, agent, rankedBy);
42 return rankedBy === "memory"
43 ? {
44 title: gt`Memory is slowing agents down`,
45 detail: gt`Using ${{ usage }} while memory is slowing agents down. Stopping it helps the rest.`,
46 }
47 : {
48 title: gt`Machine overloaded`,
49 detail: gt`Using ${{ usage }} while this machine is overloaded. Stopping it helps the rest.`,
50 };
52​
53export function showThreadHostPressureNote(note: ThreadHostPressureNote) {
54 Dialog.alert(note.title, note.detail);
56​
57/** Marks a thread whose agent is among the heaviest on a machine short on memory or CPU. */
58export function ThreadHostPressureIcon(props: {
59 readonly note: ThreadHostPressureNote;
60 readonly tintColorClassName: string;
61}) {
62 return (
63 <Pressable
64 accessibilityRole="button"
65 accessibilityLabel={props.note.detail}
66 hitSlop={8}
67 onPress={() => showThreadHostPressureNote(props.note)}
68 onLongPress={() => undefined}
69 style={({ pressed }) => ({ opacity: pressed ? 0.6 : 1 })}
70 >
71 <SymbolView
72 name="exclamationmark.triangle"
73 size={11}
74 tintColorClassName={props.tintColorClassName}
75 type="monochrome"
76 />
77 </Pressable>
78 );