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 { keccak256 } from 'lib/wallet';
15import ViewHeader from 'components/ViewHeader';
16import InlineEthereumTransaction from 'components/InlineEthereumTransaction';
17import View from 'components/View';
18import BridgeForm from 'form/BridgeForm';
19import convertToInt from 'lib/convertToInt';
21function useCastDocumentVote() {
22 const { contracts } = useNetwork();
23 const { pointCursor } = usePointCursor();
25 const _contracts = need.contracts(contracts);
26 const _point = need.point(pointCursor);
28 return useEthereumTransaction(
31 return azimuth.ecliptic.castDocumentVote(
40 useCallback(() => null, []), //TODO update polls[hash].hasVoted ?
41 GAS_LIMITS.DEFAULT //TODO update once we have a decent sample size
45export default function Senate() {
46 const { pop } = useLocalRouter();
47 const { contracts } = useNetwork();
48 const { pointCursor } = usePointCursor();
50 const _contracts = need.contracts(contracts);
51 const _point = convertToInt(need.point(pointCursor), 10);
53 const { construct, bind } = useCastDocumentVote();
55 // const [documentHash, setDocumentHash] = useState('0x...');
56 const [majorities, setMajorities] = useState([]);
57 const [proposals, setProposals] = useState([]);
58 const [polls, setPolls] = useState({});
59 const [loading, setLoading] = useState(true);
60 const [votingOn, setVotingOn] = useState({});
62 //TODO update every second?
63 const now = Math.round(new Date().getTime() / 1000);
66 let proposals = await azimuth.polls.getDocumentProposals(_contracts);
67 let majorities = await azimuth.polls.getDocumentMajorities(_contracts);
69 for (let doc of proposals) {
70 let poll = await azimuth.polls.getDocumentPoll(_contracts, doc);
71 poll.endTime = convertToInt(poll.start) + convertToInt(poll.duration);
72 poll.hasVoted = await azimuth.polls.hasVotedOnDocumentPoll(
79 setMajorities(majorities);
80 setProposals(proposals);
84 }, [_contracts, _point]);
86 const [open, closed] = useMemo(() => {
89 if (loading) return [open, closed];
90 for (let doc of proposals) {
91 if (now > polls[doc].endTime) {
97 return [open, closed];
98 }, [loading, proposals, polls, now]);
100 const doVote = useCallback(
102 construct(hash, accept);
103 setVotingOn({ hash, accept });
105 [construct, setVotingOn]
108 const didVote = useCallback(() => {
109 polls[votingOn].hasVoted = true;
112 }, [polls, setPolls, votingOn]);
114 const majorityList = useMemo(() => {
115 return majorities.map(doc => {
117 <Grid.Item full as={Text}>
120 rel="noopener noreferrer"
121 href={`https://raw.githubusercontent.com/urbit/azimuth/master/proposals/${doc}.txt`}>
129 const openList = useMemo(() => {
130 return open.map(doc => {
131 const hasVoted = polls[doc].hasVoted;
134 <Grid.Item full as={Text}>
140 'Your vote has been cast.'
143 <LinkButton onClick={() => doVote(doc, true)}>
144 {votingOn.hash === doc && votingOn.accept ? (
151 <LinkButton onClick={() => doVote(doc, false)}>
152 {votingOn.hash === doc && !votingOn.accept ? (
166 }, [open, polls, votingOn, doVote]);
168 const onDocumentChange = useCallback(({ valid, values }) => {
169 // if (values.document) {
170 // const hash = '0x' + keccak256(values.document.trim()).toString('hex');
171 // setDocumentHash(hash);
173 // setDocumentHash('0x...');
178 <View pop={pop} inset>
180 <Grid.Item full as={ViewHeader}>
181 Senate: Document Proposals
184 <Grid.Item full as={Grid}>
185 <Grid.Item full as={P}>
193 <Grid.Item full as={Grid}>
194 <Grid.Item full as={P}>
200 <BridgeForm onValues={onDocumentChange}>
201 {({ onSubmit, values }) => (
205 as={InlineEthereumTransaction}
216 // placeholder="Input some text to find its hash..."
221 // <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