@@ -5,7 +5,7 @@ import { Table, TableBody, TableHead, TableHeader, TableRow } from '@/components
5
5
import { useFetchTopics } from '@/hooks/use-fetch-topics' ;
6
6
import { cn } from '@/utils/ui' ;
7
7
import { DirectionEnum } from '@novu/shared' ;
8
- import { HTMLAttributes , useCallback , useEffect , useState } from 'react' ;
8
+ import { HTMLAttributes , useCallback } from 'react' ;
9
9
import { RiAddCircleLine } from 'react-icons/ri' ;
10
10
import { useSearchParams } from 'react-router-dom' ;
11
11
import { useTopicsNavigate } from './hooks/use-topics-navigate' ;
@@ -94,59 +94,55 @@ type TopicListTableProps = HTMLAttributes<HTMLTableElement> & {
94
94
export const TopicList = ( props : TopicListProps ) => {
95
95
const { className, ...rest } = props ;
96
96
const [ searchParams , setSearchParams ] = useSearchParams ( ) ;
97
+ const { navigateToCreateTopicPage } = useTopicsNavigate ( ) ;
97
98
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
117
100
const { filterValues, handleFiltersChange, toggleSort, resetFilters } = useTopicsUrlState ( { } ) ;
118
101
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 ;
121
122
122
- const { data, isFetching } = useFetchTopics ( currentFilters , {
123
+ const { data, isFetching, isLoading } = useFetchTopics ( fetchParams , {
123
124
meta : { errorMessage : 'Issue fetching topics' } ,
124
125
} ) ;
125
126
126
- const isLoading = isFetching ;
127
-
127
+ // Simplified Pagination Handlers
128
128
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
+ }
138
136
} , [ data ?. next , setSearchParams ] ) ;
139
137
140
138
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
+ }
150
146
} , [ data ?. previous , setSearchParams ] ) ;
151
147
152
148
const handleFirst = useCallback ( ( ) => {
@@ -157,23 +153,27 @@ export const TopicList = (props: TopicListProps) => {
157
153
} ) ;
158
154
} , [ setSearchParams ] ) ;
159
155
156
+ // Define wrapper props once
160
157
const wrapperProps = {
161
158
filterValues,
162
159
handleFiltersChange,
163
160
resetFilters,
164
- isLoading,
161
+ isLoading : isLoading , // Pass loading state
165
162
...rest ,
166
163
} ;
167
164
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
+
168
172
if ( isLoading ) {
169
173
return (
170
174
< 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 ) => (
177
177
< TopicRowSkeleton key = { index } />
178
178
) ) }
179
179
</ TopicListTable >
@@ -199,11 +199,7 @@ export const TopicList = (props: TopicListProps) => {
199
199
200
200
return (
201
201
< TopicListWrapper { ...wrapperProps } >
202
- < TopicListTable
203
- orderBy = { currentFilters . orderBy }
204
- orderDirection = { currentFilters . orderDirection }
205
- toggleSort = { toggleSort }
206
- >
202
+ < TopicListTable { ...tableProps } >
207
203
{ data . data . map ( ( topic ) => (
208
204
< TopicRow key = { topic . _id } topic = { topic } />
209
205
) ) }
0 commit comments