"use client"; import { useRef, type KeyboardEvent } from "react"; import { IconGitPullRequest } from "@tabler/icons-react"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@kandev/ui/dialog"; import { cn } from "@/lib/utils"; import { getPRStatusColor } from "@/components/github/pr-task-icon"; import { openExternalLink } from "@/lib/desktop/external-links"; import type { TaskPR } from "@/lib/types/github"; import type { TaskMR } from "@/lib/types/gitlab"; type TaskPRPickerDialogProps = { open: boolean; onOpenChange: (open: boolean) => void; prs: TaskPR[]; mrs?: TaskMR[]; }; /** * Picker shown by the open-task-PR shortcut when a task has several linked * PRs. A scrollable list of rows; ArrowUp/ArrowDown move focus (wrapping), * Enter or click opens the focused PR on GitHub and closes the dialog. */ export function TaskPRPickerDialog({ open, onOpenChange, prs, mrs = [] }: TaskPRPickerDialogProps) { const listRef = useRef(null); const openPR = (pr: TaskPR) => { void openExternalLink(pr.pr_url).catch(() => undefined); onOpenChange(false); }; const openMR = (mr: TaskMR) => { void openExternalLink(mr.mr_url).catch(() => undefined); onOpenChange(false); }; const onListKeyDown = (e: KeyboardEvent) => { if (e.key !== "ArrowDown" && e.key !== "ArrowUp") return; e.preventDefault(); const rows = Array.from( listRef.current?.querySelectorAll("button[data-pr-row]") ?? [], ); if (rows.length === 0) return; const idx = rows.findIndex((row) => row === document.activeElement); const delta = e.key === "ArrowDown" ? 1 : -1; if (idx === -1) { rows[delta === 1 ? 0 : rows.length - 1].focus(); return; } rows[(idx + delta + rows.length) % rows.length].focus(); }; // Focus the first PR row explicitly on open instead of relying on Radix's // implicit first-focusable ordering, so keyboard flow (ArrowDown/Enter) // stays stable if the dialog template's DOM order changes. const focusFirstRow = (e: Event) => { e.preventDefault(); listRef.current?.querySelector("button[data-pr-row]")?.focus(); }; return ( {mrs.length ? "Open code review" : "Open pull request"} {mrs.length ? "Choose a linked pull request or merge request to open at its provider." : `This task has ${prs.length} linked pull requests. Choose one to open on GitHub.`}
{prs.map((pr) => ( ))} {mrs.map((mr) => ( ))}
); }