Files
kandev/apps/web/components/github/my-github/quick-task-launcher.tsx
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

151 lines
4.0 KiB
TypeScript

"use client";
import { useMemo } from "react";
import { useRouter } from "next/navigation";
import type { Icon } from "@tabler/icons-react";
import { TaskCreateDialog } from "@/components/task-create-dialog";
import type { Repository, Task, Workflow, WorkflowStep } from "@/lib/types/http";
import type { GitHubPR, GitHubIssue } from "@/lib/types/github";
export type TaskPreset = {
id: string;
label: string;
hint: string;
icon: Icon;
prompt: (opts: { url: string; title: string }) => string;
};
export type LaunchPayload =
| { kind: "pr"; pr: GitHubPR; preset: TaskPreset }
| { kind: "issue"; issue: GitHubIssue; preset: TaskPreset };
type DialogState = {
title: string;
description: string;
repositoryId?: string;
branch?: string;
checkoutBranch?: string;
githubUrl?: string;
};
function matchRepo(repos: Repository[], owner: string, name: string): Repository | undefined {
return repos.find(
(r) =>
(r.provider_owner || "").toLowerCase() === owner.toLowerCase() &&
(r.provider_name || "").toLowerCase() === name.toLowerCase(),
);
}
function extractPayload(payload: LaunchPayload) {
if (payload.kind === "pr") {
return {
url: payload.pr.html_url,
title: payload.pr.title,
owner: payload.pr.repo_owner,
name: payload.pr.repo_name,
branch: payload.pr.head_branch,
};
}
return {
url: payload.issue.html_url,
title: payload.issue.title,
owner: payload.issue.repo_owner,
name: payload.issue.repo_name,
branch: undefined as string | undefined,
};
}
function buildDialogState(payload: LaunchPayload, repositories: Repository[]): DialogState {
const data = extractPayload(payload);
const repo = matchRepo(repositories, data.owner, data.name);
const description = payload.preset.prompt({ url: data.url, title: data.title });
const title = `${payload.preset.label}: ${data.title}`;
const checkoutBranch = payload.kind === "pr" ? data.branch : undefined;
if (repo) {
return {
title,
description,
repositoryId: repo.id,
branch: data.branch,
checkoutBranch,
};
}
return {
title,
description,
githubUrl: `github.com/${data.owner}/${data.name}`,
branch: data.branch,
checkoutBranch,
};
}
type QuickTaskLauncherProps = {
workspaceId: string | null;
workflows: Workflow[];
steps: WorkflowStep[];
repositories: Repository[];
payload: LaunchPayload | null;
onClose: () => void;
};
export function QuickTaskLauncher({
workspaceId,
workflows,
steps,
repositories,
payload,
onClose,
}: QuickTaskLauncherProps) {
const router = useRouter();
const defaultWorkflow = workflows[0];
const sortedStepsForWorkflow = useMemo(
() =>
steps
.filter((s) => s.workflow_id === defaultWorkflow?.id)
.sort((a, b) => a.position - b.position),
[steps, defaultWorkflow],
);
const defaultStep = sortedStepsForWorkflow[0];
const stepsForWorkflow = useMemo(
() => sortedStepsForWorkflow.map((s) => ({ id: s.id, title: s.name, events: s.events })),
[sortedStepsForWorkflow],
);
const dialog = useMemo(
() => (payload ? buildDialogState(payload, repositories) : null),
[payload, repositories],
);
const handleOpenChange = (open: boolean) => {
if (!open) onClose();
};
const handleSuccess = (task: Task) => {
onClose();
router.push(`/t/${task.id}`);
};
if (!workspaceId || !defaultWorkflow || !defaultStep || !dialog) return null;
return (
<TaskCreateDialog
open={true}
onOpenChange={handleOpenChange}
mode="create"
workspaceId={workspaceId}
workflowId={defaultWorkflow.id}
defaultStepId={defaultStep.id}
steps={stepsForWorkflow}
initialValues={{
title: dialog.title,
description: dialog.description,
repositoryId: dialog.repositoryId,
branch: dialog.branch,
checkoutBranch: dialog.checkoutBranch,
githubUrl: dialog.githubUrl,
}}
onSuccess={handleSuccess}
/>
);
}