"use client";
import { useEffect, useState, type ReactNode } from "react";
import { IconChevronDown, IconEdit, IconInfoCircle, IconRefresh } from "@tabler/icons-react";
import { Button } from "@kandev/ui/button";
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@kandev/ui/collapsible";
import { Label } from "@kandev/ui/label";
import { Popover, PopoverContent, PopoverTrigger } from "@kandev/ui/popover";
import { Switch } from "@kandev/ui/switch";
import { Tooltip, TooltipContent, TooltipTrigger } from "@kandev/ui/tooltip";
import { useResponsiveBreakpoint } from "@/hooks/use-responsive-breakpoint";
import { autoFixRoundForState } from "@/lib/github/ci-automation";
import type {
TaskCIAutomationPatch,
TaskCIPRAutomationState,
TaskPR,
TaskPRAutomationOptions,
} from "@/lib/types/github";
import { useTranslation } from "react-i18next";
/**
* Element-id scope unique per (task, repository, PR) so simultaneously
* mounted PR tabs in MultiPRCIPopover never share a Switch id/htmlFor pair.
*/
export function prAutomationScopeKey(pr: TaskPR): string {
return `${pr.task_id}-${pr.repository_id ?? "none"}-${pr.pr_number}`;
}
export function CIAutomationInfoButton() {
const { t } = useTranslation();
return (
{t("github:watchesThisTaskSLinkedPull")}
);
}
export function CIAutomationRow({
id,
label,
checked,
disabled,
onCheckedChange,
help,
describedBy,
}: {
id: string;
label: string;
checked: boolean;
disabled: boolean;
onCheckedChange: (checked: boolean) => void;
help?: ReactNode;
describedBy?: string;
}) {
const { isFinePointer, isMobile } = useResponsiveBreakpoint();
const minHeight = isMobile || !isFinePointer ? "min-h-11" : "min-h-7";
return (
);
}
export function CIAutomationErrorRow({
error,
loading,
onRetry,
}: {
error: string;
loading: boolean;
onRetry: () => void;
}) {
const { t } = useTranslation();
return (
{error}
);
}
export function CIAutomationHelpButton({
ariaLabel,
testId,
children,
}: {
ariaLabel: string;
testId: string;
children: ReactNode;
}) {
const { isFinePointer } = useResponsiveBreakpoint();
const [open, setOpen] = useState(false);
const trigger = (
);
if (!isFinePointer) {
return (
{trigger}
{children}
);
}
return (
{trigger}
{children}
);
}
function CIAutoFixRoundHelpButton({
state,
maxRounds,
}: {
state: TaskCIPRAutomationState | undefined;
maxRounds: number | null | undefined;
}) {
const { t } = useTranslation();
const round = autoFixRoundForState(state, maxRounds);
return (
{t("github:autoFixRoundExplanation", { current: round.current, max: round.max })}
);
}
function PRAgentPromptRows({
scopeKey,
prOptions,
disabled,
patchOption,
}: {
scopeKey: string;
prOptions: TaskPRAutomationOptions;
disabled: boolean;
patchOption: (patch: TaskCIAutomationPatch) => void;
}) {
const { t } = useTranslation();
const terminalHelpID = `task-pr-terminal-help-${scopeKey}`;
const terminalHelp = t("github:wakeAgentOnTerminalPrState");
return (
<>
{terminalHelp}
patchOption({ prompt_on_merged: checked })}
help={
{terminalHelp}
}
/>
patchOption({ prompt_on_closed: checked })}
/>
>
);
}
export function ReviewFollowUpSection({
scopeKey,
prOptions,
disabled,
patchOption,
}: {
scopeKey: string;
prOptions: TaskPRAutomationOptions;
disabled: boolean;
patchOption: (patch: TaskCIAutomationPatch) => void;
}) {
const { t } = useTranslation();
const { isFinePointer, isMobile } = useResponsiveBreakpoint();
const [open, setOpen] = useState(false);
const lifecycleEnabled =
prOptions.prompt_on_review_requested ||
prOptions.prompt_on_merged ||
prOptions.prompt_on_closed;
const minHeight = isMobile || !isFinePointer ? "min-h-11" : "min-h-7";
useEffect(() => {
if (lifecycleEnabled) setOpen(true);
}, [lifecycleEnabled]);
return (
);
}
function ReviewRequestedPromptRow({
scopeKey,
prOptions,
disabled,
patchOption,
}: {
scopeKey: string;
prOptions: TaskPRAutomationOptions;
disabled: boolean;
patchOption: (patch: TaskCIAutomationPatch) => void;
}) {
const { t } = useTranslation();
const helpID = `task-pr-review-requested-prompt-${scopeKey}-description`;
const help = t("github:wakeAgentOnReviewRequest");
return (
<>
{help}
patchOption({ prompt_on_review_requested: checked })}
help={
{help}
}
/>
>
);
}
export function CIAutomationHeader({
pr,
disabled,
onEditPrompt,
}: {
pr: TaskPR;
disabled: boolean;
onEditPrompt: () => void;
}) {
const { t } = useTranslation();
return (
{t("github:automation")}
{t("github:automationForPr", { number: pr.pr_number })}
);
}
export function CIAutomationOptionRows({
pr,
prOptions,
autoFixMaxRounds,
disabled,
patchOption,
automationState,
}: {
pr: TaskPR;
prOptions: TaskPRAutomationOptions;
autoFixMaxRounds: number | null | undefined;
disabled: boolean;
patchOption: (patch: TaskCIAutomationPatch) => void;
automationState: TaskCIPRAutomationState | undefined;
}) {
const { t } = useTranslation();
const scopeKey = prAutomationScopeKey(pr);
return (
<>
patchOption({ auto_fix_enabled: checked })}
help={
prOptions.auto_fix_enabled ? (
) : null
}
/>
patchOption({ auto_merge_enabled: checked })}
/>
>
);
}