-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Add user-defined thread folders to the sidebar #3071
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TheIcarusWings
wants to merge
5
commits into
pingdotgg:main
Choose a base branch
from
TheIcarusWings:t3code/sidebar-thread-folders
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
95bbc98
feat(sidebar): add user-defined thread folders
TheIcarusWings b7e73e1
fix(sidebar): indent threads inside folders with a guide line
TheIcarusWings 708f2a5
fix(sidebar): address review findings for thread folders
TheIcarusWings b954989
fix(sidebar): preserve in-folder order for multi-thread drag
TheIcarusWings c365e18
perf(sidebar): stop thread rows re-rendering during drag; clearer dra…
TheIcarusWings File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import { useSortable } from "@dnd-kit/sortable"; | ||
| import { CSS } from "@dnd-kit/utilities"; | ||
| import { ChevronRightIcon, FolderIcon } from "lucide-react"; | ||
| import { memo, useCallback, useMemo, useRef } from "react"; | ||
|
|
||
| import { SidebarMenuSubButton, SidebarMenuSubItem } from "./ui/sidebar"; | ||
| import type { ThreadGroup } from "../uiStateStore"; | ||
|
|
||
| /** dnd id namespace for a folder header (distinct from thread-row ids = threadKeys). */ | ||
| export function groupHeaderDndId(groupId: string): string { | ||
| return `group-header:${groupId}`; | ||
| } | ||
|
|
||
| interface SidebarThreadGroupRowProps { | ||
| group: ThreadGroup; | ||
| threadCount: number; | ||
| expanded: boolean; | ||
| isRenaming: boolean; | ||
| renamingTitle: string; | ||
| setRenamingTitle: (title: string) => void; | ||
| onToggle: (groupId: string) => void; | ||
| onContextMenu: (groupId: string, position: { x: number; y: number }) => void; | ||
| commitRename: (groupId: string) => void; | ||
| cancelRename: () => void; | ||
| } | ||
|
|
||
| const SidebarThreadGroupRow = memo(function SidebarThreadGroupRow( | ||
| props: SidebarThreadGroupRowProps, | ||
| ) { | ||
| const { | ||
| group, | ||
| threadCount, | ||
| expanded, | ||
| isRenaming, | ||
| renamingTitle, | ||
| setRenamingTitle, | ||
| onToggle, | ||
| onContextMenu, | ||
| commitRename, | ||
| cancelRename, | ||
| } = props; | ||
|
|
||
| const { attributes, listeners, setNodeRef, transform, transition, isDragging, isOver } = | ||
| useSortable({ id: groupHeaderDndId(group.id), disabled: isRenaming }); | ||
|
|
||
| const headerButtonRender = useMemo(() => <div role="button" tabIndex={0} />, []); | ||
| // Drag listeners are suppressed while renaming so typing in the input never | ||
| // initiates a folder drag. | ||
| const dragHandleProps = isRenaming ? {} : { ...attributes, ...listeners }; | ||
|
|
||
| const handleClick = useCallback(() => { | ||
| onToggle(group.id); | ||
| }, [group.id, onToggle]); | ||
|
|
||
| const handleKeyDown = useCallback( | ||
| (event: React.KeyboardEvent) => { | ||
| if (event.key !== "Enter" && event.key !== " ") return; | ||
| event.preventDefault(); | ||
| onToggle(group.id); | ||
| }, | ||
| [group.id, onToggle], | ||
| ); | ||
|
|
||
| const handleContextMenu = useCallback( | ||
| (event: React.MouseEvent) => { | ||
| event.preventDefault(); | ||
| onContextMenu(group.id, { x: event.clientX, y: event.clientY }); | ||
| }, | ||
| [group.id, onContextMenu], | ||
| ); | ||
|
|
||
| // Guards the input's onBlur from re-committing after Enter/Escape already | ||
| // resolved the rename (otherwise Escape cancels then blur silently commits). | ||
| const renameResolvedRef = useRef(false); | ||
|
|
||
| const handleRenameRef = useCallback((element: HTMLInputElement | null) => { | ||
| if (element) { | ||
| renameResolvedRef.current = false; | ||
| element.focus(); | ||
| element.select(); | ||
| } | ||
| }, []); | ||
|
|
||
| const handleRenameKeyDown = useCallback( | ||
| (event: React.KeyboardEvent<HTMLInputElement>) => { | ||
| event.stopPropagation(); | ||
| if (event.key === "Enter") { | ||
| event.preventDefault(); | ||
| renameResolvedRef.current = true; | ||
| commitRename(group.id); | ||
| } else if (event.key === "Escape") { | ||
| event.preventDefault(); | ||
| renameResolvedRef.current = true; | ||
| cancelRename(); | ||
| } | ||
| }, | ||
| [cancelRename, commitRename, group.id], | ||
| ); | ||
|
|
||
| const handleRenameBlur = useCallback(() => { | ||
| if (!renameResolvedRef.current) { | ||
| commitRename(group.id); | ||
| } | ||
| }, [commitRename, group.id]); | ||
|
|
||
| return ( | ||
| <SidebarMenuSubItem | ||
| ref={setNodeRef} | ||
| style={{ transform: CSS.Translate.toString(transform), transition }} | ||
| className={`w-full ${isDragging ? "z-20 opacity-80" : ""}`} | ||
| data-thread-group-item | ||
| data-thread-selection-safe | ||
| > | ||
| <SidebarMenuSubButton | ||
| render={headerButtonRender} | ||
| size="sm" | ||
| data-thread-selection-safe | ||
| data-testid={`thread-group-${group.id}`} | ||
| className={`h-6 w-full translate-x-0 cursor-pointer justify-start gap-1.5 px-1.5 text-left text-[11px] font-medium text-muted-foreground/80 hover:bg-accent hover:text-foreground ${ | ||
| isOver ? "bg-primary/20 text-foreground ring-2 ring-inset ring-primary" : "" | ||
| }`} | ||
| onClick={handleClick} | ||
| onKeyDown={handleKeyDown} | ||
| onContextMenu={handleContextMenu} | ||
| {...dragHandleProps} | ||
| > | ||
| <ChevronRightIcon | ||
| className={`-ml-0.5 size-3 shrink-0 text-muted-foreground/60 transition-transform duration-150 ${ | ||
| expanded ? "rotate-90" : "" | ||
| }`} | ||
| /> | ||
| <FolderIcon className="size-3 shrink-0 text-muted-foreground/50" /> | ||
| {isRenaming ? ( | ||
| <input | ||
| ref={handleRenameRef} | ||
| className="min-w-0 flex-1 truncate rounded border border-ring bg-transparent px-0.5 text-[11px] outline-none" | ||
| value={renamingTitle} | ||
| onChange={(event) => setRenamingTitle(event.target.value)} | ||
| onKeyDown={handleRenameKeyDown} | ||
| onBlur={handleRenameBlur} | ||
| onClick={(event) => event.stopPropagation()} | ||
| /> | ||
| ) : ( | ||
| <span className="min-w-0 flex-1 truncate">{group.name}</span> | ||
| )} | ||
| <span className="ml-auto shrink-0 tabular-nums text-[10px] text-muted-foreground/40"> | ||
| {threadCount} | ||
| </span> | ||
| </SidebarMenuSubButton> | ||
| </SidebarMenuSubItem> | ||
| ); | ||
| }); | ||
|
|
||
| export default SidebarThreadGroupRow; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| import { EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts"; | ||
| import { describe, expect, it } from "vite-plus/test"; | ||
|
|
||
| import { buildGroupedThreadLayout, threadKeyOf } from "./sidebarThreadGrouping"; | ||
| import type { ThreadGroup } from "./uiStateStore"; | ||
| import type { SidebarThreadSummary } from "./types"; | ||
|
|
||
| const ENV = EnvironmentId.make("env-1"); | ||
| const PROJ = ProjectId.make("proj-1"); | ||
|
|
||
| function makeThread(id: string): SidebarThreadSummary { | ||
| return { | ||
| id: ThreadId.make(id), | ||
| environmentId: ENV, | ||
| projectId: PROJ, | ||
| title: id, | ||
| interactionMode: "default", | ||
| session: null, | ||
| createdAt: "2026-06-13T00:00:00.000Z", | ||
| archivedAt: null, | ||
| latestTurn: null, | ||
| branch: null, | ||
| worktreePath: null, | ||
| latestUserMessageAt: null, | ||
| hasPendingApprovals: false, | ||
| hasPendingUserInput: false, | ||
| hasActionableProposedPlan: false, | ||
| }; | ||
| } | ||
|
|
||
| function group(id: string, threads: SidebarThreadSummary[]): ThreadGroup { | ||
| return { id, projectKey: "pk", name: id, threadKeys: threads.map(threadKeyOf) }; | ||
| } | ||
|
|
||
| describe("buildGroupedThreadLayout", () => { | ||
| const t1 = makeThread("t1"); | ||
| const t2 = makeThread("t2"); | ||
| const t3 = makeThread("t3"); | ||
| const t4 = makeThread("t4"); | ||
|
|
||
| it("splits threads into folder sections (in folder order) plus ungrouped", () => { | ||
| const g1 = group("g1", [t2]); | ||
| const g2 = group("g2", [t3]); | ||
| const layout = buildGroupedThreadLayout({ | ||
| visibleProjectThreads: [t1, t2, t3, t4], | ||
| projectKey: "pk", | ||
| groups: { g1, g2 }, | ||
| groupOrder: ["g2", "g1"], | ||
| groupExpandedById: {}, | ||
| }); | ||
|
|
||
| expect(layout.sections.map((s) => s.group.id)).toEqual(["g2", "g1"]); | ||
| expect(layout.sections[0]!.threads).toEqual([t3]); | ||
| expect(layout.sections[1]!.threads).toEqual([t2]); | ||
| expect(layout.ungroupedThreads).toEqual([t1, t4]); | ||
| }); | ||
|
|
||
| it("orders threads within a folder by the folder's threadKeys, not sort order", () => { | ||
| const g1: ThreadGroup = { | ||
| id: "g1", | ||
| projectKey: "pk", | ||
| name: "g1", | ||
| threadKeys: [threadKeyOf(t3), threadKeyOf(t1)], | ||
| }; | ||
| const layout = buildGroupedThreadLayout({ | ||
| visibleProjectThreads: [t1, t2, t3], | ||
| projectKey: "pk", | ||
| groups: { g1 }, | ||
| groupOrder: ["g1"], | ||
| groupExpandedById: {}, | ||
| }); | ||
| expect(layout.sections[0]!.threads).toEqual([t3, t1]); | ||
| expect(layout.ungroupedThreads).toEqual([t2]); | ||
| }); | ||
|
|
||
| it("defaults a folder to expanded and honours an explicit collapse", () => { | ||
| const g1 = group("g1", [t1]); | ||
| const layout = buildGroupedThreadLayout({ | ||
| visibleProjectThreads: [t1], | ||
| projectKey: "pk", | ||
| groups: { g1 }, | ||
| groupOrder: ["g1"], | ||
| groupExpandedById: { g1: false }, | ||
| }); | ||
| expect(layout.sections[0]!.expanded).toBe(false); | ||
| }); | ||
|
|
||
| it("ignores folders from a different project and threads no longer visible", () => { | ||
| const sameProject = group("g1", [t1]); | ||
| const otherProject: ThreadGroup = { | ||
| id: "g2", | ||
| projectKey: "other", | ||
| name: "g2", | ||
| threadKeys: [threadKeyOf(t2)], | ||
| }; | ||
| // g1 references t4, which is not in the visible set -> contributes no row. | ||
| const staleMember: ThreadGroup = { | ||
| id: "g3", | ||
| projectKey: "pk", | ||
| name: "g3", | ||
| threadKeys: [threadKeyOf(t4)], | ||
| }; | ||
| const layout = buildGroupedThreadLayout({ | ||
| visibleProjectThreads: [t1, t2], | ||
| projectKey: "pk", | ||
| groups: { g1: sameProject, g2: otherProject, g3: staleMember }, | ||
| groupOrder: ["g1", "g2", "g3"], | ||
| groupExpandedById: {}, | ||
| }); | ||
|
|
||
| expect(layout.sections.map((s) => s.group.id)).toEqual(["g1", "g3"]); | ||
| expect(layout.sections.find((s) => s.group.id === "g3")!.threads).toEqual([]); | ||
| // t2 belongs to a folder scoped to another project, so it stays ungrouped here. | ||
| expect(layout.ungroupedThreads).toEqual([t2]); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime"; | ||
|
|
||
| import type { ThreadGroup } from "./uiStateStore"; | ||
| import type { SidebarThreadSummary } from "./types"; | ||
|
|
||
| /** A folder plus the visible threads it currently holds, in folder order. */ | ||
| export interface ThreadGroupSection { | ||
| group: ThreadGroup; | ||
| threads: SidebarThreadSummary[]; | ||
| expanded: boolean; | ||
| } | ||
|
|
||
| export interface GroupedThreadLayout { | ||
| /** Folder sections in the project's folder order. */ | ||
| sections: ThreadGroupSection[]; | ||
| /** Threads not in any folder, preserving the input sort order. */ | ||
| ungroupedThreads: SidebarThreadSummary[]; | ||
| } | ||
|
|
||
| export function threadKeyOf(thread: SidebarThreadSummary): string { | ||
| return scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)); | ||
| } | ||
|
|
||
| /** | ||
| * Split a project's (already sorted, non-archived) threads into ordered folder | ||
| * sections plus the remaining ungrouped threads. Pure: no store access, so it is | ||
| * straightforward to unit test. Folders that reference threads not present in | ||
| * `visibleProjectThreads` simply render fewer rows; threads in no folder fall | ||
| * through to `ungroupedThreads`. | ||
| */ | ||
| export function buildGroupedThreadLayout(input: { | ||
| visibleProjectThreads: readonly SidebarThreadSummary[]; | ||
| projectKey: string; | ||
| groups: Record<string, ThreadGroup>; | ||
| groupOrder: readonly string[]; | ||
| groupExpandedById: Record<string, boolean>; | ||
| }): GroupedThreadLayout { | ||
| const { visibleProjectThreads, projectKey, groups, groupOrder, groupExpandedById } = input; | ||
|
|
||
| const threadByKey = new Map<string, SidebarThreadSummary>(); | ||
| for (const thread of visibleProjectThreads) { | ||
| threadByKey.set(threadKeyOf(thread), thread); | ||
| } | ||
|
|
||
| const claimed = new Set<string>(); | ||
| const sections: ThreadGroupSection[] = []; | ||
| for (const groupId of groupOrder) { | ||
| const group = groups[groupId]; | ||
| if (!group || group.projectKey !== projectKey) { | ||
| continue; | ||
| } | ||
| const threads: SidebarThreadSummary[] = []; | ||
| for (const threadKey of group.threadKeys) { | ||
| const thread = threadByKey.get(threadKey); | ||
| if (thread) { | ||
| threads.push(thread); | ||
| claimed.add(threadKey); | ||
| } | ||
| } | ||
| sections.push({ | ||
| group, | ||
| threads, | ||
| expanded: groupExpandedById[groupId] ?? true, | ||
| }); | ||
| } | ||
|
|
||
| const ungroupedThreads = visibleProjectThreads.filter( | ||
| (thread) => !claimed.has(threadKeyOf(thread)), | ||
| ); | ||
|
|
||
| return { sections, ungroupedThreads }; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.