mirror of
https://github.com/stablyai/orca.git
synced 2026-11-17 03:15:01 +00:00
Merge pull request #831 from nwparker/cloud/menu-hover-perf
perf(sidebar): open workspace menu ignores unrelated store writes
This commit is contained in:
+75
@@ -0,0 +1,75 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import { act, cleanup, renderHook } from '@testing-library/react'
|
||||
import { afterEach, describe, expect, it } from 'vitest'
|
||||
import { useAppStore } from '@/store'
|
||||
import { makeWorktree } from '@/store/slices/worktrees/worktree.test-fixture'
|
||||
import { TEST_REPO } from '@/store/repos/repo.test-fixture'
|
||||
import { makeTab } from '@/store/terminals/terminal-tab.test-fixture'
|
||||
import { useWorktreeContextMenuModel } from './use-worktree-context-menu-model'
|
||||
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
useAppStore.setState(useAppStore.getInitialState(), true)
|
||||
})
|
||||
|
||||
const worktree = makeWorktree({ id: 'repo1::/tmp/wt', repoId: TEST_REPO.id })
|
||||
const other = makeWorktree({
|
||||
id: 'repo1::/tmp/other',
|
||||
repoId: TEST_REPO.id,
|
||||
path: '/tmp/other'
|
||||
})
|
||||
|
||||
function renderOpenMenu() {
|
||||
useAppStore.setState({
|
||||
repos: [TEST_REPO],
|
||||
worktreesByRepo: { [TEST_REPO.id]: [worktree, other] },
|
||||
tabsByWorktree: { [worktree.id]: [makeTab({ id: 'tab-1', worktreeId: worktree.id })] },
|
||||
ptyIdsByTabId: {}
|
||||
})
|
||||
let renders = 0
|
||||
const hook = renderHook(() => {
|
||||
renders += 1
|
||||
return useWorktreeContextMenuModel({ worktree, children: null })
|
||||
})
|
||||
act(() => hook.result.current.setMenuOpenState(true))
|
||||
return { hook, renders: () => renders }
|
||||
}
|
||||
|
||||
// An open menu used to subscribe to whole activity maps, so every agent status, tab or PTY write
|
||||
// anywhere re-rendered the whole menu (about 200 components) while the pointer moved over it.
|
||||
describe('an open workspace menu ignores store writes that change nothing it shows', () => {
|
||||
it('another workspace’s agent status, tabs and PTYs do not re-render it', () => {
|
||||
const { renders } = renderOpenMenu()
|
||||
const before = renders()
|
||||
for (let i = 0; i < 20; i += 1) {
|
||||
act(() => {
|
||||
const state = useAppStore.getState()
|
||||
state.setAgentStatus(
|
||||
`pane-${i}`,
|
||||
{ state: 'working', prompt: `prompt ${i}`, agentType: 'codex' },
|
||||
'codex',
|
||||
{ updatedAt: i, stateStartedAt: i },
|
||||
{ tabId: `other-${i}`, worktreeId: other.id }
|
||||
)
|
||||
useAppStore.setState({
|
||||
tabsByWorktree: {
|
||||
...state.tabsByWorktree,
|
||||
[other.id]: [makeTab({ id: `other-${i}`, worktreeId: other.id })]
|
||||
},
|
||||
ptyIdsByTabId: { ...state.ptyIdsByTabId, [`other-${i}`]: [`pty-${i}`] },
|
||||
unifiedTabsByWorktree: { ...state.unifiedTabsByWorktree },
|
||||
deleteStateByWorktreeId: { ...state.deleteStateByWorktreeId }
|
||||
})
|
||||
})
|
||||
}
|
||||
expect(renders()).toBe(before)
|
||||
})
|
||||
|
||||
it('still updates live when this workspace becomes sleepable', () => {
|
||||
const { hook } = renderOpenMenu()
|
||||
expect(hook.result.current.sleepableWorktrees).toEqual([])
|
||||
act(() => useAppStore.setState({ ptyIdsByTabId: { 'tab-1': ['pty-1'] } }))
|
||||
expect(hook.result.current.sleepableWorktrees.map((item) => item.id)).toEqual([worktree.id])
|
||||
})
|
||||
})
|
||||
+30
-79
@@ -1,4 +1,3 @@
|
||||
import { selectAwakeBrowserTabsByWorktree } from '@/components/sidebar/workspace-actions/sleep/browser-tab-sleep'
|
||||
import { useIsPairedCloudPrimaryRow } from '@/lib/orca-cloud/cloud-host-rows'
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
||||
import { useAppStore } from '@/store/index'
|
||||
@@ -7,20 +6,12 @@ import type { Worktree } from '../../../../../../shared/worktree/types'
|
||||
import { useContextWorkspaceStatuses } from '../../workspace-status/presentation/use-workspace-board-statuses'
|
||||
import { useMenuScopedWorktreeCatalog } from './use-menu-scoped-worktree-catalog'
|
||||
import { useMenuLineageParent } from './use-menu-lineage-parent'
|
||||
import {
|
||||
hasSleepableWorkspaceActivity,
|
||||
useWorkspaceLineageMenuActions
|
||||
} from './workspace-lineage-menu-actions'
|
||||
import { useWorkspaceMenuActivity } from './workspace-lineage-menu-actions'
|
||||
import { getWorkspaceDeleteLineage } from '../deletion/workspace-delete-lineage'
|
||||
import { parseWorkspaceKey } from '../../../../../../shared/workspace-scope'
|
||||
import { getDeleteStateForWorktreeHost } from '../deletion/worktree-delete-state-host-match'
|
||||
import {
|
||||
EMPTY_AGENT_STATUS_BY_PANE_KEY,
|
||||
EMPTY_BROWSER_TABS_BY_WORKTREE,
|
||||
EMPTY_DELETE_STATE_BY_WORKTREE_ID,
|
||||
EMPTY_STRUCTURED_CHAT_SLEPT_AT,
|
||||
EMPTY_PTY_IDS_BY_TAB_ID,
|
||||
EMPTY_TABS_BY_WORKTREE,
|
||||
EMPTY_UNIFIED_TABS_BY_WORKTREE,
|
||||
EMPTY_WORKTREES,
|
||||
EMPTY_WORKSPACE_LINEAGE_BY_CHILD_KEY,
|
||||
EMPTY_WORKTREE_LINEAGE_BY_ID,
|
||||
getSharedWorkspaceStatus,
|
||||
@@ -102,31 +93,6 @@ export function useWorktreeContextMenuModel({
|
||||
)
|
||||
)
|
||||
const updateWorktreeLineage = useAppStore((s) => s.updateWorktreeLineage)
|
||||
const tabsByWorktree = useAppStore((s) =>
|
||||
selectMenuScopedMap(menuOpen, s.tabsByWorktree, EMPTY_TABS_BY_WORKTREE)
|
||||
)
|
||||
const ptyIdsByTabId = useAppStore((s) =>
|
||||
selectMenuScopedMap(menuOpen, s.ptyIdsByTabId, EMPTY_PTY_IDS_BY_TAB_ID)
|
||||
)
|
||||
const browserTabsByWorktree = useAppStore((s) =>
|
||||
selectMenuScopedMap(
|
||||
menuOpen,
|
||||
selectAwakeBrowserTabsByWorktree(s),
|
||||
EMPTY_BROWSER_TABS_BY_WORKTREE
|
||||
)
|
||||
)
|
||||
const unifiedTabsByWorktree = useAppStore((s) =>
|
||||
selectMenuScopedMap(menuOpen, s.unifiedTabsByWorktree, EMPTY_UNIFIED_TABS_BY_WORKTREE)
|
||||
)
|
||||
const agentStatusByPaneKey = useAppStore((s) =>
|
||||
selectMenuScopedMap(menuOpen, s.agentStatusByPaneKey, EMPTY_AGENT_STATUS_BY_PANE_KEY)
|
||||
)
|
||||
const structuredChatSleptAtByPaneKey = useAppStore((s) =>
|
||||
selectMenuScopedMap(menuOpen, s.structuredChatSleptAtByPaneKey, EMPTY_STRUCTURED_CHAT_SLEPT_AT)
|
||||
)
|
||||
const deleteStateByWorktreeId = useAppStore((s) =>
|
||||
selectMenuScopedMap(menuOpen, s.deleteStateByWorktreeId, EMPTY_DELETE_STATE_BY_WORKTREE_ID)
|
||||
)
|
||||
const scopeRef = useRef<HTMLDivElement>(null)
|
||||
const contextMenuOpenedAtRef = useRef<number | null>(null)
|
||||
const activeContextWorktrees = menuOpen ? contextWorktrees : effectiveSelectedWorktrees
|
||||
@@ -136,54 +102,42 @@ export function useWorktreeContextMenuModel({
|
||||
const workspaceScope = parseWorkspaceKey(worktree.id)
|
||||
const folderWorkspaceId =
|
||||
workspaceScope?.type === 'folder' ? workspaceScope.folderWorkspaceId : null
|
||||
const sleepActivity = useMemo(
|
||||
() => ({
|
||||
tabsByWorktree,
|
||||
ptyIdsByTabId,
|
||||
browserTabsByWorktree,
|
||||
unifiedTabsByWorktree,
|
||||
agentStatusByPaneKey,
|
||||
structuredChatSleptAtByPaneKey
|
||||
}),
|
||||
[
|
||||
agentStatusByPaneKey,
|
||||
browserTabsByWorktree,
|
||||
structuredChatSleptAtByPaneKey,
|
||||
ptyIdsByTabId,
|
||||
tabsByWorktree,
|
||||
unifiedTabsByWorktree
|
||||
]
|
||||
)
|
||||
// A paired cloud workspace can be running with nothing open here: its Sleep reaches the sandbox.
|
||||
const pairedCloudSleepable = useIsPairedCloudPrimaryRow(worktree) && !isMultiContext
|
||||
// Lineage reads only the catalog; per-workspace activity comes from narrow id selectors below.
|
||||
const lineageTargets = useMemo(
|
||||
() =>
|
||||
menuOpen && !isMultiContext
|
||||
? [
|
||||
worktree,
|
||||
...getWorkspaceDeleteLineage(worktree, allWorktrees, worktreeLineageById).descendants
|
||||
]
|
||||
: EMPTY_WORKTREES,
|
||||
[allWorktrees, isMultiContext, menuOpen, worktree, worktreeLineageById]
|
||||
)
|
||||
const activityCandidates = useMemo(
|
||||
() => [
|
||||
...activeContextWorktrees,
|
||||
...lineageTargets.filter((target) => !activeContextWorktrees.includes(target))
|
||||
],
|
||||
[activeContextWorktrees, lineageTargets]
|
||||
)
|
||||
const { sleepableIds, deletingIds } = useWorkspaceMenuActivity(menuOpen, activityCandidates)
|
||||
const sleepableWorktrees = useMemo(
|
||||
() =>
|
||||
activeContextWorktrees.filter(
|
||||
(item) =>
|
||||
hasSleepableWorkspaceActivity(item.id, sleepActivity) ||
|
||||
(pairedCloudSleepable && item.id === worktree.id)
|
||||
sleepableIds.includes(item.id) || (pairedCloudSleepable && item.id === worktree.id)
|
||||
),
|
||||
[activeContextWorktrees, sleepActivity, pairedCloudSleepable, worktree.id]
|
||||
[activeContextWorktrees, sleepableIds, pairedCloudSleepable, worktree.id]
|
||||
)
|
||||
const lineageMenuActions = useWorkspaceLineageMenuActions({
|
||||
enabled: menuOpen && !isMultiContext,
|
||||
parent: worktree,
|
||||
worktrees: allWorktrees,
|
||||
lineageById: worktreeLineageById,
|
||||
activity: sleepActivity
|
||||
})
|
||||
const lineageDescendantCount = lineageMenuActions.descendants.length
|
||||
const subtreeSleepableWorktrees = lineageMenuActions.sleepableTargets
|
||||
const deletingContext = useMemo(
|
||||
() =>
|
||||
activeContextWorktrees.some(
|
||||
(item) => getDeleteStateForWorktreeHost(item, deleteStateByWorktreeId)?.isDeleting
|
||||
),
|
||||
[activeContextWorktrees, deleteStateByWorktreeId]
|
||||
)
|
||||
const deletingSubtree = lineageMenuActions.targets.some(
|
||||
(item) => getDeleteStateForWorktreeHost(item, deleteStateByWorktreeId)?.isDeleting
|
||||
const lineageDescendantCount = Math.max(0, lineageTargets.length - 1)
|
||||
const subtreeSleepableWorktrees = useMemo(
|
||||
() => lineageTargets.filter((target) => sleepableIds.includes(target.id)),
|
||||
[lineageTargets, sleepableIds]
|
||||
)
|
||||
const deletingContext = activeContextWorktrees.some((item) => deletingIds.includes(item.id))
|
||||
const deletingSubtree = lineageTargets.some((item) => deletingIds.includes(item.id))
|
||||
const contextDeletePending = isMultiContext ? deletingContext : deletingSubtree
|
||||
const contextWorkspaceStatus = useMemo(
|
||||
() => getSharedWorkspaceStatus(activeContextWorktrees, statusesForWorktree),
|
||||
@@ -343,7 +297,6 @@ export function useWorktreeContextMenuModel({
|
||||
...commands,
|
||||
activeContextWorktrees,
|
||||
batchDeleteWorktrees,
|
||||
browserTabsByWorktree,
|
||||
children,
|
||||
contentClassName,
|
||||
contextDeletePending,
|
||||
@@ -373,7 +326,6 @@ export function useWorktreeContextMenuModel({
|
||||
parentPicker,
|
||||
parentPickerOpen,
|
||||
projectGroups,
|
||||
ptyIdsByTabId,
|
||||
removesProject,
|
||||
repo,
|
||||
scopeRef,
|
||||
@@ -385,7 +337,6 @@ export function useWorktreeContextMenuModel({
|
||||
sleepableWorktrees,
|
||||
subtreeSleepableWorktrees,
|
||||
suppressOpeningPointerEvent,
|
||||
tabsByWorktree,
|
||||
validParentWorktreeId,
|
||||
worktree,
|
||||
workspaceStatuses
|
||||
|
||||
+50
-46
@@ -1,4 +1,8 @@
|
||||
import { useMemo } from 'react'
|
||||
import { useShallow } from 'zustand/react/shallow'
|
||||
import { useAppStore } from '@/store'
|
||||
import type { AppState } from '@/store/types'
|
||||
import { selectAwakeBrowserTabsByWorktree } from '@/components/sidebar/workspace-actions/sleep/browser-tab-sleep'
|
||||
import { getDeleteStateForWorktreeHost } from '../deletion/worktree-delete-state-host-match'
|
||||
import { tabHasLivePty } from '../../../../../../shared/tab-has-live-pty'
|
||||
import type { WorktreeLineage } from '../../../../../../shared/worktree/lineage-types'
|
||||
import type { Worktree } from '../../../../../../shared/worktree/types'
|
||||
@@ -61,52 +65,52 @@ export function getWorkspaceLineageMenuActions(args: {
|
||||
}
|
||||
}
|
||||
|
||||
const EMPTY_LINEAGE_MENU_ACTIONS: WorkspaceLineageMenuActions = {
|
||||
descendants: [],
|
||||
targets: [],
|
||||
sleepableTargets: []
|
||||
const EMPTY_IDS: readonly string[] = []
|
||||
|
||||
function selectWorkspaceActivity(state: AppState): WorkspaceActivityMaps {
|
||||
const browserTabsByWorktree = selectAwakeBrowserTabsByWorktree(state)
|
||||
return {
|
||||
tabsByWorktree: state.tabsByWorktree,
|
||||
ptyIdsByTabId: state.ptyIdsByTabId,
|
||||
browserTabsByWorktree,
|
||||
unifiedTabsByWorktree: state.unifiedTabsByWorktree,
|
||||
agentStatusByPaneKey: state.agentStatusByPaneKey,
|
||||
structuredChatSleptAtByPaneKey: state.structuredChatSleptAtByPaneKey
|
||||
}
|
||||
}
|
||||
|
||||
export function useWorkspaceLineageMenuActions(
|
||||
args: Parameters<typeof getWorkspaceLineageMenuActions>[0] & { enabled: boolean }
|
||||
): WorkspaceLineageMenuActions {
|
||||
const { enabled, parent, worktrees, lineageById, activity } = args
|
||||
const {
|
||||
tabsByWorktree,
|
||||
ptyIdsByTabId,
|
||||
browserTabsByWorktree,
|
||||
unifiedTabsByWorktree,
|
||||
agentStatusByPaneKey,
|
||||
structuredChatSleptAtByPaneKey
|
||||
} = activity
|
||||
return useMemo(
|
||||
() =>
|
||||
enabled
|
||||
? getWorkspaceLineageMenuActions({
|
||||
parent,
|
||||
worktrees,
|
||||
lineageById,
|
||||
activity: {
|
||||
tabsByWorktree,
|
||||
ptyIdsByTabId,
|
||||
browserTabsByWorktree,
|
||||
unifiedTabsByWorktree,
|
||||
agentStatusByPaneKey,
|
||||
structuredChatSleptAtByPaneKey
|
||||
}
|
||||
})
|
||||
: EMPTY_LINEAGE_MENU_ACTIONS,
|
||||
[
|
||||
agentStatusByPaneKey,
|
||||
browserTabsByWorktree,
|
||||
enabled,
|
||||
structuredChatSleptAtByPaneKey,
|
||||
lineageById,
|
||||
parent,
|
||||
ptyIdsByTabId,
|
||||
tabsByWorktree,
|
||||
unifiedTabsByWorktree,
|
||||
worktrees
|
||||
]
|
||||
/**
|
||||
* Which of `worktrees` have something Sleep would close, and which are being deleted, as id lists
|
||||
* that keep their identity while unchanged. An open menu must not subscribe to the whole activity
|
||||
* and delete maps: agents, tabs and PTYs anywhere write them many times a second, and each write
|
||||
* re-rendered the whole menu under the pointer. Closed menus select a shared empty list.
|
||||
*/
|
||||
export function useWorkspaceMenuActivity(
|
||||
enabled: boolean,
|
||||
worktrees: readonly Worktree[]
|
||||
): { sleepableIds: readonly string[]; deletingIds: readonly string[] } {
|
||||
const sleepableIds = useAppStore(
|
||||
useShallow((state) => {
|
||||
if (!enabled) {
|
||||
return EMPTY_IDS
|
||||
}
|
||||
const activity = selectWorkspaceActivity(state)
|
||||
return worktrees
|
||||
.filter((item) => hasSleepableWorkspaceActivity(item.id, activity))
|
||||
.map((item) => item.id)
|
||||
})
|
||||
)
|
||||
const deletingIds = useAppStore(
|
||||
useShallow((state) =>
|
||||
enabled
|
||||
? worktrees
|
||||
.filter(
|
||||
(item) =>
|
||||
getDeleteStateForWorktreeHost(item, state.deleteStateByWorktreeId)?.isDeleting
|
||||
)
|
||||
.map((item) => item.id)
|
||||
: EMPTY_IDS
|
||||
)
|
||||
)
|
||||
return { sleepableIds, deletingIds }
|
||||
}
|
||||
|
||||
-7
@@ -25,13 +25,6 @@ const DELETE_POSITION_RESTORE_STABLE_FRAMES = 6
|
||||
export const PARENT_PICKER_EXIT_ANIMATION_MS = 200
|
||||
|
||||
// Closed menus reuse empty identities so teardown map churn cannot wake their subscriptions.
|
||||
export const EMPTY_TABS_BY_WORKTREE: AppState['tabsByWorktree'] = {}
|
||||
export const EMPTY_PTY_IDS_BY_TAB_ID: AppState['ptyIdsByTabId'] = {}
|
||||
export const EMPTY_BROWSER_TABS_BY_WORKTREE: AppState['browserTabsByWorktree'] = {}
|
||||
export const EMPTY_UNIFIED_TABS_BY_WORKTREE: AppState['unifiedTabsByWorktree'] = {}
|
||||
export const EMPTY_AGENT_STATUS_BY_PANE_KEY: AppState['agentStatusByPaneKey'] = {}
|
||||
export const EMPTY_STRUCTURED_CHAT_SLEPT_AT: AppState['structuredChatSleptAtByPaneKey'] = {}
|
||||
export const EMPTY_DELETE_STATE_BY_WORKTREE_ID: AppState['deleteStateByWorktreeId'] = {}
|
||||
export const EMPTY_WORKTREE_LINEAGE_BY_ID: AppState['worktreeLineageById'] = {}
|
||||
export const EMPTY_WORKSPACE_LINEAGE_BY_CHILD_KEY: AppState['workspaceLineageByChildKey'] = {}
|
||||
export const EMPTY_CROSS_STORE_LINEAGE_BY_STORE_KEY: AppState['crossStoreLineageByStoreKey'] = {}
|
||||
|
||||
Reference in New Issue
Block a user