The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { sessionHasLegacyPermissions, type EnvironmentId } from "@t3tools/contracts"; |
| 2 | import type { AnyRouter } from "@tanstack/react-router"; |
| 3 | import { msg } from "gt-react"; |
| 4 | import * as Schema from "effect/Schema"; |
| 5 | import { Atom } from "effect/reactivity"; |
| 6 | |
| 7 | import { getLocalStorageItem, setLocalStorageItem } from "../hooks/useLocalStorage"; |
| 8 | import { m } from "../i18n/messages"; |
| 9 | import { appAtomRegistry } from "../rpc/atomRegistry"; |
| 10 | import { environmentSummaries } from "../state/presentation"; |
| 11 | import { environmentSession } from "../state/session"; |
| 12 | import { toastManager } from "./ui/toast"; |
| 13 | |
| 14 | interface LegacyPermissionEnvironment { |
| 15 | readonly environmentId: EnvironmentId; |
| 16 | readonly label: string; |
| 17 | } |
| 18 | |
| 19 | /** Environments whose settled session still carries pre-scope permissions. */ |
| 20 | const legacyPermissionEnvironmentsAtom = Atom.make((get) => |
| 21 | get(environmentSummaries.identitiesAtom).filter(({ environmentId }) => { |
| 22 | const session = get(environmentSession.sessionStateAtom(environmentId)); |
| 23 | return ( |
| 24 | session._tag === "Success" && !session.waiting && sessionHasLegacyPermissions(session.value) |
| 25 | ); |
| 26 | }), |
| 27 | ).pipe(Atom.withLabel("web-legacy-permission-environments")); |
| 28 | |
| 29 | const dismissalKey = (environmentId: EnvironmentId) => |
| 30 | `t3code:permission-update:v1:${environmentId}`; |
| 31 | |
| 32 | /** Shows the notice and returns a withdraw that closes it without recording a dismissal. */ |
| 33 | function showPermissionNotice( |
| 34 | router: AnyRouter, |
| 35 | { environmentId, label }: LegacyPermissionEnvironment, |
| 36 | ) { |
| 37 | const key = dismissalKey(environmentId); |
| 38 | let withdrawn = false; |
| 39 | const persistDismissal = () => { |
| 40 | if (withdrawn) return; |
| 41 | try { |
| 42 | setLocalStorageItem(key, true, Schema.Boolean); |
| 43 | } catch {} |
| 44 | }; |
| 45 | const dismiss = () => { |
| 46 | persistDismissal(); |
| 47 | toastManager.close(id); |
| 48 | }; |
| 49 | const id = toastManager.add({ |
| 50 | title: m(msg("Permissions have changed for {label}", { label })), |
| 51 | description: m( |
| 52 | msg( |
| 53 | "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.", |
| 54 | ), |
| 55 | ), |
| 56 | timeout: 0, |
| 57 | onClose: persistDismissal, |
| 58 | actionProps: { |
| 59 | children: m(msg("Open Sharing & access")), |
| 60 | onClick: () => { |
| 61 | dismiss(); |
| 62 | void router.navigate({ to: "/settings/access" }); |
| 63 | }, |
| 64 | }, |
| 65 | data: { |
| 66 | actionLayout: "stacked-end", |
| 67 | secondaryActionProps: { children: m(msg("Dismiss")), onClick: dismiss }, |
| 68 | secondaryActionVariant: "ghost", |
| 69 | }, |
| 70 | }); |
| 71 | return () => { |
| 72 | withdrawn = true; |
| 73 | toastManager.close(id); |
| 74 | }; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Asks once per environment, until dismissed, to re-pair a connection that predates scoped |
| 79 | * permissions. Removing the environment withdraws its notice. |
| 80 | */ |
| 81 | export function startPermissionUpdateNotices(router: AnyRouter): () => void { |
| 82 | const shown = new Map<EnvironmentId, () => void>(); |
| 83 | const stopShowing = appAtomRegistry.subscribe( |
| 84 | legacyPermissionEnvironmentsAtom, |
| 85 | (environments) => { |
| 86 | for (const environment of environments) { |
| 87 | if (shown.has(environment.environmentId)) continue; |
| 88 | try { |
| 89 | if (getLocalStorageItem(dismissalKey(environment.environmentId), Schema.Boolean)) |
| 90 | continue; |
| 91 | } catch {} |
| 92 | shown.set(environment.environmentId, showPermissionNotice(router, environment)); |
| 93 | } |
| 94 | }, |
| 95 | { immediate: true }, |
| 96 | ); |
| 97 | const stopWithdrawing = appAtomRegistry.subscribe( |
| 98 | environmentSummaries.environmentIdsAtom, |
| 99 | (environmentIds) => { |
| 100 | const present = new Set(environmentIds); |
| 101 | for (const [environmentId, withdraw] of shown) { |
| 102 | if (present.has(environmentId)) continue; |
| 103 | shown.delete(environmentId); |
| 104 | withdraw(); |
| 105 | } |
| 106 | }, |
| 107 | ); |
| 108 | return () => { |
| 109 | stopShowing(); |
| 110 | stopWithdrawing(); |
| 111 | }; |
| 112 | } |