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

main
ts 92 lines 3.25 KB
Raw
1 import * as Dialog from "../../components/ui/Dialog";
2 import { sessionHasLegacyPermissions } from "@t3tools/contracts";
3 import { Atom } from "effect/reactivity";
4 import * as SecureStore from "expo-secure-store";
5 import { msg } from "gt-react-native";
6
7 import { m } from "../../i18n/messages";
8 import { whenProtectedDataAvailable } from "../../lib/protectedData";
9 import { appAtomRegistry } from "../../state/atom-registry";
10 import { projectEnvironmentPresentation } from "../../state/environments";
11 import { environmentPresentations } from "../../state/presentation";
12 import { environmentSession } from "../../state/session";
13
14 const storageKey = "t3code.permission-update.v1";
15
16 const legacyPermissionEnvironmentsAtom = Atom.make((get) =>
17 [...get(environmentPresentations.presentationsAtom).entries()].flatMap(
18 ([environmentId, presentation]) => {
19 const session = get(environmentSession.sessionStateAtom(environmentId));
20 return session._tag === "Success" &&
21 !session.waiting &&
22 sessionHasLegacyPermissions(session.value)
23 ? [projectEnvironmentPresentation(environmentId, presentation)]
24 : [];
25 },
26 ),
27 ).pipe(Atom.withLabel("mobile:legacy-permission-environments"));
28
29 async function loadDismissedEnvironmentIds(): Promise<Set<string>> {
30 try {
31 await whenProtectedDataAvailable();
32 const raw = await SecureStore.getItemAsync(storageKey);
33 const value: unknown = raw === null ? [] : JSON.parse(raw);
34 return new Set(
35 Array.isArray(value) ? value.filter((id): id is string => typeof id === "string") : [],
36 );
37 } catch {
38 return new Set();
39 }
40 }
41
42 /**
43 * Tells the user, once per environment, that a paired session still carries
44 * the legacy permission set. App-lifetime; presents one dialog at a time.
45 */
46 export function installPermissionUpdateNotice(): () => void {
47 let dismissed: Set<string> | null = null;
48 const shownThisLaunch = new Set<string>();
49 let showing = false;
50 let disposed = false;
51
52 const presentNext = () => {
53 if (disposed || dismissed === null || showing) return;
54 const known = dismissed;
55 const environment = appAtomRegistry
56 .get(legacyPermissionEnvironmentsAtom)
57 .find(
58 ({ environmentId }) => !known.has(environmentId) && !shownThisLaunch.has(environmentId),
59 );
60 if (!environment) return;
61 showing = true;
62 shownThisLaunch.add(environment.environmentId);
63 const dismiss = () => {
64 known.add(environment.environmentId);
65 showing = false;
66 void SecureStore.setItemAsync(storageKey, JSON.stringify([...known])).catch(() => {});
67 presentNext();
68 };
69 Dialog.alert(
70 m(msg("Permissions have changed for {environment}", { environment: environment.label })),
71 m(
72 msg(
73 "This connection still uses the old permissions, so some actions may no longer be available. Pair again using a new link with the permissions you need.",
74 ),
75 ),
76 [{ text: m(msg("Got it")), onPress: dismiss }],
77 { cancelable: false },
78 );
79 };
80
81 const unsubscribe = appAtomRegistry.subscribe(legacyPermissionEnvironmentsAtom, presentNext, {
82 immediate: true,
83 });
84 void loadDismissedEnvironmentIds().then((ids) => {
85 dismissed = ids;
86 presentNext();
87 });
88 return () => {
89 disposed = true;
90 unsubscribe();
91 };
92 }