1import React, { useCallback, useMemo, useState } from 'react';
2import { Grid, Text, P, LinkButton } from 'indigo-react';
3import * as azimuth from 'azimuth-js';
5import { useNetwork } from 'store/network';
6import { usePointCursor } from 'store/pointCursor';
8import * as need from 'lib/need';
9import useEthereumTransaction from 'lib/useEthereumTransaction';
10import { GAS_LIMITS } from 'lib/constants';
11import { useLocalRouter } from 'lib/LocalRouter';
13import ViewHeader from 'components/ViewHeader';
14import InlineEthereumTransaction from 'components/InlineEthereumTransaction';
15import View from 'components/View';
16import BridgeForm from 'form/BridgeForm';
17import convertToInt from 'lib/convertToInt';
19function useCastDocumentVote() {
20 const { contracts } = useNetwork();
21 const { pointCursor } = usePointCursor();
23 const _contracts = need.contracts(contracts);
24 const _point = need.point(pointCursor);
26 return useEthereumTransaction(
29 return azimuth.ecliptic.castDocumentVote(
38 useCallback(() => null, []), //TODO update polls[hash].hasVoted ?
39 GAS_LIMITS.DEFAULT //TODO update once we have a decent sample size
43export default function Senate() {
44 const { pop } = useLocalRouter();
45 const { contracts } = useNetwork();
46 const { pointCursor } = usePointCursor();
48 const _contracts = need.contracts(contracts);
49 const _point = convertToInt(need.point(pointCursor), 10);
51 const { construct, bind } = useCastDocumentVote();
53 // const [documentHash, setDocumentHash] = useState('0x...');
54 const [majorities, setMajorities] = useState([]);
55 const [proposals, setProposals] = useState([]);
56 const [polls, setPolls] = useState({});
57 const [loading, setLoading] = useState(true);
58 const [votingOn, setVotingOn] = useState({});
60 //TODO update every second?
61 const now = Math.round(new Date().getTime() / 1000);
64 let proposals = await azimuth.polls.getDocumentProposals(_contracts);
65 let majorities = await azimuth.polls.getDocumentMajorities(_contracts);
67 for (let doc of proposals) {
68 let poll = await azimuth.polls.getDocumentPoll(_contracts, doc);
69 poll.endTime = convertToInt(poll.start) + convertToInt(poll.duration);
70 poll.hasVoted = await azimuth.polls.hasVotedOnDocumentPoll(
77 setMajorities(majorities);
78 setProposals(proposals);
82 }, [_contracts, _point]);
84 // eslint-disable-next-line
85 const [open, closed] = useMemo(() => {
88 if (loading) return [open, closed];
89 for (let doc of proposals) {
90 if (now > polls[doc].endTime) {
96 return [open, closed];
97 }, [loading, proposals, polls, now]);
99 const doVote = useCallback(
101 construct(hash, accept);
102 setVotingOn({ hash, accept });
104 [construct, setVotingOn]
107 const didVote = useCallback(() => {
108 polls[votingOn].hasVoted = true;
111 }, [polls, setPolls, votingOn]);
113 const majorityList = useMemo(() => {
114 return majorities.map(doc => {
116 <Grid.Item full as={Text}>
119 rel="noopener noreferrer"
120 href={`https://raw.githubusercontent.com/urbit/azimuth/master/proposals/${doc}.txt`}>
128 const openList = useMemo(() => {
129 return open.map(doc => {
130 const hasVoted = polls[doc].hasVoted;
133 <Grid.Item full as={Text}>
139 'Your vote has been cast.'
142 <LinkButton onClick={() => doVote(doc, true)}>
143 {votingOn.hash === doc && votingOn.accept ? (
150 <LinkButton onClick={() => doVote(doc, false)}>
151 {votingOn.hash === doc && !votingOn.accept ? (
165 }, [open, polls, votingOn, doVote]);
167 const onDocumentChange = useCallback(({ valid, values }) => {
168 // if (values.document) {
169 // const hash = '0x' + keccak256(values.document.trim()).toString('hex');
170 // setDocumentHash(hash);
172 // setDocumentHash('0x...');
177 <View pop={pop} inset>
179 <Grid.Item full as={ViewHeader}>
180 Senate: Document Proposals
183 <Grid.Item full as={Grid}>
184 <Grid.Item full as={P}>
192 <Grid.Item full as={Grid}>
193 <Grid.Item full as={P}>
199 <BridgeForm onValues={onDocumentChange}>
200 {({ onSubmit, values }) => (
204 as={InlineEthereumTransaction}
215 // placeholder="Input some text to find its hash..."
220 // <Grid.Item full as={CopiableAddress}>
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list