Skip to content

Commit 838c936

Browse files
committed
fix(tables): reconcile agent view pins
1 parent 3e6eb75 commit 838c936

3 files changed

Lines changed: 46 additions & 4 deletions

File tree

apps/sim/app/workspace/[workspaceId]/tables/[tableId]/table.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import { useUserPermissionsContext } from '@/app/workspace/[workspaceId]/provide
4444
import {
4545
getTableViewRevision,
4646
resolveTableViewConfig,
47+
resolveTableViewPinTransition,
4748
resolveTableViewSelection,
4849
shouldApplyTableViewRevision,
4950
type TableViewRevision,
@@ -719,11 +720,16 @@ export function Table({
719720
if (appliedViewRevisionRef.current === undefined) return
720721
if (!views.some((view) => view.id === viewPin.viewId)) return
721722
consumeViewPin(tableId, viewPin.seq)
722-
if (activeViewId === viewPin.viewId || appliedViewRevisionRef.current.id === viewPin.viewId) {
723-
return
724-
}
723+
const transition = resolveTableViewPinTransition(
724+
activeViewId,
725+
appliedViewRevisionRef.current.id,
726+
viewPin.viewId,
727+
pendingCreatedViewIdRef.current
728+
)
729+
if (!transition.nextViewId) return
730+
pendingCreatedViewIdRef.current = transition.pendingCreatedViewId
725731
preservedViewStateRef.current = null
726-
setTableParams({ view: viewPin.viewId })
732+
setTableParams({ view: transition.nextViewId })
727733
}, [embedded, viewPin, views, activeViewId, tableId, consumeViewPin, setTableParams])
728734

729735
/**

apps/sim/app/workspace/[workspaceId]/tables/[tableId]/view-state.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { ALL_VIEW_PARAM } from '@/app/workspace/[workspaceId]/tables/[tableId]/s
77
import {
88
getTableViewRevision,
99
resolveTableViewConfig,
10+
resolveTableViewPinTransition,
1011
resolveTableViewSelection,
1112
shouldApplyTableViewRevision,
1213
} from '@/app/workspace/[workspaceId]/tables/[tableId]/view-state'
@@ -79,6 +80,20 @@ describe('resolveTableViewSelection', () => {
7980
})
8081
})
8182

83+
describe('resolveTableViewPinTransition', () => {
84+
it('abandons a pending local creation when an external pin replaces its URL selection', () => {
85+
expect(
86+
resolveTableViewPinTransition('view-old', 'view-created', 'view-pinned', 'view-created')
87+
).toEqual({ nextViewId: 'view-pinned', pendingCreatedViewId: null })
88+
})
89+
90+
it('keeps the pending creation when the pin is already represented locally', () => {
91+
expect(
92+
resolveTableViewPinTransition('view-pinned', 'view-created', 'view-pinned', 'view-created')
93+
).toEqual({ nextViewId: null, pendingCreatedViewId: 'view-created' })
94+
})
95+
})
96+
8297
describe('shouldApplyTableViewRevision', () => {
8398
const cached = {
8499
id: 'view-1',

apps/sim/app/workspace/[workspaceId]/tables/[tableId]/view-state.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,27 @@ export interface TableViewRevision {
4949
updatedAt: number | null
5050
}
5151

52+
export interface TableViewPinTransition {
53+
nextViewId: string | null
54+
pendingCreatedViewId: string | null
55+
}
56+
57+
/**
58+
* Resolves an external saved-view pin without leaving a locally created view
59+
* waiting for a URL selection that the pin is about to replace.
60+
*/
61+
export function resolveTableViewPinTransition(
62+
activeViewId: string | null,
63+
appliedViewId: string | null,
64+
pinnedViewId: string,
65+
pendingCreatedViewId: string | null
66+
): TableViewPinTransition {
67+
if (activeViewId === pinnedViewId || appliedViewId === pinnedViewId) {
68+
return { nextViewId: null, pendingCreatedViewId }
69+
}
70+
return { nextViewId: pinnedViewId, pendingCreatedViewId: null }
71+
}
72+
5273
export function getTableViewRevision(
5374
view: Pick<TableViewWire, 'id' | 'updatedAt'> | null
5475
): TableViewRevision {

0 commit comments

Comments
 (0)