The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { resolveEnvironmentMachineKind, type EnvironmentId } from "@t3tools/contracts"; |
| 2 | import { useLocation, useNavigate } from "@tanstack/react-router"; |
| 3 | import { useGT } from "gt-react"; |
| 4 | import { ChevronRightIcon, LayersIcon } from "lucide-react"; |
| 5 | import type { ReactNode } from "react"; |
| 6 | |
| 7 | import { cn } from "../../lib/utils"; |
| 8 | import type { SidebarProjectSnapshot } from "../../sidebarProjectGrouping"; |
| 9 | import { useEnvironments, type EnvironmentPresentation } from "../../state/environments"; |
| 10 | import { EnvironmentMachineIcon } from "../EnvironmentMachineIcon"; |
| 11 | import { ProjectFavicon } from "../ProjectFavicon"; |
| 12 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 13 | import { ITEM_ROW_CLASSNAME } from "./itemRows"; |
| 14 | import { SettingsGroup } from "./SettingsGroup"; |
| 15 | import { SettingsPageContainer, SettingsSection } from "./settingsLayout"; |
| 16 | import { useSettingsScope } from "./SettingsScopeContext"; |
| 17 | import type { SettingsScopeSearch } from "./settingsScope"; |
| 18 | import { |
| 19 | projectScopeMenuGroups, |
| 20 | selectProjectAxis, |
| 21 | settingsScopeEnvironmentAddress, |
| 22 | settingsScopeEnvironmentLabels, |
| 23 | settingsScopeProjectMachines, |
| 24 | } from "./settingsScopeAxis"; |
| 25 | |
| 26 | interface ScopeChoice { |
| 27 | label: string; |
| 28 | secondary?: string; |
| 29 | icon: ReactNode; |
| 30 | search: SettingsScopeSearch; |
| 31 | } |
| 32 | |
| 33 | export function SettingsScopeNotice({ |
| 34 | children, |
| 35 | target, |
| 36 | targetId, |
| 37 | eligibleEnvironmentIds, |
| 38 | inline = false, |
| 39 | }: { |
| 40 | children: string; |
| 41 | /** Renders inside a section instead of as its own page. */ |
| 42 | inline?: boolean; |
| 43 | target: "environment" | "all" | "project" | "checkout"; |
| 44 | targetId?: string; |
| 45 | eligibleEnvironmentIds?: readonly EnvironmentId[]; |
| 46 | }) { |
| 47 | const gt = useGT(); |
| 48 | const { selectScope, search, groups } = useSettingsScope(); |
| 49 | const navigate = useNavigate({ from: "/settings" }); |
| 50 | const pathname = useLocation({ select: (location) => location.pathname }); |
| 51 | const { environments } = useEnvironments(); |
| 52 | const environmentIcon = (environment: EnvironmentPresentation | undefined) => |
| 53 | environment ? ( |
| 54 | <EnvironmentMachineIcon |
| 55 | aria-hidden |
| 56 | kind={resolveEnvironmentMachineKind(environment.serverConfig)} |
| 57 | className="size-4 shrink-0 text-muted-foreground" |
| 58 | /> |
| 59 | ) : null; |
| 60 | const choiceGroups = |
| 61 | target === "checkout" |
| 62 | ? groups.filter((group) => !search.project || group.projectKey === search.project) |
| 63 | : [null]; |
| 64 | const projectGroups = projectScopeMenuGroups(groups, search); |
| 65 | const renderChoices = (group: SidebarProjectSnapshot | null) => { |
| 66 | const choices: ScopeChoice[] = group |
| 67 | ? group.memberProjects.map((member) => { |
| 68 | const environment = environments.find( |
| 69 | (entry) => entry.environmentId === member.environmentId, |
| 70 | ); |
| 71 | const { primary, secondary } = environment |
| 72 | ? settingsScopeEnvironmentLabels(environment, environments) |
| 73 | : { |
| 74 | primary: member.environmentLabel ?? gt("Environment"), |
| 75 | secondary: member.environmentId, |
| 76 | }; |
| 77 | return { |
| 78 | label: primary, |
| 79 | secondary: [secondary, member.workspaceRoot].filter(Boolean).join(" · "), |
| 80 | icon: environmentIcon(environment), |
| 81 | search: { |
| 82 | project: group.projectKey, |
| 83 | machine: member.environmentId, |
| 84 | checkout: member.physicalProjectKey, |
| 85 | }, |
| 86 | }; |
| 87 | }) |
| 88 | : target === "project" |
| 89 | ? projectGroups.map((group) => ({ |
| 90 | label: group.displayName, |
| 91 | secondary: [ |
| 92 | ...(group.archived ? [gt("Archived", { $context: "project state" })] : []), |
| 93 | ...(projectGroups.some( |
| 94 | (other) => |
| 95 | other.projectKey !== group.projectKey && other.displayName === group.displayName, |
| 96 | ) |
| 97 | ? settingsScopeProjectMachines(group, environments) |
| 98 | : []), |
| 99 | group.workspaceRoot, |
| 100 | ].join(" · "), |
| 101 | icon: <ProjectFavicon project={group} className="size-4 shrink-0" />, |
| 102 | search: selectProjectAxis(search, group.projectKey), |
| 103 | })) |
| 104 | : target === "environment" |
| 105 | ? environments |
| 106 | .filter( |
| 107 | (entry) => |
| 108 | eligibleEnvironmentIds === undefined || |
| 109 | eligibleEnvironmentIds.includes(entry.environmentId), |
| 110 | ) |
| 111 | .map((entry) => { |
| 112 | const { primary, secondary } = settingsScopeEnvironmentLabels(entry, environments); |
| 113 | return { |
| 114 | label: primary, |
| 115 | secondary: secondary || settingsScopeEnvironmentAddress(entry), |
| 116 | icon: environmentIcon(entry), |
| 117 | search: { machine: entry.environmentId }, |
| 118 | }; |
| 119 | }) |
| 120 | : [ |
| 121 | { |
| 122 | label: gt("Open all environments"), |
| 123 | icon: <LayersIcon aria-hidden className="size-4 shrink-0 text-muted-foreground" />, |
| 124 | search: {}, |
| 125 | }, |
| 126 | ]; |
| 127 | return choices.map((choice) => ( |
| 128 | <button |
| 129 | key={JSON.stringify(choice.search)} |
| 130 | type="button" |
| 131 | className={cn( |
| 132 | ITEM_ROW_CLASSNAME, |
| 133 | "flex w-full min-w-0 cursor-pointer items-center gap-3 text-left outline-none hover:bg-accent/50 focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring", |
| 134 | )} |
| 135 | onClick={() => { |
| 136 | if (targetId) |
| 137 | void navigate({ |
| 138 | to: pathname, |
| 139 | search: () => ({ |
| 140 | project: undefined, |
| 141 | machine: undefined, |
| 142 | checkout: undefined, |
| 143 | ...choice.search, |
| 144 | }), |
| 145 | hash: targetId, |
| 146 | }); |
| 147 | else selectScope(choice.search); |
| 148 | }} |
| 149 | > |
| 150 | {choice.icon} |
| 151 | <span className="min-w-0 flex-1"> |
| 152 | <span className="block truncate text-sm font-medium">{choice.label}</span> |
| 153 | {choice.secondary ? ( |
| 154 | <Tooltip> |
| 155 | <TooltipTrigger |
| 156 | render={<span className="block truncate text-xs text-muted-foreground" />} |
| 157 | > |
| 158 | {choice.secondary} |
| 159 | </TooltipTrigger> |
| 160 | <TooltipPopup>{choice.secondary}</TooltipPopup> |
| 161 | </Tooltip> |
| 162 | ) : null} |
| 163 | </span> |
| 164 | <ChevronRightIcon aria-hidden className="size-4 shrink-0 text-muted-foreground" /> |
| 165 | </button> |
| 166 | )); |
| 167 | }; |
| 168 | const notice = ( |
| 169 | <div className="space-y-3"> |
| 170 | <p role="status" className="px-3 text-sm text-muted-foreground sm:px-4"> |
| 171 | {children} |
| 172 | </p> |
| 173 | <div className="space-y-4"> |
| 174 | {choiceGroups.map((group) => |
| 175 | group?.displayName ? ( |
| 176 | <SettingsSection key={group.projectKey} title={group.displayName}> |
| 177 | {renderChoices(group)} |
| 178 | </SettingsSection> |
| 179 | ) : ( |
| 180 | <SettingsGroup key={group?.projectKey ?? target}>{renderChoices(group)}</SettingsGroup> |
| 181 | ), |
| 182 | )} |
| 183 | </div> |
| 184 | </div> |
| 185 | ); |
| 186 | return inline ? notice : <SettingsPageContainer>{notice}</SettingsPageContainer>; |
| 187 | } |