Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions apps/web/src/components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ import { vcsEnvironment } from "../state/vcs";
import { threadEnvironment } from "../state/threads";
import { useEnvironmentQuery } from "../state/query";
import { useAtomCommand } from "../state/use-atom-command";
import { readThreadLink } from "../threadLink";
import {
buildThreadRouteParams,
resolveActiveThreadRouteRef,
Expand Down Expand Up @@ -1772,6 +1773,25 @@ export default function Sidebar() {
);
},
});
const { copyToClipboard: copyLinkToClipboard } = useCopyToClipboard<{ link: string }>({
target: "link",
onCopy: ({ link }) => {
toastManager.add({
type: "success",
title: "Link copied",
description: link,
});
},
onError: (error) => {
toastManager.add(
stackedThreadToast({
type: "error",
title: "Failed to copy link",
description: error instanceof Error ? error.message : "An error occurred.",
}),
);
},
});
const { copyToClipboard: copyThreadIdToClipboard } = useCopyToClipboard<{ threadId: ThreadId }>({
onCopy: ({ threadId }) => {
toastManager.add({
Expand Down Expand Up @@ -3065,6 +3085,7 @@ export default function Sidebar() {
const isPinned = thread.pinnedAt != null;
// Presets resolve at menu-open time (same as the popover).
const snoozePresets = resolveSnoozePresets(new Date(), timestampFormat);
const threadLink = readThreadLink(threadRef);
const clicked = await settlePromise(() =>
api.contextMenu.show(
buildThreadActionMenuItems({
Expand All @@ -3083,6 +3104,7 @@ export default function Sidebar() {
titleRegeneration: supportsTitleRegeneration,
},
snoozePresets,
canCopyLink: threadLink !== null,
}),
position,
),
Expand Down Expand Up @@ -3158,6 +3180,11 @@ export default function Sidebar() {
case "mark-unread":
markThreadUnread(threadKey, thread.latestTurn?.completedAt);
return;
case "copy-link":
if (threadLink) {
copyLinkToClipboard(threadLink, { link: threadLink });
}
return;
case "copy-path":
if (!threadWorkspacePath) {
toastManager.add(
Expand Down Expand Up @@ -3250,6 +3277,7 @@ export default function Sidebar() {
confirmThreadArchive,
confirmThreadDelete,
copyBranchToClipboard,
copyLinkToClipboard,
copyPathToClipboard,
copyThreadIdToClipboard,
deleteThread,
Expand Down
6 changes: 6 additions & 0 deletions apps/web/src/components/threadActionMenu.logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const baseState: ThreadActionMenuState = {
isRegeneratingTitle: false,
isRunning: false,
supports: { settlement: true, snooze: true, pinning: true, titleRegeneration: true },
canCopyLink: true,
snoozePresets: [
{ id: "hour", label: "In 1 hour", whenLabel: "3:00 PM", snoozedUntil: "2026-08-07T15:00:00Z" },
],
Expand Down Expand Up @@ -44,6 +45,11 @@ describe("buildThreadActionMenuItems", () => {
expect(allIds(baseState)).not.toContain("copy-branch");
});

it("offers the thread link only when this client can build one", () => {
expect(allIds(baseState)).toContain("copy-link");
expect(allIds({ ...baseState, canCopyLink: false })).not.toContain("copy-link");
});

it("flips lifecycle labels with thread state", () => {
expect(ids({ ...baseState, isPinned: true, isSettled: true, isSnoozed: true })).toEqual(
expect.arrayContaining(["unpin", "unsettle", "unsnooze"]),
Expand Down
4 changes: 4 additions & 0 deletions apps/web/src/components/threadActionMenu.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type ThreadActionMenuId =
| "regenerate-title"
| "mark-unread"
| "copy"
| "copy-link"
| "copy-path"
| "copy-branch"
| "copy-thread-id"
Expand All @@ -41,6 +42,8 @@ export interface ThreadActionMenuState {
readonly titleRegeneration: boolean;
};
readonly snoozePresets: ReadonlyArray<SnoozePreset>;
/** False when this client cannot express the thread's address as a URL. */
readonly canCopyLink: boolean;
}

/**
Expand Down Expand Up @@ -112,6 +115,7 @@ export function buildThreadActionMenuItems(
icon: "copy",
separatorBefore: true,
children: [
...(state.canCopyLink ? [{ id: "copy-link" as const, label: "Link", icon: "link" }] : []),
{ id: "copy-path", label: "Path", icon: "folder" },
...(state.branch
? [{ id: "copy-branch" as const, label: "Branch", icon: "git-branch" }]
Expand Down
4 changes: 4 additions & 0 deletions apps/web/src/contextMenuFallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ const ICON_PATHS: Record<string, ReadonlyArray<{ tag: string; attrs: Record<stri
{ tag: "line", attrs: { x1: "10", x2: "8", y1: "3", y2: "21" } },
{ tag: "line", attrs: { x1: "16", x2: "14", y1: "3", y2: "21" } },
],
link: [
{ tag: "path", attrs: { d: "M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71" } },
{ tag: "path", attrs: { d: "M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71" } },
],
"mail-open": [
{
tag: "path",
Expand Down
16 changes: 16 additions & 0 deletions apps/web/src/hooks/useThreadActionMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
readThreadShell,
} from "../state/entities";
import { readLocalApi } from "../localApi";
import { readThreadLink } from "../threadLink";
import { useUiStateStore } from "../uiStateStore";
import { useCopyToClipboard } from "./useCopyToClipboard";
import { useNewThreadHandler } from "./useHandleNewThread";
Expand Down Expand Up @@ -98,6 +99,13 @@ export function useThreadActionMenu(input: {
},
onError: (error) => failureToast("Failed to copy branch", error),
});
const { copyToClipboard: copyLinkToClipboard } = useCopyToClipboard<{ link: string }>({
target: "link",
onCopy: ({ link }) => {
toastManager.add({ type: "success", title: "Link copied", description: link });
},
onError: (error) => failureToast("Failed to copy link", error),
});
const { copyToClipboard: copyThreadIdToClipboard } = useCopyToClipboard<{ threadId: ThreadId }>({
onCopy: ({ threadId }) => {
toastManager.add({ type: "success", title: "Thread ID copied", description: threadId });
Expand All @@ -124,6 +132,7 @@ export function useThreadActionMenu(input: {
};
const isRegeneratingTitle = thread.titleRegeneration != null;
const snoozePresets = resolveSnoozePresets(now, timestampFormat);
const threadLink = readThreadLink(threadRef);
const items = buildThreadActionMenuItems({
branch: thread.branch ?? null,
isPinned: thread.pinnedAt != null,
Expand All @@ -144,6 +153,7 @@ export function useThreadActionMenu(input: {
isRunning: thread.session?.status === "running" && thread.session.activeTurnId != null,
supports,
snoozePresets,
canCopyLink: threadLink !== null,
});
const clicked = await settlePromise(() => api.contextMenu.show(items, position));
if (clicked._tag === "Failure" || clicked.value === null) return;
Expand Down Expand Up @@ -233,6 +243,11 @@ export function useThreadActionMenu(input: {
case "mark-unread":
markThreadUnread(scopedThreadKey(threadRef), thread.latestTurn?.completedAt);
return;
case "copy-link":
if (threadLink) {
copyLinkToClipboard(threadLink, { link: threadLink });
}
return;
case "copy-path": {
const workspacePath = thread.worktreePath ?? projectCwd;
if (!workspacePath) {
Expand Down Expand Up @@ -316,6 +331,7 @@ export function useThreadActionMenu(input: {
confirmThreadArchive,
confirmThreadDelete,
copyBranchToClipboard,
copyLinkToClipboard,
copyPathToClipboard,
copyThreadIdToClipboard,
deleteThread,
Expand Down
50 changes: 50 additions & 0 deletions apps/web/src/threadLink.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { EnvironmentId, ScopedThreadRef, ThreadId } from "@t3tools/contracts";
import { describe, expect, it } from "vite-plus/test";

import { resolveThreadLink } from "./threadLink";

const ref = {
environmentId: "env_local" as EnvironmentId,
threadId: "thr_123" as ThreadId,
} as ScopedThreadRef;

describe("resolveThreadLink", () => {
it("uses the browser origin the client is served from", () => {
expect(
resolveThreadLink({
clientOrigin: "https://app.t3.codes",
environmentHttpBaseUrl: "http://192.168.1.4:3773",
ref,
}),
).toBe("https://app.t3.codes/env_local/thr_123");
});

it("falls back to the environment server when the origin is not a web origin", () => {
expect(
resolveThreadLink({
clientOrigin: "t3code://app",
environmentHttpBaseUrl: "http://127.0.0.1:3773/",
ref,
}),
).toBe("http://127.0.0.1:3773/env_local/thr_123");
});

it("returns null when neither the client nor the environment has a web origin", () => {
expect(
resolveThreadLink({ clientOrigin: "t3code://app", environmentHttpBaseUrl: null, ref }),
).toBeNull();
expect(
resolveThreadLink({ clientOrigin: null, environmentHttpBaseUrl: "not a url", ref }),
).toBeNull();
});

it("drops any path the environment base URL carries", () => {
expect(
resolveThreadLink({
clientOrigin: null,
environmentHttpBaseUrl: "https://tunnel.example.com/base/",
ref,
}),
).toBe("https://tunnel.example.com/env_local/thr_123");
});
});
47 changes: 47 additions & 0 deletions apps/web/src/threadLink.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { ScopedThreadRef } from "@t3tools/contracts";

import { readPreparedConnection } from "~/state/session";
import { buildThreadRoutePath } from "./threadRoutes";

function browserOrigin(value: string | null): string | null {
if (value === null) {
return null;
}
return value.startsWith("http://") || value.startsWith("https://") ? value : null;
}

/**
* The address a browser can open to land on a thread.
*
* In a browser the answer is the address bar: the app is served from the
* origin the user is already on. The desktop app has no address bar and its
* renderer origin (`t3code://app`) means nothing outside the app, so the link
* falls back to the environment's own HTTP base URL — the T3 server that owns
* the thread and serves the web client for it. Environments reachable only
* through a target we cannot express as an HTTP origin get no link.
*/
export function resolveThreadLink(input: {
/** `window.location.origin`, or null off a browser origin. */
readonly clientOrigin: string | null;
readonly environmentHttpBaseUrl: string | null;
readonly ref: ScopedThreadRef;
}): string | null {
const base = browserOrigin(input.clientOrigin) ?? browserOrigin(input.environmentHttpBaseUrl);
if (base === null) {
return null;
}
try {
return new URL(buildThreadRoutePath(input.ref), base).toString();
} catch {
return null;
}
}

/** `resolveThreadLink` against this client's origin and live connection. */
export function readThreadLink(ref: ScopedThreadRef): string | null {
return resolveThreadLink({
clientOrigin: typeof window === "undefined" ? null : window.location.origin,
environmentHttpBaseUrl: readPreparedConnection(ref.environmentId)?.httpBaseUrl ?? null,
ref,
});
}
5 changes: 5 additions & 0 deletions apps/web/src/threadRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,8 @@ export function resolveActiveThreadRouteRef(
}
return draftThread.promotedTo;
}

/** The pathname the web client renders a thread at. */
export function buildThreadRoutePath(ref: ScopedThreadRef): string {
return `/${encodeURIComponent(ref.environmentId)}/${encodeURIComponent(ref.threadId)}`;
}
12 changes: 12 additions & 0 deletions docs/user/thread-sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ If reordering is unavailable for one environment, update the T3 Code server runn
environment. Older servers can still pin and unpin threads, but do not understand synced ordering;
their pinned threads keep the default newest-first order below the ones you have arranged.

## Linking to a thread

Open a thread's context menu and choose **Copy → Link** to put its address on the clipboard. In a
browser the link uses the address you are already on; in the desktop app, which has no address bar,
it uses the address this client reaches the environment at — a `localhost` address for a server on
this machine, or its tunnel address when you connect remotely.

The link opens the thread on any device that can reach that address and is paired with the
environment, so it is worth checking that the address is one the other device can reach before you
send it. It is not a public share link: someone without access to the environment cannot read the
thread through it.

## Environment artwork

Dev and Nightly environments can identify themselves with artwork at the top of the sidebar and in
Expand Down
Loading