Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix: preserve topic pagination state after topic actions and new topic creation (#13463)

AnotiaWang94bd7b2f6bf3
diff --git a/src/routes/(main)/agent/_layout/Sidebar/Topic/AllTopicsDrawer/Content.tsx b/src/routes/(main)/agent/_layout/Sidebar/Topic/AllTopicsDrawer/Content.tsx
index 149901d46a5..6a85f599173 100644
--- a/src/routes/(main)/agent/_layout/Sidebar/Topic/AllTopicsDrawer/Content.tsx
+++ b/src/routes/(main)/agent/_layout/Sidebar/Topic/AllTopicsDrawer/Content.tsx
@@ -74,6 +74,12 @@ const Content = memo<ContentProps>(({ open, searchKeyword }) => {
const activeTopicList = isSearching ? searchResults : allTopicList;
const count = activeTopicList?.length || 0;
+ useEffect(() => {
+ if (fetchedCountRef.current > count) {
+ fetchedCountRef.current = count - 1;
+ }
+ }, [count]);
+
// Initial load: calculate how many items needed to fill viewport
useEffect(() => {
if (!open || initializedRef.current || isLoadingMore || isSearching) return;
diff --git a/src/routes/(main)/agent/_layout/Sidebar/Topic/TopicListContent/FlatMode/index.tsx b/src/routes/(main)/agent/_layout/Sidebar/Topic/TopicListContent/FlatMode/index.tsx
index 2ffc6b4aee2..0667fe8eda7 100644
--- a/src/routes/(main)/agent/_layout/Sidebar/Topic/TopicListContent/FlatMode/index.tsx
+++ b/src/routes/(main)/agent/_layout/Sidebar/Topic/TopicListContent/FlatMode/index.tsx
@@ -26,7 +26,7 @@ const FlatMode = memo(() => {
useChatStore((s) => [
s.activeTopicId,
s.activeThreadId,
- topicSelectors.hasMoreTopics(s),
+ topicSelectors.hasMoreTopicsForSidebar(s),
topicSelectors.isExpandingPageSize(s),
s.openAllTopicsDrawer,
]);
diff --git a/src/routes/(main)/agent/_layout/Sidebar/Topic/TopicListContent/GroupedAccordion.tsx b/src/routes/(main)/agent/_layout/Sidebar/Topic/TopicListContent/GroupedAccordion.tsx
index 71431c18d82..517fd539597 100644
--- a/src/routes/(main)/agent/_layout/Sidebar/Topic/TopicListContent/GroupedAccordion.tsx
+++ b/src/routes/(main)/agent/_layout/Sidebar/Topic/TopicListContent/GroupedAccordion.tsx
@@ -35,7 +35,7 @@ const GroupedAccordion = memo<GroupedAccordionProps>(({ GroupItem }) => {
const { topicGroupMode } = useAgentTopicGroupMode();
const [hasMore, isExpandingPageSize, openAllTopicsDrawer] = useChatStore((s) => [
- topicSelectors.hasMoreTopics(s),
+ topicSelectors.hasMoreTopicsForSidebar(s),
topicSelectors.isExpandingPageSize(s),
s.openAllTopicsDrawer,
]);
diff --git a/src/routes/(main)/group/_layout/Sidebar/Topic/AllTopicsDrawer/Content.tsx b/src/routes/(main)/group/_layout/Sidebar/Topic/AllTopicsDrawer/Content.tsx
index 84e541c16ef..6b8ae84108d 100644
--- a/src/routes/(main)/group/_layout/Sidebar/Topic/AllTopicsDrawer/Content.tsx
+++ b/src/routes/(main)/group/_layout/Sidebar/Topic/AllTopicsDrawer/Content.tsx
@@ -74,6 +74,12 @@ const Content = memo<ContentProps>(({ open, searchKeyword }) => {
const activeTopicList = isSearching ? searchResults : allTopicList;
const count = activeTopicList?.length || 0;
+ useEffect(() => {
+ if (fetchedCountRef.current > count) {
+ fetchedCountRef.current = count - 1;
+ }
+ }, [count]);
+
// Initial load: calculate how many items needed to fill viewport
useEffect(() => {
if (!open || initializedRef.current || isLoadingMore || isSearching) return;
diff --git a/src/routes/(main)/group/_layout/Sidebar/Topic/TopicListContent/ByTimeMode/index.tsx b/src/routes/(main)/group/_layout/Sidebar/Topic/TopicListContent/ByTimeMode/index.tsx
index 913d37cfa38..54ced5e0234 100644
--- a/src/routes/(main)/group/_layout/Sidebar/Topic/TopicListContent/ByTimeMode/index.tsx
+++ b/src/routes/(main)/group/_layout/Sidebar/Topic/TopicListContent/ByTimeMode/index.tsx
@@ -24,7 +24,7 @@ const ByTimeMode = memo(() => {
const topicGroupMode = useUserStore(preferenceSelectors.topicGroupMode);
const [hasMore, isExpandingPageSize, openAllTopicsDrawer] = useChatStore((s) => [
- topicSelectors.hasMoreTopics(s),
+ topicSelectors.hasMoreTopicsForSidebar(s),
topicSelectors.isExpandingPageSize(s),
s.openAllTopicsDrawer,
]);
diff --git a/src/routes/(main)/group/_layout/Sidebar/Topic/TopicListContent/FlatMode/index.tsx b/src/routes/(main)/group/_layout/Sidebar/Topic/TopicListContent/FlatMode/index.tsx
index 73070773949..58297e5d810 100644
--- a/src/routes/(main)/group/_layout/Sidebar/Topic/TopicListContent/FlatMode/index.tsx
+++ b/src/routes/(main)/group/_layout/Sidebar/Topic/TopicListContent/FlatMode/index.tsx
@@ -26,7 +26,7 @@ const FlatMode = memo(() => {
useChatStore((s) => [
s.activeTopicId,
s.activeThreadId,
- topicSelectors.hasMoreTopics(s),
+ topicSelectors.hasMoreTopicsForSidebar(s),
topicSelectors.isExpandingPageSize(s),
s.openAllTopicsDrawer,
]);
diff --git a/src/store/chat/slices/topic/action.test.ts b/src/store/chat/slices/topic/action.test.ts
index e68306e2463..3609f7e3456 100644
--- a/src/store/chat/slices/topic/action.test.ts
+++ b/src/store/chat/slices/topic/action.test.ts
@@ -71,7 +71,11 @@ beforeEach(() => {
useChatStore.setState(
{
activeAgentId: undefined,
+ activeGroupId: undefined,
activeTopicId: undefined,
+ searchTopics: [],
+ topicDataMap: {},
+ topicLoadingIds: [],
// ... initial state
},
false,
@@ -477,6 +481,255 @@ describe('topic action', () => {
useChatStore.getState().topicDataMap[topicMapKey({ agentId: sessionId })]?.items,
).toEqual(topics);
});
+
+ it('should preserve expanded topic list when first page revalidates after deletion', async () => {
+ const agentId = 'expanded-delete-agent';
+ const pageSize = 20;
+ const currentTopics = [
+ ...Array.from({ length: 19 }, (_, index) => ({
+ id: `topic-${index + 1}`,
+ title: `Topic ${index + 1}`,
+ })),
+ ...Array.from({ length: 20 }, (_, index) => ({
+ id: `topic-${index + 21}`,
+ title: `Topic ${index + 21}`,
+ })),
+ ] as ChatTopic[];
+ const refreshedFirstPage = [
+ ...Array.from({ length: 19 }, (_, index) => ({
+ id: `topic-${index + 1}`,
+ title: `Topic ${index + 1}`,
+ })),
+ { id: 'topic-21', title: 'Topic 21' },
+ ];
+
+ act(() => {
+ useChatStore.setState({
+ activeAgentId: agentId,
+ topicDataMap: {
+ [topicMapKey({ agentId })]: {
+ currentPage: 1,
+ excludeTriggers: ['cron', 'eval'],
+ hasMore: true,
+ isInbox: false,
+ items: currentTopics,
+ pageSize,
+ total: 59,
+ },
+ },
+ });
+ });
+
+ (topicService.getTopics as Mock).mockResolvedValue({
+ items: refreshedFirstPage,
+ total: 59,
+ });
+
+ const useFetchTopics = useChatStore.getState().useFetchTopics;
+
+ const swrResponse = renderHook(() =>
+ useFetchTopics(true, { agentId, excludeTriggers: ['cron', 'eval'], pageSize }),
+ );
+
+ await waitFor(() => {
+ expect(swrResponse.result.current.data).toEqual({
+ items: refreshedFirstPage,
+ total: 59,
+ });
+ });
+
+ await waitFor(() => {
+ const topicData = useChatStore.getState().topicDataMap[topicMapKey({ agentId })];
+
+ expect(topicData).toMatchObject({
+ currentPage: 1,
+ hasMore: true,
+ total: 59,
+ });
+ expect(topicData.items).toHaveLength(39);
+ expect(topicData.items.map((topic) => topic.id)).toEqual([
+ ...Array.from({ length: 19 }, (_, index) => `topic-${index + 1}`),
+ ...Array.from({ length: 20 }, (_, index) => `topic-${index + 21}`),
+ ]);
+ });
+ });
+
+ it('should preserve expanded topic list when first page reorders after favorite refresh', async () => {
+ const agentId = 'favorite-agent';
+ const pageSize = 20;
+ const currentTopics = Array.from({ length: 40 }, (_, index) => ({
+ favorite: index === 34,
+ id: `topic-${index + 1}`,
+ title: `Topic ${index + 1}`,
+ })) as ChatTopic[];
+ const refreshedFirstPage = [
+ { favorite: true, id: 'topic-35', title: 'Topic 35' },
+ ...Array.from({ length: 19 }, (_, index) => ({
+ favorite: false,
+ id: `topic-${index + 1}`,
+ title: `Topic ${index + 1}`,
+ })),
+ ];
+
+ act(() => {
+ useChatStore.setState({
+ activeAgentId: agentId,
+ topicDataMap: {
+ [topicMapKey({ agentId })]: {
+ currentPage: 1,
+ excludeTriggers: ['cron', 'eval'],
+ hasMore: true,
+ isInbox: false,
+ items: currentTopics,
+ pageSize,
+ total: 60,
+ },
+ },
+ });
+ });
+
+ (topicService.getTopics as Mock).mockResolvedValue({
+ items: refreshedFirstPage,
+ total: 60,
+ });
+
+ const useFetchTopics = useChatStore.getState().useFetchTopics;
+ const swrResponse = renderHook(() =>
+ useFetchTopics(true, { agentId, excludeTriggers: ['cron', 'eval'], pageSize }),
+ );
+
+ await waitFor(() => {
+ expect(swrResponse.result.current.data).toEqual({
+ items: refreshedFirstPage,
+ total: 60,
+ });
+ });
+
+ await waitFor(() => {
+ const topicData = useChatStore.getState().topicDataMap[topicMapKey({ agentId })];
+
+ expect(topicData).toMatchObject({
+ currentPage: 1,
+ hasMore: true,
+ total: 60,
+ });
+ expect(topicData.items).toHaveLength(40);
+ expect(topicData.items[0].id).toBe('topic-35');
+ expect(topicData.items.some((topic) => topic.id === 'topic-40')).toBe(true);
+ });
+ });
+
+ it('should reset expanded pagination when excludeTriggers changes for the same agent', async () => {
+ const agentId = 'filtered-agent';
+ const pageSize = 20;
+ const currentTopics = Array.from({ length: 40 }, (_, index) => ({
+ id: `topic-${index + 1}`,
+ title: `Topic ${index + 1}`,
+ })) as ChatTopic[];
+ const refreshedTopics = Array.from({ length: 20 }, (_, index) => ({
+ id: `new-topic-${index + 1}`,
+ title: `New Topic ${index + 1}`,
+ }));
+
+ act(() => {
+ useChatStore.setState({
+ activeAgentId: agentId,
+ topicDataMap: {
+ [topicMapKey({ agentId })]: {
+ currentPage: 1,
+ excludeTriggers: ['cron', 'eval'],
+ hasMore: true,
+ isInbox: false,
+ items: currentTopics,
+ pageSize,
+ total: 60,
+ },
+ },
+ });
+ });
+
+ (topicService.getTopics as Mock).mockResolvedValue({
+ items: refreshedTopics,
+ total: 20,
+ });
+
+ const useFetchTopics = useChatStore.getState().useFetchTopics;
+ const swrResponse = renderHook(() =>
+ useFetchTopics(true, { agentId, excludeTriggers: ['cron'], pageSize }),
+ );
+
+ await waitFor(() => {
+ expect(swrResponse.result.current.data).toEqual({ items: refreshedTopics, total: 20 });
+ });
+
+ await waitFor(() => {
+ const topicData = useChatStore.getState().topicDataMap[topicMapKey({ agentId })];
+
+ expect(topicData).toMatchObject({
+ currentPage: 0,
+ excludeTriggers: ['cron'],
+ hasMore: false,
+ total: 20,
+ });
+ expect(topicData.items).toEqual(refreshedTopics);
+ });
+ });
+
+ it('should reset expanded pagination when excludeStatuses changes for the same agent', async () => {
+ const agentId = 'status-filtered-agent';
+ const pageSize = 20;
+ const currentTopics = Array.from({ length: 40 }, (_, index) => ({
+ id: `topic-${index + 1}`,
+ title: `Topic ${index + 1}`,
+ })) as ChatTopic[];
+ const refreshedTopics = Array.from({ length: 20 }, (_, index) => ({
+ id: `active-topic-${index + 1}`,
+ title: `Active Topic ${index + 1}`,
+ }));
+
+ act(() => {
+ useChatStore.setState({
+ activeAgentId: agentId,
+ topicDataMap: {
+ [topicMapKey({ agentId })]: {
+ currentPage: 1,
+ excludeStatuses: ['completed', 'archived'],
+ hasMore: true,
+ isInbox: false,
+ items: currentTopics,
+ pageSize,
+ total: 60,
+ },
+ },
+ });
+ });
+
+ (topicService.getTopics as Mock).mockResolvedValue({
+ items: refreshedTopics,
+ total: 20,
+ });
+
+ const useFetchTopics = useChatStore.getState().useFetchTopics;
+ const swrResponse = renderHook(() =>
+ useFetchTopics(true, { agentId, excludeStatuses: ['completed'], pageSize }),
+ );
+
+ await waitFor(() => {
+ expect(swrResponse.result.current.data).toEqual({ items: refreshedTopics, total: 20 });
+ });
+
+ await waitFor(() => {
+ const topicData = useChatStore.getState().topicDataMap[topicMapKey({ agentId })];
+
+ expect(topicData).toMatchObject({
+ currentPage: 0,
+ excludeStatuses: ['completed'],
+ hasMore: false,
+ total: 20,
+ });
+ expect(topicData.items).toEqual(refreshedTopics);
+ });
+ });
});
describe('useSearchTopics', () => {
it('should search topics with the given keywords', async () => {
@@ -920,6 +1173,76 @@ describe('topic action', () => {
expect(topicService.removeTopic).not.toHaveBeenCalled();
expect(refreshTopicSpy).not.toHaveBeenCalled();
});
+
+ it('should keep expanded pagination state after removing a topic', async () => {
+ const topicId = 'topic-21';
+ const activeAgentId = 'expanded-agent';
+ const existingTopics = Array.from({ length: 40 }, (_, index) => ({
+ id: `topic-${index + 1}`,
+ title: `Topic ${index + 1}`,
+ })) as ChatTopic[];
+ const { result } = renderHook(() => useChatStore());
+
+ act(() => {
+ useChatStore.setState({
+ activeAgentId,
+ topicDataMap: {
+ [topicMapKey({ agentId: activeAgentId })]: {
+ currentPage: 1,
+ hasMore: true,
+ isInbox: false,
+ items: existingTopics,
+ pageSize: 20,
+ total: 60,
+ },
+ },
+ });
+ });
+
+ vi.spyOn(result.current, 'refreshTopic').mockResolvedValue(undefined);
+
+ await act(async () => {
+ await result.current.removeTopic(topicId);
+ });
+
+ const topicData =
+ useChatStore.getState().topicDataMap[topicMapKey({ agentId: activeAgentId })];
+
+ expect(topicService.removeTopic).toHaveBeenCalledWith(topicId);
+ expect(topicData).toMatchObject({
+ currentPage: 1,
+ hasMore: true,
+ total: 59,
+ });
+ expect(topicData.items).toHaveLength(39);
+ expect(topicData.items.some((topic) => topic.id === topicId)).toBe(false);
+ });
+
+ it('should initialize addTopic total correctly for empty containers', async () => {
+ const activeAgentId = 'empty-agent';
+ const { result } = renderHook(() => useChatStore());
+
+ act(() => {
+ useChatStore.setState({ activeAgentId, topicDataMap: {} });
+ });
+
+ act(() => {
+ result.current.internal_dispatchTopic(
+ {
+ type: 'addTopic',
+ value: { id: 'topic-1', messages: [], sessionId: activeAgentId, title: 'Topic 1' },
+ },
+ 'test/addTopic',
+ );
+ });
+
+ const topicData =
+ useChatStore.getState().topicDataMap[topicMapKey({ agentId: activeAgentId })];
+
+ expect(topicData.items).toHaveLength(1);
+ expect(topicData.total).toBe(1);
+ expect(topicData.hasMore).toBe(false);
+ });
});
describe('removeUnstarredTopic', () => {
it('should remove unstarred topics and refresh the topic list', async () => {
diff --git a/src/store/chat/slices/topic/action.ts b/src/store/chat/slices/topic/action.ts
index d18d977a4d3..6ade8ddd0d4 100644
--- a/src/store/chat/slices/topic/action.ts
+++ b/src/store/chat/slices/topic/action.ts
@@ -444,24 +444,55 @@ export class ChatTopicActionImpl {
if (!hasValidContainer) return;
const { items: topics, total: totalCount } = result;
- const hasMore = topics.length >= pageSize;
const currentData = this.#get().topicDataMap[containerKey];
+ const isRefreshingExpandedList =
+ !!currentData &&
+ currentData.currentPage > 0 &&
+ currentData.pageSize === pageSize &&
+ Boolean(currentData.isInbox) === Boolean(isInbox) &&
+ isEqual(currentData.excludeStatuses, effectiveExcludeStatuses) &&
+ isEqual(currentData.excludeTriggers, effectiveExcludeTriggers);
+
+ const nextItems = isRefreshingExpandedList
+ ? (() => {
+ const visibleCount = Math.min(currentData.items.length, totalCount);
+ const topicIds = new Set(topics.map((item) => item.id));
+
+ return [
+ ...topics,
+ ...currentData.items.filter((topic) => !topicIds.has(topic.id)),
+ ].slice(0, visibleCount);
+ })()
+ : topics;
+
+ const hasMore = totalCount > nextItems.length;
+
// no need to update map if the current key's data exists and is the same
- if (currentData && isEqual(topics, currentData.items)) return;
+ if (
+ currentData &&
+ isEqual(nextItems, currentData.items) &&
+ currentData.total === totalCount &&
+ isEqual(currentData.excludeStatuses, effectiveExcludeStatuses) &&
+ isEqual(currentData.excludeTriggers, effectiveExcludeTriggers)
+ ) {
+ return;
+ }
this.#set(
{
topicDataMap: {
...this.#get().topicDataMap,
[containerKey]: {
- currentPage: 0,
+ currentPage: isRefreshingExpandedList ? currentData.currentPage : 0,
excludeStatuses: effectiveExcludeStatuses,
excludeTriggers: effectiveExcludeTriggers,
hasMore,
+ isInbox: Boolean(isInbox),
isExpandingPageSize: false,
- items: topics,
+ isLoadingMore: false,
+ items: nextItems,
pageSize,
total: totalCount,
},
@@ -510,7 +541,8 @@ export class ChatTopicActionImpl {
});
const currentTopics = currentData?.items || [];
- const hasMore = result.items.length >= pageSize;
+ const nextItems = [...currentTopics, ...result.items];
+ const hasMore = result.total > nextItems.length;
this.#set(
{
@@ -521,8 +553,9 @@ export class ChatTopicActionImpl {
excludeStatuses,
excludeTriggers,
hasMore,
+ isInbox: currentData?.isInbox,
isLoadingMore: false,
- items: [...currentTopics, ...result.items],
+ items: nextItems,
pageSize,
total: result.total,
},
@@ -678,6 +711,7 @@ export class ChatTopicActionImpl {
// remove topic
await topicService.removeTopic(id);
+ this.#get().internal_dispatchTopic({ type: 'deleteTopic', id }, 'removeTopic');
purgeUnreadTopics([id]);
await refreshTopic();
@@ -777,6 +811,14 @@ export class ChatTopicActionImpl {
// no need to update if is the same
if (isEqual(nextItems, currentData?.items)) return;
+ const currentTotal = currentData?.total ?? currentData?.items?.length ?? 0;
+ const total =
+ payload.type === 'addTopic'
+ ? currentTotal + 1
+ : payload.type === 'deleteTopic'
+ ? Math.max(nextItems.length, currentTotal - 1)
+ : currentTotal;
+
this.#set(
{
topicDataMap: {
@@ -784,9 +826,10 @@ export class ChatTopicActionImpl {
[key]: {
...currentData,
currentPage: currentData?.currentPage ?? 0,
- hasMore: currentData?.hasMore ?? false,
+ hasMore: total > nextItems.length,
+ isInbox: currentData?.isInbox,
items: nextItems,
- total: currentData?.total ?? nextItems.length,
+ total,
},
},
},
@@ -818,14 +861,10 @@ export class ChatTopicActionImpl {
...this.#get().topicDataMap,
[key]: {
currentPage,
- // Carry filter fields forward so subsequent reads (e.g. the
- // sendMessageInServer `topicFilter` helper) keep seeing the
- // filter the SWR fetch was using; otherwise the next request
- // forgets to exclude completed/cron topics until SWR
- // revalidates.
excludeStatuses: currentData?.excludeStatuses,
excludeTriggers: currentData?.excludeTriggers,
- hasMore: items.length >= pageSize,
+ hasMore: total > nextItems.length,
+ isInbox: currentData?.isInbox,
isExpandingPageSize: false,
isLoadingMore: false,
items: nextItems,
diff --git a/src/store/chat/slices/topic/initialState.ts b/src/store/chat/slices/topic/initialState.ts
index 2eb6cae10d2..d53e94729bc 100644
--- a/src/store/chat/slices/topic/initialState.ts
+++ b/src/store/chat/slices/topic/initialState.ts
@@ -9,6 +9,7 @@ export interface TopicData {
excludeTriggers?: string[];
hasMore: boolean;
isExpandingPageSize?: boolean;
+ isInbox?: boolean;
isLoadingMore?: boolean;
items: ChatTopic[];
/**
diff --git a/src/store/chat/slices/topic/selectors.test.ts b/src/store/chat/slices/topic/selectors.test.ts
index 6a1909cbeae..382cde5a591 100644
--- a/src/store/chat/slices/topic/selectors.test.ts
+++ b/src/store/chat/slices/topic/selectors.test.ts
@@ -61,6 +61,62 @@ describe('topicSelectors', () => {
});
});
+ describe('hasMoreTopics', () => {
+ it('should return true when total exceeds pageSize even if hasMore is temporarily false', () => {
+ const state = merge(initialStore, {
+ activeAgentId: 'test',
+ topicDataMap: {
+ [topicMapKey({ agentId: 'test' })]: {
+ currentPage: 0,
+ hasMore: false,
+ items: Array.from({ length: 20 }, (_, index) => ({ id: `topic-${index}` })),
+ pageSize: 20,
+ total: 21,
+ },
+ },
+ });
+
+ expect(topicSelectors.hasMoreTopics(state)).toBe(false);
+ expect(topicSelectors.hasMoreTopicsForSidebar(state)).toBe(true);
+ });
+
+ it('should return false when all topics are already loaded', () => {
+ const state = merge(initialStore, {
+ activeAgentId: 'test',
+ topicDataMap: {
+ [topicMapKey({ agentId: 'test' })]: {
+ currentPage: 1,
+ hasMore: false,
+ items: Array.from({ length: 21 }, (_, index) => ({ id: `topic-${index}` })),
+ pageSize: 20,
+ total: 21,
+ },
+ },
+ });
+
+ expect(topicSelectors.hasMoreTopics(state)).toBe(false);
+ expect(topicSelectors.hasMoreTopicsForSidebar(state)).toBe(true);
+ });
+
+ it('should return false for sidebar when total does not exceed pageSize', () => {
+ const state = merge(initialStore, {
+ activeAgentId: 'test',
+ topicDataMap: {
+ [topicMapKey({ agentId: 'test' })]: {
+ currentPage: 1,
+ hasMore: false,
+ items: Array.from({ length: 21 }, (_, index) => ({ id: `topic-${index}` })),
+ pageSize: 30,
+ total: 21,
+ },
+ },
+ });
+
+ expect(topicSelectors.hasMoreTopics(state)).toBe(false);
+ expect(topicSelectors.hasMoreTopicsForSidebar(state)).toBe(false);
+ });
+ });
+
describe('currentActiveTopic', () => {
it('should return undefined if there is no active topic', () => {
const topic = topicSelectors.currentActiveTopic(initialStore);
diff --git a/src/store/chat/slices/topic/selectors.ts b/src/store/chat/slices/topic/selectors.ts
index d4382a66018..ea72e709c7b 100644
--- a/src/store/chat/slices/topic/selectors.ts
+++ b/src/store/chat/slices/topic/selectors.ts
@@ -168,7 +168,19 @@ const groupedTopicsForSidebar =
return buildGroupedTopics(limitedTopics, getGroupFn(groupMode, sortBy));
};
-const hasMoreTopics = (s: ChatStoreState): boolean => currentTopicData(s)?.hasMore ?? false;
+const hasMoreTopics = (s: ChatStoreState): boolean => {
+ const topicData = currentTopicData(s);
+ if (!topicData) return false;
+
+ return topicData.hasMore;
+};
+
+const hasMoreTopicsForSidebar = (s: ChatStoreState): boolean => {
+ const topicData = currentTopicData(s);
+ if (!topicData) return false;
+
+ return topicData.hasMore || topicData.total > topicData.pageSize;
+};
const isLoadingMoreTopics = (s: ChatStoreState): boolean =>
currentTopicData(s)?.isLoadingMore ?? false;
@@ -194,6 +206,7 @@ export const topicSelectors = {
groupedTopicsForSidebar,
groupedTopicsSelector,
hasMoreTopics,
+ hasMoreTopicsForSidebar,
isCreatingTopic,
isExpandingPageSize,
isInSearchMode,