fix(app): restore workspace tab focus on refresh
This commit is contained in:
@@ -15,6 +15,7 @@ import {
|
||||
} from "@/stores/navigation-active-workspace-store";
|
||||
import type { WorkspaceTabTarget } from "@/stores/workspace-tabs-store";
|
||||
import { WorkspaceScreen } from "@/screens/workspace/workspace-screen";
|
||||
import { useWorkspaceLayoutStoreHydrated } from "@/stores/workspace-layout-store";
|
||||
import {
|
||||
buildHostWorkspaceRoute,
|
||||
decodeWorkspaceIdFromPathSegment,
|
||||
@@ -88,6 +89,7 @@ function HostWorkspaceLayoutContent() {
|
||||
const navigation = useNavigation();
|
||||
const router = useRouter();
|
||||
const rootNavigationState = useRootNavigationState();
|
||||
const hasHydratedWorkspaceLayoutStore = useWorkspaceLayoutStoreHydrated();
|
||||
const consumedIntentRef = useRef<string | null>(null);
|
||||
const [intentConsumed, setIntentConsumed] = useState(false);
|
||||
const params = useLocalSearchParams<{
|
||||
@@ -128,6 +130,9 @@ function HostWorkspaceLayoutContent() {
|
||||
if (!rootNavigationState?.key) {
|
||||
return;
|
||||
}
|
||||
if (!hasHydratedWorkspaceLayoutStore) {
|
||||
return;
|
||||
}
|
||||
|
||||
const consumptionKey = `${serverId}:${workspaceId}:${openValue}`;
|
||||
if (consumedIntentRef.current === consumptionKey) {
|
||||
@@ -163,9 +168,17 @@ function HostWorkspaceLayoutContent() {
|
||||
});
|
||||
|
||||
setIntentConsumed(true);
|
||||
}, [navigation, openValue, rootNavigationState?.key, router, serverId, workspaceId]);
|
||||
}, [
|
||||
hasHydratedWorkspaceLayoutStore,
|
||||
navigation,
|
||||
openValue,
|
||||
rootNavigationState?.key,
|
||||
router,
|
||||
serverId,
|
||||
workspaceId,
|
||||
]);
|
||||
|
||||
if (openValue && !intentConsumed) {
|
||||
if (openValue && (!intentConsumed || !hasHydratedWorkspaceLayoutStore)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -219,7 +219,10 @@ function TabChip({
|
||||
onPress={() => {
|
||||
onNavigateTab(tab.tabId);
|
||||
}}
|
||||
accessibilityRole="button"
|
||||
accessibilityLabel={tooltipLabel}
|
||||
accessibilityState={{ selected: isActive }}
|
||||
aria-selected={isActive}
|
||||
>
|
||||
{isActive && (
|
||||
<View
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { shouldSeedEmptyWorkspaceDraft } from "./workspace-empty-draft-seed";
|
||||
|
||||
const readyEmptyWorkspace = {
|
||||
isRouteFocused: true,
|
||||
hasPersistenceKey: true,
|
||||
hasWorkspaceDirectory: true,
|
||||
hasHydratedWorkspaceLayoutStore: true,
|
||||
hasHydratedAgents: true,
|
||||
hasLoadedTerminals: true,
|
||||
activeAgentCount: 0,
|
||||
terminalCount: 0,
|
||||
tabCount: 0,
|
||||
};
|
||||
|
||||
describe("shouldSeedEmptyWorkspaceDraft", () => {
|
||||
it("waits for refresh-time hydration before seeding a draft", () => {
|
||||
expect(
|
||||
shouldSeedEmptyWorkspaceDraft({
|
||||
...readyEmptyWorkspace,
|
||||
hasHydratedWorkspaceLayoutStore: false,
|
||||
}),
|
||||
).toBe(false);
|
||||
expect(
|
||||
shouldSeedEmptyWorkspaceDraft({
|
||||
...readyEmptyWorkspace,
|
||||
hasHydratedAgents: false,
|
||||
}),
|
||||
).toBe(false);
|
||||
expect(
|
||||
shouldSeedEmptyWorkspaceDraft({
|
||||
...readyEmptyWorkspace,
|
||||
hasLoadedTerminals: false,
|
||||
}),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("does not seed when existing workspace content is known", () => {
|
||||
expect(
|
||||
shouldSeedEmptyWorkspaceDraft({
|
||||
...readyEmptyWorkspace,
|
||||
activeAgentCount: 1,
|
||||
}),
|
||||
).toBe(false);
|
||||
expect(
|
||||
shouldSeedEmptyWorkspaceDraft({
|
||||
...readyEmptyWorkspace,
|
||||
terminalCount: 1,
|
||||
}),
|
||||
).toBe(false);
|
||||
expect(
|
||||
shouldSeedEmptyWorkspaceDraft({
|
||||
...readyEmptyWorkspace,
|
||||
tabCount: 1,
|
||||
}),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("seeds once an empty focused workspace is fully known", () => {
|
||||
expect(shouldSeedEmptyWorkspaceDraft(readyEmptyWorkspace)).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,24 @@
|
||||
export function shouldSeedEmptyWorkspaceDraft(input: {
|
||||
isRouteFocused: boolean;
|
||||
hasPersistenceKey: boolean;
|
||||
hasWorkspaceDirectory: boolean;
|
||||
hasHydratedWorkspaceLayoutStore: boolean;
|
||||
hasHydratedAgents: boolean;
|
||||
hasLoadedTerminals: boolean;
|
||||
activeAgentCount: number;
|
||||
terminalCount: number;
|
||||
tabCount: number;
|
||||
}): boolean {
|
||||
if (
|
||||
!input.isRouteFocused ||
|
||||
!input.hasPersistenceKey ||
|
||||
!input.hasWorkspaceDirectory ||
|
||||
!input.hasHydratedWorkspaceLayoutStore ||
|
||||
!input.hasHydratedAgents ||
|
||||
!input.hasLoadedTerminals
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return input.activeAgentCount === 0 && input.terminalCount === 0 && input.tabCount === 0;
|
||||
}
|
||||
@@ -55,6 +55,7 @@ import {
|
||||
buildWorkspaceTabPersistenceKey,
|
||||
collectAllTabs,
|
||||
useWorkspaceLayoutStore,
|
||||
useWorkspaceLayoutStoreHydrated,
|
||||
} from "@/stores/workspace-layout-store";
|
||||
import type { WorkspaceTab, WorkspaceTabTarget } from "@/stores/workspace-tabs-store";
|
||||
import { useKeyboardActionHandler } from "@/hooks/use-keyboard-action-handler";
|
||||
@@ -111,6 +112,7 @@ import {
|
||||
type WorkspacePaneContentModel,
|
||||
} from "@/screens/workspace/workspace-pane-content";
|
||||
import { useMountedTabSet } from "@/screens/workspace/use-mounted-tab-set";
|
||||
import { shouldSeedEmptyWorkspaceDraft } from "@/screens/workspace/workspace-empty-draft-seed";
|
||||
import {
|
||||
buildBulkCloseConfirmationMessage,
|
||||
classifyBulkClosableTabs,
|
||||
@@ -977,6 +979,7 @@ function WorkspaceScreenContent({
|
||||
const workspaceLayout = useWorkspaceLayoutStore((state) =>
|
||||
persistenceKey ? (state.layoutByWorkspace[persistenceKey] ?? null) : null,
|
||||
);
|
||||
const hasHydratedWorkspaceLayoutStore = useWorkspaceLayoutStoreHydrated();
|
||||
const workspaceSetupSnapshot = useWorkspaceSetupStore((state) =>
|
||||
persistenceKey ? (state.snapshots[persistenceKey] ?? null) : null,
|
||||
);
|
||||
@@ -1138,6 +1141,9 @@ function WorkspaceScreenContent({
|
||||
if (!normalizedServerId || !normalizedWorkspaceId || !persistenceKey) {
|
||||
return;
|
||||
}
|
||||
if (!hasHydratedWorkspaceLayoutStore) {
|
||||
return;
|
||||
}
|
||||
|
||||
const hasActivePendingDraftCreateInWorkspace = uiTabs.some((tab) => {
|
||||
if (tab.target.kind !== "draft") {
|
||||
@@ -1158,6 +1164,7 @@ function WorkspaceScreenContent({
|
||||
});
|
||||
}, [
|
||||
hasHydratedAgents,
|
||||
hasHydratedWorkspaceLayoutStore,
|
||||
isRouteFocused,
|
||||
pendingByDraftId,
|
||||
persistenceKey,
|
||||
@@ -1246,17 +1253,19 @@ function WorkspaceScreenContent({
|
||||
]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isRouteFocused) {
|
||||
return;
|
||||
}
|
||||
if (!persistenceKey) {
|
||||
return;
|
||||
}
|
||||
if (workspaceAgentVisibility.activeAgentIds.size > 0 || terminals.length > 0) {
|
||||
emptyWorkspaceSeedRef.current = null;
|
||||
return;
|
||||
}
|
||||
if (tabs.length > 0) {
|
||||
if (
|
||||
!shouldSeedEmptyWorkspaceDraft({
|
||||
isRouteFocused,
|
||||
hasPersistenceKey: Boolean(persistenceKey),
|
||||
hasWorkspaceDirectory: Boolean(workspaceDirectory),
|
||||
hasHydratedWorkspaceLayoutStore,
|
||||
hasHydratedAgents,
|
||||
hasLoadedTerminals: terminalsQuery.isSuccess,
|
||||
activeAgentCount: workspaceAgentVisibility.activeAgentIds.size,
|
||||
terminalCount: terminals.length,
|
||||
tabCount: tabs.length,
|
||||
})
|
||||
) {
|
||||
emptyWorkspaceSeedRef.current = null;
|
||||
return;
|
||||
}
|
||||
@@ -1271,9 +1280,13 @@ function WorkspaceScreenContent({
|
||||
normalizedWorkspaceId,
|
||||
openWorkspaceDraftTab,
|
||||
persistenceKey,
|
||||
hasHydratedAgents,
|
||||
hasHydratedWorkspaceLayoutStore,
|
||||
isRouteFocused,
|
||||
terminals.length,
|
||||
terminalsQuery.isSuccess,
|
||||
tabs.length,
|
||||
workspaceDirectory,
|
||||
workspaceAgentVisibility.activeAgentIds.size,
|
||||
]);
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||
import { useEffect, useState } from "react";
|
||||
import { create } from "zustand";
|
||||
import { createJSONStorage, persist } from "zustand/middleware";
|
||||
import {
|
||||
@@ -719,3 +720,22 @@ export const useWorkspaceLayoutStore = create<WorkspaceLayoutStore>()(
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
export function useWorkspaceLayoutStoreHydrated(): boolean {
|
||||
const [hasHydrated, setHasHydrated] = useState(() =>
|
||||
useWorkspaceLayoutStore.persist.hasHydrated(),
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (useWorkspaceLayoutStore.persist.hasHydrated()) {
|
||||
setHasHydrated(true);
|
||||
return;
|
||||
}
|
||||
|
||||
return useWorkspaceLayoutStore.persist.onFinishHydration(() => {
|
||||
setHasHydrated(true);
|
||||
});
|
||||
}, []);
|
||||
|
||||
return hasHydrated;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user