1
1
/* eslint-disable prettier/prettier */
2
- import React , { useEffect , useState } from "react" ;
2
+ import React , { useCallback , useEffect , useMemo , useState } from "react" ;
3
3
4
- import { List } from "@/common/api/indexer" ;
4
+ import { List , ListRegistration } from "@/common/api/indexer" ;
5
5
import { Filter , Group , GroupType , SearchBar , SortSelect } from "@/common/ui/components" ;
6
6
import { ACCOUNT_LIST_REGISTRATION_STATUS_OPTIONS } from "@/entities/_shared" ;
7
7
8
- import { AccountCard } from "./AccountCard" ;
9
8
import { ListCardSkeleton } from "./ListCardSkeleton" ;
10
9
import { NoListItem } from "./NoListItem" ;
11
10
import { NoListItemType } from "../types" ;
11
+ import { ListAccountCard } from "./AccountCard" ;
12
12
13
13
interface ListAccountsType {
14
14
loadingListData : boolean ;
15
15
listData : List | undefined ;
16
- filteredRegistrations : any [ ] ;
16
+ listRegistrations : ListRegistration [ ] ;
17
17
isLoading : boolean ;
18
- setFilteredRegistrations : ( value : any ) => void ;
19
18
setStatus : ( value : string ) => void ;
20
19
}
21
20
@@ -24,15 +23,14 @@ export const ListAccounts = ({
24
23
loadingListData,
25
24
setStatus,
26
25
isLoading,
27
- filteredRegistrations,
28
- setFilteredRegistrations,
26
+ listRegistrations,
29
27
} : ListAccountsType ) => {
30
28
const [ search , setSearch ] = useState ( "" ) ;
31
29
const [ accountsWithAccess , setAccountsWithAccess ] = useState < string [ ] > ( [ ] ) ;
32
30
const [ statusFilter , setsStatusFilter ] = useState < string > ( "all" ) ;
33
- const [ searchedAccounts , setSearchedAccounts ] = useState < any [ ] > ( [ ] ) ;
34
31
35
- const SORT_LIST_PROJEECTS = [
32
+
33
+ const SORT_LIST_PROJECTS = [
36
34
{ label : "Most recent" , value : "recent" } ,
37
35
{ label : "Least recent" , value : "older" } ,
38
36
] ;
@@ -52,7 +50,7 @@ export const ListAccounts = ({
52
50
} ,
53
51
] ;
54
52
55
- const handleFilter = ( registration : any ) => {
53
+ const handleFilter = ( registration : ListRegistration ) => {
56
54
const matchesSearch = search
57
55
? registration . registrant ?. near_social_profile_data ?. name
58
56
?. toLowerCase ( )
@@ -63,37 +61,23 @@ export const ListAccounts = ({
63
61
return matchesSearch ;
64
62
} ;
65
63
66
- useEffect ( ( ) => {
67
- const filtered = filteredRegistrations . filter ( handleFilter ) ;
68
- setSearchedAccounts ( filtered ?? [ ] ) ;
69
- } , [ search ] ) ;
70
-
71
- const handleSort = ( sortType : string ) => {
72
- const projects = [ ...filteredRegistrations ] ;
73
-
74
- switch ( sortType ) {
75
- case "recent" :
76
- projects . sort (
77
- ( a , b ) =>
78
- new Date ( b . submitted_at ) . getTime ( ) -
79
- new Date ( a . submitted_at ) . getTime ( ) ,
80
- ) ;
81
-
82
- setFilteredRegistrations ( projects ) ;
83
- break ;
84
- case "older" :
85
- projects . sort (
86
- ( a , b ) =>
87
- new Date ( a . submitted_at ) . getTime ( ) -
88
- new Date ( b . submitted_at ) . getTime ( ) ,
89
- ) ;
90
-
91
- setFilteredRegistrations ( projects ) ;
92
- break ;
93
- default :
94
- break ;
95
- }
96
- } ;
64
+
65
+ const searchedAccounts = useMemo ( ( ) => {
66
+ return listRegistrations . filter ( handleFilter ) ;
67
+ } , [ search , handleFilter ] )
68
+
69
+ const handleSort = useCallback ( ( sortType : string ) => {
70
+ return [ ...listRegistrations ] . sort ( ( a , b ) => {
71
+ switch ( sortType ) {
72
+ case "recent" :
73
+ return new Date ( b . submitted_at ) . getTime ( ) - new Date ( a . submitted_at ) . getTime ( ) ;
74
+ case "older" :
75
+ return new Date ( a . submitted_at ) . getTime ( ) - new Date ( b . submitted_at ) . getTime ( ) ;
76
+ default :
77
+ return 0 ; // No sorting
78
+ }
79
+ } ) ;
80
+ } , [ listRegistrations ] ) ;
97
81
98
82
useEffect ( ( ) => {
99
83
if ( ! loadingListData && listData ) {
@@ -106,7 +90,7 @@ export const ListAccounts = ({
106
90
}
107
91
} , [ listData ] ) ;
108
92
109
- const data = search ? searchedAccounts : filteredRegistrations ?? [ ] ;
93
+ const data = search ? searchedAccounts : listRegistrations ?? [ ] ;
110
94
111
95
return (
112
96
< div className = "md:pb-0 md:pt-12 flex w-full flex-col px-2 pt-10" >
@@ -117,7 +101,7 @@ export const ListAccounts = ({
117
101
< span
118
102
style = { { color : "#DD3345" , marginLeft : "8px" , fontWeight : 600 } }
119
103
>
120
- { filteredRegistrations ?. length }
104
+ { listRegistrations ?. length }
121
105
</ span >
122
106
</ div >
123
107
</ div >
@@ -128,7 +112,7 @@ export const ListAccounts = ({
128
112
/>
129
113
< Filter groups = { tagsList } />
130
114
< SortSelect
131
- options = { SORT_LIST_PROJEECTS }
115
+ options = { SORT_LIST_PROJECTS }
132
116
onValueChange = { handleSort }
133
117
/>
134
118
</ div >
@@ -142,7 +126,7 @@ export const ListAccounts = ({
142
126
) : data ?. length ? (
143
127
< div className = "md:grid-cols-2 lg:grid-cols-3 mt-8 grid w-full grid-cols-1 gap-8" >
144
128
{ data ?. map ( ( item , index ) => (
145
- < AccountCard
129
+ < ListAccountCard
146
130
accountsWithAccess = { accountsWithAccess }
147
131
dataForList = { item }
148
132
key = { index }
0 commit comments