Files
kandev/apps/web/components/task-create-dialog-helpers.ts
T
João Salavisa ccc62d9324 feat(github): configurable quick-action presets and PR branch checkout (#689)
* feat(github): configurable quick-action presets and PR branch checkout

- Add workspace-scoped action presets (icon, label, hint, prompt template) for PRs and Issues, editable from GitHub settings with reset-to-defaults.
- When starting a task from a PR, check out the PR head branch directly in the worktree instead of branching off main.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix(github): extract action-preset service to keep service.go under line limit

Splits the preset CRUD methods out of the oversized service.go and applies prettier formatting to three frontend files flagged by CI.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix(github): address review feedback on action presets

- Prevent infinite refetch loop when fetching presets fails by tracking attempted workspaces in a ref instead of re-entering the effect on every loading-state change.
- Drop unused savePR/saveIssue helpers from the hook; only save() is consumed.
- Reorder the init spread in updateGitHubActionPresets/resetGitHubActionPresets so callers can't accidentally overwrite method/body.
- Tighten the GitHubActionPreset.icon type with string & {} so known icon keys keep autocomplete.
- Sharpen the eye-icon test to assert the exact IconEye mapping.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix(github): address second round of review feedback

- Seed preset editor drafts from current store state via lazy initializers so a warm mount shows the user's saved customisations instead of silently reverting to defaults when Save is clicked.
- Guard Reset button with its own in-flight state to prevent double-firing.
- Stop freezing resolved defaults into the stored row on a partial update; persist only what the store has for the untouched kind so future default improvements still reach those workspaces.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* refactor(github): tighten quick-actions editor UI

Replace the stacked PR/Issue editors and multi-line preset cards with a tabbed layout and single-line rows. Each row shows icon · label · hint inline; an "Edit prompt" toggle reveals the template textarea only when needed. Drops the redundant section headings, up/down reorder buttons, and verbose template helper label (hint moved into the textarea placeholder).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix(github): widen icon select trigger so the icon is visible

The icon select trigger had a fixed 40px width which the SelectTrigger's built-in chevron, padding, and icon gap all competed for, clipping the icon out of view. Drop the width override so the trigger sizes to its content.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

---------

Co-authored-by: Kandev Agent <agent@kandev.dev>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-24 14:10:59 +01:00

195 lines
6.1 KiB
TypeScript

import type { useRouter } from "next/navigation";
import type { Task, Branch, LocalRepository } from "@/lib/types/http";
import type { AgentProfileOption } from "@/lib/state/slices";
import type { AppState } from "@/lib/state/store";
import type { StepType } from "@/components/task-create-dialog-types";
import { selectPreferredBranch } from "@/lib/utils";
import { getLocalStorage } from "@/lib/local-storage";
import { STORAGE_KEYS } from "@/lib/settings/constants";
import { useContextFilesStore } from "@/lib/state/context-files-store";
import { linkToTask } from "@/lib/links";
import { INTENT_PLAN } from "@/lib/state/layout-manager";
import { createTask } from "@/lib/api";
import type { FileAttachment } from "@/components/task/chat/file-attachment";
import type { MessageAttachment } from "@/lib/services/session-launch-service";
type CreateTaskParams = Parameters<typeof createTask>[0];
export type { CreateTaskParams };
/** Converts FileAttachment array to MessageAttachment array for the launch request. */
export function toMessageAttachments(
attachments: FileAttachment[],
): MessageAttachment[] | undefined {
if (attachments.length === 0) return undefined;
return attachments.map((att) =>
att.isImage
? { type: "image" as const, data: att.data, mime_type: att.mimeType }
: {
type: "resource" as const,
data: att.data,
mime_type: att.mimeType,
name: att.fileName,
},
);
}
export function autoSelectBranch(branchList: Branch[], setBranch: (value: string) => void): void {
const lastUsedBranch = getLocalStorage<string | null>(STORAGE_KEYS.LAST_BRANCH, null);
if (
lastUsedBranch &&
branchList.some((b) => {
const displayName = b.type === "remote" && b.remote ? `${b.remote}/${b.name}` : b.name;
return displayName === lastUsedBranch;
})
) {
setBranch(lastUsedBranch);
return;
}
const preferredBranch = selectPreferredBranch(branchList);
if (preferredBranch) setBranch(preferredBranch);
}
export function computePassthroughProfile(
agentProfileId: string,
agentProfiles: AgentProfileOption[],
) {
if (!agentProfileId) return false;
return (
agentProfiles.find((p: AgentProfileOption) => p.id === agentProfileId)?.cli_passthrough === true
);
}
export function computeEffectiveStepId(
selectedWorkflowId: string | null,
workflowId: string | null,
fetchedSteps: StepType[] | null,
defaultStepId: string | null,
) {
return selectedWorkflowId && selectedWorkflowId !== workflowId && fetchedSteps
? (fetchedSteps[0]?.id ?? null)
: defaultStepId;
}
export function computeIsTaskStarted(
isEditMode: boolean,
editingTask?: { state?: Task["state"] } | null,
) {
if (!isEditMode || !editingTask?.state) return false;
return editingTask.state !== "TODO" && editingTask.state !== "CREATED";
}
export type ActivatePlanModeArgs = {
sessionId: string;
taskId: string;
setActiveDocument: AppState["setActiveDocument"];
setPlanMode: AppState["setPlanMode"];
router: ReturnType<typeof useRouter>;
};
export function activatePlanMode({
sessionId,
taskId,
setActiveDocument,
setPlanMode,
router,
}: ActivatePlanModeArgs) {
setActiveDocument(sessionId, { type: "plan", taskId });
setPlanMode(sessionId, true);
useContextFilesStore.getState().addFile(sessionId, { path: "plan:context", name: "Plan" });
router.push(linkToTask(taskId, INTENT_PLAN));
}
export type BuildCreatePayloadArgs = {
workspaceId: string;
effectiveWorkflowId: string;
trimmedTitle: string;
trimmedDescription: string;
repositoriesPayload: CreateTaskParams["repositories"];
agentProfileId: string;
executorId: string;
executorProfileId: string;
withAgent: boolean;
planMode?: boolean;
attachments?: MessageAttachment[];
parentId?: string;
};
export function buildCreateTaskPayload(args: BuildCreatePayloadArgs): CreateTaskParams {
return {
workspace_id: args.workspaceId,
workflow_id: args.effectiveWorkflowId,
title: args.trimmedTitle,
description: args.trimmedDescription,
repositories: args.repositoriesPayload,
state: args.withAgent ? "IN_PROGRESS" : "CREATED",
start_agent: args.withAgent ? true : undefined,
prepare_session: args.withAgent ? undefined : true,
agent_profile_id: args.agentProfileId || undefined,
executor_id: args.executorId || undefined,
executor_profile_id: args.executorProfileId || undefined,
plan_mode: args.planMode || undefined,
attachments: args.attachments,
parent_id: args.parentId || undefined,
};
}
export function validateCreateInputs(inputs: {
trimmedTitle: string;
workspaceId: string | null;
effectiveWorkflowId: string | null;
repositoryId: string;
selectedLocalRepo: LocalRepository | null;
githubUrl?: string;
agentProfileId: string;
}): boolean {
return Boolean(
inputs.trimmedTitle &&
inputs.workspaceId &&
inputs.effectiveWorkflowId &&
inputs.agentProfileId &&
(inputs.repositoryId || inputs.selectedLocalRepo || inputs.githubUrl?.trim()),
);
}
/** Builds the repositories payload for task creation from the current form state. */
export function buildRepositoriesPayload(opts: {
useGitHubUrl: boolean;
githubUrl: string;
branch: string;
githubPrHeadBranch: string | null;
repositoryId: string;
selectedLocalRepo: LocalRepository | null;
}): NonNullable<CreateTaskParams["repositories"]> {
if (opts.useGitHubUrl && opts.githubUrl) {
return [
{
repository_id: "",
base_branch: opts.branch || undefined,
checkout_branch: opts.githubPrHeadBranch || undefined,
github_url: opts.githubUrl.trim(),
},
];
}
if (opts.repositoryId) {
return [
{
repository_id: opts.repositoryId,
base_branch: opts.branch || undefined,
checkout_branch: opts.githubPrHeadBranch || undefined,
},
];
}
if (opts.selectedLocalRepo) {
return [
{
repository_id: "",
base_branch: opts.branch || undefined,
local_path: opts.selectedLocalRepo.path,
default_branch: opts.selectedLocalRepo.default_branch || undefined,
},
];
}
return [];
}