70 lines
2.0 KiB
TypeScript
70 lines
2.0 KiB
TypeScript
import type { DockviewApi } from "dockview-react";
|
|
|
|
export type SessionPanelActivationArgs = {
|
|
sessionPanelExistedBefore: boolean;
|
|
prevTaskId: string | null;
|
|
prevSessionId: string | null;
|
|
currentTaskId: string | null;
|
|
currentSessionId: string;
|
|
currentActivePanelId: string | null;
|
|
workflowFocusRequested?: boolean;
|
|
};
|
|
|
|
export function shouldPreserveActivePanel(
|
|
sessionPanelExistedBefore: boolean,
|
|
activePanelId: string | null,
|
|
): boolean {
|
|
return !sessionPanelExistedBefore && !!activePanelId && activePanelId !== "chat";
|
|
}
|
|
|
|
export function isChatPlaceholderSelected(api: DockviewApi): boolean {
|
|
const chatPanel = api.getPanel("chat");
|
|
return chatPanel?.group.activePanel?.id === chatPanel?.id;
|
|
}
|
|
|
|
export function activateChatReplacement(
|
|
api: DockviewApi,
|
|
effectiveSessionId: string,
|
|
shouldActivateReplacement: boolean,
|
|
): void {
|
|
if (!shouldActivateReplacement) return;
|
|
api.getPanel(`session:${effectiveSessionId}`)?.api.setActive();
|
|
}
|
|
|
|
export function restorePreservedActivePanel(
|
|
api: DockviewApi,
|
|
panelId: string | null,
|
|
shouldRestore: boolean,
|
|
): void {
|
|
if (!shouldRestore || !panelId) return;
|
|
api.getPanel(panelId)?.api.setActive();
|
|
}
|
|
|
|
export function shouldActivateSessionPanel(args: SessionPanelActivationArgs): boolean {
|
|
const {
|
|
sessionPanelExistedBefore,
|
|
prevTaskId,
|
|
prevSessionId,
|
|
currentTaskId,
|
|
currentSessionId,
|
|
currentActivePanelId,
|
|
workflowFocusRequested = false,
|
|
} = args;
|
|
const sessionPanelId = `session:${currentSessionId}`;
|
|
if (workflowFocusRequested) return true;
|
|
if (!sessionPanelExistedBefore) {
|
|
return (
|
|
!currentActivePanelId ||
|
|
currentActivePanelId === "chat" ||
|
|
currentActivePanelId === sessionPanelId
|
|
);
|
|
}
|
|
const isFirstMount = prevTaskId === null && prevSessionId === null;
|
|
if (isFirstMount) {
|
|
return !currentActivePanelId || currentActivePanelId === sessionPanelId;
|
|
}
|
|
const taskChanged = prevTaskId !== currentTaskId;
|
|
const sessionChanged = prevSessionId !== null && prevSessionId !== currentSessionId;
|
|
return sessionChanged && !taskChanged;
|
|
}
|