+36
![github-actions[bot]](/assets/img/avatar_default.png)








Julius Marminge
maria-rcks
Bilal Bakr
shivam
Vitalii Yehorov
Jake Leventhal
Claude Opus 5
Alex Southwell
github-actions[bot]
Nicholas Wasmiller
PB
Exotic
Yash Singh
Devin
Theo Browne
Gabriel De Andrade
Dara Adedeji
scratchyone
Dominic Roy
chukfinley
Primož Ajdišek
benthecarman
NaveDanan
aaditagrawal
Aditya Garud
Nick Anisimov
MacKinley Smith
Yordis Prieto
t3-code[bot]
AKolenda
Guillermo Casanova
Otavio Salvador
Shirish Pothi
Ishaan Kothari
Bob Fowler
Anton Bezdenezhnykh
ValeraZSD
Ephraim
Ryan Ilano
Alex
maco
Cursor
Tristan Knight
PR Batch Tester
oliver
kamkm
de34391427
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Bilal Bakr <[email protected]> Co-authored-by: shivam <[email protected]> Co-authored-by: Vitalii Yehorov <[email protected]> Co-authored-by: Jake Leventhal <[email protected]> Co-authored-by: Claude Opus 5 (1M context) <[email protected]> Co-authored-by: Alex Southwell <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Nicholas Wasmiller <[email protected]> Co-authored-by: PB <[email protected]> Co-authored-by: Exotic <[email protected]> Co-authored-by: Yash Singh <[email protected]> Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Theo Browne <[email protected]> Co-authored-by: Gabriel De Andrade <[email protected]> Co-authored-by: Dara Adedeji <[email protected]> Co-authored-by: scratchyone <[email protected]> Co-authored-by: Dominic Roy <[email protected]> Co-authored-by: chukfinley <[email protected]> Co-authored-by: Primož Ajdišek <[email protected]> Co-authored-by: benthecarman <[email protected]> Co-authored-by: NaveDanan <[email protected]> Co-authored-by: aaditagrawal <[email protected]> Co-authored-by: Aditya Garud <[email protected]> Co-authored-by: Nick Anisimov <[email protected]> Co-authored-by: MacKinley Smith <[email protected]> Co-authored-by: Yordis Prieto <[email protected]> Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com> Co-authored-by: AKolenda <[email protected]> Co-authored-by: Guillermo Casanova <[email protected]> Co-authored-by: Otavio Salvador <[email protected]> Co-authored-by: Shirish Pothi <[email protected]> Co-authored-by: Ishaan Kothari <[email protected]> Co-authored-by: Bob Fowler <[email protected]> Co-authored-by: Anton Bezdenezhnykh <[email protected]> Co-authored-by: ValeraZSD <[email protected]> Co-authored-by: Ephraim <[email protected]> Co-authored-by: Ryan Ilano <[email protected]> Co-authored-by: Alex <[email protected]> Co-authored-by: maco <[email protected]> Co-authored-by: Cursor <[email protected]> Co-authored-by: Tristan Knight <[email protected]> Co-authored-by: PR Batch Tester <[email protected]> Co-authored-by: oliver <[email protected]> Co-authored-by: kamkm <[email protected]> Signed-off-by: Yordis Prieto <[email protected]>
361 lines
14 KiB
TypeScript
361 lines
14 KiB
TypeScript
import { scopeProjectRef } from "@t3tools/client-runtime/environment";
|
|
import { requestCustomSnooze } from "../components/CustomSnoozeDialog";
|
|
import {
|
|
type AtomCommandResult,
|
|
isAtomCommandInterrupted,
|
|
settlePromise,
|
|
squashAtomCommandFailure,
|
|
} from "@t3tools/client-runtime/state/runtime";
|
|
import { canSnooze, effectiveSnoozed } from "@t3tools/client-runtime/state/thread-settled";
|
|
import type { ScopedThreadRef, ThreadId } from "@t3tools/contracts";
|
|
import { useRouter } from "@tanstack/react-router";
|
|
import { useCallback, useMemo } from "react";
|
|
|
|
import { resolveSnoozePresets } from "../components/Sidebar.snooze";
|
|
import {
|
|
buildThreadActionMenuItems,
|
|
type ThreadActionMenuId,
|
|
} from "../components/threadActionMenu.logic";
|
|
import { stackedThreadToast, toastManager } from "../components/ui/toast";
|
|
import { threadEnvironment } from "../state/threads";
|
|
import { useAtomCommand } from "../state/use-atom-command";
|
|
import {
|
|
readEnvironmentSupportsAutoSettleOptOut,
|
|
readEnvironmentSupportsPinning,
|
|
readEnvironmentSupportsSettlement,
|
|
readEnvironmentSupportsSnooze,
|
|
readEnvironmentSupportsTitleRegeneration,
|
|
readThreadShell,
|
|
useProjects,
|
|
} from "../state/entities";
|
|
import { usePrimaryEnvironmentId } from "../state/environments";
|
|
import { readLocalApi } from "../localApi";
|
|
import {
|
|
deriveLogicalProjectKeyFromSettings,
|
|
derivePhysicalProjectKey,
|
|
selectProjectGroupingSettings,
|
|
} from "../logicalProject";
|
|
import { buildPhysicalToLogicalProjectKeyMap } from "../sidebarProjectGrouping";
|
|
import { threadRuntimeCanArchive } from "@t3tools/client-runtime/state/models";
|
|
import { useCopyToClipboard } from "./useCopyToClipboard";
|
|
import { useNewThreadHandler } from "./useHandleNewThread";
|
|
import { useClientSettings } from "./useSettings";
|
|
import { useThreadActions } from "./useThreadActions";
|
|
|
|
function failureToast(title: string, error: unknown) {
|
|
toastManager.add(
|
|
stackedThreadToast({
|
|
type: "error",
|
|
title,
|
|
description: error instanceof Error ? error.message : "An error occurred.",
|
|
}),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* The per-thread action menu (pin, settle, snooze, rename, copy, delete…) as
|
|
* a self-contained hook, for surfaces other than the sidebar row — today the
|
|
* chat header. Renders through the native context-menu bridge and dispatches
|
|
* through the same mutations the sidebar uses.
|
|
*
|
|
* Unlike the sidebar, settle and snooze here never navigate away: the caller
|
|
* is acting on the thread they are reading, and ChatView's parked-thread
|
|
* banner already offers the way back.
|
|
*/
|
|
export function useThreadActionMenu(input: {
|
|
readonly threadRef: ScopedThreadRef | null;
|
|
/** Fallback for "Copy path" when the thread has no worktree. */
|
|
readonly projectCwd: string | null;
|
|
readonly onStartRename: () => void;
|
|
}) {
|
|
const { threadRef, projectCwd, onStartRename } = input;
|
|
const router = useRouter();
|
|
const projects = useProjects();
|
|
const primaryEnvironmentId = usePrimaryEnvironmentId();
|
|
const projectGroupingSettings = useClientSettings(selectProjectGroupingSettings);
|
|
const logicalProjectKeyByPhysicalKey = useMemo(
|
|
() =>
|
|
buildPhysicalToLogicalProjectKeyMap({
|
|
projects,
|
|
settings: projectGroupingSettings,
|
|
primaryEnvironmentId,
|
|
}),
|
|
[primaryEnvironmentId, projectGroupingSettings, projects],
|
|
);
|
|
const {
|
|
settleThread,
|
|
unsettleThread,
|
|
snoozeThread,
|
|
unsnoozeThread,
|
|
pinThread,
|
|
confirmAndUnpinThread,
|
|
setThreadAutoSettle,
|
|
archiveThread,
|
|
deleteThread,
|
|
markThreadUnread,
|
|
} = useThreadActions();
|
|
const updateThreadMetadata = useAtomCommand(threadEnvironment.updateMetadata, {
|
|
reportFailure: false,
|
|
});
|
|
const handleNewThread = useNewThreadHandler();
|
|
const confirmThreadDelete = useClientSettings((s) => s.confirmThreadDelete);
|
|
const confirmThreadArchive = useClientSettings((s) => s.confirmThreadArchive);
|
|
const timestampFormat = useClientSettings((s) => s.timestampFormat);
|
|
const { copyToClipboard: copyPathToClipboard } = useCopyToClipboard<{ path: string }>({
|
|
onCopy: ({ path }) => {
|
|
toastManager.add({ type: "success", title: "Path copied", description: path });
|
|
},
|
|
onError: (error) => failureToast("Failed to copy path", error),
|
|
});
|
|
const { copyToClipboard: copyBranchToClipboard } = useCopyToClipboard<{ branch: string }>({
|
|
target: "branch name",
|
|
onCopy: ({ branch }) => {
|
|
toastManager.add({ type: "success", title: "Branch copied", description: branch });
|
|
},
|
|
onError: (error) => failureToast("Failed to copy branch", error),
|
|
});
|
|
const { copyToClipboard: copyThreadIdToClipboard } = useCopyToClipboard<{ threadId: ThreadId }>({
|
|
onCopy: ({ threadId }) => {
|
|
toastManager.add({ type: "success", title: "Thread ID copied", description: threadId });
|
|
},
|
|
onError: (error) => failureToast("Failed to copy thread ID", error),
|
|
});
|
|
|
|
const openMenu = useCallback(
|
|
(position: { x: number; y: number }) => {
|
|
if (threadRef === null) return;
|
|
void (async () => {
|
|
const api = readLocalApi();
|
|
if (!api) return;
|
|
// Snapshot at open time — the menu is modal, so state read now is
|
|
// what the user is looking at.
|
|
const thread = readThreadShell(threadRef);
|
|
if (!thread) return;
|
|
const now = new Date();
|
|
const supports = {
|
|
settlement: readEnvironmentSupportsSettlement(threadRef.environmentId),
|
|
autoSettleOptOut: readEnvironmentSupportsAutoSettleOptOut(threadRef.environmentId),
|
|
snooze: readEnvironmentSupportsSnooze(threadRef.environmentId),
|
|
pinning: readEnvironmentSupportsPinning(threadRef.environmentId),
|
|
titleRegeneration: readEnvironmentSupportsTitleRegeneration(threadRef.environmentId),
|
|
};
|
|
const isRegeneratingTitle = thread.titleRegeneration != null;
|
|
const snoozePresets = resolveSnoozePresets(now, timestampFormat);
|
|
const items = buildThreadActionMenuItems({
|
|
branch: thread.branch ?? null,
|
|
projectFilter: null,
|
|
isPinned: thread.pinnedAt != null,
|
|
isSettled: supports.settlement && thread.settledOverride === "settled",
|
|
autoSettleEnabled: thread.autoSettleDisabledAt == null,
|
|
isSnoozed: supports.snooze && effectiveSnoozed(thread, { now: now.toISOString() }),
|
|
canSnoozeNow: canSnooze(thread, { now: now.toISOString() }),
|
|
isRegeneratingTitle,
|
|
isRunning: !threadRuntimeCanArchive(thread.runtime),
|
|
supports,
|
|
snoozePresets,
|
|
});
|
|
const clicked = await settlePromise(() => api.contextMenu.show(items, position));
|
|
if (clicked._tag === "Failure" || clicked.value === null) return;
|
|
const action: ThreadActionMenuId = clicked.value;
|
|
if (action.startsWith("snooze:")) {
|
|
const preset =
|
|
action === "snooze:custom"
|
|
? await requestCustomSnooze()
|
|
: snoozePresets.find((candidate) => `snooze:${candidate.id}` === action);
|
|
if (!preset) return;
|
|
const result = await snoozeThread(threadRef, preset.snoozedUntil);
|
|
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
|
|
failureToast("Failed to snooze thread", squashAtomCommandFailure(result));
|
|
}
|
|
return;
|
|
}
|
|
const reportFailure = async (
|
|
title: string,
|
|
run: () => Promise<AtomCommandResult<unknown, unknown>>,
|
|
) => {
|
|
const result = await run();
|
|
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
|
|
failureToast(title, squashAtomCommandFailure(result));
|
|
}
|
|
};
|
|
switch (action) {
|
|
case "project-settings": {
|
|
const project = projects.find(
|
|
(candidate) =>
|
|
candidate.environmentId === thread.environmentId &&
|
|
candidate.id === thread.projectId,
|
|
);
|
|
if (!project) return;
|
|
const projectKey =
|
|
logicalProjectKeyByPhysicalKey.get(derivePhysicalProjectKey(project)) ??
|
|
deriveLogicalProjectKeyFromSettings(project, projectGroupingSettings);
|
|
void router.navigate({
|
|
to: "/projects/$projectKey",
|
|
params: { projectKey },
|
|
});
|
|
return;
|
|
}
|
|
case "new-thread-on-branch": {
|
|
// Explicit branch carry-over: reuse the thread's worktree when it
|
|
// has one, otherwise its branch on the local checkout.
|
|
const result = await settlePromise(() =>
|
|
handleNewThread(scopeProjectRef(threadRef.environmentId, thread.projectId), {
|
|
branch: thread.branch,
|
|
worktreePath: thread.worktreePath,
|
|
envMode: thread.worktreePath ? "worktree" : "local",
|
|
startFromOrigin: false,
|
|
}),
|
|
);
|
|
if (result._tag === "Failure") {
|
|
failureToast("Could not create thread", squashAtomCommandFailure(result));
|
|
}
|
|
return;
|
|
}
|
|
case "settle":
|
|
await reportFailure("Failed to settle thread", () => settleThread(threadRef));
|
|
return;
|
|
case "unsettle":
|
|
await reportFailure("Failed to un-settle thread", () => unsettleThread(threadRef));
|
|
return;
|
|
case "unsnooze":
|
|
await reportFailure("Failed to wake thread", () => unsnoozeThread(threadRef));
|
|
return;
|
|
case "pin":
|
|
await reportFailure("Failed to pin thread", () => pinThread(threadRef));
|
|
return;
|
|
case "unpin": {
|
|
await reportFailure("Failed to unpin thread", () => confirmAndUnpinThread(threadRef));
|
|
return;
|
|
}
|
|
case "auto-settle:enabled":
|
|
case "auto-settle:disabled":
|
|
await reportFailure("Failed to update auto-settle", () =>
|
|
setThreadAutoSettle(threadRef, action === "auto-settle:enabled"),
|
|
);
|
|
return;
|
|
case "rename":
|
|
onStartRename();
|
|
return;
|
|
case "regenerate-title":
|
|
if (isRegeneratingTitle) return;
|
|
await reportFailure("Failed to regenerate thread title", () =>
|
|
updateThreadMetadata({
|
|
environmentId: threadRef.environmentId,
|
|
input: { threadId: threadRef.threadId, regenerateTitle: true },
|
|
}),
|
|
);
|
|
return;
|
|
case "mark-unread":
|
|
markThreadUnread(threadRef);
|
|
return;
|
|
case "copy-path": {
|
|
const workspacePath = thread.worktreePath ?? projectCwd;
|
|
if (!workspacePath) {
|
|
toastManager.add(
|
|
stackedThreadToast({
|
|
type: "error",
|
|
title: "Path unavailable",
|
|
description: "This thread does not have a workspace path to copy.",
|
|
}),
|
|
);
|
|
return;
|
|
}
|
|
copyPathToClipboard(workspacePath, { path: workspacePath });
|
|
return;
|
|
}
|
|
case "copy-branch":
|
|
if (thread.branch) {
|
|
copyBranchToClipboard(thread.branch, { branch: thread.branch });
|
|
}
|
|
return;
|
|
case "copy-thread-id":
|
|
copyThreadIdToClipboard(thread.id, { threadId: thread.id });
|
|
return;
|
|
case "archive": {
|
|
if (confirmThreadArchive) {
|
|
const confirmed = await settlePromise(() =>
|
|
api.dialogs.confirm(`Archive thread "${thread.title}"?`),
|
|
);
|
|
if (confirmed._tag === "Failure" || !confirmed.value) return;
|
|
}
|
|
let didArchive = false;
|
|
const result = await archiveThread(threadRef, {
|
|
onArchived: () => {
|
|
didArchive = true;
|
|
},
|
|
});
|
|
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
|
|
failureToast(
|
|
didArchive ? "Thread archived, but navigation failed" : "Failed to archive thread",
|
|
squashAtomCommandFailure(result),
|
|
);
|
|
}
|
|
return;
|
|
}
|
|
case "delete": {
|
|
if (confirmThreadDelete) {
|
|
const confirmed = await settlePromise(() =>
|
|
api.dialogs.confirm(
|
|
[
|
|
`Delete thread "${thread.title}"?`,
|
|
"This permanently clears conversation history for this thread.",
|
|
].join("\n"),
|
|
{ variant: "destructive" },
|
|
),
|
|
);
|
|
if (confirmed._tag === "Failure" || !confirmed.value) return;
|
|
}
|
|
const deleted = await deleteThread(threadRef);
|
|
if (
|
|
deleted._tag === "Failure" &&
|
|
!isAtomCommandInterrupted(deleted) &&
|
|
// A failure with the thread already gone is worktree cleanup
|
|
// failing after a successful delete — deleteThread has toasted
|
|
// that itself, and "Failed to delete thread" would be a lie.
|
|
readThreadShell(threadRef) !== null
|
|
) {
|
|
failureToast("Failed to delete thread", squashAtomCommandFailure(deleted));
|
|
}
|
|
return;
|
|
}
|
|
default:
|
|
return;
|
|
}
|
|
})();
|
|
},
|
|
[
|
|
archiveThread,
|
|
confirmThreadArchive,
|
|
confirmThreadDelete,
|
|
confirmAndUnpinThread,
|
|
copyBranchToClipboard,
|
|
copyPathToClipboard,
|
|
copyThreadIdToClipboard,
|
|
deleteThread,
|
|
handleNewThread,
|
|
logicalProjectKeyByPhysicalKey,
|
|
markThreadUnread,
|
|
onStartRename,
|
|
pinThread,
|
|
projectCwd,
|
|
projectGroupingSettings,
|
|
projects,
|
|
router,
|
|
setThreadAutoSettle,
|
|
settleThread,
|
|
snoozeThread,
|
|
threadRef,
|
|
timestampFormat,
|
|
unsettleThread,
|
|
unsnoozeThread,
|
|
updateThreadMetadata,
|
|
],
|
|
);
|
|
|
|
const closeMenu = useCallback(() => {
|
|
void readLocalApi()?.contextMenu.close();
|
|
}, []);
|
|
|
|
return { openMenu, closeMenu };
|
|
}
|