Files
kandev/apps/web/components/task-create-dialog-effects.ts
T
Kandev Agent 9e8079de69 feat(multi-repo): per-repo git ops, unified branch endpoint, chip-style task create
Bundles a string of multi-repo fixes and refactors that came up while
testing the feature end-to-end:

- workspace_path stored on multi-repo TaskEnvironment was one level too
  high, so agentctl booted with the wrong WorkDir on cold start; fixed
  in resolveTaskEnvWorkspacePath + GetWorkspaceInfoForSession.
- Stage/Unstage/Discard/Commit/Revert/Reset/Amend now thread `repo`
  through agentctl client → WS handler → frontend hook → SessionGit
  fan-out, so the right git repo is targeted (was running at the task
  root, which isn't a git repo for multi-repo tasks).
- agentctl GitLog is multi-repo aware: when no `repo` is specified and
  per-repo trackers exist, fans out one log per repo and merges sorted
  newest-first with RepositoryName stamped on each commit.
- Per-repo grouping for files (Changes panel) and commits, with
  collapsible group headers; commits section defaults to collapsed.
  Algorithm extracted into one generic groupByRepositoryName helper
  (was copy-pasted three times).
- Task creation dialog rewritten around a unified `fs.repositories[]`
  list — no more "primary vs extras" split. Chip-style row with one
  pill per repo + branch, "+" to add, x to remove, GitHub URL toggle
  inline. Discovered (on-machine) repos appear in any chip's dropdown.
  Removes ~6 fields and 3 helpers from the dialog state.
- Branch listing endpoints collapsed to one:
  GET /workspaces/:id/branches?repository_id=X | path=Y
  Frontend collapses to a single useBranches hook with a tagged-union
  source. Drops two HTTP routes, two service methods, two API client
  funcs, and one whole hook file.

Tests added across the chain: workspace_path resolver, sort-commits-
by-date, manager poll-mode propagation, RepoSubpaths, chip grouping
+ collapse + per-repo isLatest, group-by-repo helper, dialog payload
builder + chip wiring + discovered-repo flow.
2026-04-26 21:23:48 +01:00

385 lines
14 KiB
TypeScript

"use client";
import { useEffect } from "react";
import type { Repository, Executor } from "@/lib/types/http";
import type { AgentProfileOption } from "@/lib/state/slices";
import { DEFAULT_LOCAL_EXECUTOR_TYPE } from "@/lib/utils";
import { useToast } from "@/components/toast-provider";
import { discoverRepositoriesAction } from "@/app/actions/workspaces";
import { getLocalStorage } from "@/lib/local-storage";
import { STORAGE_KEYS } from "@/lib/settings/constants";
import { listWorkflowSteps } from "@/lib/api/domains/workflow-api";
import { fetchRepoBranches, fetchPRInfo } from "@/lib/api/domains/github-api";
import type {
DialogFormState,
StoreSelections,
TaskCreateEffectsArgs,
} from "@/components/task-create-dialog-types";
export function useWorkflowAgentProfileEffect(
fs: DialogFormState,
workflows: Array<{ id: string; agent_profile_id?: string }>,
agentProfiles: AgentProfileOption[],
) {
const { selectedWorkflowId, setAgentProfileId, setWorkflowAgentProfileId } = fs;
useEffect(() => {
if (!selectedWorkflowId) {
setWorkflowAgentProfileId("");
return;
}
const workflow = workflows.find((w) => w.id === selectedWorkflowId);
if (workflow?.agent_profile_id) {
// Always lock the selector when the workflow specifies an agent profile.
// This prevents the race condition where agentProfiles hasn't loaded yet.
setWorkflowAgentProfileId(workflow.agent_profile_id);
// Only set the agentProfileId once the profile is confirmed available.
const profileExists = agentProfiles.some((p) => p.id === workflow.agent_profile_id);
if (profileExists) {
setAgentProfileId(workflow.agent_profile_id);
}
} else {
setWorkflowAgentProfileId("");
// Restore the user's last-used agent profile when unlocking
const lastId = getLocalStorage<string | null>(STORAGE_KEYS.LAST_AGENT_PROFILE_ID, null);
setAgentProfileId(lastId ?? "");
}
}, [selectedWorkflowId, workflows, agentProfiles, setAgentProfileId, setWorkflowAgentProfileId]);
}
export function useWorkflowStepsEffect(fs: DialogFormState, workflowId: string | null) {
const { selectedWorkflowId, setFetchedSteps } = fs;
useEffect(() => {
if (!selectedWorkflowId || selectedWorkflowId === workflowId) {
void Promise.resolve().then(() => setFetchedSteps(null));
return;
}
let cancelled = false;
listWorkflowSteps(selectedWorkflowId)
.then((response) => {
if (cancelled) return;
const sorted = [...response.steps].sort((a, b) => a.position - b.position);
setFetchedSteps(sorted.map((s) => ({ id: s.id, title: s.name, events: s.events })));
})
.catch(() => {
if (!cancelled) setFetchedSteps(null);
});
return () => {
cancelled = true;
};
}, [selectedWorkflowId, workflowId, setFetchedSteps]);
}
export function useRepositoryAutoSelectEffect(
fs: DialogFormState,
open: boolean,
workspaceId: string | null,
repositories: Repository[],
) {
// Auto-seed the unified repos list with the user's last-used repo (or the
// workspace's only repo) on open. Skipped when URL mode is active or the
// user already has at least one row picked.
const { repositories: rows, useGitHubUrl, setRepositories } = fs;
useEffect(() => {
if (!open || !workspaceId || useGitHubUrl) return;
const hasPickedRow = rows.some((r) => r.repositoryId || r.localPath);
if (hasPickedRow) return;
const lastUsedRepoId = getLocalStorage<string | null>(STORAGE_KEYS.LAST_REPOSITORY_ID, null);
let pickId: string | null = null;
if (lastUsedRepoId && repositories.some((r: Repository) => r.id === lastUsedRepoId)) {
pickId = lastUsedRepoId;
} else if (repositories.length === 1) {
pickId = repositories[0].id;
}
if (!pickId) return;
const id = pickId;
void Promise.resolve().then(() =>
setRepositories([{ key: "row-0", repositoryId: id, branch: "" }]),
);
}, [open, repositories, rows, useGitHubUrl, workspaceId, setRepositories]);
}
export function useDiscoverReposEffect(
fs: DialogFormState,
open: boolean,
workspaceId: string | null,
repositoriesLoading: boolean,
toast: ReturnType<typeof useToast>["toast"],
) {
const {
discoverReposLoaded,
discoverReposLoading,
setDiscoveredRepositories,
setDiscoverReposLoading,
setDiscoverReposLoaded,
} = fs;
useEffect(() => {
if (!open || !workspaceId || repositoriesLoading || discoverReposLoaded || discoverReposLoading)
return;
void Promise.resolve()
.then(() => setDiscoverReposLoading(true))
.then(() => discoverRepositoriesAction(workspaceId))
.then((r) => {
setDiscoveredRepositories(r.repositories);
})
.catch((e) => {
toast({
title: "Failed to discover repositories",
description: e instanceof Error ? e.message : "Request failed",
variant: "error",
});
setDiscoveredRepositories([]);
})
.finally(() => {
setDiscoverReposLoading(false);
setDiscoverReposLoaded(true);
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
discoverReposLoaded,
discoverReposLoading,
open,
fs.discoveredRepositories.length,
repositoriesLoading,
toast,
workspaceId,
]);
}
// useLocalBranchesEffect removed: the chip row's RepoChip loads branches per
// repo via useRepositoryBranches now (the store dedupes by id), so we don't
// need a per-form "primary local repo branches" cache anymore.
export function useDefaultSelectionsEffect(
fs: DialogFormState,
open: boolean,
sel: StoreSelections,
workflows: Array<{ id: string; agent_profile_id?: string }>,
) {
const { agentProfiles, executors, workspaceDefaults } = sel;
const {
agentProfileId,
workflowAgentProfileId,
selectedWorkflowId,
executorId,
executorProfileId,
setAgentProfileId,
setExecutorId,
setExecutorProfileId,
} = fs;
useEffect(() => {
// Check synchronously whether the selected workflow has an agent override.
// This avoids a race condition where workflowAgentProfileId state hasn't
// been committed yet by the workflow effect running in the same cycle.
const workflowHasAgent = selectedWorkflowId
? workflows.some((w) => w.id === selectedWorkflowId && w.agent_profile_id)
: false;
if (
!open ||
agentProfileId ||
workflowAgentProfileId ||
workflowHasAgent ||
agentProfiles.length === 0
)
return;
const lastId = getLocalStorage<string | null>(STORAGE_KEYS.LAST_AGENT_PROFILE_ID, null);
if (lastId && agentProfiles.some((p: AgentProfileOption) => p.id === lastId)) {
void Promise.resolve().then(() => setAgentProfileId(lastId));
return;
}
const defId = workspaceDefaults?.default_agent_profile_id ?? null;
if (defId && agentProfiles.some((p: AgentProfileOption) => p.id === defId)) {
void Promise.resolve().then(() => setAgentProfileId(defId));
return;
}
void Promise.resolve().then(() => setAgentProfileId(agentProfiles[0].id));
}, [
open,
agentProfileId,
workflowAgentProfileId,
selectedWorkflowId,
workflows,
agentProfiles,
workspaceDefaults,
setAgentProfileId,
]);
useEffect(() => {
if (!open || executorId || executors.length === 0) return;
const defId = workspaceDefaults?.default_executor_id ?? null;
if (defId && executors.some((e: Executor) => e.id === defId)) {
void Promise.resolve().then(() => setExecutorId(defId));
return;
}
const local = executors.find((e: Executor) => e.type === DEFAULT_LOCAL_EXECUTOR_TYPE);
void Promise.resolve().then(() => setExecutorId(local?.id ?? executors[0].id));
}, [open, executorId, executors, workspaceDefaults, setExecutorId]);
useEffect(() => {
// Auto-select executor profile: last used (localStorage) → first available
if (!open || executorProfileId || executors.length === 0) return;
const allProfiles = executors.flatMap((e) =>
(e.profiles ?? []).map((p) => ({ ...p, _executorId: e.id })),
);
if (allProfiles.length === 0) return;
const lastId = getLocalStorage<string | null>(STORAGE_KEYS.LAST_EXECUTOR_PROFILE_ID, null);
const pick = lastId && allProfiles.some((p) => p.id === lastId) ? lastId : allProfiles[0].id;
void Promise.resolve().then(() => setExecutorProfileId(pick));
}, [open, executorProfileId, executors, setExecutorProfileId]);
// Derive executorId from the selected executor profile
useEffect(() => {
if (!executorProfileId) return;
for (const executor of executors) {
const match = (executor.profiles ?? []).find((p) => p.id === executorProfileId);
if (match) {
void Promise.resolve().then(() => setExecutorId(executor.id));
return;
}
}
}, [executorProfileId, executors, setExecutorId]);
}
/**
* Auto-selects a sensible branch in the GitHub URL flow. Per-repo (workspace
* or discovered) branch auto-select happens inside RepoChip when a row's
* branches load — that keeps the per-row state confined to the chip.
*/
export function useBranchAutoSelectEffect(fs: DialogFormState) {
const { githubBranch, githubBranches, useGitHubUrl, setGitHubBranch, githubPrHeadBranch } = fs;
useEffect(() => {
if (!useGitHubUrl || githubBranches.length === 0 || githubBranch) return;
if (githubPrHeadBranch) {
const prBranch = githubBranches.find((b) => b.name === githubPrHeadBranch);
if (prBranch) {
setGitHubBranch(prBranch.name);
return;
}
}
// GitHub URL branches are referenced by name only (no remote prefix);
// selectPreferredBranch expects origin-prefixed remotes, so pick directly.
const preferred =
githubBranches.find((b) => b.name === "main") ??
githubBranches.find((b) => b.name === "master") ??
githubBranches[0];
if (preferred) setGitHubBranch(preferred.name);
}, [githubBranch, githubBranches, useGitHubUrl, setGitHubBranch, githubPrHeadBranch]);
}
/** Parse a GitHub URL to extract owner, repo, and optional PR number. Returns null if invalid. */
function parseGitHubUrl(url: string): { owner: string; repo: string; prNumber?: number } | null {
const trimmed = url.trim();
if (!trimmed) return null;
// Try PR URL first: github.com/owner/repo/pull/123 (with optional trailing path/hash like /files#diff-...)
const prMatch = trimmed.match(
/(?:https?:\/\/)?(?:www\.)?github\.com\/([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+)\/pull\/(\d+)(?:[/?#].*)?$/,
);
if (prMatch) {
return { owner: prMatch[1], repo: prMatch[2], prNumber: parseInt(prMatch[3], 10) };
}
// Fall back to repo URL: github.com/owner/repo
const match = trimmed.match(
/(?:https?:\/\/)?(?:www\.)?github\.com\/([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+?)(?:\.git)?\/?$/,
);
if (!match) return null;
return { owner: match[1], repo: match[2] };
}
export function useGitHubUrlBranchesEffect(fs: DialogFormState, open: boolean) {
const {
useGitHubUrl,
githubUrl,
setGitHubBranches,
setGitHubBranchesLoading,
setGitHubUrlError,
setGitHubPrHeadBranch,
} = fs;
useEffect(() => {
if (!open || !useGitHubUrl) {
setGitHubBranchesLoading(false);
return;
}
const trimmed = githubUrl.trim();
if (!trimmed) {
setGitHubBranches([]);
setGitHubBranchesLoading(false);
setGitHubUrlError(null);
return;
}
const parsed = parseGitHubUrl(githubUrl);
if (!parsed) {
setGitHubBranches([]);
setGitHubPrHeadBranch(null);
setGitHubBranchesLoading(false);
setGitHubUrlError("Invalid GitHub URL — expected github.com/owner/repo or .../pull/123");
return;
}
let cancelled = false;
setGitHubUrlError(null);
setGitHubBranchesLoading(true);
setGitHubPrHeadBranch(null);
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 15_000);
const fetchOpts = { init: { signal: controller.signal } };
const branchesPromise = fetchRepoBranches(parsed.owner, parsed.repo, fetchOpts);
const prPromise = parsed.prNumber
? fetchPRInfo(parsed.owner, parsed.repo, parsed.prNumber, fetchOpts).catch(() => null)
: Promise.resolve(null);
Promise.all([branchesPromise, prPromise])
.then(([branchesRes, prInfo]) => {
if (cancelled) return;
setGitHubBranches(
branchesRes.branches.map((b) => ({ name: b.name, type: "remote" as const })),
);
setGitHubUrlError(null);
if (prInfo) {
setGitHubPrHeadBranch(prInfo.head_branch);
}
})
.catch((err) => {
if (cancelled) return;
const isAbort = err instanceof DOMException && err.name === "AbortError";
const isNotConfigured = err instanceof Error && err.message.includes("not configured");
let errorMessage = "Repository not found or not accessible";
if (isAbort) {
errorMessage = "Request timed out. Check your GitHub configuration in Settings.";
} else if (isNotConfigured) {
errorMessage = "GitHub is not configured. Set up a token in Settings > GitHub.";
}
setGitHubUrlError(errorMessage);
setGitHubBranches([]);
})
.finally(() => {
clearTimeout(timeoutId);
if (!cancelled) setGitHubBranchesLoading(false);
});
return () => {
cancelled = true;
clearTimeout(timeoutId);
controller.abort();
};
}, [
open,
useGitHubUrl,
githubUrl,
setGitHubBranches,
setGitHubBranchesLoading,
setGitHubUrlError,
setGitHubPrHeadBranch,
]);
}
export function useTaskCreateDialogEffects(fs: DialogFormState, args: TaskCreateEffectsArgs) {
const { open, workspaceId, workflowId, repositories, repositoriesLoading } = args;
const { agentProfiles, executors, workspaceDefaults, toast, workflows } = args;
useWorkflowStepsEffect(fs, workflowId);
useWorkflowAgentProfileEffect(fs, workflows, agentProfiles);
useRepositoryAutoSelectEffect(fs, open, workspaceId, repositories);
useDiscoverReposEffect(fs, open, workspaceId, repositoriesLoading, toast);
useBranchAutoSelectEffect(fs);
useDefaultSelectionsEffect(fs, open, { agentProfiles, executors, workspaceDefaults }, workflows);
useGitHubUrlBranchesEffect(fs, open);
}