@@ -17,7 +17,7 @@ import { useChatHistory, useMarkTaskRead } from '@/hooks/queries/tasks'
1717import type { ChatContext } from '@/stores/panel'
1818import { MothershipChat , MothershipView , TemplatePrompts , UserInput } from './components'
1919import { getMothershipUseChatOptions , useChat , useMothershipResize } from './hooks'
20- import type { FileAttachmentForApi , MothershipResource , MothershipResourceType } from './types'
20+ import type { FileAttachmentForApi , MothershipResourceType } from './types'
2121
2222const logger = createLogger ( 'Home' )
2323
@@ -261,51 +261,42 @@ export function Home({ chatId }: HomeProps = {}) {
261261 return ( ) => window . removeEventListener ( 'mothership-send-message' , handler )
262262 } , [ sendMessage ] )
263263
264- const handleContextAdd = useCallback (
265- ( context : ChatContext ) => {
266- let resourceType : MothershipResourceType | null = null
267- let resourceId : string | null = null
268- const resourceTitle : string = context . label
269-
264+ const resolveResourceFromContext = useCallback (
265+ ( context : ChatContext ) : { type : MothershipResourceType ; id : string } | null => {
270266 switch ( context . kind ) {
271267 case 'workflow' :
272268 case 'current_workflow' :
273- resourceType = 'workflow'
274- resourceId = context . workflowId
275- break
269+ return context . workflowId ? { type : 'workflow' , id : context . workflowId } : null
276270 case 'knowledge' :
277- if ( context . knowledgeId ) {
278- resourceType = 'knowledgebase'
279- resourceId = context . knowledgeId
280- }
281- break
271+ return context . knowledgeId ? { type : 'knowledgebase' , id : context . knowledgeId } : null
282272 case 'table' :
283- if ( context . tableId ) {
284- resourceType = 'table'
285- resourceId = context . tableId
286- }
287- break
273+ return context . tableId ? { type : 'table' , id : context . tableId } : null
288274 case 'file' :
289- if ( context . fileId ) {
290- resourceType = 'file'
291- resourceId = context . fileId
292- }
293- break
275+ return context . fileId ? { type : 'file' , id : context . fileId } : null
294276 default :
295- break
277+ return null
296278 }
279+ } ,
280+ [ ]
281+ )
297282
298- if ( resourceType && resourceId ) {
299- const resource : MothershipResource = {
300- type : resourceType ,
301- id : resourceId ,
302- title : resourceTitle ,
303- }
304- addResource ( resource )
283+ const handleContextAdd = useCallback (
284+ ( context : ChatContext ) => {
285+ const resolved = resolveResourceFromContext ( context )
286+ if ( resolved ) {
287+ addResource ( { ...resolved , title : context . label } )
305288 handleResourceEvent ( )
306289 }
307290 } ,
308- [ addResource , handleResourceEvent ]
291+ [ resolveResourceFromContext , addResource , handleResourceEvent ]
292+ )
293+
294+ const handleContextRemove = useCallback (
295+ ( context : ChatContext ) => {
296+ const resolved = resolveResourceFromContext ( context )
297+ if ( resolved ) removeResource ( resolved . type , resolved . id )
298+ } ,
299+ [ resolveResourceFromContext , removeResource ]
309300 )
310301
311302 const hasMessages = messages . length > 0
@@ -345,6 +336,7 @@ export function Home({ chatId }: HomeProps = {}) {
345336 onStopGeneration = { handleStopGeneration }
346337 userId = { session ?. user ?. id }
347338 onContextAdd = { handleContextAdd }
339+ onContextRemove = { handleContextRemove }
348340 />
349341 </ div >
350342 </ div >
@@ -375,6 +367,7 @@ export function Home({ chatId }: HomeProps = {}) {
375367 userId = { session ?. user ?. id }
376368 chatId = { resolvedChatId }
377369 onContextAdd = { handleContextAdd }
370+ onContextRemove = { handleContextRemove }
378371 editValue = { editingInputValue }
379372 onEditValueConsumed = { clearEditingValue }
380373 animateInput = { isInputEntering }
0 commit comments