"use client";
import { memo, type ReactNode } from "react";
import Link from "@/components/routing/app-link";
import { IconBug, IconCircleDot } from "@tabler/icons-react";
import { Button } from "@kandev/ui/button";
import { Tooltip, TooltipContent, TooltipTrigger } from "@kandev/ui/tooltip";
import { PageTopbar, type ParentCrumb } from "@/components/page-topbar";
import { useOfficeProject } from "@/hooks/use-office-workspace-data";
import { TaskTopBarTitle } from "@/components/task/task-top-bar-title";
import { EditorsMenu } from "@/components/task/editors-menu";
import { LayoutPresetSelector } from "@/components/task/layout-preset-selector";
import { DocumentControls } from "@/components/task/document/document-controls";
import { PRTopbarButton } from "@/components/github/pr-topbar-button";
import { MRTopbarButton } from "@/components/gitlab/mr-topbar-button";
import { JiraTicketButton, extractJiraKey } from "@/components/jira/jira-ticket-button";
import { LinearIssueButton, extractLinearKey } from "@/components/linear/linear-issue-button";
import { useJiraAvailable } from "@/hooks/domains/jira/use-jira-availability";
import { useLinearAvailable } from "@/hooks/domains/linear/use-linear-availability";
import { PortForwardButton } from "@/components/task/port-forward-dialog";
import { ExecutorSettingsButton } from "@/components/task/executor-settings-button";
import { TaskUnarchiveButton } from "@/components/task/task-unarchive-button";
import { TaskAssigneeControl } from "@/components/task/task-assignee-control";
import { WorkflowStepper, type WorkflowStepperStep } from "@/components/task/workflow-stepper";
import { TaskTopBarPluginActions } from "@/components/task/task-top-bar-plugin-actions";
import { TopbarMetrics } from "@/components/system-metrics/topbar-metrics";
import { RegisteredChangeRequestStatus } from "@/components/integrations/registered-change-request-status";
import { isDebugUI } from "@/lib/config";
import { useTranslation } from "react-i18next";
type TaskTopBarProps = {
taskId?: string | null;
activeSessionId?: string | null;
taskTitle?: string;
/** `owner/repo` (or the repository name) of the task's primary repository. */
repositoryLabel?: string | null;
showDebugOverlay?: boolean;
onToggleDebugOverlay?: () => void;
workflowSteps?: WorkflowStepperStep[];
currentStepId?: string | null;
workflowId?: string | null;
workspaceId?: string | null;
projectId?: string | null;
issueUrl?: string;
issueNumber?: number;
isArchived?: boolean;
embeddedVscodeSupported?: boolean;
remoteExecutorType?: string | null;
officeTaskHref?: string | null;
onTaskUnarchived?: (taskId: string) => void;
onMoveStart?: () => void;
onMoveError?: (error: unknown) => void;
};
const TaskTopBar = memo(function TaskTopBar({
taskId,
activeSessionId,
taskTitle,
repositoryLabel,
showDebugOverlay,
onToggleDebugOverlay,
workflowSteps,
currentStepId,
workflowId,
workspaceId,
projectId,
isArchived,
embeddedVscodeSupported,
issueUrl,
issueNumber,
remoteExecutorType,
officeTaskHref,
onTaskUnarchived,
onMoveStart,
onMoveError,
}: TaskTopBarProps) {
const { t } = useTranslation();
// Projects only exist for office-owned tasks, so kanban-mode tasks render no
// ancestry trail at all.
const project = useOfficeProject(projectId);
const showExecutorSettings =
!isArchived && shouldShowExecutorEnvironmentControls(remoteExecutorType);
const parents = buildTaskCrumbs(project, repositoryLabel);
return (
}
parents={parents}
// This bar is desktop-only (the mobile session bar owns phones), so the
// phone-only home crumb and status trigger have no surface here.
homeAffordance="none"
showStatusTrigger={false}
leftActions={
showExecutorSettings ? (
) : undefined
}
center={
workflowSteps && workflowSteps.length > 0 ? (
) : undefined
}
// The stepper handles its own truncation (`w-full min-w-0 overflow-hidden`),
// so the center zone may shrink instead of pushing chrome out of the bar.
centerClassName="min-w-0 shrink"
actions={
}
/>
);
});
/**
* Ancestry crumbs for the open task: its office project (when it has one) and
* then its repository, closest to the title. The repository crumb carries no
* `href` on purpose: it orients ("this task is in kdlbs/kandev") rather than
* navigating, since there is no repository route to land on.
*
* A multi-repository task names its primary repository only, which is the same
* one the rest of the page (branch pickers, Changes) treats as primary.
*/
function buildTaskCrumbs(
project: { id: string; name: string } | null | undefined,
repositoryLabel: string | null | undefined,
): ParentCrumb[] | undefined {
const crumbs: ParentCrumb[] = [];
if (project) crumbs.push({ label: project.name, href: `/office/projects/${project.id}` });
if (repositoryLabel) crumbs.push({ label: repositoryLabel });
return crumbs.length > 0 ? crumbs : undefined;
}
// IssueTrackerButtons picks the right ticket status button for a task whose
// title already carries an external issue key. Jira and Linear use the same
// TEAM-NUMBER identifier shape, so both `extract` calls would match
// "ENG-123" — we resolve ambiguity by preferring whichever integration is
// currently available for the workspace, with Jira winning the tie-break since
// it shipped first. Creating new links lives in the task Link menu.
function IssueTrackerButtons({
workspaceId,
taskTitle,
}: {
workspaceId: string | null | undefined;
taskTitle: string | null | undefined;
}) {
const jiraAvailable = useJiraAvailable(workspaceId);
const linearAvailable = useLinearAvailable(workspaceId);
const jiraKey = extractJiraKey(taskTitle);
const linearKey = extractLinearKey(taskTitle);
if (jiraKey && jiraAvailable) {
return ;
}
if (linearKey && linearAvailable) {
return ;
}
return null;
}
function TopbarCluster({
label,
className = "",
children,
}: {
label: string;
className?: string;
children: ReactNode;
}) {
return (
{children}
);
}
function DebugOverlayToggle({
showDebugOverlay,
onToggleDebugOverlay,
}: {
showDebugOverlay?: boolean;
onToggleDebugOverlay: () => void;
}) {
const { t } = useTranslation();
const label = showDebugOverlay ? t("task:hideDebugInfo") : t("task:showDebugInfo");
return (
{label}
);
}
function AttentionStatusGroup({
taskId,
activeSessionId,
isArchived,
workspaceId,
taskTitle,
issueUrl,
issueNumber,
}: {
taskId?: string | null;
activeSessionId?: string | null;
isArchived?: boolean;
workspaceId?: string | null;
taskTitle?: string;
issueUrl?: string;
issueNumber?: number;
}) {
const { t } = useTranslation();
return (
{!isArchived && (
<>
{/* PR (GitHub) and MR (GitLab) buttons each render nothing when no
rows match, so showing both covers GitHub-only, GitLab-only, and
multi-repo tasks without needing an explicit provider switch. */}
>
)}
);
}
function GitHubIssueTopbarButton({
issueUrl,
issueNumber,
}: {
issueUrl?: string;
issueNumber?: number;
}) {
const { t } = useTranslation();
if (!issueUrl) return null;
const label = issueNumber ? `#${issueNumber}` : t("task:issue");
const tooltip = issueNumber
? t("task:githubIssueNumbered", { number: issueNumber })
: t("task:githubIssue2");
return (
{tooltip}
);
}
function TopbarToolsGroup({
activeSessionId,
showDebugOverlay,
onToggleDebugOverlay,
isArchived,
embeddedVscodeSupported,
}: {
activeSessionId?: string | null;
showDebugOverlay?: boolean;
onToggleDebugOverlay?: () => void;
isArchived?: boolean;
embeddedVscodeSupported?: boolean;
}) {
const { t } = useTranslation();
const showDebugToggle = isDebugUI() && onToggleDebugOverlay;
return (
{!isArchived && (
<>
>
)}
{showDebugToggle && (
)}
);
}
/** Right section: status/attention + tools rendered inline.
* The former overflow popover was removed in the UI overhaul — every cluster
* is always visible so users don't have to discover the dots menu. */
function TopBarRight({
taskId,
activeSessionId,
showDebugOverlay,
onToggleDebugOverlay,
isArchived,
workspaceId,
embeddedVscodeSupported,
taskTitle,
issueUrl,
issueNumber,
officeTaskHref,
onTaskUnarchived,
}: {
taskId?: string | null;
activeSessionId?: string | null;
showDebugOverlay?: boolean;
onToggleDebugOverlay?: () => void;
isArchived?: boolean;
workspaceId?: string | null;
embeddedVscodeSupported?: boolean;
taskTitle?: string;
issueUrl?: string;
issueNumber?: number;
officeTaskHref?: string | null;
onTaskUnarchived?: (taskId: string) => void;
}) {
const { t } = useTranslation();
return (