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
| 1 | import { fileBasename } from "@t3tools/shared/path"; |
| 2 | import type { SidebarProjectSnapshot } from "../../sidebarProjectGrouping"; |
| 3 | import type { EnvironmentPresentation } from "../../state/environments"; |
| 4 | import type { ResolvedSettingsScope, SettingsScopeSearch } from "./settingsScope"; |
| 5 | |
| 6 | type ScopeCheckout = SidebarProjectSnapshot["memberProjects"][number]; |
| 7 | type ScopeEnvironment = Pick<EnvironmentPresentation, "environmentId" | "label" | "displayUrl">; |
| 8 | |
| 9 | export function settingsScopeEnvironmentAddress(environment: ScopeEnvironment) { |
| 10 | if (!environment.displayUrl) return environment.environmentId; |
| 11 | try { |
| 12 | return new URL(environment.displayUrl).host || environment.displayUrl; |
| 13 | } catch { |
| 14 | return environment.displayUrl; |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | export function settingsScopeEnvironmentLabels( |
| 19 | environment: ScopeEnvironment, |
| 20 | environments: readonly ScopeEnvironment[], |
| 21 | ) { |
| 22 | const duplicates = environments.filter( |
| 23 | (other) => |
| 24 | other.environmentId !== environment.environmentId && other.label === environment.label, |
| 25 | ); |
| 26 | const address = settingsScopeEnvironmentAddress(environment); |
| 27 | const secondary = duplicates.some((other) => settingsScopeEnvironmentAddress(other) === address) |
| 28 | ? environment.displayUrl || environment.environmentId |
| 29 | : address; |
| 30 | return { |
| 31 | primary: environment.label, |
| 32 | secondary: duplicates.length ? secondary : "", |
| 33 | }; |
| 34 | } |
| 35 | |
| 36 | export function settingsScopeEnvironmentLabel( |
| 37 | environment: ScopeEnvironment, |
| 38 | environments: readonly ScopeEnvironment[], |
| 39 | ) { |
| 40 | const { primary, secondary } = settingsScopeEnvironmentLabels(environment, environments); |
| 41 | return secondary ? `${primary} · ${secondary}` : primary; |
| 42 | } |
| 43 | |
| 44 | export function settingsScopeProjectMachines( |
| 45 | group: SidebarProjectSnapshot, |
| 46 | environments: readonly ScopeEnvironment[], |
| 47 | includeAddresses = false, |
| 48 | ) { |
| 49 | const machines = group.memberProjects.map((member) => { |
| 50 | const environment = environments.find((entry) => entry.environmentId === member.environmentId); |
| 51 | if (!environment) return member.environmentLabel ?? member.environmentId; |
| 52 | const { primary, secondary } = settingsScopeEnvironmentLabels(environment, environments); |
| 53 | const address = |
| 54 | secondary || (includeAddresses ? settingsScopeEnvironmentAddress(environment) : ""); |
| 55 | return includeAddresses || address ? `${primary} · ${address}` : primary; |
| 56 | }); |
| 57 | return Array.from(new Set(machines)); |
| 58 | } |
| 59 | |
| 60 | export const ALL_ENVIRONMENTS_VALUE = "all"; |
| 61 | export const ALL_PROJECTS_VALUE = "all"; |
| 62 | export const ALL_CHECKOUTS_VALUE = "all"; |
| 63 | |
| 64 | export function environmentAxisValue( |
| 65 | search: SettingsScopeSearch, |
| 66 | resolvedEnvironmentId?: string | null, |
| 67 | ): string { |
| 68 | return search.machine ?? resolvedEnvironmentId ?? ALL_ENVIRONMENTS_VALUE; |
| 69 | } |
| 70 | |
| 71 | export function projectAxisValue(search: SettingsScopeSearch): string { |
| 72 | return search.project ?? ALL_PROJECTS_VALUE; |
| 73 | } |
| 74 | |
| 75 | export function projectScopeMenuGroups( |
| 76 | groups: readonly SidebarProjectSnapshot[], |
| 77 | search: Pick<SettingsScopeSearch, "machine">, |
| 78 | ) { |
| 79 | const { machine } = search; |
| 80 | if (machine === undefined) return groups; |
| 81 | return groups.filter((group) => |
| 82 | group.memberProjects.some((member) => member.environmentId === machine), |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | export function selectEnvironmentAxis( |
| 87 | search: SettingsScopeSearch, |
| 88 | value: string, |
| 89 | ): SettingsScopeSearch { |
| 90 | const next: SettingsScopeSearch = {}; |
| 91 | if (search.project) next.project = search.project; |
| 92 | if (value !== ALL_ENVIRONMENTS_VALUE) next.machine = value; |
| 93 | return next; |
| 94 | } |
| 95 | |
| 96 | export function selectProjectAxis(search: SettingsScopeSearch, value: string): SettingsScopeSearch { |
| 97 | const next: SettingsScopeSearch = {}; |
| 98 | if (value !== ALL_PROJECTS_VALUE) next.project = value; |
| 99 | if (search.machine) next.machine = search.machine; |
| 100 | return next; |
| 101 | } |
| 102 | |
| 103 | /** Empty unless the selection spans several checkouts of one environment, which only this axis can split. */ |
| 104 | export function settingsScopeCheckoutChoices( |
| 105 | group: SidebarProjectSnapshot, |
| 106 | search: Pick<SettingsScopeSearch, "machine" | "checkout">, |
| 107 | ): readonly ScopeCheckout[] { |
| 108 | const selected = group.memberProjects.find( |
| 109 | (member) => member.physicalProjectKey === search.checkout, |
| 110 | ); |
| 111 | const environmentId = search.machine ?? selected?.environmentId; |
| 112 | const members = |
| 113 | environmentId === undefined |
| 114 | ? group.memberProjects |
| 115 | : group.memberProjects.filter((member) => member.environmentId === environmentId); |
| 116 | return members.length > 1 && |
| 117 | members.every((member) => member.environmentId === members[0]!.environmentId) |
| 118 | ? members |
| 119 | : []; |
| 120 | } |
| 121 | |
| 122 | export function selectCheckoutAxis( |
| 123 | search: SettingsScopeSearch, |
| 124 | checkout: ScopeCheckout | null, |
| 125 | ): SettingsScopeSearch { |
| 126 | const { checkout: _previous, ...rest } = search; |
| 127 | return checkout |
| 128 | ? { ...rest, machine: checkout.environmentId, checkout: checkout.physicalProjectKey } |
| 129 | : rest; |
| 130 | } |
| 131 | |
| 132 | /** Repeated folder names fall back to the full path so the closed menu still identifies the checkout. */ |
| 133 | export function settingsScopeCheckoutLabel( |
| 134 | checkout: ScopeCheckout, |
| 135 | checkouts: readonly ScopeCheckout[], |
| 136 | ) { |
| 137 | const name = fileBasename(checkout.workspaceRoot); |
| 138 | return checkouts.filter((other) => fileBasename(other.workspaceRoot) === name).length > 1 |
| 139 | ? checkout.workspaceRoot |
| 140 | : name; |
| 141 | } |
| 142 | |
| 143 | export function selectSingleEnvironmentScope( |
| 144 | search: SettingsScopeSearch, |
| 145 | scope: ResolvedSettingsScope, |
| 146 | environments: readonly { |
| 147 | readonly environmentId: EnvironmentPresentation["environmentId"]; |
| 148 | readonly connection: Pick<EnvironmentPresentation["connection"], "phase">; |
| 149 | }[], |
| 150 | primaryEnvironmentId: string | null, |
| 151 | ): SettingsScopeSearch { |
| 152 | if (search.machine || scope.kind === "unavailable") return search; |
| 153 | const selectedIds = new Set(scope.environmentIds); |
| 154 | const candidates = environments.filter((environment) => |
| 155 | selectedIds.has(environment.environmentId), |
| 156 | ); |
| 157 | const selected = |
| 158 | candidates.find((environment) => environment.environmentId === primaryEnvironmentId) ?? |
| 159 | candidates.find((environment) => environment.connection.phase === "connected") ?? |
| 160 | candidates[0]; |
| 161 | return selected ? { ...search, machine: selected.environmentId } : search; |
| 162 | } |