@@ -7,17 +7,73 @@ import {
77} from "@t3tools/contracts" ;
88import { useCallback } from "react" ;
99
10+ import { isElectron } from "../../env" ;
11+ import { usePrimarySessionState } from "../../environments/primary" ;
1012import { useUpdateEnvironmentSettings } from "../../hooks/useSettings" ;
13+ import { usePrimaryEnvironmentId } from "../../state/environments" ;
14+ import { useEnvironmentSessionState } from "../../state/session" ;
1115import { ENVIRONMENT_MACHINE_KIND_LABELS , EnvironmentMachineIcon } from "../EnvironmentMachineIcon" ;
1216import { Select , SelectItem , SelectPopup , SelectTrigger , SelectValue } from "../ui/select" ;
17+ import { Tooltip , TooltipPopup , TooltipTrigger } from "../ui/tooltip" ;
18+ import {
19+ resolvePrimaryOperateAccess ,
20+ resolveRemoteOperateAccess ,
21+ } from "./ProviderSettingsPanel.logic" ;
1322
1423const AUTOMATIC_VALUE = "automatic" ;
1524
25+ /**
26+ * Why the picker is inert, in the order the user can do something about it.
27+ * Null means it can be changed.
28+ */
29+ export function resolveEnvironmentIconPickerLock ( input : {
30+ readonly serverConfig : ServerConfig | null ;
31+ readonly operateAccess : "granted" | "denied" | "pending" ;
32+ } ) : string | null {
33+ if ( input . serverConfig === null ) {
34+ return "Connect to this environment to change its icon." ;
35+ }
36+ if ( input . serverConfig . environment . capabilities . environmentIcon !== true ) {
37+ return "This environment's server is too old to keep an icon. Update it to choose one." ;
38+ }
39+ if ( input . operateAccess === "denied" ) {
40+ return "Your session on this environment cannot change its settings." ;
41+ }
42+ return null ;
43+ }
44+
45+ // Same split the provider settings use: the desktop app owns its primary
46+ // server outright, a browser session on the primary checks its cookie
47+ // session's scopes, and a remote checks the scopes its own server reports.
48+ function useEnvironmentOperateAccess ( environmentId : EnvironmentId ) {
49+ const isPrimary = usePrimaryEnvironmentId ( ) === environmentId ;
50+ const primarySession = usePrimarySessionState ( ) ;
51+ const remoteSession = useEnvironmentSessionState ( environmentId ) ;
52+ if ( isPrimary ) {
53+ return isElectron
54+ ? "granted"
55+ : resolvePrimaryOperateAccess ( {
56+ isPrimary : true ,
57+ hasDesktopBridge : false ,
58+ session : primarySession . data ,
59+ isPending : primarySession . isPending ,
60+ hasError : primarySession . error !== null ,
61+ } ) ;
62+ }
63+ return resolveRemoteOperateAccess ( {
64+ session : remoteSession . data ,
65+ isPending : remoteSession . isPending ,
66+ hasError : remoteSession . hasError ,
67+ } ) ;
68+ }
69+
1670/**
1771 * Picks the machine glyph an environment wears everywhere it is listed.
1872 * "Automatic" clears the override so the server's own detection shows
1973 * through; the label says what that currently resolves to so the user can
20- * tell whether detection got it right before overriding.
74+ * tell whether detection got it right before overriding. The control stays
75+ * visible while locked so the current icon still reads, the same way
76+ * server-scoped rows go inert instead of disappearing.
2177 */
2278export function EnvironmentIconPicker ( {
2379 environmentId,
@@ -29,6 +85,8 @@ export function EnvironmentIconPicker({
2985 readonly size ?: "xs" | "sm" ;
3086} ) {
3187 const updateSettings = useUpdateEnvironmentSettings ( environmentId ) ;
88+ const operateAccess = useEnvironmentOperateAccess ( environmentId ) ;
89+ const lock = resolveEnvironmentIconPickerLock ( { serverConfig, operateAccess } ) ;
3290 const override = serverConfig ?. settings . environmentIcon ?? null ;
3391 const detected = serverConfig ?. environment . platform . machine ?? null ;
3492 const resolved = resolveEnvironmentMachineKind ( serverConfig ) ;
@@ -48,8 +106,8 @@ export function EnvironmentIconPicker({
48106 [ updateSettings ] ,
49107 ) ;
50108
51- return (
52- < Select value = { value } onValueChange = { handleValueChange } disabled = { serverConfig = == null } >
109+ const select = (
110+ < Select value = { value } onValueChange = { handleValueChange } disabled = { lock ! == null } >
53111 < SelectTrigger size = { size } className = "w-full sm:w-52" aria-label = "Environment icon" >
54112 < SelectValue >
55113 < span className = "flex min-w-0 items-center gap-2" >
@@ -78,4 +136,26 @@ export function EnvironmentIconPicker({
78136 </ SelectPopup >
79137 </ Select >
80138 ) ;
139+
140+ if ( lock === null ) {
141+ return select ;
142+ }
143+ return (
144+ < Tooltip >
145+ < TooltipTrigger
146+ render = {
147+ // Focusable so keyboard users can still reach the explanation.
148+ < span
149+ tabIndex = { 0 }
150+ className = "flex w-full items-center rounded-md outline-none focus-visible:ring-2 focus-visible:ring-ring sm:w-auto"
151+ />
152+ }
153+ >
154+ < span className = "flex w-full items-center sm:w-auto" > { select } </ span >
155+ </ TooltipTrigger >
156+ < TooltipPopup side = "top" className = "max-w-72" >
157+ { lock }
158+ </ TooltipPopup >
159+ </ Tooltip >
160+ ) ;
81161}
0 commit comments