fix(client): flow occupancy is a subscribed source; an unloading occupant withdraws the open flow

hasDirectoryFlow was a plain per-render read: a flow plugin unloading (HMR)
while its dialog was open left flowOpen stuck with nobody to cancel,
permanently disabling the workspace actions. Occupancy now rides
useSyncExternalStore over the hole's registration subscription, and an
empty hole withdraws an active flow; the menu entry also reacts to
activation without a reopen (ds-review-bot).
This commit is contained in:
creatixchu
2026-07-29 02:36:08 +08:00
parent 3f23347d10
commit 7114cc7475
7 changed files with 77 additions and 22 deletions
@@ -254,6 +254,7 @@ export function WorkspaceBrowser({
insertSessionBefore,
createWorkspace,
hasDirectoryFlow,
subscribeDirectoryFlow,
renderSlot,
}: WorkspaceBrowserProps) {
const workspaces = useWorkspaces(state => state.items)
@@ -373,6 +374,7 @@ export function WorkspaceBrowser({
useWorkspaces={useWorkspaces}
createWorkspace={createWorkspace}
hasDirectoryFlow={hasDirectoryFlow}
subscribeDirectoryFlow={subscribeDirectoryFlow}
renderDirectoryFlow={owner => renderSlot('sidebar.workspaces.directoryFlow', owner)}
createOnly
side="right"
@@ -7,7 +7,7 @@
* opens the flow, adopts the picked path, and owns the error surface.
*/
import type { ReactNode, RefObject } from 'react'
import { useCallback, useRef, useState } from 'react'
import { useCallback, useEffect, useRef, useState, useSyncExternalStore } from 'react'
import {
Button, IconFolderClose16, IconPlusOutline16, Menu, Modal, type MenuEntry,
} from '@deepseek-ai/dsh-client-ui-primitives'
@@ -33,8 +33,10 @@ export interface WorkspaceCreateFlowProps {
useWorkspaces: <S>(selector: (state: WorkspaceListState) => S) => S
/** Create or adopt a real Host Workspace. */
createWorkspace: (input: { name: string } | { path: string }) => Promise<WorkspaceView>
/** Whether this surface's directory-flow hole is occupied (read per menu render; empty hides the local-folder entry). */
/** Whether this surface's directory-flow hole is occupied (empty hides the local-folder entry). */
hasDirectoryFlow: () => boolean
/** Registration-change subscription for the same hole (the uSES pair of hasDirectoryFlow). */
subscribeDirectoryFlow: (listener: () => void) => () => void
/** Render this surface's directory-flow hole with the owner conversation (the entry's narrowed renderSlot). */
renderDirectoryFlow: (owner: DirectoryFlowOwnerProps) => ReactNode
/** A real Workspace was picked or created. */
@@ -60,6 +62,7 @@ export function WorkspaceCreateFlow({
useWorkspaces,
createWorkspace,
hasDirectoryFlow,
subscribeDirectoryFlow,
renderDirectoryFlow,
onPick,
onClose,
@@ -91,11 +94,17 @@ export function WorkspaceCreateFlow({
&& workspaces.some(workspace => workspace.title === normalizedWorkspaceName)
// The occupied hole gates the picking affordance: with no composed flow the
// entry simply is not there (the seam's documented no-flow default). Read
// per render while the menu is open — registrations land through plugin
// activation, and the menu re-renders on every toggle.
// entry simply is not there (the seam's documented no-flow default). The
// subscription keeps occupancy live: flow plugins activate (and HMR-reload)
// independently of this menu's renders.
const flowAvailable = useSyncExternalStore(subscribeDirectoryFlow, hasDirectoryFlow)
// An occupant that unloads mid-interaction leaves nobody to cancel: an
// open flow over an empty hole withdraws so the menu actions come back.
useEffect(() => {
if (!flowAvailable) setFlowOpen(false)
}, [flowAvailable])
const createEntries: MenuEntry[] = [
...(hasDirectoryFlow()
...(flowAvailable
? [{ id: OPEN_LOCAL_FOLDER, label: 'Open local folder…', icon: <IconFolderClose16 size={16} />, disabled: flowBusy }]
: []),
{ id: CREATE_NEW, label: 'Create a new workspace', icon: <IconPlusOutline16 size={16} />, disabled: flowBusy },
@@ -288,6 +297,7 @@ export function WorkspacePicker({
onClose,
createWorkspace,
hasDirectoryFlow,
subscribeDirectoryFlow,
renderSlot,
}: WorkspacePickerProps) {
return (
@@ -297,6 +307,7 @@ export function WorkspacePicker({
useWorkspaces={useWorkspaces}
createWorkspace={createWorkspace}
hasDirectoryFlow={hasDirectoryFlow}
subscribeDirectoryFlow={subscribeDirectoryFlow}
renderDirectoryFlow={owner => renderSlot('conversation.hero.workspace.directoryFlow', owner)}
selectedId={selectedId}
onPick={onPick}
@@ -62,11 +62,18 @@ export type DirectoryFlowSlotName =
/** Directory-picking share both trigger surfaces consume. */
export type DirectoryPickingInjected = {
/**
* Whether this surface's directory-flow hole is occupied — read when the
* menu opens; an empty hole hides the "Open local folder…" entry (the
* no-flow composition simply has no picking affordance).
* Whether this surface's directory-flow hole is occupied — an empty hole
* hides the "Open local folder…" entry (the no-flow composition simply has
* no picking affordance).
*/
hasDirectoryFlow: () => boolean
/**
* Subscribe to the hole's registration changes (the uSES pair of
* {@link hasDirectoryFlow}): the trigger surface withdraws an open flow
* whose occupant unloaded mid-interaction — nobody is left to cancel it.
* @returns the unsubscriber.
*/
subscribeDirectoryFlow: (listener: () => void) => () => void
}
/**
@@ -49,10 +49,12 @@ export function apply(ctx: ClientContext): void {
},
createWorkspace: input => ctx.workspaces.create(input),
hasDirectoryFlow: () => ctx.slots.entries('sidebar.workspaces.directoryFlow').length > 0,
subscribeDirectoryFlow: listener => ctx.slots.subscribe('sidebar.workspaces.directoryFlow', listener),
})
const pickerInjected = (): WorkspacePickerInjected => ({
createWorkspace: input => ctx.workspaces.create(input),
hasDirectoryFlow: () => ctx.slots.entries('conversation.hero.workspace.directoryFlow').length > 0,
subscribeDirectoryFlow: listener => ctx.slots.subscribe('conversation.hero.workspace.directoryFlow', listener),
})
// Declaration-aware registration (deferRegistration): each owner's
// declaring apply may activate after this one, and a register into an