on d760cbf help sign in
bridge/src/views/Admin/AdminEditPermissions.js
143 lines 3.9 kB history raw
1import React, { useState, useCallback } from 'react';
2import { Just, Nothing } from 'folktale/maybe';
3import * as need from 'lib/need';
4import { Grid } from 'indigo-react';
6import { ForwardButton } from 'components/Buttons';
7import { matchBlinky } from 'components/Blinky';
9import { ROUTE_NAMES } from 'lib/routeNames';
10import { ETH_ZERO_ADDR } from 'lib/wallet';
12import useLifecycle from 'lib/useLifecycle';
13import { useNetwork } from 'store/network';
14import { useHistory } from 'store/history';
15import { useWallet } from 'store/wallet';
16import { usePointCursor } from 'store/pointCursor';
17import { usePointCache } from 'store/pointCache';
18import ViewHeader from 'components/ViewHeader';
19import usePermissionsForPoint from 'lib/usePermissionsForPoint';
21//TODO consolidate with azimuth-js' getActivationBlock
22const getRekeyDate = async (web3, contracts, point) => {
23 const logs = await contracts.azimuth.getPastEvents('ChangedKeys', {
24 fromBlock: 0,
25 toBlock: 'latest',
26 filter: { point: [point] },
27 });
28 if (logs.length === 0) {
29 return null;
30 } else {
31 // last log is most recent
32 const block = await web3.eth.getBlock(logs[logs.length - 1].blockNumber);
33 return new Date(block.timestamp * 1000);
34 }
35};
37export default function AdminEditPermissions() {
38 const history = useHistory();
39 const { web3, contracts } = useNetwork();
40 const { wallet } = useWallet();
41 const { pointCursor } = usePointCursor();
42 const { getDetails } = usePointCache();
44 const [rekeyDate, setRekeyDate] = useState(Nothing());
46 const point = need.point(pointCursor);
47 const userAddress = need.wallet(wallet).address;
49 const pointDetails = need.details(getDetails(point));
50 const { isOwner, canManage, canSpawn, canVote } = usePermissionsForPoint(
51 userAddress,
52 point
53 );
55 useLifecycle(() => {
56 getRekeyDate(need.web3(web3), need.contracts(contracts), point).then(
57 res => {
58 if (res === null) setRekeyDate(Just('unknown'));
59 else setRekeyDate(Just(res.toString()));
60 }
61 );
62 });
64 const goSetManagement = useCallback(
65 () => history.push(ROUTE_NAMES.SET_MANAGEMENT_PROXY),
66 [history]
67 );
69 const goSetSpawn = useCallback(
70 () => history.push(ROUTE_NAMES.SET_SPAWN_PROXY),
71 [history]
72 );
74 const goSetVoting = useCallback(
75 () => history.push(ROUTE_NAMES.SET_VOTING_PROXY),
76 [history]
77 );
79 const goSetKeys = useCallback(() => history.push(ROUTE_NAMES.SET_KEYS), [
80 history,
81 ]);
83 const proxyAction = (name, address, onClick) => {
84 if (address === ETH_ZERO_ADDR) {
85 address = 'Not yet set';
86 }
88 return (
89 <Grid.Item full>
90 <ForwardButton disabled={!isOwner} onClick={onClick} detail={address}>
91 {`${name} Proxy Address`}
92 </ForwardButton>
93 </Grid.Item>
94 );
95 };
97 const networkKeysDetail =
98 pointDetails.keyRevisionNumber === 0 ? (
99 'Not yet configured'
100 ) : (
101 <>
102 {`Revision #${pointDetails.keyRevisionNumber}, since `}
103 {matchBlinky(rekeyDate)}
104 </>
105 );
107 return (
108 <Grid>
109 <Grid.Item full as={ViewHeader}>
110 Permissions
111 </Grid.Item>
112 <Grid.Item full as={ForwardButton} detail={pointDetails.owner} disabled>
113 Ownership Address
114 </Grid.Item>
115 <Grid.Divider />
116 {proxyAction('Management', pointDetails.managementProxy, goSetManagement)}
117 <Grid.Divider />
119 {canSpawn && (
120 <>
121 {proxyAction('Spawn', pointDetails.spawnProxy, goSetSpawn)}
122 <Grid.Divider />
123 </>
124 )}
126 {canVote && (
127 <>
128 {proxyAction('Voting', pointDetails.votingProxy, goSetVoting)}
129 <Grid.Divider />
130 </>
131 )}
132 <Grid.Item
133 full
134 as={ForwardButton}
135 disabled={!canManage}
136 onClick={goSetKeys}
137 detail={networkKeysDetail}>
138 Network keys
139 </Grid.Item>
140 <Grid.Divider />
141 </Grid>
142 );