))}
{canLink ? (
<>
{t("gitlab:linkAnotherMergeRequest")}
>
) : null}
);
}
function useMRDesktopReviewOpener(mobile: boolean) {
const addMRPanel = useDockviewStore((state) => state.addMRPanel);
const dockviewReady = useDockviewStore((state) => state.api !== null);
const isRestoringLayout = useDockviewStore((state) => state.isRestoringLayout);
// Not restoring right now is necessary but NOT sufficient. Navigating
// straight to /t/:id runs dockview's `onReady` while the session→env mapping
// is still hydrating, so it builds a throwaway DEFAULT layout with a null env
// (see tryRestoreLayout) and `isRestoringLayout` is false the whole time.
// `switchEnvLayout` only replaces that layout wholesale via `api.fromJSON`
// once the env arrives, discarding any panel added in the gap — the user
// clicks the MR button during session load and nothing ever opens. The env
// being adopted is what marks the layout as actually settled.
const envAdopted = useDockviewStore((state) => state.currentLayoutEnvId !== null);
const layoutSettled = envAdopted && !isRestoringLayout;
const activeSessionId = useAppStore((state) => state.tasks.activeSessionId);
const setMobileSessionReview = useAppStore((state) => state.setMobileSessionReview);
const [pendingDesktopMR, setPendingDesktopMR] = useState(null);
useEffect(() => {
if (!dockviewReady || !layoutSettled || !pendingDesktopMR) return;
// Only re-apply once the layout has actually settled: at that point no
// switchEnvLayout restore can land afterwards and discard this add. The
// intent is cleared here so a later user-initiated layout change (preset
// switch, maximize) cannot resurrect a panel the user has since closed.
openDesktopMRReview(addMRPanel, pendingDesktopMR);
setPendingDesktopMR(null);
}, [addMRPanel, dockviewReady, layoutSettled, pendingDesktopMR]);
return (mr: TaskMR) => {
if (mobile) {
if (activeSessionId) openMobileMRReview(setMobileSessionReview, activeSessionId, mr);
return;
}
// Open straight away when we can, so the panel appears on click rather
// than after the session finishes loading — but never into a layout that
// is being replaced right now. Retain the intent until dockview exists AND
// the env layout has settled: `currentLayoutEnvId` outlives `setApi(null)`,
// so a stale env id can leave `layoutSettled` true while dockview is still
// remounting, and dropping the intent there would lose the click entirely.
if (dockviewReady && !isRestoringLayout) openDesktopMRReview(addMRPanel, mr);
if (!dockviewReady || !layoutSettled) setPendingDesktopMR(mr);
};
}
function MRMenuButton({
taskId,
mrs,
canLink,
compact,
mobile,
onLink,
onUnlink,
}: {
taskId: string;
mrs: TaskMR[];
canLink: boolean;
compact: boolean;
mobile: boolean;
onLink: () => void;
onUnlink: (associationId: string) => void;
}) {
const single = mrs.length === 1 ? mrs[0] : null;
const openReview = useMRDesktopReviewOpener(mobile);
const {
usesTouchDrawer,
open: popoverOpen,
onOpenChange: onPopoverOpenChange,
onTriggerEnter,
onTriggerLeave,
onContentEnter,
onContentLeave,
} = useMRPopoverInteractions();
// Hover preview only makes sense for the single-MR case (MRCIPopover takes
// one TaskMR); a multi-MR aggregate popover is out of scope (§9). On touch
// there is no hover at all, so the popover apparatus is skipped entirely —
// that path keeps the click-driven dropdown unchanged, below.
const showHoverPreview = single != null && !usesTouchDrawer;
if (showHoverPreview) {
// Single MR, desktop: click opens the MR detail panel directly (mirrors
// GitHub's PRSingleButton) — no intermediate dropdown. Hovering shows a
// popover with everything else: link/open-in-GitLab/unlink, CI + review
// summary, automation controls, and a merge action when ready.
return (
{
onPopoverOpenChange(false);
openReview(single);
}}
/>
e.preventDefault()}
>
openReview(single)}
onLink={onLink}
onUnlink={() => onUnlink(single.id)}
/>
);
}
return (
);
}
function MRMenuTriggerButtonInDropdown({
single,
count,
compact,
mobile,
}: {
single: TaskMR | null;
count: number;
compact: boolean;
mobile: boolean;
}) {
return (
);
}
const EMPTY_REPOSITORIES: Repository[] = [];
const EMPTY_TASK_REPOSITORIES: Array<{ repository_id: string }> = [];
function MRTopbarControl({
taskId,
mrs,
gitlabAvailable,
compact,
mobile,
onLink,
onUnlink,
}: {
taskId: string;
mrs: TaskMR[];
gitlabAvailable: boolean;
compact: boolean;
mobile: boolean;
onLink: () => void;
onUnlink: (associationId: string) => void;
}) {
if (mrs.length > 0) {
return (
);
}
return null;
}
export const MRTopbarButton = memo(function MRTopbarButton({
compact = false,
mobile = false,
}: {
compact?: boolean;
mobile?: boolean;
}) {
const [linkOpen, setLinkOpen] = useState(false);
const activeTaskId = useAppStore((s) => s.tasks.activeTaskId);
const workspaceId = useAppStore((s) => s.workspaces.activeId);
const repositories = useAppStore((state) =>
workspaceId
? (state.repositories.itemsByWorkspaceId[workspaceId] ?? EMPTY_REPOSITORIES)
: EMPTY_REPOSITORIES,
);
const task = useTaskById(activeTaskId);
useWorkspaceMRs(workspaceId);
const mrs = useTaskMRs(activeTaskId);
const gitlabAvailable = useGitLabAvailable();
const unlink = useUnlinkTaskMR(workspaceId);
if (!activeTaskId || !workspaceId) return null;
return (
<>
setLinkOpen(true)}
onUnlink={(associationId) => void unlink(associationId)}
/>
>
);
});