import { DEFAULT_SERVER_SETTINGS, PROJECT_FILE_BACKED_SETTINGS, type ProjectFileBackedSettingKey, resolveEnvironmentMachineKind, type ServerSettings, type WorktreeSubmodules, } from "@t3tools/contracts"; import { CheckIcon, LayersIcon } from "lucide-react"; import * as Equal from "effect/Equal"; import { cn } from "../../lib/utils"; import type { EnvironmentPresentation } from "../../state/environments"; import { EnvironmentMachineIcon } from "../EnvironmentMachineIcon"; import { resolveEnvModeLabel, WORKTREE_SUBMODULES_LABELS } from "../BranchToolbar.logic"; import { PULL_REQUEST_MERGE_METHOD_LABELS } from "../pullRequest/pullRequestDetail.logic"; import { Button, InlineButton } from "../ui/button"; import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover"; import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; import type { ProjectOverrideEntry, ScopedSettingsTarget } from "./scopedSettings"; import { isProjectScopedSettingKey } from "./scopedSettings"; interface InheritanceLayer { readonly key: "project" | "environment" | "t3.json" | "built-in"; readonly label: string; readonly value: string; readonly effective: boolean; readonly set: boolean; } const WRITING_STYLE_LABELS: Record = { repo_conventions: "Repository conventions", conventional_commits: "Conventional Commits", custom: "Custom instructions", }; /** Human labels for the values the chain can show; falls back to a type summary. */ function formatValue(key: keyof ServerSettings, value: unknown): string { if (value === null || value === undefined) { return key === "pullRequestMergeMethod" ? "Last selected" : key === "sidebarAutoSettleAfterDays" ? "Never" : key === "defaultModelSelection" ? "Automatic" : key === "sourceControlWriterModelSelection" ? "Text generation model" : key === "defaultThreadEnvMode" || key === "worktreeSubmodules" ? "Inherit" : "Not set"; } if (typeof value === "boolean") return value ? "On" : "Off"; if (typeof value === "number") { return key === "sidebarAutoSettleAfterDays" ? `${value} ${value === 1 ? "day" : "days"}` : String(value); } if (typeof value === "string") { if (key === "defaultThreadEnvMode" && (value === "local" || value === "worktree")) { return resolveEnvModeLabel(value); } if (key === "worktreeSubmodules" && value in WORKTREE_SUBMODULES_LABELS) { return WORKTREE_SUBMODULES_LABELS[value as WorktreeSubmodules]; } if (key === "pullRequestMergeMethod" && value in PULL_REQUEST_MERGE_METHOD_LABELS) { return PULL_REQUEST_MERGE_METHOD_LABELS[ value as keyof typeof PULL_REQUEST_MERGE_METHOD_LABELS ]; } return value === "" ? "Empty" : value; } if (Array.isArray(value)) return `${value.length} ${value.length === 1 ? "item" : "items"}`; if (typeof value === "object") { if ("model" in value && typeof value.model === "string") return value.model; if ("mode" in value && typeof value.mode === "string") { return WRITING_STYLE_LABELS[value.mode] ?? value.mode; } } return "Custom"; } /** * The layers a setting resolves through for one target, top-down: the * project override when the target is a project, the environment's value, * the checkout's t3.json for file-backed keys, and the built-in default. The * first layer that is set wins. Same order as `resolveProjectSettings`. */ export function settingInheritanceLayers( target: ScopedSettingsTarget, environmentSettings: ServerSettings, key: keyof ServerSettings, ): readonly InheritanceLayer[] { const environmentValue = environmentSettings[key]; const source = isProjectScopedSettingKey(key) ? target.sources[key] : "environment"; const environmentSet = !Equal.equals(environmentValue, DEFAULT_SERVER_SETTINGS[key]); const fileBacked = isProjectFileBackedSettingKey(key); const layers: InheritanceLayer[] = []; if (target.projectId !== null && isProjectScopedSettingKey(key)) { layers.push({ key: "project", label: "Project", value: source === "project" ? formatValue(key, target.settings[key]) : "Inherits", effective: source === "project", set: source === "project", }); } layers.push({ key: "environment", label: target.label, value: environmentSet ? formatValue(key, environmentValue) : "Inherits", effective: source === "environment" && environmentSet, set: environmentSet, }); if (fileBacked && target.projectId !== null) { layers.push({ key: "t3.json", label: "t3.json", value: source === "t3.json" ? formatValue(key, target.settings[key]) : "Inherits", effective: source === "t3.json", set: source === "t3.json", }); } // For a file-backed key the built-in is what the resolver produced with // nothing set, not the null the schema decodes to. const builtIn = fileBacked ? PROJECT_FILE_BACKED_SETTINGS[key].builtIn : DEFAULT_SERVER_SETTINGS[key]; layers.push({ key: "built-in", label: "Default", value: formatValue(key, builtIn), effective: source === "environment" && !environmentSet, set: true, }); return layers; } function isProjectFileBackedSettingKey( key: keyof ServerSettings, ): key is ProjectFileBackedSettingKey { return Object.hasOwn(PROJECT_FILE_BACKED_SETTINGS, key); } export type SettingInheritanceState = | "default" | "environment" | "inherited" | "overridden" | "mixed"; /** * A small indicator beside a row's title that opens a top-down view of where * the setting's value comes from on each selected target. It sits inline so * narrowing to a project does not add a caption line to every row. */ export interface SettingOverridingProject extends ProjectOverrideEntry { readonly label: string; /** Jumps the breadcrumb to this project so its override can be edited. */ readonly open: () => void; } export function SettingInheritance({ state, summary, targets, environments, keys, overridingProjects = [], onClearOverrides, }: { state: SettingInheritanceState; summary: string; targets: readonly ScopedSettingsTarget[]; environments: readonly Pick[]; keys: readonly (keyof ServerSettings)[]; /** At environment scope: projects whose own value hides the environment's. */ overridingProjects?: readonly SettingOverridingProject[]; onClearOverrides?: (entries: readonly ProjectOverrideEntry[]) => void; }) { const key = keys[0]; if (!key || targets.length === 0) return null; const overrideSummary = overridingProjects.length > 0 ? `${summary} ยท ${overridingProjects.length} project ${overridingProjects.length === 1 ? "override" : "overrides"}` : summary; const chains = targets.flatMap((target) => { const environment = environments.find( (candidate) => candidate.environmentId === target.environmentId, ); if (!environment?.serverConfig) return []; return [ { target, environment: { ...environment, serverConfig: environment.serverConfig }, machine: resolveEnvironmentMachineKind(environment.serverConfig), layers: settingInheritanceLayers(target, environment.serverConfig.settings, key), }, ]; }); return ( } /> } > {overrideSummary}
{chains.map(({ target, environment, machine, layers }) => (

{target.label}

    {layers.map((layer) => (
  1. {layer.key === "environment" ? "Environment" : layer.label} {layer.value} {layer.effective ? ( ) : ( )}
  2. ))}
{(() => { const overriding = overridingProjects.filter( (project) => project.environmentId === target.environmentId, ); if (overriding.length === 0) return null; const overrides = environment.serverConfig.settings.projectSettingsOverrides; return (
Overridden by {onClearOverrides ? ( onClearOverrides(overriding)}> Reset {overriding.length === 1 ? "it" : "all"} ) : null}
    {overriding.map((project) => (
  • {project.label} {isProjectScopedSettingKey(key) ? formatValue(key, overrides[project.projectId]?.[key]) : null}
  • ))}
); })()}
))}
); }