Skip to content

Commit 7b8fa00

Browse files
committed
fix(chat): remove resource panel tab when context mention is deleted from input
1 parent 4ff9b84 commit 7b8fa00

File tree

3 files changed

+44
-34
lines changed

3 files changed

+44
-34
lines changed

apps/sim/app/workspace/[workspaceId]/home/components/mothership-chat/mothership-chat.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ interface MothershipChatProps {
3737
userId?: string
3838
chatId?: string
3939
onContextAdd?: (context: ChatContext) => void
40+
onContextRemove?: (context: ChatContext) => void
4041
editValue?: string
4142
onEditValueConsumed?: () => void
4243
layout?: 'mothership-view' | 'copilot-view'
@@ -83,6 +84,7 @@ export function MothershipChat({
8384
userId,
8485
chatId,
8586
onContextAdd,
87+
onContextRemove,
8688
editValue,
8789
onEditValueConsumed,
8890
layout = 'mothership-view',
@@ -207,6 +209,7 @@ export function MothershipChat({
207209
isInitialView={false}
208210
userId={userId}
209211
onContextAdd={onContextAdd}
212+
onContextRemove={onContextRemove}
210213
editValue={editValue}
211214
onEditValueConsumed={onEditValueConsumed}
212215
onEnterWhileEmpty={handleEnterWhileEmpty}

apps/sim/app/workspace/[workspaceId]/home/components/user-input/user-input.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ interface UserInputProps {
108108
isInitialView?: boolean
109109
userId?: string
110110
onContextAdd?: (context: ChatContext) => void
111+
onContextRemove?: (context: ChatContext) => void
111112
onEnterWhileEmpty?: () => boolean
112113
}
113114

@@ -121,6 +122,7 @@ export function UserInput({
121122
isInitialView = true,
122123
userId,
123124
onContextAdd,
125+
onContextRemove,
124126
onEnterWhileEmpty,
125127
}: UserInputProps) {
126128
const { workspaceId } = useParams<{ workspaceId: string }>()
@@ -170,6 +172,18 @@ export function UserInput({
170172
[addContext, onContextAdd]
171173
)
172174

175+
const onContextRemoveRef = useRef(onContextRemove)
176+
onContextRemoveRef.current = onContextRemove
177+
178+
const prevSelectedContextsRef = useRef<ChatContext[]>([])
179+
useEffect(() => {
180+
const prev = prevSelectedContextsRef.current
181+
const curr = contextManagement.selectedContexts
182+
const removed = prev.filter((p) => !curr.some((c) => c.kind === p.kind && c.label === p.label))
183+
if (removed.length > 0) removed.forEach((ctx) => onContextRemoveRef.current?.(ctx))
184+
prevSelectedContextsRef.current = curr
185+
}, [contextManagement.selectedContexts])
186+
173187
const existingResourceKeys = useMemo(() => {
174188
const keys = new Set<string>()
175189
for (const ctx of contextManagement.selectedContexts) {

apps/sim/app/workspace/[workspaceId]/home/home.tsx

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { useChatHistory, useMarkTaskRead } from '@/hooks/queries/tasks'
1717
import type { ChatContext } from '@/stores/panel'
1818
import { MothershipChat, MothershipView, TemplatePrompts, UserInput } from './components'
1919
import { getMothershipUseChatOptions, useChat, useMothershipResize } from './hooks'
20-
import type { FileAttachmentForApi, MothershipResource, MothershipResourceType } from './types'
20+
import type { FileAttachmentForApi, MothershipResourceType } from './types'
2121

2222
const 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

Comments
 (0)