Skip to content

Commit e3d06ec

Browse files
committed
Update topic-list.tsx
1 parent d7c0a3a commit e3d06ec

File tree

1 file changed

+51
-55
lines changed

1 file changed

+51
-55
lines changed

apps/dashboard/src/components/topics/topic-list.tsx

+51-55
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Table, TableBody, TableHead, TableHeader, TableRow } from '@/components
55
import { useFetchTopics } from '@/hooks/use-fetch-topics';
66
import { cn } from '@/utils/ui';
77
import { DirectionEnum } from '@novu/shared';
8-
import { HTMLAttributes, useCallback, useEffect, useState } from 'react';
8+
import { HTMLAttributes, useCallback } from 'react';
99
import { RiAddCircleLine } from 'react-icons/ri';
1010
import { useSearchParams } from 'react-router-dom';
1111
import { useTopicsNavigate } from './hooks/use-topics-navigate';
@@ -94,59 +94,55 @@ type TopicListTableProps = HTMLAttributes<HTMLTableElement> & {
9494
export const TopicList = (props: TopicListProps) => {
9595
const { className, ...rest } = props;
9696
const [searchParams, setSearchParams] = useSearchParams();
97+
const { navigateToCreateTopicPage } = useTopicsNavigate();
9798

98-
// Read directly from URL params for data fetching
99-
const afterParam = searchParams.get('after') || '';
100-
const beforeParam = searchParams.get('before') || '';
101-
const keyParam = searchParams.get('key') || '';
102-
const nameParam = searchParams.get('name') || '';
103-
const orderByParam = (searchParams.get('orderBy') as TopicsSortableColumn) || '_id';
104-
const orderDirectionParam = (searchParams.get('orderDirection') as DirectionEnum) || DirectionEnum.DESC;
105-
106-
// Create filter values from URL params
107-
const currentFilters: TopicsFilter = {
108-
after: afterParam || undefined,
109-
before: beforeParam || undefined,
110-
key: keyParam || undefined,
111-
name: nameParam || undefined,
112-
orderBy: orderByParam,
113-
orderDirection: orderDirectionParam,
114-
limit: 10,
115-
};
116-
99+
// Use the hook as the primary source for URL state - orderBy/orderDirection are likely within filterValues
117100
const { filterValues, handleFiltersChange, toggleSort, resetFilters } = useTopicsUrlState({});
118101

119-
const areFiltersApplied = keyParam || nameParam || beforeParam || afterParam;
120-
const limit = 10;
102+
// Pagination state remains derived directly from URL for fetching
103+
const after = searchParams.get('after') || undefined;
104+
const before = searchParams.get('before') || undefined;
105+
const limit = 10; // Keep limit definition
106+
107+
// Consolidate fetch parameters
108+
const fetchParams: TopicsFilter = {
109+
// Use values from the hook
110+
key: filterValues.key,
111+
name: filterValues.name,
112+
orderBy: filterValues.orderBy,
113+
orderDirection: filterValues.orderDirection,
114+
// Pagination params from URL
115+
after: after,
116+
before: before,
117+
limit: limit,
118+
};
119+
120+
// Determine if filters are active based on hook values
121+
const areFiltersApplied = filterValues.key || filterValues.name || before || after;
121122

122-
const { data, isFetching } = useFetchTopics(currentFilters, {
123+
const { data, isFetching, isLoading } = useFetchTopics(fetchParams, {
123124
meta: { errorMessage: 'Issue fetching topics' },
124125
});
125126

126-
const isLoading = isFetching;
127-
127+
// Simplified Pagination Handlers
128128
const handleNext = useCallback(() => {
129-
setSearchParams((prev) => {
130-
prev.delete('before');
131-
132-
if (data?.next) {
133-
prev.set('after', data.next);
134-
}
135-
136-
return prev;
137-
});
129+
if (data?.next) {
130+
setSearchParams((prev) => {
131+
prev.delete('before');
132+
prev.set('after', data.next as string);
133+
return prev;
134+
});
135+
}
138136
}, [data?.next, setSearchParams]);
139137

140138
const handlePrevious = useCallback(() => {
141-
setSearchParams((prev) => {
142-
prev.delete('after');
143-
144-
if (data?.previous) {
145-
prev.set('before', data.previous);
146-
}
147-
148-
return prev;
149-
});
139+
if (data?.previous) {
140+
setSearchParams((prev) => {
141+
prev.delete('after');
142+
prev.set('before', data.previous as string);
143+
return prev;
144+
});
145+
}
150146
}, [data?.previous, setSearchParams]);
151147

152148
const handleFirst = useCallback(() => {
@@ -157,23 +153,27 @@ export const TopicList = (props: TopicListProps) => {
157153
});
158154
}, [setSearchParams]);
159155

156+
// Define wrapper props once
160157
const wrapperProps = {
161158
filterValues,
162159
handleFiltersChange,
163160
resetFilters,
164-
isLoading,
161+
isLoading: isLoading, // Pass loading state
165162
...rest,
166163
};
167164

165+
// Define table props once
166+
const tableProps = {
167+
orderBy: filterValues.orderBy, // Use state from hook via filterValues
168+
orderDirection: filterValues.orderDirection, // Use state from hook via filterValues
169+
toggleSort,
170+
};
171+
168172
if (isLoading) {
169173
return (
170174
<TopicListWrapper {...wrapperProps}>
171-
<TopicListTable
172-
orderBy={currentFilters.orderBy}
173-
orderDirection={currentFilters.orderDirection}
174-
toggleSort={toggleSort}
175-
>
176-
{new Array(limit).fill(0).map((_, index) => (
175+
<TopicListTable {...tableProps}>
176+
{Array.from({ length: limit }).map((_, index) => (
177177
<TopicRowSkeleton key={index} />
178178
))}
179179
</TopicListTable>
@@ -199,11 +199,7 @@ export const TopicList = (props: TopicListProps) => {
199199

200200
return (
201201
<TopicListWrapper {...wrapperProps}>
202-
<TopicListTable
203-
orderBy={currentFilters.orderBy}
204-
orderDirection={currentFilters.orderDirection}
205-
toggleSort={toggleSort}
206-
>
202+
<TopicListTable {...tableProps}>
207203
{data.data.map((topic) => (
208204
<TopicRow key={topic._id} topic={topic} />
209205
))}

0 commit comments

Comments
 (0)