Skip to content

Commit 1d4ddfe

Browse files
committed
fix(organizations): preserve chat context during pagination
1 parent 95047af commit 1d4ddfe

3 files changed

Lines changed: 38 additions & 6 deletions

File tree

apps/sim/app/o/[organizationId]/components/organization-sidebar/components/chats-section/chats-section.test.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,36 @@ describe('ChatsSection', () => {
122122
expect(other?.className).not.toContain('surface-active')
123123
})
124124

125+
it('keeps a bookmarked chat visible when collapsing expanded history', async () => {
126+
await render({ pathname: CHATS[5].href })
127+
expect(container.querySelectorAll('a[href^="/o/org-1/chat/"]')).toHaveLength(6)
128+
expect(container.querySelector(`a[href="${CHATS[5].href}"]`)?.className).toContain(
129+
'surface-active'
130+
)
131+
const more = Array.from(container.querySelectorAll('button')).find(
132+
(button) => button.textContent === 'See more'
133+
)!
134+
await act(async () => more.click())
135+
expect(container.querySelectorAll('a[href^="/o/org-1/chat/"]')).toHaveLength(8)
136+
const less = Array.from(container.querySelectorAll('button')).find(
137+
(button) => button.textContent === 'See less'
138+
)!
139+
await act(async () => less.click())
140+
expect(container.querySelectorAll('a[href^="/o/org-1/chat/"]')).toHaveLength(6)
141+
expect(container.querySelector(`a[href="${CHATS[5].href}"]`)).not.toBeNull()
142+
})
143+
144+
it('derives the visible range from the route without retaining automatic expansion', async () => {
145+
await render({ pathname: CHATS[7].href })
146+
expect(container.querySelectorAll('a[href^="/o/org-1/chat/"]')).toHaveLength(8)
147+
expect(container.textContent).not.toContain('See more')
148+
expect(container.textContent).not.toContain('See less')
149+
150+
await render({ pathname: null })
151+
expect(container.querySelectorAll('a[href^="/o/org-1/chat/"]')).toHaveLength(5)
152+
expect(container.textContent).toContain('See more')
153+
})
154+
125155
it.each([false, true])('renames via the options menu with collapsed=%s', async (isCollapsed) => {
126156
hoverState.isOpen = isCollapsed
127157
await render({ isCollapsed })

apps/sim/app/o/[organizationId]/components/organization-sidebar/components/chats-section/chats-section.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,10 @@ export function ChatsSection({
128128
}: ChatsSectionProps) {
129129
const actions = useOrganizationChatActions({ organizationId, chats })
130130
const { menu, hover, rename, selectedChat } = actions
131-
const [visibleCount, setVisibleCount] = useState(PAGE_SIZE)
131+
const [requestedCount, setRequestedCount] = useState(PAGE_SIZE)
132+
const minimumCount = Math.max(PAGE_SIZE, chats.findIndex((chat) => chat.href === pathname) + 1)
133+
const visibleCount = Math.min(chats.length, Math.max(requestedCount, minimumCount))
134+
const hasMore = chats.length > visibleCount
132135
const menuOpenChatId = menu.isOpen ? selectedChat?.id : null
133136
const saveRename = () => {
134137
void rename.saveRename()
@@ -217,16 +220,14 @@ export function ChatsSection({
217220
/>
218221
)
219222
)}
220-
{chats.length > PAGE_SIZE && (
223+
{(hasMore || visibleCount > minimumCount) && (
221224
<Chip
222225
fullWidth
223226
onClick={() =>
224-
setVisibleCount(
225-
chats.length > visibleCount ? visibleCount + PAGE_SIZE : PAGE_SIZE
226-
)
227+
setRequestedCount(hasMore ? visibleCount + PAGE_SIZE : PAGE_SIZE)
227228
}
228229
>
229-
{chats.length > visibleCount ? 'See more' : 'See less'}
230+
{hasMore ? 'See more' : 'See less'}
230231
</Chip>
231232
)}
232233
</>

apps/sim/app/o/[organizationId]/components/organization-sidebar/organization-sidebar.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ export const OrganizationSidebar = memo(function OrganizationSidebar() {
266266
/>
267267
{searchAccess.memberScoped && (
268268
<OrganizationChats
269+
key={organization.id}
269270
organizationId={organization.id}
270271
isCollapsed={isCollapsed}
271272
pathname={pathname}

0 commit comments

Comments
 (0)