@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
tsx 88 lines 3.49 KB
Raw
1 import { useAtomValue } from "@effect/atom-react";
2 import { Atom } from "effect/reactivity";
3 import { useLocale, useMessages } from "gt-react-native";
4 import * as Linking from "expo-linking";
5 import { useEffect, useEffectEvent } from "react";
6 import { Platform } from "react-native";
7 import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
8 import { environmentCatalog } from "../connection/catalog";
9 import { environmentPresentations } from "../state/presentation";
10 import { republishAgentActivityWidget } from "./publishAgentActivityWidget";
11 import { publishSubscriptionUsage } from "./publishSubscriptionUsage";
12 import { buildSubscriptionUsageSnapshot } from "./subscriptionUsageSnapshot";
13 import { createWidgetTheme } from "./widgetTheme";
14 import type { MessageResolver } from "../i18n/messages";
15
16 // Isolate quota changes from the much busier thread/config presentation stream.
17 // One atom per UI language, so a language switch republishes the widget's copy.
18 const snapshotAtoms = new Map<string, ReturnType<typeof makeSnapshotAtom>>();
19
20 function makeSnapshotAtom(resolve: MessageResolver) {
21 return Atom.make((get) =>
22 buildSubscriptionUsageSnapshot(
23 get(environmentPresentations.presentationsWithProvidersAtom),
24 Linking.createURL("settings/usage", { queryParams: { tab: "limits" } }),
25 Platform.OS === "android" ? Infinity : 6,
26 resolve,
27 ),
28 ).pipe(Atom.withEquality((a, b) => JSON.stringify(a) === JSON.stringify(b)));
29 }
30
31 function snapshotAtom(locale: string, resolve: MessageResolver) {
32 let atom = snapshotAtoms.get(locale);
33 if (atom === undefined) {
34 atom = makeSnapshotAtom(resolve);
35 snapshotAtoms.set(locale, atom);
36 }
37 return atom;
38 }
39
40 export function SubscriptionUsageCoordinator() {
41 const {
42 themeMode,
43 themeAppearance,
44 themeVariablesByAppearance,
45 isReady: appearanceReady,
46 } = useAppearancePreferences();
47 const catalog = useAtomValue(environmentCatalog.catalogValueAtom);
48 const resolve = useMessages();
49 const locale = useLocale();
50 const snapshot = useAtomValue(snapshotAtom(locale, resolve));
51 const androidAppearance = Platform.OS === "android" ? themeAppearance : null;
52 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- publishes subscription usage and theme to the native home screen widget
53 useEffect(() => {
54 if (!catalog.isReady || !appearanceReady) return;
55 void Promise.resolve()
56 .then(() =>
57 publishSubscriptionUsage({
58 ...snapshot,
59 theme: createWidgetTheme(themeMode, themeVariablesByAppearance),
60 }),
61 )
62 .catch((error: unknown) => {
63 console.warn("Could not update subscription usage widget", error);
64 });
65 }, [
66 catalog.isReady,
67 appearanceReady,
68 snapshot,
69 themeMode,
70 themeVariablesByAppearance,
71 // oxlint-disable-next-line react/exhaustive-effect-dependencies -- Android stores resolved colors; System appearance changes must republish them.
72 androidAppearance,
73 ]);
74 const republishActivity = useEffectEvent(() => {
75 republishAgentActivityWidget(resolve).catch((error: unknown) => {
76 console.warn("Could not update agent activity widget", error);
77 });
78 });
79 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- republishes the agent activity widget's copy in the new UI language
80 useEffect(
81 () => republishActivity(),
82 [
83 // oxlint-disable-next-line react/exhaustive-effect-dependencies -- the widget's copy follows the UI language.
84 locale,
85 ],
86 );
87 return null;
88 }