Skip to content

Commit e486a22

Browse files
committed
fix(ui): synchronize resource selection updates
1 parent ff00d33 commit e486a22

3 files changed

Lines changed: 28 additions & 7 deletions

File tree

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ import { captureEvent } from '@/lib/posthog/client'
3636
import { persistImportedWorkflow } from '@/lib/workflows/operations/import-export'
3737
import { RESOURCE_HEADER_CLASSES } from '@/app/workspace/[workspaceId]/home/components/mothership-view/components/resource-tabs/resource-tab-controls'
3838
import { resolveWorkspaceResourceRef } from '@/app/workspace/[workspaceId]/home/resolve-resource-ref'
39-
import { resolveResourceEventPresentation } from '@/app/workspace/[workspaceId]/home/resource-view-policy'
39+
import {
40+
resolveResourceEventPresentation,
41+
resolveResourceSelectionUpdate,
42+
} from '@/app/workspace/[workspaceId]/home/resource-view-policy'
4043
import { resourceParam, resourceUrlKeys } from '@/app/workspace/[workspaceId]/home/search-params'
4144
import { useFolders } from '@/hooks/queries/folders'
4245
import { useMarkMothershipChatRead } from '@/hooks/queries/mothership-chats'
@@ -103,6 +106,8 @@ export function Home({ chatId, userName, userId }: HomeProps) {
103106
...resourceParam.parser,
104107
...resourceUrlKeys,
105108
})
109+
const activeResourceParamRef = useRef(activeResourceParam)
110+
activeResourceParamRef.current = activeResourceParam
106111
/**
107112
* Strips any leftover URL fragment on selection change, preserving the old
108113
* effect's `url.hash = ''` (the only hash usage on this surface) without a
@@ -115,11 +120,13 @@ export function Home({ chatId, userName, userId }: HomeProps) {
115120
*/
116121
const setActiveResourceUrl = useCallback<Dispatch<SetStateAction<string | null>>>(
117122
(action) => {
123+
const nextResourceId = resolveResourceSelectionUpdate(activeResourceParamRef.current, action)
124+
activeResourceParamRef.current = nextResourceId
118125
if (typeof window !== 'undefined' && window.location.hash) {
119126
const { pathname, search } = window.location
120127
window.history.replaceState(window.history.state, '', `${pathname}${search}`)
121128
}
122-
void setResourceParam(action)
129+
void setResourceParam(nextResourceId)
123130
},
124131
[setResourceParam]
125132
)
@@ -213,8 +220,6 @@ export function Home({ chatId, userName, userId }: HomeProps) {
213220
}, [])
214221
const resourceCollapseOwnedByUserRef = useRef(false)
215222
const resourceSelectionOwnedByUserRef = useRef(false)
216-
const activeResourceParamRef = useRef(activeResourceParam)
217-
activeResourceParamRef.current = activeResourceParam
218223

219224
function handleResourceEvent(resourceId: string, options?: ResourceEventOptions) {
220225
const activeResourceId = activeResourceParamRef.current

apps/sim/app/workspace/[workspaceId]/home/resource-view-policy.test.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { describe, expect, it } from 'vitest'
2-
import { resolveResourceEventPresentation } from '@/app/workspace/[workspaceId]/home/resource-view-policy'
2+
import {
3+
resolveResourceEventPresentation,
4+
resolveResourceSelectionUpdate,
5+
} from '@/app/workspace/[workspaceId]/home/resource-view-policy'
36

47
const DEFAULT_INPUT = {
58
activeResourceId: 'file-1',
@@ -80,11 +83,15 @@ describe('resolveResourceEventPresentation', () => {
8083
})
8184
})
8285

83-
it('follows agent work after the user-selected resource is removed', () => {
86+
it('follows same-batch agent work after the user-selected resource is removed', () => {
87+
const activeResourceId = resolveResourceSelectionUpdate('file-1', (currentResourceId) =>
88+
currentResourceId === 'file-1' ? null : currentResourceId
89+
)
90+
8491
expect(
8592
resolveResourceEventPresentation({
8693
...DEFAULT_INPUT,
87-
activeResourceId: null,
94+
activeResourceId,
8895
selectionOwnedByUser: true,
8996
})
9097
).toEqual({

apps/sim/app/workspace/[workspaceId]/home/resource-view-policy.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
import type { SetStateAction } from 'react'
2+
3+
export function resolveResourceSelectionUpdate(
4+
currentResourceId: string | null,
5+
update: SetStateAction<string | null>
6+
): string | null {
7+
return typeof update === 'function' ? update(currentResourceId) : update
8+
}
9+
110
export interface ResourceEventPresentationInput {
211
activeResourceId: string | null
312
activationRequested: boolean

0 commit comments

Comments
 (0)