@@ -227,44 +227,70 @@ export const getProposalTitle = (
227227} ;
228228
229229const selectFilteredProposalIds = ( store : RootState ) => {
230+ const hasStatusFilter = store . filteredState !== null ;
231+ const hasTitleSearch = store . titleSearchValue !== undefined ;
230232 const proposalsIds =
231233 store . totalProposalCount >= 0
232234 ? Array . from ( Array ( store . totalProposalCount ) . keys ( ) ) . sort ( ( a , b ) => b - a )
233235 : [ ] ;
234236
237+ if ( ! hasStatusFilter && ! hasTitleSearch ) {
238+ return proposalsIds ;
239+ }
240+
235241 const detailedData = proposalsIds . map ( ( id ) =>
236242 getCombineProposalDataById ( store , id ) ,
237243 ) ;
238244
245+ type CombineProposalData = ReturnType < typeof getCombineProposalDataById > ;
246+ type SortableProposalData = {
247+ id : number ;
248+ creationTime ?: number ;
249+ finishedTimestamp ?: number ;
250+ } ;
251+
252+ const getProposalSortTimestamp = ( proposal : CombineProposalData ) => {
253+ const data = proposal ?. proposal ?. data as SortableProposalData ;
254+ return data ?. creationTime ?? data ?. finishedTimestamp ?? data ?. id ?? 0 ;
255+ } ;
256+
257+ const sortByDateDesc = ( a : CombineProposalData , b : CombineProposalData ) =>
258+ getProposalSortTimestamp ( b ) - getProposalSortTimestamp ( a ) ;
259+
260+ if ( hasStatusFilter && ! hasTitleSearch ) {
261+ return detailedData
262+ . filter ( ( proposal ) =>
263+ store . filteredState !== 7
264+ ? proposal ?. proposal . combineState === store . filteredState
265+ : proposal ?. proposal . combineState === 0 ||
266+ proposal ?. proposal . combineState === 1 ||
267+ proposal ?. proposal . combineState === 2 ,
268+ )
269+ . sort ( sortByDateDesc )
270+ . map ( ( proposal ) => proposal ?. proposal . data . id || 0 ) ;
271+ }
272+
239273 const fuse = new Fuse ( detailedData , {
240274 keys : [ 'proposal.data.title' ] ,
241275 threshold : 0.3 ,
242276 distance : 1000 ,
243277 } ) ;
244278
245- return store . filteredState === null && store . titleSearchValue === undefined
246- ? proposalsIds
247- : store . filteredState !== null && store . titleSearchValue === undefined
248- ? detailedData
249- . filter ( ( proposal ) =>
250- store . filteredState !== 7
251- ? proposal ?. proposal . combineState === store . filteredState
252- : proposal ?. proposal . combineState === 0 ||
253- proposal ?. proposal . combineState === 1 ||
254- proposal ?. proposal . combineState === 2 ,
255- )
256- . map ( ( proposal ) => proposal ?. proposal . data . id || 0 )
257- : store . filteredState === null && store . titleSearchValue !== undefined
258- ? fuse
259- . search ( store . titleSearchValue || '' )
260- . map ( ( item ) => item . item ?. proposal . data . id || 0 )
261- : fuse
262- . search ( store . titleSearchValue || '' )
263- . filter (
264- ( item ) =>
265- item . item ?. proposal . combineState === store . filteredState ,
266- )
267- . map ( ( item ) => item . item ?. proposal . data . id || 0 ) ;
279+ type FuseSearchResult = { item : CombineProposalData } ;
280+ const sortFuseResultByDateDesc = ( a : FuseSearchResult , b : FuseSearchResult ) =>
281+ sortByDateDesc ( a . item , b . item ) ;
282+
283+ const searchResults = fuse . search ( store . titleSearchValue || '' ) ;
284+ if ( ! hasStatusFilter ) {
285+ return searchResults
286+ . sort ( sortFuseResultByDateDesc )
287+ . map ( ( item ) => item . item ?. proposal . data . id || 0 ) ;
288+ }
289+
290+ return searchResults
291+ . filter ( ( item ) => item . item ?. proposal . combineState === store . filteredState )
292+ . sort ( sortFuseResultByDateDesc )
293+ . map ( ( item ) => item . item ?. proposal . data . id || 0 ) ;
268294} ;
269295
270296export const selectPaginatedIds = ( store : RootState ) => {
0 commit comments