Files
kandev/apps/web/hooks/domains/session/use-session-worktrees.ts
T
João Salavisa ea84b913b4 feat: choose worktree when opening IDE on multi-repo tasks (#1743)
* feat: choose worktree when opening IDE on multi-repo tasks

On multi-repo tasks the open-in-IDE button always opened the first
worktree. The editors menu now shows a worktree picker (repo + branch)
when a session has multiple worktrees, and each editor in the dropdown
expands into a per-worktree submenu.

- open-editor/open-folder accept an optional worktree_id; empty keeps
  the first-worktree fallback so existing clients are unaffected
- session DTOs now serialize the full worktrees list instead of only
  the flattened first-worktree fields
- useSessionWorktrees unions API worktrees with the WS-populated list
- new Playwright spec covers the picker on a real multi-repo session

* fix: address review feedback on worktree picker

- fall back on empty-string worktree fields, not only null/undefined
- bind optional open-folder body via EOF check instead of ContentLength
  so chunked requests keep their worktree_id
- wrap disabled editor buttons in a focusable span so tooltips work
- add hook test asserting worktreeId is forwarded as worktree_id

* fix: map editor service errors to HTTP status, drop e2e sleep

- open-editor/open-folder return 404/400/409 for known service errors
  (unknown worktree or editor, invalid config, unavailable editor)
  instead of a blanket 500
- replace the picker spec's 750ms sleep with an explicit composer click
  so the autofocus race is deterministic

---------

Co-authored-by: Kandev Agent <agent@kandev.dev>
2026-07-16 22:43:06 +01:00

66 lines
2.3 KiB
TypeScript

import { useMemo } from "react";
import { useAppStore } from "@/components/state-provider";
import { useSession } from "@/hooks/domains/session/use-session";
import type { Worktree } from "@/lib/state/slices/session/types";
import type { TaskSession } from "@/lib/types/http";
/**
* Resolves the worktrees of a session by unioning the API-provided
* `session.worktrees` (complete, survives reloads) with the WS-populated
* per-session list (fresher after live add-branch events), filling in details
* from the WS worktrees map. Falls back to the legacy single `worktree_id`
* field when neither source has entries.
*/
export function resolveSessionWorktrees(
sessionId: string,
session: TaskSession | null,
worktrees: Record<string, Worktree>,
sessionWorktreeIds: string[] | undefined,
): Worktree[] {
const result: Worktree[] = [...(session?.worktrees ?? [])]
.sort((a, b) => a.position - b.position)
.map((wt) => {
const id = wt.worktree_id || wt.id;
const live = worktrees[id];
// `||` (not `??`): the Go backend may serialize these as empty strings,
// which should also fall back to the live WS-populated values.
return {
id,
sessionId,
repositoryId: wt.repository_id || live?.repositoryId,
path: wt.worktree_path || live?.path,
branch: wt.worktree_branch || live?.branch,
};
});
const seen = new Set(result.map((wt) => wt.id));
for (const id of sessionWorktreeIds ?? []) {
const live = worktrees[id];
if (!live || seen.has(id)) continue;
seen.add(id);
result.push(live);
}
if (result.length === 0 && session?.worktree_id) {
const worktree = worktrees[session.worktree_id];
return worktree ? [worktree] : [];
}
return result;
}
export function useSessionWorktrees(sessionId: string | null) {
const { session } = useSession(sessionId);
const worktrees = useAppStore((state) => state.worktrees.items);
const sessionWorktreesBySessionId = useAppStore(
(state) => state.sessionWorktreesBySessionId.itemsBySessionId,
);
return useMemo(() => {
if (!sessionId) return [];
return resolveSessionWorktrees(
sessionId,
session,
worktrees,
sessionWorktreesBySessionId[sessionId],
);
}, [session, sessionId, sessionWorktreesBySessionId, worktrees]);
}