feat(app): retain workspace screens on sidebar switch
This commit is contained in:
@@ -210,13 +210,15 @@ export async function openWorkspaceWithAgents(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function expectWorkspaceTabVisible(page: Page, agentId: string): Promise<void> {
|
export async function expectWorkspaceTabVisible(page: Page, agentId: string): Promise<void> {
|
||||||
await expect(page.getByTestId(`workspace-tab-agent_${agentId}`).first()).toBeVisible({
|
await expect(
|
||||||
timeout: 30_000,
|
page.getByTestId(`workspace-tab-agent_${agentId}`).filter({ visible: true }).first(),
|
||||||
});
|
).toBeVisible({ timeout: 30_000 });
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function expectWorkspaceTabHidden(page: Page, agentId: string): Promise<void> {
|
export async function expectWorkspaceTabHidden(page: Page, agentId: string): Promise<void> {
|
||||||
await expect(page.getByTestId(`workspace-tab-agent_${agentId}`)).toHaveCount(0, {
|
await expect(
|
||||||
|
page.getByTestId(`workspace-tab-agent_${agentId}`).filter({ visible: true }),
|
||||||
|
).toHaveCount(0, {
|
||||||
timeout: 30_000,
|
timeout: 30_000,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,16 +23,18 @@ export async function gotoWorkspace(page: Page, cwd: string): Promise<void> {
|
|||||||
|
|
||||||
/** Wait for the workspace tab bar to be visible. */
|
/** Wait for the workspace tab bar to be visible. */
|
||||||
export async function waitForTabBar(page: Page): Promise<void> {
|
export async function waitForTabBar(page: Page): Promise<void> {
|
||||||
await expect(page.getByTestId("workspace-tabs-row").first()).toBeVisible({
|
await expect(
|
||||||
|
page.getByTestId("workspace-tabs-row").filter({ visible: true }).first(),
|
||||||
|
).toBeVisible({
|
||||||
timeout: 30_000,
|
timeout: 30_000,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Return all tab test IDs currently in the tab bar. */
|
/** Return all tab test IDs currently in the tab bar. */
|
||||||
export async function getTabTestIds(page: Page): Promise<string[]> {
|
export async function getTabTestIds(page: Page): Promise<string[]> {
|
||||||
const tabs = page.locator(
|
const tabs = page
|
||||||
'[data-testid^="workspace-tab-"]:not([data-testid^="workspace-tab-context-"])',
|
.locator('[data-testid^="workspace-tab-"]:not([data-testid^="workspace-tab-context-"])')
|
||||||
);
|
.filter({ visible: true });
|
||||||
const count = await tabs.count();
|
const count = await tabs.count();
|
||||||
const ids: string[] = [];
|
const ids: string[] = [];
|
||||||
for (let i = 0; i < count; i++) {
|
for (let i = 0; i < count; i++) {
|
||||||
@@ -55,6 +57,7 @@ export async function getActiveTabTestId(page: Page): Promise<string | null> {
|
|||||||
.locator(
|
.locator(
|
||||||
'[data-testid^="workspace-tab-"]:not([data-testid^="workspace-tab-context-"])[aria-selected="true"]',
|
'[data-testid^="workspace-tab-"]:not([data-testid^="workspace-tab-context-"])[aria-selected="true"]',
|
||||||
)
|
)
|
||||||
|
.filter({ visible: true })
|
||||||
.first();
|
.first();
|
||||||
if (await activeTab.isVisible().catch(() => false)) {
|
if (await activeTab.isVisible().catch(() => false)) {
|
||||||
return activeTab.getAttribute("data-testid");
|
return activeTab.getAttribute("data-testid");
|
||||||
@@ -67,14 +70,14 @@ export async function getActiveTabTestId(page: Page): Promise<string | null> {
|
|||||||
|
|
||||||
/** Click the new agent tab button in the tab bar. Creates a draft/chat tab directly. */
|
/** Click the new agent tab button in the tab bar. Creates a draft/chat tab directly. */
|
||||||
export async function clickNewTabButton(page: Page): Promise<void> {
|
export async function clickNewTabButton(page: Page): Promise<void> {
|
||||||
const button = page.getByTestId("workspace-new-agent-tab");
|
const button = page.getByTestId("workspace-new-agent-tab").filter({ visible: true }).first();
|
||||||
await expect(button).toBeVisible({ timeout: 10_000 });
|
await expect(button).toBeVisible({ timeout: 10_000 });
|
||||||
await button.click();
|
await button.click();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Click the new terminal button in the workspace tab bar. Creates a terminal tab directly. */
|
/** Click the new terminal button in the workspace tab bar. Creates a terminal tab directly. */
|
||||||
export async function clickNewTerminalButton(page: Page): Promise<void> {
|
export async function clickNewTerminalButton(page: Page): Promise<void> {
|
||||||
const button = page.getByTestId("workspace-new-terminal");
|
const button = page.getByTestId("workspace-new-terminal").filter({ visible: true }).first();
|
||||||
await expect(button).toBeVisible({ timeout: 10_000 });
|
await expect(button).toBeVisible({ timeout: 10_000 });
|
||||||
await button.click();
|
await button.click();
|
||||||
}
|
}
|
||||||
@@ -94,26 +97,30 @@ export async function waitForLauncherPanel(page: Page): Promise<void> {
|
|||||||
|
|
||||||
/** Assert the new agent tab button is visible in the tab bar. */
|
/** Assert the new agent tab button is visible in the tab bar. */
|
||||||
export async function assertNewChatTileVisible(page: Page): Promise<void> {
|
export async function assertNewChatTileVisible(page: Page): Promise<void> {
|
||||||
await expect(page.getByTestId("workspace-new-agent-tab").first()).toBeVisible();
|
await expect(
|
||||||
|
page.getByTestId("workspace-new-agent-tab").filter({ visible: true }).first(),
|
||||||
|
).toBeVisible();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Assert the new terminal button is visible in the tab bar. */
|
/** Assert the new terminal button is visible in the tab bar. */
|
||||||
export async function assertTerminalTileVisible(page: Page): Promise<void> {
|
export async function assertTerminalTileVisible(page: Page): Promise<void> {
|
||||||
await expect(page.getByTestId("workspace-new-terminal").first()).toBeVisible();
|
await expect(
|
||||||
|
page.getByTestId("workspace-new-terminal").filter({ visible: true }).first(),
|
||||||
|
).toBeVisible();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ─── Tab creation actions ─────────────────────────────────────────────────
|
// ─── Tab creation actions ─────────────────────────────────────────────────
|
||||||
|
|
||||||
/** Click the new agent tab button to create a draft/chat tab. */
|
/** Click the new agent tab button to create a draft/chat tab. */
|
||||||
export async function clickNewChat(page: Page): Promise<void> {
|
export async function clickNewChat(page: Page): Promise<void> {
|
||||||
const button = page.getByTestId("workspace-new-agent-tab");
|
const button = page.getByTestId("workspace-new-agent-tab").filter({ visible: true }).first();
|
||||||
await expect(button).toBeVisible({ timeout: 10_000 });
|
await expect(button).toBeVisible({ timeout: 10_000 });
|
||||||
await button.click();
|
await button.click();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Click the new terminal button to create a terminal tab. */
|
/** Click the new terminal button to create a terminal tab. */
|
||||||
export async function clickTerminal(page: Page): Promise<void> {
|
export async function clickTerminal(page: Page): Promise<void> {
|
||||||
const button = page.getByTestId("workspace-new-terminal");
|
const button = page.getByTestId("workspace-new-terminal").filter({ visible: true }).first();
|
||||||
await expect(button).toBeVisible({ timeout: 10_000 });
|
await expect(button).toBeVisible({ timeout: 10_000 });
|
||||||
await button.click();
|
await button.click();
|
||||||
}
|
}
|
||||||
@@ -131,13 +138,14 @@ export async function waitForTabWithTitle(
|
|||||||
page
|
page
|
||||||
.locator('[data-testid^="workspace-tab-"]:not([data-testid^="workspace-tab-context-"])')
|
.locator('[data-testid^="workspace-tab-"]:not([data-testid^="workspace-tab-context-"])')
|
||||||
.filter({ hasText: matcher })
|
.filter({ hasText: matcher })
|
||||||
|
.filter({ visible: true })
|
||||||
.first(),
|
.first(),
|
||||||
).toBeVisible({ timeout });
|
).toBeVisible({ timeout });
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Assert the new agent tab button is visible in the tab bar. */
|
/** Assert the new agent tab button is visible in the tab bar. */
|
||||||
export async function assertSingleNewTabButton(page: Page): Promise<void> {
|
export async function assertSingleNewTabButton(page: Page): Promise<void> {
|
||||||
const buttons = page.getByTestId("workspace-new-agent-tab");
|
const buttons = page.getByTestId("workspace-new-agent-tab").filter({ visible: true });
|
||||||
const count = await buttons.count();
|
const count = await buttons.count();
|
||||||
expect(count).toBeGreaterThanOrEqual(1);
|
expect(count).toBeGreaterThanOrEqual(1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,18 +13,30 @@ export async function getWorkspaceTabTestIds(page: Page): Promise<string[]> {
|
|||||||
return ids;
|
return ids;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function visibleTestId(page: Page, testId: string) {
|
||||||
|
return page.getByTestId(testId).filter({ visible: true });
|
||||||
|
}
|
||||||
|
|
||||||
export async function waitForWorkspaceTabsVisible(page: Page): Promise<void> {
|
export async function waitForWorkspaceTabsVisible(page: Page): Promise<void> {
|
||||||
await expect(page.getByTestId("workspace-tabs-row").first()).toBeVisible({
|
await expect(visibleTestId(page, "workspace-tabs-row").first()).toBeVisible({
|
||||||
timeout: 30_000,
|
timeout: 30_000,
|
||||||
});
|
});
|
||||||
await expect(page.getByTestId("workspace-new-agent-tab").first()).toBeVisible({
|
await expect(visibleTestId(page, "workspace-new-agent-tab").first()).toBeVisible({
|
||||||
timeout: 30_000,
|
timeout: 30_000,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getVisibleWorkspaceAgentTabIds(page: Page): Promise<string[]> {
|
export async function getVisibleWorkspaceAgentTabIds(page: Page): Promise<string[]> {
|
||||||
const allTabIds = await getWorkspaceTabTestIds(page);
|
const tabs = page.locator('[data-testid^="workspace-tab-agent_"]').filter({ visible: true });
|
||||||
return allTabIds.filter((id) => id.startsWith("workspace-tab-agent_"));
|
const count = await tabs.count();
|
||||||
|
const ids: string[] = [];
|
||||||
|
for (let index = 0; index < count; index += 1) {
|
||||||
|
const testId = await tabs.nth(index).getAttribute("data-testid");
|
||||||
|
if (testId && !ids.includes(testId)) {
|
||||||
|
ids.push(testId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ids;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function expectOnlyWorkspaceAgentTabsVisible(
|
export async function expectOnlyWorkspaceAgentTabsVisible(
|
||||||
@@ -38,7 +50,7 @@ export async function expectOnlyWorkspaceAgentTabsVisible(
|
|||||||
expect(unexpected).toEqual([]);
|
expect(unexpected).toEqual([]);
|
||||||
expect(visible.length).toBe(expected.size);
|
expect(visible.length).toBe(expected.size);
|
||||||
for (const expectedId of expectedAgentIds) {
|
for (const expectedId of expectedAgentIds) {
|
||||||
await expect(page.getByTestId(`workspace-tab-agent_${expectedId}`)).toBeVisible({
|
await expect(visibleTestId(page, `workspace-tab-agent_${expectedId}`).first()).toBeVisible({
|
||||||
timeout: 30_000,
|
timeout: 30_000,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -103,8 +103,8 @@ export async function expectWorkspaceHeader(
|
|||||||
page: Page,
|
page: Page,
|
||||||
input: { title: string; subtitle: string },
|
input: { title: string; subtitle: string },
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const titleLocator = page.getByTestId("workspace-header-title");
|
const titleLocator = page.getByTestId("workspace-header-title").filter({ visible: true });
|
||||||
const subtitleLocator = page.getByTestId("workspace-header-subtitle");
|
const subtitleLocator = page.getByTestId("workspace-header-subtitle").filter({ visible: true });
|
||||||
|
|
||||||
await expect(titleLocator.first()).toHaveText(input.title, {
|
await expect(titleLocator.first()).toHaveText(input.title, {
|
||||||
timeout: 30_000,
|
timeout: 30_000,
|
||||||
|
|||||||
@@ -96,6 +96,14 @@ test.describe("Workspace navigation regression", () => {
|
|||||||
`workspace-tab-agent_${firstAgent.id}`,
|
`workspace-tab-agent_${firstAgent.id}`,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
const firstDeckEntry = page.getByTestId(
|
||||||
|
`workspace-deck-entry-${serverId}:${firstWorkspace.workspaceId}`,
|
||||||
|
);
|
||||||
|
const secondDeckEntry = page.getByTestId(
|
||||||
|
`workspace-deck-entry-${serverId}:${secondWorkspace.workspaceId}`,
|
||||||
|
);
|
||||||
|
await expect(firstDeckEntry).toBeVisible({ timeout: 30_000 });
|
||||||
|
|
||||||
await switchWorkspaceViaSidebar({
|
await switchWorkspaceViaSidebar({
|
||||||
page,
|
page,
|
||||||
serverId,
|
serverId,
|
||||||
@@ -126,27 +134,45 @@ test.describe("Workspace navigation regression", () => {
|
|||||||
await expect(getVisibleWorkspaceAgentTabIds(page)).resolves.toEqual([
|
await expect(getVisibleWorkspaceAgentTabIds(page)).resolves.toEqual([
|
||||||
`workspace-tab-agent_${secondAgent.id}`,
|
`workspace-tab-agent_${secondAgent.id}`,
|
||||||
]);
|
]);
|
||||||
|
await expect(firstDeckEntry).toBeAttached();
|
||||||
|
await expect(firstDeckEntry).toBeHidden();
|
||||||
|
await expect(secondDeckEntry).toBeVisible({ timeout: 30_000 });
|
||||||
|
await expect(page.locator('[data-testid^="workspace-deck-entry-"]')).toHaveCount(2);
|
||||||
|
|
||||||
|
await switchWorkspaceViaSidebar({
|
||||||
|
page,
|
||||||
|
serverId,
|
||||||
|
targetWorkspacePath: firstWorkspace.workspaceId,
|
||||||
|
});
|
||||||
|
await waitForWorkspaceTabsVisible(page);
|
||||||
|
await expect(page).toHaveURL(buildHostWorkspaceRoute(serverId, firstWorkspace.workspaceId), {
|
||||||
|
timeout: 30_000,
|
||||||
|
});
|
||||||
|
await expect(firstDeckEntry).toBeVisible({ timeout: 30_000 });
|
||||||
|
await expect(secondDeckEntry).toBeAttached();
|
||||||
|
await expect(secondDeckEntry).toBeHidden();
|
||||||
|
await expect(page.locator('[data-testid^="workspace-deck-entry-"]')).toHaveCount(2);
|
||||||
|
|
||||||
await page.reload();
|
await page.reload();
|
||||||
await waitForSidebarHydration(page);
|
await waitForSidebarHydration(page);
|
||||||
await waitForWorkspaceTabsVisible(page);
|
await waitForWorkspaceTabsVisible(page);
|
||||||
await expect(page).toHaveURL(buildHostWorkspaceRoute(serverId, secondWorkspace.workspaceId), {
|
await expect(page).toHaveURL(buildHostWorkspaceRoute(serverId, firstWorkspace.workspaceId), {
|
||||||
timeout: 30_000,
|
timeout: 30_000,
|
||||||
});
|
});
|
||||||
await expectSidebarWorkspaceSelected({
|
await expectSidebarWorkspaceSelected({
|
||||||
page,
|
page,
|
||||||
serverId,
|
serverId,
|
||||||
workspaceId: secondWorkspace.workspaceId,
|
workspaceId: firstWorkspace.workspaceId,
|
||||||
});
|
});
|
||||||
await expectWorkspaceHeader(page, {
|
await expectWorkspaceHeader(page, {
|
||||||
title: secondWorkspace.workspaceName,
|
title: firstWorkspace.workspaceName,
|
||||||
subtitle: secondWorkspace.projectDisplayName,
|
subtitle: firstWorkspace.projectDisplayName,
|
||||||
});
|
});
|
||||||
await expectWorkspaceTabVisible(page, secondAgent.id);
|
await expectWorkspaceTabVisible(page, firstAgent.id);
|
||||||
await expectWorkspaceTabHidden(page, firstAgent.id);
|
await expectWorkspaceTabHidden(page, secondAgent.id);
|
||||||
await expectOnlyWorkspaceAgentTabsVisible(page, [secondAgent.id]);
|
await expectOnlyWorkspaceAgentTabsVisible(page, [firstAgent.id]);
|
||||||
await expect(getVisibleWorkspaceAgentTabIds(page)).resolves.toEqual([
|
await expect(getVisibleWorkspaceAgentTabIds(page)).resolves.toEqual([
|
||||||
`workspace-tab-agent_${secondAgent.id}`,
|
`workspace-tab-agent_${firstAgent.id}`,
|
||||||
]);
|
]);
|
||||||
} finally {
|
} finally {
|
||||||
for (const agentId of agentIds) {
|
for (const agentId of agentIds) {
|
||||||
|
|||||||
@@ -94,7 +94,7 @@ test.describe("Workspace setup streaming", () => {
|
|||||||
await navigateToWorkspaceViaSidebar(page, workspace.id);
|
await navigateToWorkspaceViaSidebar(page, workspace.id);
|
||||||
|
|
||||||
await waitForWorkspaceTabsVisible(page);
|
await waitForWorkspaceTabsVisible(page);
|
||||||
await page.getByTestId("workspace-new-agent-tab").first().click();
|
await page.getByTestId("workspace-new-agent-tab").filter({ visible: true }).first().click();
|
||||||
await expect(page.getByRole("textbox", { name: "Message agent..." }).first()).toBeVisible({
|
await expect(page.getByRole("textbox", { name: "Message agent..." }).first()).toBeVisible({
|
||||||
timeout: 30_000,
|
timeout: 30_000,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -83,7 +83,10 @@ import {
|
|||||||
parseWorkspaceOpenIntent,
|
parseWorkspaceOpenIntent,
|
||||||
decodeWorkspaceIdFromPathSegment,
|
decodeWorkspaceIdFromPathSegment,
|
||||||
} from "@/utils/host-routes";
|
} from "@/utils/host-routes";
|
||||||
import { syncNavigationActiveWorkspace } from "@/stores/navigation-active-workspace-store";
|
import {
|
||||||
|
addBrowserActiveWorkspaceLocationListener,
|
||||||
|
syncNavigationActiveWorkspace,
|
||||||
|
} from "@/stores/navigation-active-workspace-store";
|
||||||
import { isWeb, isNative } from "@/constants/platform";
|
import { isWeb, isNative } from "@/constants/platform";
|
||||||
|
|
||||||
polyfillCrypto();
|
polyfillCrypto();
|
||||||
@@ -843,6 +846,7 @@ function NavigationActiveWorkspaceObserver() {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
syncNavigationActiveWorkspace(navigationRef);
|
syncNavigationActiveWorkspace(navigationRef);
|
||||||
|
const unsubscribeBrowserLocation = addBrowserActiveWorkspaceLocationListener();
|
||||||
const unsubscribeState = navigationRef.addListener("state", () => {
|
const unsubscribeState = navigationRef.addListener("state", () => {
|
||||||
syncNavigationActiveWorkspace(navigationRef);
|
syncNavigationActiveWorkspace(navigationRef);
|
||||||
});
|
});
|
||||||
@@ -850,6 +854,7 @@ function NavigationActiveWorkspaceObserver() {
|
|||||||
syncNavigationActiveWorkspace(navigationRef);
|
syncNavigationActiveWorkspace(navigationRef);
|
||||||
});
|
});
|
||||||
return () => {
|
return () => {
|
||||||
|
unsubscribeBrowserLocation();
|
||||||
unsubscribeState();
|
unsubscribeState();
|
||||||
unsubscribeReady();
|
unsubscribeReady();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { useEffect, useRef, useState } from "react";
|
import { useEffect, useMemo, useRef, useState } from "react";
|
||||||
import { useNavigation } from "@react-navigation/native";
|
import { useNavigation } from "@react-navigation/native";
|
||||||
|
import { StyleSheet, View } from "react-native";
|
||||||
import {
|
import {
|
||||||
useGlobalSearchParams,
|
useGlobalSearchParams,
|
||||||
useLocalSearchParams,
|
useLocalSearchParams,
|
||||||
@@ -7,6 +8,11 @@ import {
|
|||||||
useRootNavigationState,
|
useRootNavigationState,
|
||||||
} from "expo-router";
|
} from "expo-router";
|
||||||
import { HostRouteBootstrapBoundary } from "@/components/host-route-bootstrap-boundary";
|
import { HostRouteBootstrapBoundary } from "@/components/host-route-bootstrap-boundary";
|
||||||
|
import {
|
||||||
|
activateNavigationWorkspaceSelection,
|
||||||
|
type ActiveWorkspaceSelection,
|
||||||
|
useNavigationActiveWorkspaceSelection,
|
||||||
|
} from "@/stores/navigation-active-workspace-store";
|
||||||
import type { WorkspaceTabTarget } from "@/stores/workspace-tabs-store";
|
import type { WorkspaceTabTarget } from "@/stores/workspace-tabs-store";
|
||||||
import { WorkspaceScreen } from "@/screens/workspace/workspace-screen";
|
import { WorkspaceScreen } from "@/screens/workspace/workspace-screen";
|
||||||
import {
|
import {
|
||||||
@@ -95,6 +101,23 @@ function HostWorkspaceLayoutContent() {
|
|||||||
? (decodeWorkspaceIdFromPathSegment(workspaceValue) ?? "")
|
? (decodeWorkspaceIdFromPathSegment(workspaceValue) ?? "")
|
||||||
: "";
|
: "";
|
||||||
const openValue = getParamValue(globalParams.open);
|
const openValue = getParamValue(globalParams.open);
|
||||||
|
const routeWorkspaceSelection = useMemo(
|
||||||
|
() =>
|
||||||
|
serverId && workspaceId
|
||||||
|
? {
|
||||||
|
serverId,
|
||||||
|
workspaceId,
|
||||||
|
}
|
||||||
|
: null,
|
||||||
|
[serverId, workspaceId],
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!routeWorkspaceSelection) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
activateNavigationWorkspaceSelection(routeWorkspaceSelection);
|
||||||
|
}, [routeWorkspaceSelection]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!openValue) {
|
if (!openValue) {
|
||||||
@@ -144,11 +167,73 @@ function HostWorkspaceLayoutContent() {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return <WorkspaceDeck fallbackSelection={routeWorkspaceSelection} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
function areWorkspaceSelectionsEqual(
|
||||||
|
left: ActiveWorkspaceSelection | null,
|
||||||
|
right: ActiveWorkspaceSelection | null,
|
||||||
|
): boolean {
|
||||||
|
return left?.serverId === right?.serverId && left?.workspaceId === right?.workspaceId;
|
||||||
|
}
|
||||||
|
|
||||||
|
function WorkspaceDeck({
|
||||||
|
fallbackSelection,
|
||||||
|
}: {
|
||||||
|
fallbackSelection: ActiveWorkspaceSelection | null;
|
||||||
|
}) {
|
||||||
|
const activeSelection = useNavigationActiveWorkspaceSelection() ?? fallbackSelection;
|
||||||
|
const [mountedSelections, setMountedSelections] = useState<ActiveWorkspaceSelection[]>(() =>
|
||||||
|
activeSelection ? [activeSelection] : [],
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!activeSelection) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setMountedSelections((current) => {
|
||||||
|
if (current.some((selection) => areWorkspaceSelectionsEqual(selection, activeSelection))) {
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
return [...current, activeSelection];
|
||||||
|
});
|
||||||
|
}, [activeSelection]);
|
||||||
|
|
||||||
|
if (!activeSelection) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<WorkspaceScreen
|
<View style={styles.deck}>
|
||||||
key={`${serverId}:${workspaceId}`}
|
{mountedSelections.map((selection) => {
|
||||||
serverId={serverId}
|
const isActive = areWorkspaceSelectionsEqual(selection, activeSelection);
|
||||||
workspaceId={workspaceId}
|
return (
|
||||||
/>
|
<View
|
||||||
|
key={`${selection.serverId}:${selection.workspaceId}`}
|
||||||
|
style={isActive ? styles.activeDeckEntry : styles.inactiveDeckEntry}
|
||||||
|
testID={`workspace-deck-entry-${selection.serverId}:${selection.workspaceId}`}
|
||||||
|
>
|
||||||
|
<WorkspaceScreen
|
||||||
|
serverId={selection.serverId}
|
||||||
|
workspaceId={selection.workspaceId}
|
||||||
|
isRouteFocused={isActive}
|
||||||
|
/>
|
||||||
|
</View>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</View>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const styles = StyleSheet.create({
|
||||||
|
deck: {
|
||||||
|
flex: 1,
|
||||||
|
},
|
||||||
|
activeDeckEntry: {
|
||||||
|
flex: 1,
|
||||||
|
},
|
||||||
|
inactiveDeckEntry: {
|
||||||
|
display: "none",
|
||||||
|
flex: 1,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|||||||
@@ -1,17 +1,27 @@
|
|||||||
import { useCallback } from "react";
|
import { useCallback } from "react";
|
||||||
import { router } from "expo-router";
|
import { router } from "expo-router";
|
||||||
|
import {
|
||||||
|
activateNavigationWorkspaceSelection,
|
||||||
|
getNavigationActiveWorkspaceSelection,
|
||||||
|
} from "@/stores/navigation-active-workspace-store";
|
||||||
import { buildHostWorkspaceRoute } from "@/utils/host-routes";
|
import { buildHostWorkspaceRoute } from "@/utils/host-routes";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Navigate to a workspace screen. Uses router.navigate() which,
|
* Open a workspace. Once the workspace shell is mounted, switching workspaces
|
||||||
* combined with getId on the workspace Stack.Screen, ensures:
|
* is app-level state so native-stack does not rebuild every retained screen.
|
||||||
* - Only one instance of each workspace exists in the stack
|
|
||||||
* - History is preserved (back button works)
|
|
||||||
* - No duplicate workspace screens
|
|
||||||
*/
|
*/
|
||||||
export function navigateToWorkspace(serverId: string, workspaceId: string) {
|
export function navigateToWorkspace(serverId: string, workspaceId: string) {
|
||||||
|
const activeWorkspace = getNavigationActiveWorkspaceSelection();
|
||||||
|
if (activeWorkspace) {
|
||||||
|
activateNavigationWorkspaceSelection(
|
||||||
|
{ serverId, workspaceId },
|
||||||
|
{ updateBrowserHistory: true, historyMode: "push" },
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const href = buildHostWorkspaceRoute(serverId, workspaceId);
|
const href = buildHostWorkspaceRoute(serverId, workspaceId);
|
||||||
router.navigate(href);
|
router.navigate(href as any);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useWorkspaceNavigation() {
|
export function useWorkspaceNavigation() {
|
||||||
|
|||||||
@@ -133,6 +133,7 @@ const EMPTY_SET = new Set<string>();
|
|||||||
type WorkspaceScreenProps = {
|
type WorkspaceScreenProps = {
|
||||||
serverId: string;
|
serverId: string;
|
||||||
workspaceId: string;
|
workspaceId: string;
|
||||||
|
isRouteFocused?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
type WorkspaceScreenContentProps = WorkspaceScreenProps & {
|
type WorkspaceScreenContentProps = WorkspaceScreenProps & {
|
||||||
@@ -571,15 +572,16 @@ function useStableTabDescriptorMap(tabDescriptors: WorkspaceTabDescriptor[]) {
|
|||||||
return tabDescriptorMap;
|
return tabDescriptorMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function WorkspaceScreen({ serverId, workspaceId }: WorkspaceScreenProps) {
|
export function WorkspaceScreen({ serverId, workspaceId, isRouteFocused }: WorkspaceScreenProps) {
|
||||||
const isFocused = useIsFocused();
|
const navigationFocused = useIsFocused();
|
||||||
|
const effectiveRouteFocused = isRouteFocused ?? navigationFocused;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ExplorerSidebarAnimationProvider>
|
<ExplorerSidebarAnimationProvider>
|
||||||
<WorkspaceScreenContent
|
<WorkspaceScreenContent
|
||||||
serverId={serverId}
|
serverId={serverId}
|
||||||
workspaceId={workspaceId}
|
workspaceId={workspaceId}
|
||||||
isRouteFocused={isFocused}
|
isRouteFocused={effectiveRouteFocused}
|
||||||
/>
|
/>
|
||||||
</ExplorerSidebarAnimationProvider>
|
</ExplorerSidebarAnimationProvider>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,14 +1,21 @@
|
|||||||
import { useSyncExternalStore } from "react";
|
import { useSyncExternalStore } from "react";
|
||||||
import {
|
import {
|
||||||
|
buildHostWorkspaceRoute,
|
||||||
decodeWorkspaceIdFromPathSegment,
|
decodeWorkspaceIdFromPathSegment,
|
||||||
parseHostWorkspaceRouteFromPathname,
|
parseHostWorkspaceRouteFromPathname,
|
||||||
} from "@/utils/host-routes";
|
} from "@/utils/host-routes";
|
||||||
|
import { isWeb } from "@/constants/platform";
|
||||||
|
|
||||||
interface ActiveWorkspaceSelection {
|
export interface ActiveWorkspaceSelection {
|
||||||
serverId: string;
|
serverId: string;
|
||||||
workspaceId: string;
|
workspaceId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface ActivateWorkspaceSelectionOptions {
|
||||||
|
updateBrowserHistory?: boolean;
|
||||||
|
historyMode?: "push" | "replace";
|
||||||
|
}
|
||||||
|
|
||||||
type NavigationRouteLike = {
|
type NavigationRouteLike = {
|
||||||
name?: unknown;
|
name?: unknown;
|
||||||
params?: unknown;
|
params?: unknown;
|
||||||
@@ -45,6 +52,36 @@ function emitIfChanged(next: ActiveWorkspaceSelection | null) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getBrowserLocationWorkspace(): ActiveWorkspaceSelection | null {
|
||||||
|
if (!isWeb || typeof window === "undefined") {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return parseHostWorkspaceRouteFromPathname(window.location.pathname);
|
||||||
|
}
|
||||||
|
|
||||||
|
function writeBrowserWorkspaceUrl(
|
||||||
|
next: ActiveWorkspaceSelection,
|
||||||
|
options: ActivateWorkspaceSelectionOptions,
|
||||||
|
) {
|
||||||
|
if (!options.updateBrowserHistory || !isWeb || typeof window === "undefined") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const nextPath = buildHostWorkspaceRoute(next.serverId, next.workspaceId);
|
||||||
|
const currentUrl = new URL(window.location.href);
|
||||||
|
if (currentUrl.pathname === nextPath && !currentUrl.search && !currentUrl.hash) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const nextUrl = new URL(nextPath, window.location.origin);
|
||||||
|
const mode = options.historyMode ?? "push";
|
||||||
|
if (mode === "replace") {
|
||||||
|
window.history.replaceState(null, "", nextUrl.toString());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
window.history.pushState(null, "", nextUrl.toString());
|
||||||
|
}
|
||||||
|
|
||||||
function extractActiveWorkspaceFromRoute(
|
function extractActiveWorkspaceFromRoute(
|
||||||
route: NavigationRouteLike | undefined,
|
route: NavigationRouteLike | undefined,
|
||||||
): ActiveWorkspaceSelection | null {
|
): ActiveWorkspaceSelection | null {
|
||||||
@@ -91,6 +128,36 @@ export function syncNavigationActiveWorkspace(navigationRef: NavigationObserverR
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function activateNavigationWorkspaceSelection(
|
||||||
|
next: ActiveWorkspaceSelection,
|
||||||
|
options: ActivateWorkspaceSelectionOptions = {},
|
||||||
|
) {
|
||||||
|
emitIfChanged(next);
|
||||||
|
writeBrowserWorkspaceUrl(next, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getNavigationActiveWorkspaceSelection(): ActiveWorkspaceSelection | null {
|
||||||
|
return getSnapshot();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function syncBrowserActiveWorkspaceFromLocation() {
|
||||||
|
emitIfChanged(getBrowserLocationWorkspace());
|
||||||
|
}
|
||||||
|
|
||||||
|
export function addBrowserActiveWorkspaceLocationListener(): () => void {
|
||||||
|
if (!isWeb || typeof window === "undefined") {
|
||||||
|
return () => {};
|
||||||
|
}
|
||||||
|
|
||||||
|
const handlePopState = () => {
|
||||||
|
syncBrowserActiveWorkspaceFromLocation();
|
||||||
|
};
|
||||||
|
window.addEventListener("popstate", handlePopState);
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener("popstate", handlePopState);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export function useNavigationActiveWorkspaceSelection(): ActiveWorkspaceSelection | null {
|
export function useNavigationActiveWorkspaceSelection(): ActiveWorkspaceSelection | null {
|
||||||
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user