Files
DeepTutor/web/hooks/useWorkspaceBinding.ts
Bingxi Zhao (Frank) da856ad670 release: v1.6.9
Ship folder-based workspaces and data migration, daily practice, redesigned
provider and model settings, and streaming conversation improvements.
Synchronize README translations and document the archive/permanent-delete
upgrade behavior in assets/releases/ver1-6-9.md.

Reduce initial JavaScript by deduplicating English resources and loading
animation features after hydration. Keep the existing route size budgets.
Update regression fixtures and browser audits for scoped URLs and the new
settings and learning interfaces.
2026-09-21 02:30:40 +08:00

58 lines
2.4 KiB
TypeScript

'use client'
import { useCallback, useLayoutEffect, useRef, useState } from 'react'
import { transferWorkspaceDraft } from '@/lib/workspace-drafts'
import { updateSessionOrganization } from '@/lib/session-api'
import { selectWorkspace as navigateWorkspace } from '@/lib/workspace-scope'
type BindingState = { sessionKey: string; sessionId: string | null; isStreaming: boolean }
/** Persist a move before changing its source session, even if navigation occurs meanwhile. */
export function useWorkspaceBinding(
state: BindingState,
configureSession: (patch: { workspaceId: string | null }, key: string) => void,
) {
const [failure, setFailure] = useState<{ key: string; message: string } | null>(null)
const [pendingKey, setPendingKey] = useState<string | null>(null)
const pending = useRef(new Set<string>())
const currentState = useRef(state)
const mounted = useRef(true)
useLayoutEffect(() => { currentState.current = state }, [state])
useLayoutEffect(() => {
mounted.current = true
return () => { mounted.current = false }
}, [])
const selectWorkspace = useCallback((workspaceId: string) => {
const key = state.sessionKey
const sid = state.sessionId
if (state.isStreaming || pending.current.has(key)) return
setFailure(null)
const apply = () => configureSession({ workspaceId: workspaceId || null }, key)
if (!sid) {
// The destination URL creates the draft in its scope after navigation.
// Do not rebind the source runtime before its draft has been saved.
void navigateWorkspace(workspaceId, '/chat', { beforeNavigate: () => transferWorkspaceDraft(workspaceId) })
return
}
pending.current.add(key)
setPendingKey(key)
void updateSessionOrganization(sid, { workspace_id: workspaceId || null })
.then(() => {
apply()
if (mounted.current && currentState.current.sessionKey === key) {
void navigateWorkspace(workspaceId, `/chat/${encodeURIComponent(sid)}`)
}
})
.catch((err: unknown) => setFailure({ key, message: err instanceof Error ? err.message : String(err) }))
.finally(() => {
pending.current.delete(key)
setPendingKey(current => current === key ? null : current)
})
}, [configureSession, state.isStreaming, state.sessionId, state.sessionKey])
return {
selectWorkspace,
error: failure?.key === state.sessionKey ? failure.message : '',
pending: pendingKey === state.sessionKey,
}
}