Files
kandev/apps/web/components/linear/linear-quick-task-launcher.tsx
T
João Salavisa 7b5b3f329d feat: add Linear integration (#736)
* feat: add Linear integration

Mirrors the existing Jira integration so a workspace can connect to Linear
with a personal API key, browse and search issues, and start a Kandev task
from any issue. Backend uses Linear's GraphQL API with a workspace-scoped
config + secret store and a 90s auth-health poller; the frontend adds a
settings page, an issue dialog with state transitions, link/import buttons
in the task UI, and a paginated /linear browse page with a Start task flow.

* fix(linear): address PR review feedback

- Route httpListStates through Service.ListStates instead of clientFor
- Extract repeated workspace_id required string into a constant
- Log GetConfig errors during credential resolution instead of dropping them
- Clear popover error state on close in import + link buttons
- Pick the first workflow with steps in the quick task launcher (don't hard-pin to workflows[0])
- Reset useLinearEnabled state on workspace change via tag-based pattern
- Add cursor-pointer to anchor + Link elements per project convention
- Add tests: handlers_test.go, linear-api.test.ts, use-linear-enabled.test.ts, linear-issue-common.test.tsx
- Document internal/jira and internal/linear in CLAUDE.md/AGENTS.md

* fix(linear): address second-round PR review feedback

- Round Linear's fractional state position instead of truncating it via
  int(math.Round(s.Position)) so adjacent fractional positions don't collapse
- Force LinearSettings to remount on workspace change with key={id} so the
  previous workspace's config / form / testResult / teams cannot bleed into
  the next workspace before the new fetches settle
- Broaden the legacy-value useLinearEnabled test to cover "true", "1", "yes",
  and "legacy" so a regression to strict value === "true" would fail

* fix(linear): address third-round PR review feedback

- Propagate context.Canceled / context.DeadlineExceeded from ListStates in
  GetIssue instead of swallowing them — a caller cancellation must not return
  a partial-success response
- Drop the one-off contains() test helper in handlers_test.go and use
  strings.Contains directly so the assertion is self-documenting
- Add getLinearIssue URL-shape test that locks identifier-as-path-segment
  vs workspaceId-as-query-param so accidentally swapping them would fail

* test(linear): extract repeated literals in linear-api.test.ts

Pulls "ws-1", the API base URL, and the auth-method literal into module-
level constants so the assertions read as the route shape they care about
and the duplicate-strings ESLint rule (currently warn, threshold 4)
stays quiet as the file grows.

---------

Co-authored-by: Kandev Agent <agent@kandev.dev>
2026-05-01 10:50:14 +01:00

105 lines
3.2 KiB
TypeScript

"use client";
import { useMemo } from "react";
import { useRouter } from "next/navigation";
import { TaskCreateDialog } from "@/components/task-create-dialog";
import type { Task, Workflow, WorkflowStep } from "@/lib/types/http";
import type { LinearIssue } from "@/lib/types/linear";
type QuickTaskLauncherProps = {
workspaceId: string | null;
workflows: Workflow[];
steps: WorkflowStep[];
issue: LinearIssue | null;
onClose: () => void;
};
// Build the prefilled title/description for a Linear-launched task. Mirrors
// the Jira prompt shape so users coming from either integration get a
// consistent skeleton.
function buildDialogState(issue: LinearIssue) {
const title = `${issue.identifier}: ${issue.title}`;
const description = [
`Linear issue: ${issue.identifier}`,
`URL: ${issue.url}`,
"",
"Title:",
issue.title,
"",
"Description:",
issue.description?.trim() || "(no description)",
].join("\n");
return { title, description };
}
// Renders the TaskCreateDialog prefilled from a Linear issue. Hidden until an
// `issue` is supplied, which keeps the dialog a single React tree per page.
export function LinearQuickTaskLauncher({
workspaceId,
workflows,
steps,
issue,
onClose,
}: QuickTaskLauncherProps) {
const router = useRouter();
// Default to the first workflow that actually has steps, not just
// workflows[0] — otherwise an empty first workflow blocks Start task even
// when other workflows would work fine. Bundling the lookup, the sort and
// the dialog projection into one memo keeps the launcher's hook list flat
// and avoids cascading useMemo deps.
const launchData = useMemo(() => {
for (const wf of workflows) {
const wfSteps = steps
.filter((s) => s.workflow_id === wf.id)
.sort((a, b) => a.position - b.position);
if (wfSteps.length > 0) {
return {
defaultWorkflow: wf as Workflow | undefined,
defaultStep: wfSteps[0],
stepsForWorkflow: wfSteps.map((s) => ({
id: s.id,
title: s.name,
events: s.events,
})),
};
}
}
return {
defaultWorkflow: undefined as Workflow | undefined,
defaultStep: undefined as WorkflowStep | undefined,
stepsForWorkflow: [] as Array<{
id: string;
title: string;
events: WorkflowStep["events"];
}>,
};
}, [workflows, steps]);
const { defaultWorkflow, defaultStep, stepsForWorkflow } = launchData;
const dialog = useMemo(() => (issue ? buildDialogState(issue) : null), [issue]);
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 }}
onSuccess={handleSuccess}
/>
);
}