Skip to content

Commit e8f62d6

Browse files
committed
feat(web): allow custom environment names
1 parent 5719e8a commit e8f62d6

5 files changed

Lines changed: 143 additions & 16 deletions

File tree

‎apps/web/src/components/settings/ConnectionsSettings.tsx‎

Lines changed: 99 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import * as DateTime from "effect/DateTime";
3838
import * as Option from "effect/Option";
3939

4040
import { useCopyToClipboard } from "../../hooks/useCopyToClipboard";
41+
import { useClientSettings, useUpdateClientSettings } from "../../hooks/useSettings";
4142
import { cn } from "../../lib/utils";
4243
import { formatElapsedDurationLabel, formatExpiresInLabel } from "../../timestampFormat";
4344
import { resolveDesktopPairingUrl, resolveHostedPairingUrl } from "./pairingUrls";
@@ -1340,13 +1341,15 @@ type SavedBackendListRowProps = {
13401341
environment: EnvironmentPresentation;
13411342
removingEnvironmentId: EnvironmentId | null;
13421343
onConnect: (environmentId: EnvironmentId) => void;
1344+
onRename: (environment: EnvironmentPresentation) => void;
13431345
onRemove: (environmentId: EnvironmentId) => void;
13441346
};
13451347

13461348
function SavedBackendListRow({
13471349
environment,
13481350
removingEnvironmentId,
13491351
onConnect,
1352+
onRename,
13501353
onRemove,
13511354
}: SavedBackendListRowProps) {
13521355
const environmentId = environment.environmentId;
@@ -1389,14 +1392,12 @@ function SavedBackendListRow({
13891392
[copyTraceIdToClipboard],
13901393
);
13911394
const versionMismatch = resolveServerConfigVersionMismatch(environment.serverConfig);
1392-
const sshTarget =
1393-
environment.entry.target._tag === "SshConnectionTarget" &&
1394-
Option.isSome(environment.entry.profile) &&
1395-
environment.entry.profile.value._tag === "SshConnectionProfile"
1396-
? environment.entry.profile.value.target
1397-
: null;
13981395
const metadataBits = [
1399-
sshTarget ? `SSH ${formatDesktopSshTarget(sshTarget)}` : null,
1396+
environment.displayUrl
1397+
? environment.entry.target._tag === "SshConnectionTarget"
1398+
? `SSH ${environment.displayUrl}`
1399+
: environment.displayUrl
1400+
: null,
14001401
environment.relayManaged ? "T3 Connect" : null,
14011402
].filter((value): value is string => value !== null);
14021403

@@ -1456,6 +1457,9 @@ function SavedBackendListRow({
14561457
) : null}
14571458
</div>
14581459
<div className="flex w-full shrink-0 items-center gap-2 sm:w-auto sm:justify-end">
1460+
<Button size="xs" variant="outline" onClick={() => onRename(environment)}>
1461+
Rename
1462+
</Button>
14591463
{isWslEnvironment ? (
14601464
<Tooltip>
14611465
<TooltipTrigger
@@ -1713,6 +1717,8 @@ export function ConnectionsSettings() {
17131717
const desktopBridge = window.desktopBridge;
17141718
const { environments } = useEnvironments();
17151719
const primaryEnvironment = usePrimaryEnvironment();
1720+
const environmentDisplayNames = useClientSettings((settings) => settings.environmentDisplayNames);
1721+
const updateClientSettings = useUpdateClientSettings();
17161722
const connectPairing = useAtomCommand(connectPairingAtom, { reportFailure: false });
17171723
const connectSshEnvironment = useAtomCommand(connectSshEnvironmentAtom, {
17181724
reportFailure: false,
@@ -1796,6 +1802,9 @@ export function ConnectionsSettings() {
17961802
const [isAddingSavedBackend, setIsAddingSavedBackend] = useState(false);
17971803
const [removingSavedEnvironmentId, setRemovingSavedEnvironmentId] =
17981804
useState<EnvironmentId | null>(null);
1805+
const [renameEnvironmentTarget, setRenameEnvironmentTarget] =
1806+
useState<EnvironmentPresentation | null>(null);
1807+
const [renameEnvironmentValue, setRenameEnvironmentValue] = useState("");
17991808
const [isUpdatingDesktopServerExposure, setIsUpdatingDesktopServerExposure] = useState(false);
18001809
const [isDesktopServerExposureDialogOpen, setIsDesktopServerExposureDialogOpen] = useState(false);
18011810
const [isUpdatingTailscaleServe, setIsUpdatingTailscaleServe] = useState(false);
@@ -2121,7 +2130,7 @@ export function ConnectionsSettings() {
21212130
return;
21222131
}
21232132

2124-
const result = await connectSshEnvironment({ target, label: "" });
2133+
const result = await connectSshEnvironment({ target, label: target.alias });
21252134
if (result._tag === "Failure") {
21262135
if (!isAtomCommandInterrupted(result)) {
21272136
setSavedBackendError(formatDesktopSshConnectionError(squashAtomCommandFailure(result)));
@@ -2228,6 +2237,41 @@ export function ConnectionsSettings() {
22282237
[retryEnvironment],
22292238
);
22302239

2240+
const handleStartRenameEnvironment = useCallback(
2241+
(environment: EnvironmentPresentation) => {
2242+
setRenameEnvironmentTarget(environment);
2243+
setRenameEnvironmentValue(environmentDisplayNames[environment.environmentId] ?? "");
2244+
},
2245+
[environmentDisplayNames],
2246+
);
2247+
2248+
const handleCloseRenameEnvironment = useCallback(() => {
2249+
setRenameEnvironmentTarget(null);
2250+
setRenameEnvironmentValue("");
2251+
}, []);
2252+
2253+
const handleSaveRenameEnvironment = useCallback(() => {
2254+
if (!renameEnvironmentTarget) {
2255+
return;
2256+
}
2257+
const environmentId = renameEnvironmentTarget.environmentId;
2258+
const displayName = renameEnvironmentValue.trim();
2259+
const nextDisplayNames = { ...environmentDisplayNames };
2260+
if (displayName === "") {
2261+
delete nextDisplayNames[environmentId];
2262+
} else {
2263+
nextDisplayNames[environmentId] = displayName;
2264+
}
2265+
updateClientSettings({ environmentDisplayNames: nextDisplayNames });
2266+
handleCloseRenameEnvironment();
2267+
}, [
2268+
environmentDisplayNames,
2269+
handleCloseRenameEnvironment,
2270+
renameEnvironmentTarget,
2271+
renameEnvironmentValue,
2272+
updateClientSettings,
2273+
]);
2274+
22312275
const handleRemoveSavedBackend = useCallback(
22322276
async (environmentId: EnvironmentId) => {
22332277
setRemovingSavedEnvironmentId(environmentId);
@@ -3385,6 +3429,7 @@ export function ConnectionsSettings() {
33853429
environment={environment}
33863430
removingEnvironmentId={removingSavedEnvironmentId}
33873431
onConnect={handleConnectSavedBackend}
3432+
onRename={handleStartRenameEnvironment}
33883433
onRemove={handleRemoveSavedBackend}
33893434
/>
33903435
))}
@@ -3393,6 +3438,52 @@ export function ConnectionsSettings() {
33933438
savedEnvironments={savedEnvironments}
33943439
/>
33953440
</SettingsSection>
3441+
<Dialog
3442+
open={renameEnvironmentTarget !== null}
3443+
onOpenChange={(open) => {
3444+
if (!open) {
3445+
handleCloseRenameEnvironment();
3446+
}
3447+
}}
3448+
>
3449+
<DialogPopup className="max-w-md">
3450+
<DialogHeader>
3451+
<DialogTitle>Rename environment</DialogTitle>
3452+
<DialogDescription>
3453+
Set an optional name for this client. Leave it empty to use the environment’s default
3454+
name.
3455+
</DialogDescription>
3456+
</DialogHeader>
3457+
<DialogPanel className="space-y-4">
3458+
<label className="grid gap-1.5">
3459+
<span className="text-xs font-medium text-foreground">Display name</span>
3460+
<Input
3461+
autoFocus
3462+
value={renameEnvironmentValue}
3463+
placeholder={renameEnvironmentTarget?.defaultLabel}
3464+
onChange={(event) => setRenameEnvironmentValue(event.target.value)}
3465+
onKeyDown={(event) => {
3466+
if (event.key === "Enter") {
3467+
event.preventDefault();
3468+
handleSaveRenameEnvironment();
3469+
}
3470+
}}
3471+
/>
3472+
</label>
3473+
{renameEnvironmentTarget?.displayUrl ? (
3474+
<p className="truncate text-xs text-muted-foreground">
3475+
{renameEnvironmentTarget.displayUrl}
3476+
</p>
3477+
) : null}
3478+
</DialogPanel>
3479+
<DialogFooter>
3480+
<Button variant="outline" onClick={handleCloseRenameEnvironment}>
3481+
Cancel
3482+
</Button>
3483+
<Button onClick={handleSaveRenameEnvironment}>Save</Button>
3484+
</DialogFooter>
3485+
</DialogPopup>
3486+
</Dialog>
33963487
</SettingsPageContainer>
33973488
);
33983489
}

‎apps/web/src/hooks/useSettings.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { safeErrorLogAttributes } from "@t3tools/client-runtime/errors";
2727
import { ensureLocalApi } from "~/localApi";
2828
import * as Struct from "effect/Struct";
2929
import { primaryServerSettingsAtom, serverEnvironment } from "~/state/server";
30-
import { usePrimaryEnvironment } from "~/state/environments";
30+
import { primaryEnvironmentIdAtom } from "~/state/primaryEnvironment";
3131
import { useAtomCommand } from "~/state/use-atom-command";
3232

3333
const CLIENT_SETTINGS_PERSISTENCE_ERROR_SCOPE = "[CLIENT_SETTINGS]";
@@ -276,7 +276,7 @@ export function useUpdateEnvironmentSettings(environmentId: EnvironmentId) {
276276
}
277277

278278
export function useUpdatePrimarySettings() {
279-
return useUpdateSettingsTarget(usePrimaryEnvironment()?.environmentId ?? null);
279+
return useUpdateSettingsTarget(useAtomValue(primaryEnvironmentIdAtom));
280280
}
281281

282282
export function useUpdateClientSettings() {

‎apps/web/src/state/environments.ts‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as Option from "effect/Option";
99
import { useMemo } from "react";
1010

1111
import { environmentCatalog } from "../connection/catalog";
12+
import { useClientSettings } from "../hooks/useSettings";
1213
import { environmentPresentations, useEnvironmentPresentation } from "./presentation";
1314
import { primaryEnvironmentIdAtom } from "./primaryEnvironment";
1415
import { useEnvironmentQuery } from "./query";
@@ -18,18 +19,22 @@ import { usePreparedConnection } from "./session";
1819
export interface EnvironmentPresentation extends BaseEnvironmentPresentation {
1920
readonly environmentId: EnvironmentId;
2021
readonly label: string;
22+
readonly defaultLabel: string;
2123
readonly displayUrl: string | null;
2224
readonly relayManaged: boolean;
2325
}
2426

2527
function projectEnvironmentPresentation(
2628
environmentId: EnvironmentId,
2729
presentation: BaseEnvironmentPresentation,
30+
environmentDisplayNames: Readonly<Record<string, string>>,
2831
): EnvironmentPresentation {
32+
const defaultLabel = presentation.entry.target.label;
2933
return {
3034
...presentation,
3135
environmentId,
32-
label: presentation.entry.target.label,
36+
label: environmentDisplayNames[environmentId] ?? defaultLabel,
37+
defaultLabel,
3338
displayUrl: connectionCatalogDisplayUrl(presentation.entry),
3439
relayManaged: presentation.entry.target._tag === "RelayConnectionTarget",
3540
};
@@ -39,13 +44,14 @@ export function useEnvironments() {
3944
const catalog = useAtomValue(environmentCatalog.catalogValueAtom);
4045
const networkStatus = useAtomValue(environmentCatalog.networkStatusValueAtom);
4146
const presentationById = useAtomValue(environmentPresentations.presentationsAtom);
47+
const environmentDisplayNames = useClientSettings((settings) => settings.environmentDisplayNames);
4248

4349
const environments = useMemo(
4450
() =>
4551
[...presentationById.entries()].map(([environmentId, presentation]) =>
46-
projectEnvironmentPresentation(environmentId, presentation),
52+
projectEnvironmentPresentation(environmentId, presentation, environmentDisplayNames),
4753
),
48-
[presentationById],
54+
[environmentDisplayNames, presentationById],
4955
);
5056

5157
return {
@@ -64,12 +70,13 @@ export function useEnvironment(
6470
environmentId: EnvironmentId | null,
6571
): EnvironmentPresentation | null {
6672
const { presentation } = useEnvironmentPresentation(environmentId);
73+
const environmentDisplayNames = useClientSettings((settings) => settings.environmentDisplayNames);
6774
return useMemo(
6875
() =>
6976
environmentId === null || presentation === null
7077
? null
71-
: projectEnvironmentPresentation(environmentId, presentation),
72-
[environmentId, presentation],
78+
: projectEnvironmentPresentation(environmentId, presentation, environmentDisplayNames),
79+
[environmentDisplayNames, environmentId, presentation],
7380
);
7481
}
7582

‎packages/contracts/src/settings.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, it } from "vite-plus/test";
22
import * as Schema from "effect/Schema";
33

4+
import { EnvironmentId } from "./baseSchemas.ts";
45
import { ProviderInstanceId } from "./providerInstance.ts";
56
import {
67
ClientSettingsSchema,
@@ -33,6 +34,30 @@ describe("ClientSettings word wrap", () => {
3334
});
3435
});
3536

37+
describe("ClientSettings environment display names", () => {
38+
const environmentId = EnvironmentId.make("environment-1");
39+
40+
it("defaults to no client-local overrides", () => {
41+
expect(decodeClientSettings({}).environmentDisplayNames).toEqual({});
42+
});
43+
44+
it("trims saved overrides", () => {
45+
expect(
46+
decodeClientSettings({
47+
environmentDisplayNames: { [environmentId]: " Workstation " },
48+
}).environmentDisplayNames,
49+
).toEqual({ [environmentId]: "Workstation" });
50+
});
51+
52+
it("rejects empty overrides", () => {
53+
expect(() =>
54+
decodeClientSettingsPatch({
55+
environmentDisplayNames: { [environmentId]: " " },
56+
}),
57+
).toThrow();
58+
});
59+
});
60+
3661
describe("ClientSettings glass opacity", () => {
3762
it("defaults to a readable translucent surface", () => {
3863
expect(decodeClientSettings({}).glassOpacity).toBe(80);

‎packages/contracts/src/settings.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as Effect from "effect/Effect";
22
import * as Duration from "effect/Duration";
33
import * as Schema from "effect/Schema";
44
import * as SchemaTransformation from "effect/SchemaTransformation";
5-
import { TrimmedNonEmptyString, TrimmedString } from "./baseSchemas.ts";
5+
import { EnvironmentId, TrimmedNonEmptyString, TrimmedString } from "./baseSchemas.ts";
66
import { DEFAULT_GIT_TEXT_GENERATION_MODEL, ProviderOptionSelections } from "./model.ts";
77
import { ModelSelection } from "./orchestration.ts";
88
import { ProviderInstanceConfig, ProviderInstanceId } from "./providerInstance.ts";
@@ -67,6 +67,9 @@ export const ClientSettingsSchema = Schema.Struct({
6767
Schema.withDecodingDefault(Effect.succeed([])),
6868
),
6969
diffIgnoreWhitespace: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(true))),
70+
environmentDisplayNames: Schema.Record(EnvironmentId, TrimmedNonEmptyString).pipe(
71+
Schema.withDecodingDefault(Effect.succeed({})),
72+
),
7073
glassOpacity: GlassOpacity.pipe(
7174
Schema.withDecodingDefault(Effect.succeed(DEFAULT_GLASS_OPACITY)),
7275
),
@@ -573,6 +576,7 @@ export const ClientSettingsPatch = Schema.Struct({
573576
confirmThreadArchive: Schema.optionalKey(Schema.Boolean),
574577
confirmThreadDelete: Schema.optionalKey(Schema.Boolean),
575578
diffIgnoreWhitespace: Schema.optionalKey(Schema.Boolean),
579+
environmentDisplayNames: Schema.optionalKey(Schema.Record(EnvironmentId, TrimmedNonEmptyString)),
576580
glassOpacity: Schema.optionalKey(GlassOpacity),
577581
favorites: Schema.optionalKey(
578582
Schema.Array(

0 commit comments

Comments
 (0)