MCPcopy Create free account
hub / github.com/codeaashu/claude-code / useSessionBackgrounding

Function useSessionBackgrounding

src/hooks/useSessionBackgrounding.ts:27–158  ·  view source on GitHub ↗
({
  setMessages,
  setIsLoading,
  resetLoadingState,
  setAbortController,
  onBackgroundQuery,
}: UseSessionBackgroundingProps)

Source from the content-addressed store, hash-verified

25}
26
27export function useSessionBackgrounding({
28 setMessages,
29 setIsLoading,
30 resetLoadingState,
31 setAbortController,
32 onBackgroundQuery,
33}: UseSessionBackgroundingProps): UseSessionBackgroundingResult {
34 const foregroundedTaskId = useAppState(s => s.foregroundedTaskId)
35 const foregroundedTask = useAppState(s =>
36 s.foregroundedTaskId ? s.tasks[s.foregroundedTaskId] : undefined,
37 )
38 const setAppState = useSetAppState()
39 const lastSyncedMessagesLengthRef = useRef<number>(0)
40
41 const handleBackgroundSession = useCallback(() => {
42 if (foregroundedTaskId) {
43 // Re-background the foregrounded task
44 setAppState(prev => {
45 const taskId = prev.foregroundedTaskId
46 if (!taskId) return prev
47 const task = prev.tasks[taskId]
48 if (!task) {
49 return { ...prev, foregroundedTaskId: undefined }
50 }
51 return {
52 ...prev,
53 foregroundedTaskId: undefined,
54 tasks: {
55 ...prev.tasks,
56 [taskId]: { ...task, isBackgrounded: true },
57 },
58 }
59 })
60 setMessages([])
61 resetLoadingState()
62 setAbortController(null)
63 return
64 }
65
66 onBackgroundQuery()
67 }, [
68 foregroundedTaskId,
69 setAppState,
70 setMessages,
71 resetLoadingState,
72 setAbortController,
73 onBackgroundQuery,
74 ])
75
76 // Sync foregrounded task's messages and loading state to the main view
77 useEffect(() => {
78 if (!foregroundedTaskId) {
79 // Reset when no foregrounded task
80 lastSyncedMessagesLengthRef.current = 0
81 return
82 }
83
84 if (!foregroundedTask || foregroundedTask.type !== 'local_agent') {

Callers 1

REPLFunction · 0.85

Calls 2

useAppStateFunction · 0.85
useSetAppStateFunction · 0.85

Tested by

no test coverage detected