From 58974b2bf99c4ced616579bc94d9101695e189aa Mon Sep 17 00:00:00 2001 From: Donald Silveira Date: Mon, 28 Sep 2026 16:09:46 -0300 Subject: [PATCH 1/2] =?UTF-8?q?feat(web):=20manage=20provider=20profiles?= =?UTF-8?q?=20in=20Settings=20=E2=86=92=20Profiles?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Profiles were a section of the Providers page, and projects were assigned one at a time from General, so seeing or changing which projects belong to a company took several trips. - Settings → Profiles shows one card per profile: its providers, default model and projects, with Add project, Remove, Edit and New profile. Projects without a profile are listed with Assign…. Like Providers, it edits one environment and respects read-only sessions. - The environment's existing default profile is visible: a Default badge, Make default / Unset default, and "By default" on projects that only inherit it. - Creating a profile can pick its projects and make it the fallback; the fallback starts checked for a first profile, so every project lands somewhere. - Assign, remove and create writes use the smallest override (none when the environment default already gives the target) and are covered by tests. Profile resolution is shared with the client runtime. - The profile section leaves the Providers page; the switcher's "Add profile" and settings search point here. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../ProviderProfilesSettings.logic.test.ts | 62 +++ .../ProviderProfilesSettings.logic.ts | 53 ++- .../settings/ProviderProfilesSettings.tsx | 233 ++++------ .../settings/ProviderSettingsPanel.tsx | 11 +- .../settings/SettingsSidebarNav.tsx | 2 + .../src/components/settings/settingsSearch.ts | 8 +- apps/web/src/profileScope/ProfileSwitcher.tsx | 2 +- .../src/profileScope/ProfilesSettingsPage.tsx | 411 ++++++++++++++++++ apps/web/src/routeTree.gen.ts | 21 + apps/web/src/routes/settings.profiles.tsx | 7 + docs/user/project-settings.md | 38 +- 11 files changed, 667 insertions(+), 181 deletions(-) create mode 100644 apps/web/src/profileScope/ProfilesSettingsPage.tsx create mode 100644 apps/web/src/routes/settings.profiles.tsx diff --git a/apps/web/src/components/settings/ProviderProfilesSettings.logic.test.ts b/apps/web/src/components/settings/ProviderProfilesSettings.logic.test.ts index 8a60999b65ede..0affa63b315e6 100644 --- a/apps/web/src/components/settings/ProviderProfilesSettings.logic.test.ts +++ b/apps/web/src/components/settings/ProviderProfilesSettings.logic.test.ts @@ -8,7 +8,9 @@ import { applyServerSettingsPatch } from "@t3tools/shared/serverSettings"; import { describe, expect, it } from "vite-plus/test"; import { + buildCreateProviderProfilePatch, buildDeleteProviderProfilePatch, + buildProjectProfilePatch, decodeProviderProfileValue, encodeProviderProfileValue, NO_PROVIDER_PROFILE_VALUE, @@ -92,3 +94,63 @@ describe("provider profile select values", () => { expect(decodeProviderProfileValue("none")).toBeUndefined(); }); }); + +describe("buildProjectProfilePatch", () => { + const resolve = (patch: ReturnType, projectId: ProjectId) => + resolveProjectProviderProfileId(applyServerSettingsPatch(settings, patch), projectId); + + it("drops the override when the environment default already gives the target", () => { + const next = applyServerSettingsPatch( + settings, + buildProjectProfilePatch(settings, other, acme), + ); + expect(next.projectSettingsOverrides).not.toHaveProperty(other); + expect(resolveProjectProviderProfileId(next, other)).toBe(acme); + }); + + it("writes an explicit override otherwise, keeping the project's other overrides", () => { + const patch = buildProjectProfilePatch(settings, optedIn, globex); + expect(applyServerSettingsPatch(settings, patch).projectSettingsOverrides[optedIn]).toEqual({ + providerProfileId: globex, + defaultRuntimeMode: "approval-required", + }); + }); + + it("removes a project from the default profile with an explicit opt-out", () => { + expect(resolve(buildProjectProfilePatch(settings, inheriting, null), inheriting)).toBeNull(); + }); +}); + +describe("buildCreateProviderProfilePatch", () => { + const initech = ProviderProfileId.make("initech"); + const profile = { name: "Initech", instanceIds: [], defaultModelSelection: null }; + + it("creates the profile with its projects and can make it the default", () => { + const next = applyServerSettingsPatch( + settings, + buildCreateProviderProfilePatch(settings, { + id: initech, + profile, + projectIds: [other, inheriting], + makeDefault: true, + }), + ); + expect(next.providerProfiles[initech]).toEqual(profile); + expect(next.providerProfileId).toBe(initech); + expect(resolveProjectProviderProfileId(next, other)).toBe(initech); + expect(resolveProjectProviderProfileId(next, inheriting)).toBe(initech); + // Projects left out keep what they had; the opt-out stays an opt-out. + expect(resolveProjectProviderProfileId(next, optedOut)).toBeNull(); + }); + + it("leaves the environment default alone unless asked", () => { + const patch = buildCreateProviderProfilePatch(settings, { + id: initech, + profile, + projectIds: [], + makeDefault: false, + }); + expect(patch).not.toHaveProperty("providerProfileId"); + expect(patch).not.toHaveProperty("projectSettingsOverrides"); + }); +}); diff --git a/apps/web/src/components/settings/ProviderProfilesSettings.logic.ts b/apps/web/src/components/settings/ProviderProfilesSettings.logic.ts index 50678853332da..8f0a809e1244b 100644 --- a/apps/web/src/components/settings/ProviderProfilesSettings.logic.ts +++ b/apps/web/src/components/settings/ProviderProfilesSettings.logic.ts @@ -1,6 +1,7 @@ import { type ProjectId, type ProjectSettingsOverrides, + type ProviderProfile, ProviderProfileId, type ServerSettings, type ServerSettingsPatch, @@ -62,17 +63,55 @@ export function buildDeleteProviderProfilePatch( }; } -/** The profile a project uses: its own override, including an explicit "no profile", else the environment's. */ -export function resolveProjectProviderProfileId( +/** + * Makes a project use `target` (a profile, or `null` for every provider) with the smallest + * override: none when the environment default already gives it, else an explicit one. Other + * overrides on the project are kept. + */ +export function buildProjectProfilePatch( settings: Pick, projectId: ProjectId, -): ProviderProfileId | null { - const entry = settings.projectSettingsOverrides[projectId]; - return entry !== undefined && Object.hasOwn(entry, "providerProfileId") - ? (entry.providerProfileId ?? null) - : settings.providerProfileId; + target: ProviderProfileId | null, +): ServerSettingsPatch { + const next = + target === settings.providerProfileId + ? clearProjectSettingsOverrides(settings, projectId, ["providerProfileId"]) + : { ...settings.projectSettingsOverrides[projectId], providerProfileId: target }; + return { projectSettingsOverrides: { [projectId]: next } }; } +/** + * Saves a new profile in one write, with the projects picked for it and, optionally, as the + * profile projects without their own use. + */ +export function buildCreateProviderProfilePatch( + settings: Pick, + input: { + readonly id: ProviderProfileId; + readonly profile: ProviderProfile; + readonly projectIds: ReadonlyArray; + readonly makeDefault: boolean; + }, +): ServerSettingsPatch { + const next = input.makeDefault ? { ...settings, providerProfileId: input.id } : settings; + const overrides: Record = {}; + for (const projectId of input.projectIds) { + Object.assign( + overrides, + buildProjectProfilePatch(next, projectId, input.id).projectSettingsOverrides, + ); + } + return { + providerProfiles: { [input.id]: input.profile }, + ...(input.makeDefault ? { providerProfileId: input.id } : {}), + ...(Object.keys(overrides).length > 0 ? { projectSettingsOverrides: overrides } : {}), + }; +} + +// Kept importable from here for the settings screens; the shared client runtime owns it so web +// and mobile resolve a project's profile identically. +export { resolveProjectProviderProfileId } from "@t3tools/client-runtime/state/profile-scope"; + /** * Select values for the profile picker. Profile ids are user-derived slugs * (a profile named "None" gets id `none`), so they are prefixed to never diff --git a/apps/web/src/components/settings/ProviderProfilesSettings.tsx b/apps/web/src/components/settings/ProviderProfilesSettings.tsx index ad07ee0db3ea0..3976d2e850d0f 100644 --- a/apps/web/src/components/settings/ProviderProfilesSettings.tsx +++ b/apps/web/src/components/settings/ProviderProfilesSettings.tsx @@ -1,6 +1,7 @@ +import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; import { - type EnvironmentId, type ModelSelection, + type ProjectId, type ProviderInstanceId, type ProviderProfile, type ProviderProfileId, @@ -8,10 +9,9 @@ import { type UnifiedSettings, } from "@t3tools/contracts"; import { createModelSelection } from "@t3tools/shared/model"; -import { PencilIcon, PipetteIcon, PlusIcon } from "lucide-react"; +import { PipetteIcon } from "lucide-react"; import { useMemo, useState } from "react"; -import { useUpdateEnvironmentSettings } from "../../hooks/useSettings"; import { cn } from "../../lib/utils"; import { getCustomModelOptionsByInstance } from "../../modelSelection"; import { @@ -21,10 +21,9 @@ import { resolveDefaultProviderModelSelection, sortProviderInstanceEntries, } from "../../providerInstances"; -import { useProjects } from "../../state/entities"; import { ProviderInstanceIcon } from "../chat/ProviderInstanceIcon"; +import { ProjectFavicon } from "../ProjectFavicon"; import { ProviderModelPicker } from "../chat/ProviderModelPicker"; -import { ProviderProfileChip } from "../ProviderProfileChip"; import { Button, InlineButton } from "../ui/button"; import { Checkbox } from "../ui/checkbox"; import { @@ -38,19 +37,13 @@ import { } from "../ui/dialog"; import { Input } from "../ui/input"; import { Label } from "../ui/label"; -import { - buildDeleteProviderProfilePatch, - PROVIDER_PROFILE_COLORS, - providerProfileIdFromName, - resolveProjectProviderProfileId, -} from "./ProviderProfilesSettings.logic"; +import { PROVIDER_PROFILE_COLORS } from "./ProviderProfilesSettings.logic"; import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover"; import { ProviderCustomColorPanel } from "./ProviderAccentColorPicker"; -import { searchableSetting } from "./settingsSearch"; -import { SETTINGS_PICKER_TRIGGER_CLASSNAME, SettingsRow, SettingsSection } from "./settingsLayout"; +import { SETTINGS_PICKER_TRIGGER_CLASSNAME } from "./settingsLayout"; /** Provider instance entries for an environment, in settings order. */ -function useProviderInstanceEntries( +export function useProviderInstanceEntries( providers: ReadonlyArray, settings: Pick, ): ReadonlyArray { @@ -93,7 +86,7 @@ export function ProviderProfileInstances({ ); } -function describeProfileModel( +export function describeProfileModel( selection: ModelSelection | null, entries: ReadonlyArray, ): string { @@ -104,135 +97,19 @@ function describeProfileModel( return `${entry.displayName} · ${model?.name ?? selection.model}`; } -interface EditingProfile { - readonly id: ProviderProfileId | null; - readonly profile: ProviderProfile; +export interface AssignableProject { + readonly project: EnvironmentProject; + readonly currentProfileName: string | null; } -/** - * Profiles group this environment's provider instances, usually one per - * company or client. Projects pick a profile in their settings. - */ -export function ProviderProfilesSettings({ - environmentId, - settings, - providers, - readOnly, -}: { - readonly environmentId: EnvironmentId; - readonly settings: UnifiedSettings; - readonly providers: ReadonlyArray; - readonly readOnly: boolean; -}) { - const updateSettings = useUpdateEnvironmentSettings(environmentId); - const entries = useProviderInstanceEntries(providers, settings); - const allProjects = useProjects(); - const [editing, setEditing] = useState(null); - const profiles = Object.entries(settings.providerProfiles) as [ - ProviderProfileId, - ProviderProfile, - ][]; - const projectsByProfile = useMemo(() => { - const byProfile = new Map(); - for (const project of allProjects) { - if (project.environmentId !== environmentId) continue; - const id = resolveProjectProviderProfileId(settings, project.id); - if (id !== null) byProfile.set(id, [...(byProfile.get(id) ?? []), project.title]); - } - return byProfile; - }, [allProjects, environmentId, settings]); - - const startNew = () => - setEditing({ - id: null, - profile: { - name: "", - color: - PROVIDER_PROFILE_COLORS[profiles.length % PROVIDER_PROFILE_COLORS.length] ?? - PROVIDER_PROFILE_COLORS[0], - instanceIds: [], - defaultModelSelection: null, - }, - }); +export interface NewProfileExtras { + readonly projectIds: ReadonlyArray; + readonly makeDefault: boolean; +} - return ( - <> - - - New profile - - ) - } - > - {profiles.length === 0 ? ( - - ) : ( - profiles.map(([id, profile]) => { - const projects = projectsByProfile.get(id) ?? []; - return ( - - - - {projects.length === 0 - ? "No projects" - : `${projects.length} ${projects.length === 1 ? "project" : "projects"}: ${projects.join(", ")}`} - - - } - description={`Default model: ${describeProfileModel(profile.defaultModelSelection, entries)}`} - status={ - - } - control={ - readOnly ? null : ( - - ) - } - /> - ); - }) - )} - - {editing && !readOnly ? ( - setEditing(null)} - onSave={(profile) => { - const id = - editing.id ?? providerProfileIdFromName(profile.name, settings.providerProfiles); - updateSettings({ providerProfiles: { [id]: profile } }); - setEditing(null); - }} - onDelete={ - editing.id - ? () => { - if (editing.id) - updateSettings(buildDeleteProviderProfilePatch(settings, editing.id)); - setEditing(null); - } - : undefined - } - /> - ) : null} - - ); +export interface EditingProfile { + readonly id: ProviderProfileId | null; + readonly profile: ProviderProfile; } /** The last swatch in the profile color row: shows a custom color once picked, and edits it. */ @@ -277,12 +154,14 @@ function ProviderProfileCustomColor({ ); } -function ProviderProfileEditorDialog({ +export function ProviderProfileEditorDialog({ initial, entries, settings, providers, usedBy, + assignableProjects, + defaultOptionChecked = false, onClose, onSave, onDelete, @@ -292,12 +171,27 @@ function ProviderProfileEditorDialog({ readonly settings: UnifiedSettings; readonly providers: ReadonlyArray; readonly usedBy: number; + /** Creating only: projects the new profile can start with, and what each uses today. */ + readonly assignableProjects?: ReadonlyArray; + /** + * Creating with `assignableProjects` also offers to make the profile the one projects without + * their own use; this is whether that starts checked. + */ + readonly defaultOptionChecked?: boolean; readonly onClose: () => void; - readonly onSave: (profile: ProviderProfile) => void; + readonly onSave: (profile: ProviderProfile, extras: NewProfileExtras) => void; readonly onDelete: (() => void) | undefined; }) { const [draft, setDraft] = useState(initial.profile); const [confirmingDelete, setConfirmingDelete] = useState(false); + const [projectIds, setProjectIds] = useState>(() => new Set()); + const [makeDefault, setMakeDefault] = useState(defaultOptionChecked); + const creating = initial.id === null; + const save = () => + onSave( + { ...draft, name }, + { projectIds: [...projectIds], makeDefault: creating && makeDefault }, + ); const chosen = new Set(draft.instanceIds); const chosenEntries = entries.filter((entry) => chosen.has(entry.instanceId)); const selection = resolveDefaultProviderModelSelection( @@ -347,7 +241,7 @@ function ProviderProfileEditorDialog({ className="grid gap-4" onSubmit={(event) => { event.preventDefault(); - if (name && draft.instanceIds.length > 0) onSave({ ...draft, name }); + if (name && draft.instanceIds.length > 0) save(); }} >
@@ -468,6 +362,52 @@ function ProviderProfileEditorDialog({ own.
+ + {creating && assignableProjects && assignableProjects.length > 0 ? ( +
+ Projects +
+ {assignableProjects.map(({ project, currentProfileName }) => ( + + ))} +
+
+ ) : null} + + {creating && assignableProjects ? ( + + ) : null} @@ -496,10 +436,7 @@ function ProviderProfileEditorDialog({ - diff --git a/apps/web/src/components/settings/ProviderSettingsPanel.tsx b/apps/web/src/components/settings/ProviderSettingsPanel.tsx index 3431a553a20fe..396d93fe9fa5a 100644 --- a/apps/web/src/components/settings/ProviderSettingsPanel.tsx +++ b/apps/web/src/components/settings/ProviderSettingsPanel.tsx @@ -82,7 +82,6 @@ import { stackedThreadToast, toastManager } from "../ui/toast"; import { AddProviderInstanceDialog } from "./AddProviderInstanceDialog"; import { ExpandableText } from "./ExpandableText"; import { ProviderInstanceCard } from "./ProviderInstanceCard"; -import { ProviderProfilesSettings } from "./ProviderProfilesSettings"; import { UsageProviderSettings } from "./UsageProviderSettings"; import { ProviderSetupSection, readAntigravityAuthMethod } from "./ProviderSetupSection"; import { DRIVER_OPTIONS, getDriverOption } from "./providerDriverMeta"; @@ -337,8 +336,7 @@ function ProviderSettingsPanelContent(target: ProviderSettingsTarget) { if ( !target.scoped && (searchTargetId === searchableSetting("provider-health-check-interval").id || - searchTargetId === searchableSetting("usage-providers").id || - searchTargetId === searchableSetting("provider-profiles").id) && + searchTargetId === searchableSetting("usage-providers").id) && !selectedEnvironmentCanRenderSettings && searchableEnvironmentId !== undefined ) { @@ -1129,13 +1127,6 @@ export function EnvironmentProviderSettings({ - - > = { "/settings/appearance": "Appearance", "/settings/keybindings": "Keybindings", "/settings/snap-shot": "SnapShots", + "/settings/profiles": "Profiles", "/settings/providers": "Providers", "/settings/skills": "Skills", "/settings/integrations": "Integrations", @@ -624,8 +626,8 @@ export const SETTINGS_SEARCH_ITEMS = [ }, { id: "provider-profiles", - title: "Provider profiles", - to: "/settings/providers", + title: "Profiles", + to: "/settings/profiles", searchTerms: ["group providers company client account project default model new profile"], providerSettingsOnly: true, }, @@ -921,6 +923,8 @@ const SETTINGS_CATEGORY_SCOPES: Readonly - void navigate({ to: "/settings/providers" })}> + void navigate({ to: "/settings/profiles" })}> Add profile diff --git a/apps/web/src/profileScope/ProfilesSettingsPage.tsx b/apps/web/src/profileScope/ProfilesSettingsPage.tsx new file mode 100644 index 0000000000000..bc61272d2c0d9 --- /dev/null +++ b/apps/web/src/profileScope/ProfilesSettingsPage.tsx @@ -0,0 +1,411 @@ +import { useAtomValue } from "@effect/atom-react"; +import { + profileScopeForId, + resolveProjectProfileId, +} from "@t3tools/client-runtime/state/profile-scope"; +import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; +import type { + EnvironmentId, + ProjectId, + ProviderProfile, + ProviderProfileId, +} from "@t3tools/contracts"; +import { PencilIcon, PlusIcon } from "lucide-react"; +import { useMemo, useState } from "react"; + +import { usePrimarySessionState } from "../environments/primary"; +import { isElectron } from "../env"; +import { ProjectFavicon } from "../components/ProjectFavicon"; +import { + describeProfileModel, + ProviderProfileEditorDialog, + ProviderProfileInstances, + useProviderInstanceEntries, + type EditingProfile, +} from "../components/settings/ProviderProfilesSettings"; +import { + buildCreateProviderProfilePatch, + buildDeleteProviderProfilePatch, + buildProjectProfilePatch, + PROVIDER_PROFILE_COLORS, + providerProfileIdFromName, +} from "../components/settings/ProviderProfilesSettings.logic"; +import { + resolvePrimaryOperateAccess, + resolveRemoteOperateAccess, + type ProviderOperateAccess, +} from "../components/settings/ProviderSettingsPanel.logic"; +import { useSettingsScope } from "../components/settings/SettingsScopeContext"; +import { + SettingsPageContainer, + SettingsRow, + SettingsSection, +} from "../components/settings/settingsLayout"; +import { Badge } from "../components/ui/badge"; +import { Button, InlineButton } from "../components/ui/button"; +import { + Menu, + MenuGroup, + MenuGroupLabel, + MenuItem, + MenuPopup, + MenuTrigger, +} from "../components/ui/menu"; +import { useEnvironmentSettings, useUpdateEnvironmentSettings } from "../hooks/useSettings"; +import { useProjects } from "../state/entities"; +import { EMPTY_SERVER_PROVIDERS, serverEnvironment } from "../state/server"; +import { useEnvironmentSessionState } from "../state/session"; +import { ProfileAvatar } from "./ProfileSwitcher"; + +/** + * Settings → Profiles: each company or client as a card holding its providers, default model and + * projects. Profiles reference provider instances, which are per machine, so like Providers the + * page edits one environment: the selected one, or the representative of the selection. + */ +export function ProfilesSettingsPage() { + const { environment, scope } = useSettingsScope(); + return ( + + {environment ? ( + environment.entry.target._tag === "PrimaryConnectionTarget" ? ( + + ) : ( + + ) + ) : ( +

+ {scope.kind === "environment" + ? `Reconnect ${scope.label} to manage its profiles.` + : "Connect an environment to manage profiles."} +

+ )} +
+ ); +} + +// Write access follows Providers: the desktop app owns its primary server, a browser session +// checks the scopes it was granted, and other environments report theirs. +function PrimaryEnvironmentProfiles({ environmentId }: { environmentId: EnvironmentId }) { + const session = usePrimarySessionState(); + const access = resolvePrimaryOperateAccess({ + isPrimary: true, + hasDesktopBridge: isElectron, + session: session.data, + isPending: session.isPending, + hasError: session.error !== null, + }); + return ; +} + +function RemoteEnvironmentProfiles({ environmentId }: { environmentId: EnvironmentId }) { + const session = useEnvironmentSessionState(environmentId); + const access = resolveRemoteOperateAccess({ + session: session.data, + isPending: session.isPending, + hasError: session.hasError, + }); + return ; +} + +interface ProjectProfileEntry { + readonly project: EnvironmentProject; + readonly profileId: ProviderProfileId | null; + /** True when the project follows the environment default rather than choosing a profile. */ + readonly inherited: boolean; +} + +function profileOption(id: ProviderProfileId, profile: ProviderProfile) { + return { + scope: profileScopeForId(id), + label: profile.name, + profile: { id, ...profile, color: profile.color ?? null }, + }; +} + +function EnvironmentProfiles({ + environmentId, + access, +}: { + environmentId: EnvironmentId; + access: ProviderOperateAccess; +}) { + const settings = useEnvironmentSettings(environmentId); + const updateSettings = useUpdateEnvironmentSettings(environmentId); + const providers = + useAtomValue(serverEnvironment.providersValueAtom(environmentId)) ?? EMPTY_SERVER_PROVIDERS; + const entries = useProviderInstanceEntries(providers, settings); + const allProjects = useProjects(); + const [editing, setEditing] = useState(null); + const readOnly = access !== "granted"; + + const profiles = Object.entries(settings.providerProfiles) as [ + ProviderProfileId, + ProviderProfile, + ][]; + const defaultProfileId = + settings.providerProfileId !== null && settings.providerProfiles[settings.providerProfileId] + ? settings.providerProfileId + : null; + const projects = useMemo>( + () => + allProjects + .filter((project) => project.environmentId === environmentId) + .map((project) => ({ + project, + profileId: resolveProjectProfileId(settings, project.id), + inherited: !Object.hasOwn( + settings.projectSettingsOverrides[project.id] ?? {}, + "providerProfileId", + ), + })) + .toSorted((left, right) => left.project.title.localeCompare(right.project.title)), + [allProjects, environmentId, settings], + ); + const unassigned = projects.filter((entry) => entry.profileId === null); + const assign = (projectId: ProjectId, target: ProviderProfileId | null) => + updateSettings(buildProjectProfilePatch(settings, projectId, target)); + + const startNew = () => + setEditing({ + id: null, + profile: { + name: "", + color: + PROVIDER_PROFILE_COLORS[profiles.length % PROVIDER_PROFILE_COLORS.length] ?? + PROVIDER_PROFILE_COLORS[0], + instanceIds: [], + defaultModelSelection: null, + }, + }); + const newProfileButton = readOnly ? null : ( + + ); + + return ( + <> + {profiles.length === 0 ? ( + + + + ) : ( + profiles.map(([id, profile]) => { + const members = projects.filter((entry) => entry.profileId === id); + const candidates = projects.filter((entry) => entry.profileId !== id); + const isDefault = id === defaultProfileId; + return ( + } + headerAction={ + + {isDefault ? ( + + Default + + ) : null} + {readOnly ? null : isDefault ? ( + updateSettings({ providerProfileId: null })} + > + Unset default + + ) : ( + updateSettings({ providerProfileId: id })} + > + Make default + + )} + {readOnly ? null : ( + + )} + + } + > + + } + /> + + {describeProfileModel(profile.defaultModelSelection, entries)} + + } + /> + assign(projectId, id)} + /> + ) + } + > + {members.length > 0 ? ( +
    + {members.map(({ project, inherited }) => ( +
  • + + {project.title} + {inherited ? ( + By default + ) : null} + {readOnly ? null : ( + assign(project.id, null)}> + Remove + + )} +
  • + ))} +
+ ) : null} +
+
+ ); + }) + )} + + {profiles.length > 0 && newProfileButton ?
{newProfileButton}
: null} + + {profiles.length > 0 && unassigned.length > 0 ? ( + + {unassigned.map(({ project }) => ( + + + {project.title} + + } + description="Offers every provider." + control={ + readOnly ? null : ( + + }> + Assign… + + + {profiles.map(([id, profile]) => ( + assign(project.id, id)}> + + {profile.name} + + ))} + + + ) + } + /> + ))} + + ) : null} + + {editing && !readOnly ? ( + entry.profileId === editing.id).length : 0 + } + assignableProjects={projects.map(({ project, profileId }) => ({ + project, + currentProfileName: profileId + ? (settings.providerProfiles[profileId]?.name ?? null) + : null, + }))} + // A first profile is usually "the company I work for": make it the fallback so every + // project lands somewhere, not in an Unassigned bucket. + defaultOptionChecked={profiles.length === 0 && defaultProfileId === null} + onClose={() => setEditing(null)} + onSave={(profile, extras) => { + if (editing.id) { + updateSettings({ providerProfiles: { [editing.id]: profile } }); + } else { + const id = providerProfileIdFromName(profile.name, settings.providerProfiles); + updateSettings(buildCreateProviderProfilePatch(settings, { id, profile, ...extras })); + } + setEditing(null); + }} + onDelete={ + editing.id + ? () => { + if (editing.id) + updateSettings(buildDeleteProviderProfilePatch(settings, editing.id)); + setEditing(null); + } + : undefined + } + /> + ) : null} + + ); +} + +/** Projects of this environment outside the profile; picking one moves it here. */ +function AddProjectMenu({ + candidates, + profileNames, + onPick, +}: { + candidates: ReadonlyArray; + profileNames: Readonly>; + onPick: (projectId: ProjectId) => void; +}) { + if (candidates.length === 0) return null; + return ( + + }> + + Add project + + + + Move a project into this profile + {candidates.map(({ project, profileId }) => ( + onPick(project.id)}> + + {project.title} + + {profileId ? (profileNames[profileId]?.name ?? "") : "No profile"} + + + ))} + + + + ); +} diff --git a/apps/web/src/routeTree.gen.ts b/apps/web/src/routeTree.gen.ts index 07fc44f3b1d82..fbfa04ea942f0 100644 --- a/apps/web/src/routeTree.gen.ts +++ b/apps/web/src/routeTree.gen.ts @@ -22,6 +22,7 @@ import { Route as SettingsSnapShotRouteImport } from './routes/settings.snap-sho import { Route as SettingsSkillsRouteImport } from './routes/settings.skills' import { Route as SettingsProvidersRouteImport } from './routes/settings.providers' import { Route as SettingsProjectsRouteImport } from './routes/settings.projects' +import { Route as SettingsProfilesRouteImport } from './routes/settings.profiles' import { Route as SettingsOpenSourceLicensesRouteImport } from './routes/settings.open-source-licenses' import { Route as SettingsKeybindingsRouteImport } from './routes/settings.keybindings' import { Route as SettingsIntegrationsRouteImport } from './routes/settings.integrations' @@ -101,6 +102,11 @@ const SettingsProjectsRoute = SettingsProjectsRouteImport.update({ path: '/projects', getParentRoute: () => SettingsRoute, } as any) +const SettingsProfilesRoute = SettingsProfilesRouteImport.update({ + id: '/profiles', + path: '/profiles', + getParentRoute: () => SettingsRoute, +} as any) const SettingsOpenSourceLicensesRoute = SettingsOpenSourceLicensesRouteImport.update({ id: '/open-source-licenses', @@ -193,6 +199,7 @@ export interface FileRoutesByFullPath { '/settings/integrations': typeof SettingsIntegrationsRoute '/settings/keybindings': typeof SettingsKeybindingsRoute '/settings/open-source-licenses': typeof SettingsOpenSourceLicensesRoute + '/settings/profiles': typeof SettingsProfilesRoute '/settings/projects': typeof SettingsProjectsRoute '/settings/providers': typeof SettingsProvidersRoute '/settings/skills': typeof SettingsSkillsRoute @@ -220,6 +227,7 @@ export interface FileRoutesByTo { '/settings/integrations': typeof SettingsIntegrationsRoute '/settings/keybindings': typeof SettingsKeybindingsRoute '/settings/open-source-licenses': typeof SettingsOpenSourceLicensesRoute + '/settings/profiles': typeof SettingsProfilesRoute '/settings/projects': typeof SettingsProjectsRoute '/settings/providers': typeof SettingsProvidersRoute '/settings/skills': typeof SettingsSkillsRoute @@ -250,6 +258,7 @@ export interface FileRoutesById { '/settings/integrations': typeof SettingsIntegrationsRoute '/settings/keybindings': typeof SettingsKeybindingsRoute '/settings/open-source-licenses': typeof SettingsOpenSourceLicensesRoute + '/settings/profiles': typeof SettingsProfilesRoute '/settings/projects': typeof SettingsProjectsRoute '/settings/providers': typeof SettingsProvidersRoute '/settings/skills': typeof SettingsSkillsRoute @@ -281,6 +290,7 @@ export interface FileRouteTypes { | '/settings/integrations' | '/settings/keybindings' | '/settings/open-source-licenses' + | '/settings/profiles' | '/settings/projects' | '/settings/providers' | '/settings/skills' @@ -308,6 +318,7 @@ export interface FileRouteTypes { | '/settings/integrations' | '/settings/keybindings' | '/settings/open-source-licenses' + | '/settings/profiles' | '/settings/projects' | '/settings/providers' | '/settings/skills' @@ -337,6 +348,7 @@ export interface FileRouteTypes { | '/settings/integrations' | '/settings/keybindings' | '/settings/open-source-licenses' + | '/settings/profiles' | '/settings/projects' | '/settings/providers' | '/settings/skills' @@ -451,6 +463,13 @@ declare module '@tanstack/react-router' { preLoaderRoute: typeof SettingsProjectsRouteImport parentRoute: typeof SettingsRoute } + '/settings/profiles': { + id: '/settings/profiles' + path: '/profiles' + fullPath: '/settings/profiles' + preLoaderRoute: typeof SettingsProfilesRouteImport + parentRoute: typeof SettingsRoute + } '/settings/open-source-licenses': { id: '/settings/open-source-licenses' path: '/open-source-licenses' @@ -581,6 +600,7 @@ interface SettingsRouteChildren { SettingsIntegrationsRoute: typeof SettingsIntegrationsRoute SettingsKeybindingsRoute: typeof SettingsKeybindingsRoute SettingsOpenSourceLicensesRoute: typeof SettingsOpenSourceLicensesRoute + SettingsProfilesRoute: typeof SettingsProfilesRoute SettingsProjectsRoute: typeof SettingsProjectsRoute SettingsProvidersRoute: typeof SettingsProvidersRoute SettingsSkillsRoute: typeof SettingsSkillsRoute @@ -598,6 +618,7 @@ const SettingsRouteChildren: SettingsRouteChildren = { SettingsIntegrationsRoute: SettingsIntegrationsRoute, SettingsKeybindingsRoute: SettingsKeybindingsRoute, SettingsOpenSourceLicensesRoute: SettingsOpenSourceLicensesRoute, + SettingsProfilesRoute: SettingsProfilesRoute, SettingsProjectsRoute: SettingsProjectsRoute, SettingsProvidersRoute: SettingsProvidersRoute, SettingsSkillsRoute: SettingsSkillsRoute, diff --git a/apps/web/src/routes/settings.profiles.tsx b/apps/web/src/routes/settings.profiles.tsx new file mode 100644 index 0000000000000..0df24958b8c9c --- /dev/null +++ b/apps/web/src/routes/settings.profiles.tsx @@ -0,0 +1,7 @@ +import { createFileRoute } from "@tanstack/react-router"; + +import { ProfilesSettingsPage } from "../profileScope/ProfilesSettingsPage"; + +export const Route = createFileRoute("/settings/profiles")({ + component: ProfilesSettingsPage, +}); diff --git a/docs/user/project-settings.md b/docs/user/project-settings.md index e21d8b8ecd1b7..4266423cbb85b 100644 --- a/docs/user/project-settings.md +++ b/docs/user/project-settings.md @@ -64,19 +64,31 @@ applies when the project and environment are both on **Inherit**. ## Provider profiles -If you keep separate provider instances per company or client, such as a Claude and a Codex -instance for each with their own accounts and MCP servers, group them into profiles in -**Settings → Providers**. Each profile names its instances and can set a default model. - -Pick a profile with **Provider profile** in **Settings → General**, with a project selected to -assign it to that project, or with no project to set the environment default. That project's -model picker then lists only the profile's instances, and new threads start on the profile's -default model unless the project sets its own. **No profile** offers every instance. On mobile, the -same choice is on the **New threads** server settings page. - -The eye button at the bottom of the picker's provider rail reveals instances outside the profile. -Models picked from them are marked **Outside profile** because they run with that instance's -credentials and MCP servers. Existing threads keep their provider. +If you work for several companies or clients, give each a profile in **Settings → Profiles**. A +profile names the provider instances its projects may use, such as a Claude and a Codex instance +signed in to that company's accounts, and can set a default model. On web and desktop the sidebar +can then show one profile's work at a time; see [Work across companies](./thread-sidebar.md#work-across-companies). + +Create a profile with **New profile**, choose its providers, and tick the projects that belong to +it. **Use for projects without a profile** makes it the fallback: new projects and any project you +leave unassigned use it. It starts checked for your first profile. Each card lists its projects; +use **Add project** to move one in and **Remove** to take one out. **Make default** and **Unset +default** change the fallback later. Projects that use a profile only because it is the default +are marked **By default**. Until you create a profile, nothing about providers or the sidebar +changes. + +A project with a profile offers only that profile's instances in the model picker, and new threads +start on the profile's default model unless the project sets its own. **No profile** offers every +instance. Every other setting resolves as usual, from the project, then the environment. The eye +button at the bottom of the picker's provider rail reveals instances outside the profile. Models +picked from them are marked **Outside profile** because they run with that instance's credentials +and MCP servers. Existing threads keep their provider. + +Profiles belong to an environment, like its providers, so the page edits the environment chosen at +the top. Create the profile on each machine you use, with the same name when you first create it: +the sidebar then treats them as one company, even if you rename one later. You can also choose a +project's profile with **Provider profile** in **Settings → General** with the project selected. +On mobile, the same choice is on the **New threads** server settings page. ## Storage cleanup From 99ffc6c39e8b5ee76d080ec0343877cac6b4300d Mon Sep 17 00:00:00 2001 From: Donald Silveira Date: Mon, 28 Sep 2026 16:47:39 -0300 Subject: [PATCH 2/2] fix(web): start the Profiles editor fresh per environment and restore its search anchor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review and CI fixes for Settings → Profiles: - With several environments selected, the representative environment can change without a remount when one disconnects, and an open editor draft could then be saved into the next environment's profile of the same id. The per-environment part (now `EnvironmentProfiles`) is keyed by environment id. A regression test covers the fallback. - Settings search jumps to `#provider-profiles`, which the page never rendered. One stable anchor now wraps the page, with or without profiles or an environment, so search scrolls and focuses it, including repeat searches. Covered by the same test file. - Export `ProfileAvatar` and `resolveProjectProviderProfileId` here, where they gain callers outside their modules (knip). Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/profileScope/EnvironmentProfiles.tsx | 350 ++++++++++++++++ apps/web/src/profileScope/ProfileSwitcher.tsx | 2 +- .../ProfilesSettingsPage.test.tsx | 86 ++++ .../src/profileScope/ProfilesSettingsPage.tsx | 394 ++---------------- .../client-runtime/src/state/profileScope.ts | 2 +- 5 files changed, 468 insertions(+), 366 deletions(-) create mode 100644 apps/web/src/profileScope/EnvironmentProfiles.tsx create mode 100644 apps/web/src/profileScope/ProfilesSettingsPage.test.tsx diff --git a/apps/web/src/profileScope/EnvironmentProfiles.tsx b/apps/web/src/profileScope/EnvironmentProfiles.tsx new file mode 100644 index 0000000000000..47ba1dbd4923f --- /dev/null +++ b/apps/web/src/profileScope/EnvironmentProfiles.tsx @@ -0,0 +1,350 @@ +import { useAtomValue } from "@effect/atom-react"; +import { + profileScopeForId, + resolveProjectProfileId, +} from "@t3tools/client-runtime/state/profile-scope"; +import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; +import type { + EnvironmentId, + ProjectId, + ProviderProfile, + ProviderProfileId, +} from "@t3tools/contracts"; +import { PencilIcon, PlusIcon } from "lucide-react"; +import { useMemo, useState } from "react"; + +import { ProjectFavicon } from "../components/ProjectFavicon"; +import { + describeProfileModel, + ProviderProfileEditorDialog, + ProviderProfileInstances, + useProviderInstanceEntries, + type EditingProfile, +} from "../components/settings/ProviderProfilesSettings"; +import { + buildCreateProviderProfilePatch, + buildDeleteProviderProfilePatch, + buildProjectProfilePatch, + PROVIDER_PROFILE_COLORS, + providerProfileIdFromName, +} from "../components/settings/ProviderProfilesSettings.logic"; +import type { ProviderOperateAccess } from "../components/settings/ProviderSettingsPanel.logic"; +import { SettingsRow, SettingsSection } from "../components/settings/settingsLayout"; +import { Badge } from "../components/ui/badge"; +import { Button, InlineButton } from "../components/ui/button"; +import { + Menu, + MenuGroup, + MenuGroupLabel, + MenuItem, + MenuPopup, + MenuTrigger, +} from "../components/ui/menu"; +import { useEnvironmentSettings, useUpdateEnvironmentSettings } from "../hooks/useSettings"; +import { useProjects } from "../state/entities"; +import { EMPTY_SERVER_PROVIDERS, serverEnvironment } from "../state/server"; +import { ProfileAvatar } from "./ProfileSwitcher"; + +interface ProjectProfileEntry { + readonly project: EnvironmentProject; + readonly profileId: ProviderProfileId | null; + /** True when the project follows the environment default rather than choosing a profile. */ + readonly inherited: boolean; +} + +function profileOption(id: ProviderProfileId, profile: ProviderProfile) { + return { + scope: profileScopeForId(id), + label: profile.name, + profile: { id, ...profile, color: profile.color ?? null }, + }; +} + +/** One environment's profiles as cards, with the editor dialog. */ +export function EnvironmentProfiles({ + environmentId, + access, +}: { + environmentId: EnvironmentId; + access: ProviderOperateAccess; +}) { + const settings = useEnvironmentSettings(environmentId); + const updateSettings = useUpdateEnvironmentSettings(environmentId); + const providers = + useAtomValue(serverEnvironment.providersValueAtom(environmentId)) ?? EMPTY_SERVER_PROVIDERS; + const entries = useProviderInstanceEntries(providers, settings); + const allProjects = useProjects(); + const [editing, setEditing] = useState(null); + const readOnly = access !== "granted"; + + const profiles = Object.entries(settings.providerProfiles) as [ + ProviderProfileId, + ProviderProfile, + ][]; + const defaultProfileId = + settings.providerProfileId !== null && settings.providerProfiles[settings.providerProfileId] + ? settings.providerProfileId + : null; + const projects = useMemo>( + () => + allProjects + .filter((project) => project.environmentId === environmentId) + .map((project) => ({ + project, + profileId: resolveProjectProfileId(settings, project.id), + inherited: !Object.hasOwn( + settings.projectSettingsOverrides[project.id] ?? {}, + "providerProfileId", + ), + })) + .toSorted((left, right) => left.project.title.localeCompare(right.project.title)), + [allProjects, environmentId, settings], + ); + const unassigned = projects.filter((entry) => entry.profileId === null); + const assign = (projectId: ProjectId, target: ProviderProfileId | null) => + updateSettings(buildProjectProfilePatch(settings, projectId, target)); + + const startNew = () => + setEditing({ + id: null, + profile: { + name: "", + color: + PROVIDER_PROFILE_COLORS[profiles.length % PROVIDER_PROFILE_COLORS.length] ?? + PROVIDER_PROFILE_COLORS[0], + instanceIds: [], + defaultModelSelection: null, + }, + }); + const newProfileButton = readOnly ? null : ( + + ); + + return ( + <> + {profiles.length === 0 ? ( + + + + ) : ( + profiles.map(([id, profile]) => { + const members = projects.filter((entry) => entry.profileId === id); + const candidates = projects.filter((entry) => entry.profileId !== id); + const isDefault = id === defaultProfileId; + return ( + } + headerAction={ + + {isDefault ? ( + + Default + + ) : null} + {readOnly ? null : isDefault ? ( + updateSettings({ providerProfileId: null })} + > + Unset default + + ) : ( + updateSettings({ providerProfileId: id })} + > + Make default + + )} + {readOnly ? null : ( + + )} + + } + > + + } + /> + + {describeProfileModel(profile.defaultModelSelection, entries)} + + } + /> + assign(projectId, id)} + /> + ) + } + > + {members.length > 0 ? ( +
    + {members.map(({ project, inherited }) => ( +
  • + + {project.title} + {inherited ? ( + By default + ) : null} + {readOnly ? null : ( + assign(project.id, null)}> + Remove + + )} +
  • + ))} +
+ ) : null} +
+
+ ); + }) + )} + + {profiles.length > 0 && newProfileButton ?
{newProfileButton}
: null} + + {profiles.length > 0 && unassigned.length > 0 ? ( + + {unassigned.map(({ project }) => ( + + + {project.title} + + } + description="Offers every provider." + control={ + readOnly ? null : ( + + }> + Assign… + + + {profiles.map(([id, profile]) => ( + assign(project.id, id)}> + + {profile.name} + + ))} + + + ) + } + /> + ))} + + ) : null} + + {editing && !readOnly ? ( + entry.profileId === editing.id).length : 0 + } + assignableProjects={projects.map(({ project, profileId }) => ({ + project, + currentProfileName: profileId + ? (settings.providerProfiles[profileId]?.name ?? null) + : null, + }))} + // A first profile is usually "the company I work for": make it the fallback so every + // project lands somewhere, not in an Unassigned bucket. + defaultOptionChecked={profiles.length === 0 && defaultProfileId === null} + onClose={() => setEditing(null)} + onSave={(profile, extras) => { + if (editing.id) { + updateSettings({ providerProfiles: { [editing.id]: profile } }); + } else { + const id = providerProfileIdFromName(profile.name, settings.providerProfiles); + updateSettings(buildCreateProviderProfilePatch(settings, { id, profile, ...extras })); + } + setEditing(null); + }} + onDelete={ + editing.id + ? () => { + if (editing.id) + updateSettings(buildDeleteProviderProfilePatch(settings, editing.id)); + setEditing(null); + } + : undefined + } + /> + ) : null} + + ); +} + +/** Projects of this environment outside the profile; picking one moves it here. */ +function AddProjectMenu({ + candidates, + profileNames, + onPick, +}: { + candidates: ReadonlyArray; + profileNames: Readonly>; + onPick: (projectId: ProjectId) => void; +}) { + if (candidates.length === 0) return null; + return ( + + }> + + Add project + + + + Move a project into this profile + {candidates.map(({ project, profileId }) => ( + onPick(project.id)}> + + {project.title} + + {profileId ? (profileNames[profileId]?.name ?? "") : "No profile"} + + + ))} + + + + ); +} diff --git a/apps/web/src/profileScope/ProfileSwitcher.tsx b/apps/web/src/profileScope/ProfileSwitcher.tsx index 562815b5056b8..0bc8dd09b412b 100644 --- a/apps/web/src/profileScope/ProfileSwitcher.tsx +++ b/apps/web/src/profileScope/ProfileSwitcher.tsx @@ -42,7 +42,7 @@ interface ScopeOption { * beside it. The small square overhangs its 16px icon slot by 2px per side, so its center lines up * with the icons of neighboring rows. */ -function ProfileAvatar({ +export function ProfileAvatar({ option, size, }: { diff --git a/apps/web/src/profileScope/ProfilesSettingsPage.test.tsx b/apps/web/src/profileScope/ProfilesSettingsPage.test.tsx new file mode 100644 index 0000000000000..c9b6b4ee09609 --- /dev/null +++ b/apps/web/src/profileScope/ProfilesSettingsPage.test.tsx @@ -0,0 +1,86 @@ +import { act, createElement } from "react"; +import { create, type ReactTestRenderer } from "react-test-renderer"; +import { describe, expect, it, vi } from "vite-plus/test"; + +import { searchableSetting } from "../components/settings/settingsSearch"; +import { ProfilesSettingsPage } from "./ProfilesSettingsPage"; + +const harness = vi.hoisted(() => ({ + scope: {} as Record, + lifecycle: [] as string[], +})); + +vi.mock("../components/settings/SettingsScopeContext", () => ({ + useSettingsScope: () => harness.scope, +})); +vi.mock("../components/settings/settingsLayout", async () => { + const { createElement: h } = await import("react"); + type Children = { readonly children?: import("react").ReactNode }; + return { + SettingsPageContainer: ({ children }: Children) => h("main", null, children), + SettingsSearchTarget: ({ children, ...props }: Children & Record) => + h("div", props, children), + }; +}); +vi.mock("../environments/primary", () => ({ + usePrimarySessionState: () => ({ data: null, isPending: false, error: null }), +})); +vi.mock("../state/session", () => ({ + useEnvironmentSessionState: () => ({ data: null, isPending: false, hasError: false }), +})); +// Stands in for the cards and editor: holds an in-progress draft for the environment it mounted +// with, like the editor dialog's state. +vi.mock("./EnvironmentProfiles", async () => { + const { createElement: h, useEffect, useState } = await import("react"); + return { + EnvironmentProfiles: ({ environmentId }: { environmentId: string }) => { + const [draft] = useState(() => `draft for ${environmentId}`); + useEffect(() => { + harness.lifecycle.push(`mount ${environmentId}`); + return () => { + harness.lifecycle.push(`unmount ${environmentId}`); + }; + }, [environmentId]); + return h("section", { "data-draft": draft }); + }, + }; +}); + +function remote(environmentId: string) { + return { environmentId, entry: { target: { _tag: "RemoteConnectionTarget" } } }; +} + +describe("ProfilesSettingsPage", () => { + const anchorId = searchableSetting("provider-profiles").id; + const anchors = (renderer: ReactTestRenderer) => + renderer.root.findAll((node) => node.type === "div" && node.props.id === anchorId); + + it("starts fresh when the representative environment changes, so drafts never cross over", () => { + harness.lifecycle.length = 0; + // "All environments": A represents the selection until it disconnects and B takes over. + harness.scope = { environment: remote("env-a"), scope: { kind: "all" } }; + let renderer!: ReactTestRenderer; + act(() => { + renderer = create(createElement(ProfilesSettingsPage)); + }); + harness.scope = { environment: remote("env-b"), scope: { kind: "all" } }; + act(() => renderer.update(createElement(ProfilesSettingsPage))); + + expect(harness.lifecycle).toEqual(["mount env-a", "unmount env-a", "mount env-b"]); + expect(renderer.root.findByType("section").props["data-draft"]).toBe("draft for env-b"); + act(() => renderer.unmount()); + }); + + it("keeps the search anchor with and without an environment", () => { + harness.scope = { environment: null, scope: { kind: "all" } }; + let renderer!: ReactTestRenderer; + act(() => { + renderer = create(createElement(ProfilesSettingsPage)); + }); + expect(anchors(renderer)).toHaveLength(1); + harness.scope = { environment: remote("env-a"), scope: { kind: "all" } }; + act(() => renderer.update(createElement(ProfilesSettingsPage))); + expect(anchors(renderer)).toHaveLength(1); + act(() => renderer.unmount()); + }); +}); diff --git a/apps/web/src/profileScope/ProfilesSettingsPage.tsx b/apps/web/src/profileScope/ProfilesSettingsPage.tsx index bc61272d2c0d9..a48177526247b 100644 --- a/apps/web/src/profileScope/ProfilesSettingsPage.tsx +++ b/apps/web/src/profileScope/ProfilesSettingsPage.tsx @@ -1,61 +1,16 @@ -import { useAtomValue } from "@effect/atom-react"; -import { - profileScopeForId, - resolveProjectProfileId, -} from "@t3tools/client-runtime/state/profile-scope"; -import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; -import type { - EnvironmentId, - ProjectId, - ProviderProfile, - ProviderProfileId, -} from "@t3tools/contracts"; -import { PencilIcon, PlusIcon } from "lucide-react"; -import { useMemo, useState } from "react"; +import type { EnvironmentId } from "@t3tools/contracts"; import { usePrimarySessionState } from "../environments/primary"; import { isElectron } from "../env"; -import { ProjectFavicon } from "../components/ProjectFavicon"; -import { - describeProfileModel, - ProviderProfileEditorDialog, - ProviderProfileInstances, - useProviderInstanceEntries, - type EditingProfile, -} from "../components/settings/ProviderProfilesSettings"; -import { - buildCreateProviderProfilePatch, - buildDeleteProviderProfilePatch, - buildProjectProfilePatch, - PROVIDER_PROFILE_COLORS, - providerProfileIdFromName, -} from "../components/settings/ProviderProfilesSettings.logic"; import { resolvePrimaryOperateAccess, resolveRemoteOperateAccess, - type ProviderOperateAccess, } from "../components/settings/ProviderSettingsPanel.logic"; import { useSettingsScope } from "../components/settings/SettingsScopeContext"; -import { - SettingsPageContainer, - SettingsRow, - SettingsSection, -} from "../components/settings/settingsLayout"; -import { Badge } from "../components/ui/badge"; -import { Button, InlineButton } from "../components/ui/button"; -import { - Menu, - MenuGroup, - MenuGroupLabel, - MenuItem, - MenuPopup, - MenuTrigger, -} from "../components/ui/menu"; -import { useEnvironmentSettings, useUpdateEnvironmentSettings } from "../hooks/useSettings"; -import { useProjects } from "../state/entities"; -import { EMPTY_SERVER_PROVIDERS, serverEnvironment } from "../state/server"; +import { SettingsPageContainer, SettingsSearchTarget } from "../components/settings/settingsLayout"; +import { searchableSetting } from "../components/settings/settingsSearch"; import { useEnvironmentSessionState } from "../state/session"; -import { ProfileAvatar } from "./ProfileSwitcher"; +import { EnvironmentProfiles } from "./EnvironmentProfiles"; /** * Settings → Profiles: each company or client as a card holding its providers, default model and @@ -66,19 +21,33 @@ export function ProfilesSettingsPage() { const { environment, scope } = useSettingsScope(); return ( - {environment ? ( - environment.entry.target._tag === "PrimaryConnectionTarget" ? ( - + {/* One stable anchor for settings search, whether or not profiles or an environment exist. */} + + {environment ? ( + // Keyed: with several environments selected, the representative can change without a + // remount (one disconnects), and an open draft must not be saved into the next one. + environment.entry.target._tag === "PrimaryConnectionTarget" ? ( + + ) : ( + + ) ) : ( - - ) - ) : ( -

- {scope.kind === "environment" - ? `Reconnect ${scope.label} to manage its profiles.` - : "Connect an environment to manage profiles."} -

- )} +

+ {scope.kind === "environment" + ? `Reconnect ${scope.label} to manage its profiles.` + : "Connect an environment to manage profiles."} +

+ )} +
); } @@ -106,306 +75,3 @@ function RemoteEnvironmentProfiles({ environmentId }: { environmentId: Environme }); return ; } - -interface ProjectProfileEntry { - readonly project: EnvironmentProject; - readonly profileId: ProviderProfileId | null; - /** True when the project follows the environment default rather than choosing a profile. */ - readonly inherited: boolean; -} - -function profileOption(id: ProviderProfileId, profile: ProviderProfile) { - return { - scope: profileScopeForId(id), - label: profile.name, - profile: { id, ...profile, color: profile.color ?? null }, - }; -} - -function EnvironmentProfiles({ - environmentId, - access, -}: { - environmentId: EnvironmentId; - access: ProviderOperateAccess; -}) { - const settings = useEnvironmentSettings(environmentId); - const updateSettings = useUpdateEnvironmentSettings(environmentId); - const providers = - useAtomValue(serverEnvironment.providersValueAtom(environmentId)) ?? EMPTY_SERVER_PROVIDERS; - const entries = useProviderInstanceEntries(providers, settings); - const allProjects = useProjects(); - const [editing, setEditing] = useState(null); - const readOnly = access !== "granted"; - - const profiles = Object.entries(settings.providerProfiles) as [ - ProviderProfileId, - ProviderProfile, - ][]; - const defaultProfileId = - settings.providerProfileId !== null && settings.providerProfiles[settings.providerProfileId] - ? settings.providerProfileId - : null; - const projects = useMemo>( - () => - allProjects - .filter((project) => project.environmentId === environmentId) - .map((project) => ({ - project, - profileId: resolveProjectProfileId(settings, project.id), - inherited: !Object.hasOwn( - settings.projectSettingsOverrides[project.id] ?? {}, - "providerProfileId", - ), - })) - .toSorted((left, right) => left.project.title.localeCompare(right.project.title)), - [allProjects, environmentId, settings], - ); - const unassigned = projects.filter((entry) => entry.profileId === null); - const assign = (projectId: ProjectId, target: ProviderProfileId | null) => - updateSettings(buildProjectProfilePatch(settings, projectId, target)); - - const startNew = () => - setEditing({ - id: null, - profile: { - name: "", - color: - PROVIDER_PROFILE_COLORS[profiles.length % PROVIDER_PROFILE_COLORS.length] ?? - PROVIDER_PROFILE_COLORS[0], - instanceIds: [], - defaultModelSelection: null, - }, - }); - const newProfileButton = readOnly ? null : ( - - ); - - return ( - <> - {profiles.length === 0 ? ( - - - - ) : ( - profiles.map(([id, profile]) => { - const members = projects.filter((entry) => entry.profileId === id); - const candidates = projects.filter((entry) => entry.profileId !== id); - const isDefault = id === defaultProfileId; - return ( - } - headerAction={ - - {isDefault ? ( - - Default - - ) : null} - {readOnly ? null : isDefault ? ( - updateSettings({ providerProfileId: null })} - > - Unset default - - ) : ( - updateSettings({ providerProfileId: id })} - > - Make default - - )} - {readOnly ? null : ( - - )} - - } - > - - } - /> - - {describeProfileModel(profile.defaultModelSelection, entries)} - - } - /> - assign(projectId, id)} - /> - ) - } - > - {members.length > 0 ? ( -
    - {members.map(({ project, inherited }) => ( -
  • - - {project.title} - {inherited ? ( - By default - ) : null} - {readOnly ? null : ( - assign(project.id, null)}> - Remove - - )} -
  • - ))} -
- ) : null} -
-
- ); - }) - )} - - {profiles.length > 0 && newProfileButton ?
{newProfileButton}
: null} - - {profiles.length > 0 && unassigned.length > 0 ? ( - - {unassigned.map(({ project }) => ( - - - {project.title} - - } - description="Offers every provider." - control={ - readOnly ? null : ( - - }> - Assign… - - - {profiles.map(([id, profile]) => ( - assign(project.id, id)}> - - {profile.name} - - ))} - - - ) - } - /> - ))} - - ) : null} - - {editing && !readOnly ? ( - entry.profileId === editing.id).length : 0 - } - assignableProjects={projects.map(({ project, profileId }) => ({ - project, - currentProfileName: profileId - ? (settings.providerProfiles[profileId]?.name ?? null) - : null, - }))} - // A first profile is usually "the company I work for": make it the fallback so every - // project lands somewhere, not in an Unassigned bucket. - defaultOptionChecked={profiles.length === 0 && defaultProfileId === null} - onClose={() => setEditing(null)} - onSave={(profile, extras) => { - if (editing.id) { - updateSettings({ providerProfiles: { [editing.id]: profile } }); - } else { - const id = providerProfileIdFromName(profile.name, settings.providerProfiles); - updateSettings(buildCreateProviderProfilePatch(settings, { id, profile, ...extras })); - } - setEditing(null); - }} - onDelete={ - editing.id - ? () => { - if (editing.id) - updateSettings(buildDeleteProviderProfilePatch(settings, editing.id)); - setEditing(null); - } - : undefined - } - /> - ) : null} - - ); -} - -/** Projects of this environment outside the profile; picking one moves it here. */ -function AddProjectMenu({ - candidates, - profileNames, - onPick, -}: { - candidates: ReadonlyArray; - profileNames: Readonly>; - onPick: (projectId: ProjectId) => void; -}) { - if (candidates.length === 0) return null; - return ( - - }> - - Add project - - - - Move a project into this profile - {candidates.map(({ project, profileId }) => ( - onPick(project.id)}> - - {project.title} - - {profileId ? (profileNames[profileId]?.name ?? "") : "No profile"} - - - ))} - - - - ); -} diff --git a/packages/client-runtime/src/state/profileScope.ts b/packages/client-runtime/src/state/profileScope.ts index 7fb4ab363162e..e8319713460e5 100644 --- a/packages/client-runtime/src/state/profileScope.ts +++ b/packages/client-runtime/src/state/profileScope.ts @@ -45,7 +45,7 @@ export function profileScopeProjectKey(environmentId: EnvironmentId, projectId: * The profile a project uses on its own environment: its override, including an explicit * "no profile", else the environment default. */ -function resolveProjectProviderProfileId( +export function resolveProjectProviderProfileId( settings: Pick, projectId: ProjectId, ): ProviderProfileId | null {