"use client";
import { memo, useCallback, useEffect, useLayoutEffect, useRef, useState } from "react";
import { SessionMobileTopBar } from "./session-mobile-top-bar";
import { SessionMobileBottomNav } from "./session-mobile-bottom-nav";
import { SessionTaskSwitcherSheet } from "./session-task-switcher-sheet";
import { MobileFileViewerPanel } from "./mobile-file-viewer-panel";
import { TaskChatPanel } from "../task-chat-panel";
import { TaskPlanPanel } from "../task-plan-panel";
import { MobileChangesPanel } from "./mobile-changes-panel";
import { SessionMobileReviewDialog } from "./session-mobile-review-dialog";
import { TaskFilesPanel } from "../task-files-panel";
import { PassthroughToolbar } from "../passthrough-toolbar";
import { MobileTerminalKeybar, KEYBAR_HEIGHT_PX } from "./mobile-terminal-keybar";
import { MobileTerminalPane } from "./mobile-terminal-pane";
import { MobileSessionsPicker } from "./mobile-sessions-section";
import { SessionPanelContent } from "@kandev/ui/pannel-session";
import { useSessionLayoutState } from "@/hooks/use-session-layout-state";
import { useVisualViewportOffset } from "@/hooks/use-visual-viewport-offset";
import { useToast } from "@/components/toast-provider";
import { useAppStatusDrawer } from "@/components/app-status-bar/app-status-surface-provider";
import { fetchAndOpenFile } from "../file-browser-hooks";
import { useMobileMRSelection } from "./use-mobile-mr-selection";
import type { MobileSessionPanel } from "@/lib/state/slices/ui/types";
import type { OpenFileTab } from "@/lib/types/backend";
import { useReviewPRSelection } from "@/hooks/domains/github/use-review-pr-selection";
import { PRDetailPanelComponent } from "@/components/github/pr-detail-panel";
import { prTaskKey } from "@/components/github/pr-utils";
import { ReviewPRSelector } from "@/components/review/review-pr-selector";
import { MRDetailPanelComponent, mrTaskKey } from "@/components/gitlab/mr-detail-panel";
import { PluginTaskPanel } from "../plugin-task-panel";
import { parsePluginPanelId } from "@/lib/state/layout-manager/plugin-panels";
import { useEffectiveMobilePanel, type MobileReviewSource } from "./mobile-plugin-panel-lifecycle";
import { useTranslation } from "react-i18next";
export { resolveMobilePluginPanel } from "./mobile-plugin-panel-lifecycle";
function useMobilePanelChangeHandler(
reviewSource: MobileReviewSource,
handlePanelChangeAndClearSheet: (panel: MobileSessionPanel) => void,
handleReviewPanelChange: (panel: MobileSessionPanel) => void,
) {
return useCallback(
(panel: MobileSessionPanel) => {
if (panel === "review" && reviewSource === "github") {
handlePanelChangeAndClearSheet(panel);
return;
}
handleReviewPanelChange(panel);
},
[handlePanelChangeAndClearSheet, handleReviewPanelChange, reviewSource],
);
}
export function resolveMobileReviewSource(
hasGitHubPR: boolean,
hasGitLabMR: boolean,
): MobileReviewSource {
if (hasGitHubPR) return "github";
if (hasGitLabMR) return "gitlab";
return null;
}
const TOP_NAV_HEIGHT = "3.5rem";
const BOTTOM_NAV_HEIGHT = "3.25rem";
type SessionMobileLayoutProps = {
workspaceId: string | null;
workflowId: string | null;
sessionId?: string | null;
baseBranch?: string;
worktreeBranch?: string | null;
taskTitle?: string;
isRemoteExecutor?: boolean;
remoteExecutorType?: string | null;
remoteExecutorName?: string | null;
remoteState?: string | null;
remoteCreatedAt?: string | null;
remoteCheckedAt?: string | null;
remoteStatusError?: string | null;
isArchived?: boolean;
};
function MobileChatPanelContent({
activeTaskId,
isPassthroughMode,
effectiveSessionId,
onOpenFile,
}: {
activeTaskId: string | null;
isPassthroughMode: boolean;
effectiveSessionId: string | null;
onOpenFile: (path: string, repo?: string) => void;
}) {
const { t } = useTranslation();
if (!activeTaskId) {
return (
{t("task:noTaskSelected")}
);
}
return (
{isPassthroughMode ? (
) : (
)}
);
}
type MobilePanelAreaProps = {
currentMobilePanel: MobileSessionPanel;
activeTaskId: string | null;
isPassthroughMode: boolean;
effectiveSessionId: string | null;
selectedFile: OpenFileTab | null;
selectedFilePreview: boolean;
selectedDiff: { path: string; content?: string } | null;
handleOpenFileFromChat: (path: string, repo?: string, preview?: boolean) => void;
handleClearSelectedDiff: () => void;
handleOpenFile: (file: OpenFileTab) => void;
handlePanelChangeAndClearSheet: (panel: MobileSessionPanel) => void;
topNavHeight: string;
bottomNavHeight: string;
reviewSource: MobileReviewSource;
mrKey?: string;
prKey?: string;
reviewPRs: ReturnType["prs"];
selectedReviewPR: ReturnType["selectedPR"];
onSelectReviewPR: ReturnType["selectPR"];
};
/** Keeps terminal content's visible bottom glued to the keybar top. When the
* keyboard is up, the content area already pads for the bottom nav (which
* is now under the keyboard), so we subtract it back out and add the
* keyboard height instead. */
export function terminalPaddingBottom(
keyboardOpen: boolean,
bottomOffset: number,
bottomNavHeight: string,
): string {
return keyboardOpen
? `calc(${bottomOffset + KEYBAR_HEIGHT_PX}px - ${bottomNavHeight} - env(safe-area-inset-bottom, 0px))`
: `${KEYBAR_HEIGHT_PX}px`;
}
export function MobilePanelArea({
currentMobilePanel,
activeTaskId,
isPassthroughMode,
effectiveSessionId,
selectedFile,
selectedFilePreview,
selectedDiff,
handleOpenFileFromChat,
handleClearSelectedDiff,
handleOpenFile,
handlePanelChangeAndClearSheet,
topNavHeight,
bottomNavHeight,
reviewSource,
mrKey,
prKey,
reviewPRs,
selectedReviewPR,
onSelectReviewPR,
}: MobilePanelAreaProps) {
const { keyboardOpen, bottomOffset } = useVisualViewportOffset();
const terminalPadding = terminalPaddingBottom(keyboardOpen, bottomOffset, bottomNavHeight);
return (
{currentMobilePanel === "chat" && (
)}
{currentMobilePanel === "plan" && (
)}
{currentMobilePanel === "changes" && (
)}
{currentMobilePanel === "files" && (
{selectedFile ? (
handlePanelChangeAndClearSheet("files")}
/>
) : (
)}
)}
{currentMobilePanel === "terminal" && (
)}
{currentMobilePanel === "review" && reviewSource === "github" && prKey && (
)}
{currentMobilePanel === "review" && reviewSource === "gitlab" && mrKey && (
)}
);
}
/**
* Renders a mobile-enabled plugin task panel (AC7) — `currentMobilePanel` is
* a `plugin::` id from `SessionMobileBottomNav`'s
* plugin-contributed nav entries. A non-plugin id (the common case) is a
* no-op, so this stays a single trailing branch instead of one per plugin.
*/
function MobilePluginPanel({ currentMobilePanel }: { currentMobilePanel: MobileSessionPanel }) {
const parsed = parsePluginPanelId(currentMobilePanel);
if (!parsed) return null;
return (
);
}
function MobileGitHubReviewPanel({
prKey,
reviewPRs,
selectedReviewPR,
onSelectReviewPR,
}: Pick & {
prKey: string;
}) {
return (
);
}
type MobileTopBarStickyProps = {
activeTaskId: string | null;
workspaceId: string | null;
taskTitle?: string;
effectiveSessionId: string | null;
baseBranch?: string;
worktreeBranch?: string | null;
onMenuClick: () => void;
showApproveButton: boolean;
onApprove: () => void;
isRemoteExecutor?: boolean;
remoteExecutorType?: string | null;
remoteExecutorName?: string | null;
remoteState?: string | null;
remoteCreatedAt?: string | null;
remoteCheckedAt?: string | null;
remoteStatusError?: string | null;
isArchived?: boolean;
};
function MobileTopBarSticky(props: MobileTopBarStickyProps) {
return (
);
}
export function useMobilePanelHandlers({
effectiveSessionId,
handlePanelChange,
}: {
effectiveSessionId: string | null;
handlePanelChange: (panel: MobileSessionPanel) => void;
}) {
const { toast } = useToast();
const [selectedFile, setSelectedFile] = useState(null);
const [selectedFilePreview, setSelectedFilePreview] = useState(false);
const [trackedSessionId, setTrackedSessionId] = useState(effectiveSessionId);
const latestRequestIdRef = useRef(0);
const openFileAbortRef = useRef(null);
// Reset viewer when switching sessions — adjust state during render per
// https://react.dev/learn/you-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes
if (trackedSessionId !== effectiveSessionId) {
setTrackedSessionId(effectiveSessionId);
setSelectedFile(null);
setSelectedFilePreview(false);
}
useLayoutEffect(() => {
latestRequestIdRef.current += 1;
openFileAbortRef.current?.abort();
openFileAbortRef.current = null;
}, [effectiveSessionId]);
useEffect(
() => () => {
openFileAbortRef.current?.abort();
openFileAbortRef.current = null;
},
[],
);
const handleOpenFileFromChat = useCallback(
(path: string, repo?: string, preview = false) => {
if (!effectiveSessionId) return;
const requestId = (latestRequestIdRef.current += 1);
openFileAbortRef.current?.abort();
const controller = new AbortController();
openFileAbortRef.current = controller;
void Promise.resolve(
fetchAndOpenFile(
effectiveSessionId,
path,
(file) => {
if (requestId !== latestRequestIdRef.current || controller.signal.aborted) return;
setSelectedFile(file);
setSelectedFilePreview(preview);
handlePanelChange("files");
},
toast,
{ repo, signal: controller.signal },
),
).finally(() => {
if (openFileAbortRef.current === controller) {
openFileAbortRef.current = null;
}
});
},
[effectiveSessionId, handlePanelChange, toast],
);
const handleOpenFile = useCallback(
(file: OpenFileTab) => {
latestRequestIdRef.current += 1;
openFileAbortRef.current?.abort();
openFileAbortRef.current = null;
setSelectedFile(file);
setSelectedFilePreview(false);
handlePanelChange("files");
},
[handlePanelChange],
);
const handlePanelChangeAndClearSheet = useCallback(
(panel: MobileSessionPanel) => {
latestRequestIdRef.current += 1;
openFileAbortRef.current?.abort();
openFileAbortRef.current = null;
setSelectedFile(null);
setSelectedFilePreview(false);
handlePanelChange(panel);
},
[handlePanelChange],
);
return {
selectedFile,
selectedFilePreview,
handleOpenFileFromChat,
handleOpenFile,
handlePanelChangeAndClearSheet,
};
}
type SessionMobileFooterProps = {
sessionId: string | null;
activePanel: MobileSessionPanel;
onPanelChange: (panel: MobileSessionPanel) => void;
planBadge: boolean;
changesBadge: number;
hasReview: boolean;
showStatus: boolean;
onOpenStatus: () => void;
connectionIssueSeverity: import("@/lib/types/connection").ConnectionIssueSeverity;
};
function SessionMobileFooter({
sessionId,
activePanel,
onPanelChange,
planBadge,
changesBadge,
hasReview,
showStatus,
onOpenStatus,
connectionIssueSeverity,
}: SessionMobileFooterProps) {
return (
<>
>
);
}
function StatusAwareSessionMobileFooter(
props: Omit,
) {
const { enabled, issueSeverity, openStatusDrawer } = useAppStatusDrawer();
return (
);
}
export const SessionMobileLayout = memo(function SessionMobileLayout(
props: SessionMobileLayoutProps,
) {
const {
activeTaskId,
effectiveSessionId,
isPassthroughMode,
selectedDiff,
handleClearSelectedDiff,
totalChangesCount,
hasUnseenPlanUpdate,
showApproveButton,
handleApprove,
currentMobilePanel,
handlePanelChange,
isTaskSwitcherOpen,
handleMenuClick,
setMobileSessionTaskSwitcherOpen,
} = useSessionLayoutState({ sessionId: props.sessionId });
const {
selectedFile,
selectedFilePreview,
handleOpenFileFromChat,
handleOpenFile,
handlePanelChangeAndClearSheet,
} = useMobilePanelHandlers({ effectiveSessionId, handlePanelChange });
const mobilePR = useReviewPRSelection(activeTaskId);
const mobileMR = useMobileMRSelection(
activeTaskId,
effectiveSessionId,
currentMobilePanel,
handlePanelChangeAndClearSheet,
mobilePR.prs.length > 0,
);
const reviewSource = resolveMobileReviewSource(mobilePR.prs.length > 0, mobileMR.mrs.length > 0);
const effectiveMobilePanel = useEffectiveMobilePanel(
currentMobilePanel,
reviewSource,
handlePanelChange,
);
const handleMobilePanelChange = useMobilePanelChangeHandler(
reviewSource,
handlePanelChangeAndClearSheet,
mobileMR.handlePanelChange,
);
return (
{/* Task Switcher Sheet */}
);
});