* perf(app): retain inactive workspace screens App-wide routes cleared the active workspace selection, which removed the retained workspace deck from the React tree and made returning rebuild every panel. Keep mounted deck entries inactive across those routes and suspend inactive panel subscriptions so state is preserved without background work. * perf(app): suspend remaining inactive queries Retained composers and commit diff panels still subscribed through query hooks that did not receive panel activity. Propagate the existing activity gate so app-wide routes leave those hidden surfaces dormant. * refactor(app): separate panel activity from connectivity A hidden retained panel is still connected to its host. Keep connectivity truthful, pause work inside query and stream owners, and preserve terminal emulator identity while its stream is detached. * test(app): target active retained controls * fix(app): retain compact and terminal route content * fix(app): hide inactive workspace overlays
125 lines
4.4 KiB
TypeScript
125 lines
4.4 KiB
TypeScript
import { useCallback } from "react";
|
|
import { useQuery } from "@tanstack/react-query";
|
|
import { useTranslation } from "react-i18next";
|
|
import { Terminal } from "lucide-react-native";
|
|
import { Text, View } from "react-native";
|
|
import invariant from "tiny-invariant";
|
|
import type { ListTerminalsResponse } from "@getpaseo/protocol/messages";
|
|
import { deriveTerminalActivityStatusBucket } from "@getpaseo/protocol/terminal-activity";
|
|
import { TerminalPane } from "@/components/terminal-pane";
|
|
import { usePaneContext, usePaneFocus } from "@/panels/pane-context";
|
|
import type { PanelDescriptor, PanelRegistration } from "@/panels/panel-registry";
|
|
import { queryClient } from "@/data/query-client";
|
|
import { buildTerminalsQueryKey } from "@/screens/workspace/terminals/state";
|
|
import { usePanelStore } from "@/stores/panel-store";
|
|
import { useSessionStore } from "@/stores/session-store";
|
|
import { useWorkspaceDirectory, useWorkspaceFields } from "@/stores/session-store-hooks";
|
|
|
|
type ListTerminalsPayload = ListTerminalsResponse["payload"];
|
|
|
|
const CENTERED_PADDED_STYLE = {
|
|
flex: 1,
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
padding: 16,
|
|
} as const;
|
|
|
|
function trimNonEmpty(value: string | null | undefined): string | null {
|
|
if (typeof value !== "string") {
|
|
return null;
|
|
}
|
|
const trimmed = value.trim();
|
|
return trimmed.length > 0 ? trimmed : null;
|
|
}
|
|
|
|
function useTerminalPanelDescriptor(
|
|
target: { kind: "terminal"; terminalId: string },
|
|
context: { serverId: string; workspaceId: string },
|
|
): PanelDescriptor {
|
|
const { t } = useTranslation();
|
|
const client = useSessionStore((state) => state.sessions[context.serverId]?.client ?? null);
|
|
const workspaceDirectory = useWorkspaceDirectory(context.serverId, context.workspaceId);
|
|
const terminalsQuery = useQuery(
|
|
{
|
|
queryKey: buildTerminalsQueryKey(
|
|
context.serverId,
|
|
workspaceDirectory,
|
|
context.workspaceId || null,
|
|
),
|
|
enabled: Boolean(client && workspaceDirectory),
|
|
queryFn: async (): Promise<ListTerminalsPayload> => {
|
|
if (!client || !workspaceDirectory) {
|
|
throw new Error("Workspace directory not found");
|
|
}
|
|
return client.listTerminals(workspaceDirectory, undefined, {
|
|
workspaceId: context.workspaceId || undefined,
|
|
});
|
|
},
|
|
staleTime: 5_000,
|
|
},
|
|
queryClient,
|
|
);
|
|
const terminal =
|
|
terminalsQuery.data?.terminals.find((entry) => entry.id === target.terminalId) ?? null;
|
|
const label =
|
|
trimNonEmpty(terminal?.title ?? terminal?.name ?? null) ??
|
|
t("workspace.tabs.fallback.terminal");
|
|
|
|
return {
|
|
label,
|
|
subtitle: t("workspace.tabs.fallback.terminal"),
|
|
tooltip: label,
|
|
titleState: "ready",
|
|
icon: Terminal,
|
|
statusBucket: deriveTerminalActivityStatusBucket(terminal?.activity),
|
|
};
|
|
}
|
|
|
|
function TerminalPanel() {
|
|
const { serverId, workspaceId, target, openFileInWorkspace } = usePaneContext();
|
|
const { isWorkspaceFocused, isPaneFocused } = usePaneFocus();
|
|
const workspaceFields = useWorkspaceFields(serverId, workspaceId, (w) => ({
|
|
workspaceDirectory: w.workspaceDirectory,
|
|
isGitCheckout: w.projectKind === "git",
|
|
}));
|
|
const workspaceDirectory = workspaceFields?.workspaceDirectory || null;
|
|
const isGitCheckout = workspaceFields?.isGitCheckout ?? false;
|
|
const openFileExplorerForCheckout = usePanelStore((state) => state.openFileExplorerForCheckout);
|
|
const handleOpenFileExplorer = useCallback(() => {
|
|
if (!workspaceDirectory) {
|
|
return;
|
|
}
|
|
openFileExplorerForCheckout({
|
|
isCompact: true,
|
|
checkout: { serverId, cwd: workspaceDirectory, isGit: isGitCheckout },
|
|
});
|
|
}, [isGitCheckout, openFileExplorerForCheckout, serverId, workspaceDirectory]);
|
|
invariant(target.kind === "terminal", "TerminalPanel requires terminal target");
|
|
|
|
if (!workspaceDirectory) {
|
|
return (
|
|
<View style={CENTERED_PADDED_STYLE}>
|
|
<Text>Workspace directory not found.</Text>
|
|
</View>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<TerminalPane
|
|
serverId={serverId}
|
|
cwd={workspaceDirectory}
|
|
terminalId={target.terminalId}
|
|
isWorkspaceFocused={isWorkspaceFocused}
|
|
isPaneFocused={isPaneFocused}
|
|
onOpenFileExplorer={handleOpenFileExplorer}
|
|
onOpenWorkspaceFile={openFileInWorkspace}
|
|
/>
|
|
);
|
|
}
|
|
|
|
export const terminalPanelRegistration: PanelRegistration<"terminal"> = {
|
|
kind: "terminal",
|
|
component: TerminalPanel,
|
|
useDescriptor: useTerminalPanelDescriptor,
|
|
};
|