1- import { ReactElement , useCallback , useEffect , useMemo , useState } from 'react'
1+ import { ReactElement , useCallback , useEffect , useState } from 'react'
22import { useSelector } from 'react-redux'
33import styled from 'styled-components'
4- import { ButtonLink , Table , TableHeader , TableRow , Text } from '@gnosis.pm/safe-react-components'
4+ import { makeStyles , TableCell , TableContainer , TableRow } from '@material-ui/core'
5+ import { ButtonLink , Icon } from '@gnosis.pm/safe-react-components'
6+ import { keccak256 , fromAscii } from 'web3-utils'
7+ import cn from 'classnames'
58
69import Block from 'src/components/layout/Block'
710import Heading from 'src/components/layout/Heading'
811import Paragraph from 'src/components/layout/Paragraph/index'
912import { lg } from 'src/theme/variables'
1013import { currentSafeWithNames } from 'src/logic/safe/store/selectors'
11- import { getChainInfo } from 'src/config'
14+ import { getChainInfo , getExplorerInfo } from 'src/config'
1215import { checksumAddress } from 'src/utils/checksumAddress'
1316import { getWeb3 } from 'src/logic/wallets/getWeb3'
1417import { AddDelegateModal } from 'src/routes/safe/components/Settings/Delegates/AddDelegateModal'
1518import { userAccountSelector } from 'src/logic/wallets/store/selectors'
16- import { keccak256 , fromAscii } from 'web3-utils'
19+ import Table from 'src/components/Table'
20+ import { cellWidth } from 'src/components/Table/TableHead'
21+ import { DELEGATE_ADDRESS_ID , DELEGATOR_ADDRESS_ID , generateColumns } from './columns'
22+ import { styles } from './style'
23+ import PrefixedEthHashInfo from 'src/components/PrefixedEthHashInfo'
24+ import Row from 'src/components/layout/Row'
25+ import ButtonHelper from 'src/components/ButtonHelper'
26+ import { grantedSelector } from 'src/routes/safe/container/selector'
1727
1828// TODO: these types will come from the Client GW SDK once #72 is merged
1929type Page < T > = {
@@ -40,22 +50,19 @@ const StyledHeading = styled(Heading)`
4050 padding-bottom: 0;
4151`
4252
53+ const useStyles = makeStyles ( styles )
54+
4355const Delegates = ( ) : ReactElement => {
4456 const { address : safeAddress } = useSelector ( currentSafeWithNames )
4557 const userAccount = useSelector ( userAccountSelector )
58+ const granted = useSelector ( grantedSelector )
4659 const { transactionService } = getChainInfo ( )
4760 const [ delegatesList , setDelegatesList ] = useState < DelegateResponse [ 'results' ] > ( [ ] )
4861 const [ addDelegateModalOpen , setAddDelegateModalOpen ] = useState < boolean > ( false )
62+ const columns = generateColumns ( )
63+ const autoColumns = columns . filter ( ( { custom } ) => ! custom )
4964
50- const headerCells : TableHeader [ ] = useMemo (
51- ( ) => [
52- { id : 'delegate' , label : 'Delegate' } ,
53- { id : 'delegator' , label : 'Delegator' } ,
54- { id : 'label' , label : 'Label' } ,
55- ] ,
56- [ ] ,
57- )
58- const rows : TableRow [ ] = useMemo ( ( ) => [ ] , [ ] )
65+ const classes = useStyles ( styles )
5966
6067 const fetchDelegates = useCallback ( ( ) => {
6168 const url = `${ transactionService } /api/v1/safes/${ safeAddress } /delegates/`
@@ -82,22 +89,6 @@ const Delegates = (): ReactElement => {
8289 fetchDelegates ( )
8390 } , [ fetchDelegates , safeAddress , transactionService ] )
8491
85- useEffect ( ( ) => {
86- if ( delegatesList . length ) {
87- let index = 0
88- for ( const obj of delegatesList ) {
89- rows . push ( {
90- id : `${ index ++ } ` ,
91- cells : [
92- { id : 'delegate' , content : < Text size = "xl" > { checksumAddress ( obj . delegate ) } </ Text > } ,
93- { id : 'delegator' , content : < Text size = "xl" > { checksumAddress ( obj . delegator ) } </ Text > } ,
94- { id : 'label' , content : < Text size = "xl" > { obj . label } </ Text > } ,
95- ] ,
96- } )
97- }
98- }
99- } , [ delegatesList , rows ] )
100-
10192 const handleAddDelegate = async ( { address, label } ) => {
10293 // close Add delegate modal
10394 setAddDelegateModalOpen ( false )
@@ -140,7 +131,73 @@ const Delegates = (): ReactElement => {
140131 Add delegate
141132 </ ButtonLink >
142133 < pre > { JSON . stringify ( delegatesList , undefined , 2 ) } </ pre >
143- < Table headers = { headerCells } rows = { rows } />
134+ < TableContainer >
135+ < Table columns = { columns } data = { delegatesList } defaultFixed disableLoadingOnEmptyTable disablePagination >
136+ { ( data ) =>
137+ data . map ( ( row , index ) => {
138+ const hideBorderBottom = index >= 3 && index === data . size - 1 && classes . noBorderBottom
139+ return (
140+ < TableRow className = { cn ( classes . hide , hideBorderBottom ) } key = { index } >
141+ { autoColumns . map ( ( column ) => {
142+ const displayEthHash = [ DELEGATE_ADDRESS_ID , DELEGATOR_ADDRESS_ID ] . includes ( column . id )
143+ return (
144+ < TableCell component = "td" key = { column . id } style = { cellWidth ( column . width ) } >
145+ { displayEthHash ? (
146+ < Block justify = "left" >
147+ < PrefixedEthHashInfo
148+ hash = { row [ column . id ] }
149+ shortenHash = { 4 }
150+ showCopyBtn
151+ showAvatar
152+ explorerUrl = { getExplorerInfo ( row [ column . id ] ) }
153+ />
154+ </ Block >
155+ ) : (
156+ row [ column . id ]
157+ ) }
158+ </ TableCell >
159+ )
160+ } ) }
161+ < TableCell component = "td" >
162+ < Row align = "end" className = { classes . actions } >
163+ < ButtonHelper
164+ onClick = { ( ) => {
165+ // setSelectedEntry({
166+ // entry: row,
167+ // isOwnerAddress: userOwner,
168+ // })
169+ // setEditCreateEntryModalOpen(true)
170+ } }
171+ >
172+ < Icon
173+ size = "sm"
174+ type = "edit"
175+ tooltip = "Edit delegate"
176+ className = { granted ? classes . editEntryButton : classes . editEntryButtonNonOwner }
177+ />
178+ </ ButtonHelper >
179+ < ButtonHelper
180+ onClick = { ( ) => {
181+ // setSelectedEntry({ entry: row })
182+ // setDeleteEntryModalOpen(true)
183+ } }
184+ >
185+ < Icon
186+ size = "sm"
187+ type = "delete"
188+ color = "error"
189+ tooltip = "Remove delegate"
190+ className = { granted ? classes . removeEntryButton : classes . removeEntryButtonNonOwner }
191+ />
192+ </ ButtonHelper >
193+ </ Row >
194+ </ TableCell >
195+ </ TableRow >
196+ )
197+ } )
198+ }
199+ </ Table >
200+ </ TableContainer >
144201 < AddDelegateModal
145202 isOpen = { addDelegateModalOpen }
146203 onClose = { ( ) => setAddDelegateModalOpen ( false ) }
0 commit comments