1
- import React , { useState , useMemo } from "react" ;
2
-
1
+ import React from "react" ;
2
+ import { Routes , Route , useNavigate , useSearchParams , useLocation , Navigate } from "react-router-dom" ;
3
+ import { useAccount } from "wagmi" ;
3
4
import styled , { css } from "styled-components" ;
4
5
import { MAX_WIDTH_LANDSCAPE , landscapeStyle } from "styles/landscapeStyle" ;
5
6
import { responsiveSize } from "styles/responsiveSize" ;
6
-
7
- import { useNavigate , useParams , useSearchParams } from "react-router-dom" ;
8
- import { useAccount } from "wagmi" ;
9
7
import { Tabs as TabsComponent } from "@kleros/ui-components-library" ;
10
8
11
- import { isUndefined } from "utils/index" ;
12
- import { decodeURIFilter , useRootPath } from "utils/uri" ;
13
- import { DisputeDetailsFragment , useMyCasesQuery } from "queries/useCasesQuery" ;
14
- import { useUserQuery } from "queries/useUser" ;
15
- import { OrderDirection } from "src/graphql/graphql" ;
16
- import CasesDisplay from "components/CasesDisplay" ;
9
+ import PnkIcon from "svgs/icons/pnk.svg" ;
10
+ import DocIcon from "svgs/icons/doc.svg" ;
11
+ import VotedIcon from "svgs/icons/voted-ballot.svg" ;
12
+
17
13
import ConnectWallet from "components/ConnectWallet" ;
18
14
import FavoriteCases from "components/FavoriteCases" ;
19
15
import ScrollTop from "components/ScrollTop" ;
20
- import JurorInfo from "./JurorInfo " ;
16
+ import JurorCard from "./JurorCard " ;
21
17
import Stakes from "./Stakes" ;
18
+ import Cases from "./Cases" ;
19
+ import Votes from "./Votes" ;
22
20
23
21
const Container = styled . div `
24
22
width: 100%;
@@ -34,17 +32,17 @@ const Container = styled.div`
34
32
) }
35
33
` ;
36
34
37
- const StyledCasesDisplay = styled ( CasesDisplay ) `
38
- margin-top: ${ responsiveSize ( 24 , 32 ) } ;
39
-
40
- .title {
41
- margin-bottom: ${ responsiveSize ( 12 , 24 ) } ;
42
- }
43
- ` ;
44
-
45
35
const StyledTabs = styled ( TabsComponent ) `
46
36
width: 100%;
47
37
margin-top: ${ responsiveSize ( 16 , 32 ) } ;
38
+ > * {
39
+ display: flex;
40
+ flex-wrap: wrap;
41
+ font-size: ${ responsiveSize ( 14 , 16 ) } ;
42
+ > svg {
43
+ margin-right: 8px !important;
44
+ }
45
+ }
48
46
` ;
49
47
50
48
const ConnectWalletContainer = styled . div `
@@ -56,60 +54,51 @@ const ConnectWalletContainer = styled.div`
56
54
` ;
57
55
58
56
const TABS = [
59
- { text : "Stakes" , value : 0 } ,
60
- { text : "Cases" , value : 1 } ,
61
- { text : "Votes" , value : 2 } ,
57
+ { text : "Stakes" , value : 0 , Icon : PnkIcon , path : "stakes" } ,
58
+ { text : "Cases" , value : 1 , Icon : DocIcon , path : "cases/1/desc/all" } ,
59
+ { text : "Votes" , value : 2 , Icon : VotedIcon , path : "votes" } ,
62
60
] ;
63
61
62
+ const getTabIndex = ( currentPath : string ) => {
63
+ return TABS . findIndex ( ( tab ) => currentPath . includes ( tab . path . split ( "/" ) [ 0 ] ) ) ;
64
+ } ;
65
+
64
66
const Profile : React . FC = ( ) => {
65
67
const { isConnected, address : connectedAddress } = useAccount ( ) ;
66
- const { page, order, filter } = useParams ( ) ;
67
68
const [ searchParams ] = useSearchParams ( ) ;
68
- const location = useRootPath ( ) ;
69
+ const { pathname } = useLocation ( ) ;
69
70
const navigate = useNavigate ( ) ;
70
71
const searchParamAddress = searchParams . get ( "address" ) ?. toLowerCase ( ) ;
71
72
const addressToQuery = searchParamAddress || connectedAddress ?. toLowerCase ( ) ;
72
- const casesPerPage = 3 ;
73
- const pageNumber = parseInt ( page ?? "1" ) ;
74
- const disputeSkip = casesPerPage * ( pageNumber - 1 ) ;
75
- const decodedFilter = decodeURIFilter ( filter ?? "all" ) ;
76
- const { data : disputesData } = useMyCasesQuery (
77
- addressToQuery ,
78
- disputeSkip ,
79
- decodedFilter ,
80
- order === "asc" ? OrderDirection . Asc : OrderDirection . Desc
81
- ) ;
82
- const { data : userData } = useUserQuery ( addressToQuery , decodedFilter ) ;
83
- const totalCases = userData ?. user ?. disputes . length ;
84
- const totalResolvedCases = parseInt ( userData ?. user ?. totalResolvedDisputes ) ;
85
- const totalPages = useMemo (
86
- ( ) => ( ! isUndefined ( totalCases ) ? Math . ceil ( totalCases / casesPerPage ) : 1 ) ,
87
- [ totalCases , casesPerPage ]
88
- ) ;
89
- const [ currentTab , setCurrentTab ] = useState ( 0 ) ;
73
+
74
+ const handleTabChange = ( tabIndex : number ) => {
75
+ const selectedTab = TABS [ tabIndex ] ;
76
+ const basePath = `/profile/${ selectedTab . path } ` ;
77
+ const queryParam = searchParamAddress ? `?address=${ searchParamAddress } ` : "" ;
78
+ navigate ( `${ basePath } ${ queryParam } ` ) ;
79
+ } ;
90
80
91
81
return (
92
82
< Container >
93
83
{ isConnected || searchParamAddress ? (
94
84
< >
95
- < JurorInfo { ...{ addressToQuery } } />
96
- < StyledTabs currentValue = { currentTab } items = { TABS } callback = { ( n ) => setCurrentTab ( n ) } />
97
- { currentTab === 0 && < Stakes { ...{ addressToQuery } } /> }
98
- { currentTab === 1 && (
99
- < StyledCasesDisplay
100
- title = "Cases Drawn"
101
- disputes = { userData ?. user !== null ? ( disputesData ?. user ?. disputes as DisputeDetailsFragment [ ] ) : [ ] }
102
- numberDisputes = { totalCases }
103
- numberClosedDisputes = { totalResolvedCases }
104
- totalPages = { totalPages }
105
- currentPage = { pageNumber }
106
- setCurrentPage = { ( newPage : number ) =>
107
- navigate ( `${ location } /${ newPage } /${ order } /${ filter } ?${ searchParams . toString ( ) } ` )
85
+ < JurorCard { ...{ addressToQuery } } />
86
+ < StyledTabs
87
+ currentValue = { getTabIndex ( pathname ) }
88
+ items = { TABS }
89
+ callback = { ( tabIndex : number ) => handleTabChange ( tabIndex ) }
90
+ />
91
+ < Routes >
92
+ < Route path = "stakes" element = { < Stakes { ...{ addressToQuery } } /> } />
93
+ < Route path = "cases/:page/:order/:filter" element = { < Cases { ...{ addressToQuery } } /> } />
94
+ < Route path = "votes" element = { < Votes /> } />
95
+ < Route
96
+ path = "*"
97
+ element = {
98
+ < Navigate to = { `${ searchParamAddress ? `stakes?address=${ searchParamAddress } ` : "stakes" } ` } replace />
108
99
}
109
- { ...{ casesPerPage } }
110
100
/>
111
- ) }
112
- { currentTab === 2 && null }
101
+ </ Routes >
113
102
</ >
114
103
) : (
115
104
< ConnectWalletContainer >
0 commit comments