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
43function renderDocumentHash(doc) {
44 return doc.slice(0, 34) + '\n ' + doc.slice(34);
47export default function Senate() {
48 const { pop } = useLocalRouter();
49 const { contracts } = useNetwork();
50 const { pointCursor } = usePointCursor();
52 const _contracts = need.contracts(contracts);
53 const _point = convertToInt(need.point(pointCursor), 10);
55 const { construct, bind } = useCastDocumentVote();
57 // const [documentHash, setDocumentHash] = useState('0x...');
58 const [majorities, setMajorities] = useState([]);
59 const [proposals, setProposals] = useState([]);
60 const [polls, setPolls] = useState({});
61 const [loading, setLoading] = useState(true);
62 const [votingOn, setVotingOn] = useState({});
64 //TODO update every second?
65 const now = Math.round(new Date().getTime() / 1000);
68 let proposals = await azimuth.polls.getDocumentProposals(_contracts);
69 let majorities = await azimuth.polls.getDocumentMajorities(_contracts);
71 for (let doc of proposals) {
72 let poll = await azimuth.polls.getDocumentPoll(_contracts, doc);
73 poll.endTime = convertToInt(poll.start) + convertToInt(poll.duration);
74 poll.hasVoted = await azimuth.polls.hasVotedOnDocumentPoll(
81 setMajorities(majorities);
82 setProposals(proposals);
86 }, [_contracts, _point]);
88 // eslint-disable-next-line
89 const [open, closed] = useMemo(() => {
92 if (loading) return [open, closed];
93 for (let doc of proposals) {
94 if (now > polls[doc].endTime) {
100 return [open, closed];
101 }, [loading, proposals, polls, now]);
103 const doVote = useCallback(
105 construct(hash, accept);
106 setVotingOn({ hash, accept });
108 [construct, setVotingOn]
111 const didVote = useCallback(() => {
112 polls[votingOn].hasVoted = true;
115 }, [polls, setPolls, votingOn]);
117 const majorityList = useMemo(() => {
118 return majorities.map(doc => {
120 <Grid.Item full className="mv2" as={P}>
123 rel="noopener noreferrer"
124 href={`https://raw.githubusercontent.com/urbit/azimuth/master/proposals/${doc}.txt`}
125 class="mb4 mono wrap pre">
126 {renderDocumentHash(doc)}↗
133 const openList = useMemo(() => {
134 return open.map(doc => {
135 const hasVoted = polls[doc].hasVoted;
138 <Grid.Item full className="mv2" as={Text}>
139 <P className="m0 mono wrap pre">{renderDocumentHash(doc)}</P>
142 'Your vote has been cast.'
145 <LinkButton onClick={() => doVote(doc, true)}>
146 {votingOn.hash === doc && votingOn.accept ? (
153 <LinkButton onClick={() => doVote(doc, false)}>
154 {votingOn.hash === doc && !votingOn.accept ? (
168 }, [open, polls, votingOn, doVote]);
170 const onDocumentChange = useCallback(({ valid, values }) => {
171 // if (values.document) {
172 // const hash = '0x' + keccak256(values.document.trim()).toString('hex');
173 // setDocumentHash(hash);
175 // setDocumentHash('0x...');
180 <View pop={pop} inset>
182 <Grid.Item full as={ViewHeader}>
183 Senate: Document Proposals
186 <Grid.Item full as={Grid}>
187 <Grid.Item full as={P}>
193 <Grid.Divider className="mv4" />
195 <Grid.Item full as={Grid}>
196 <Grid.Item full as={P}>
202 <BridgeForm onValues={onDocumentChange}>
203 {({ onSubmit, values }) => (
207 as={InlineEthereumTransaction}
218 // placeholder="Input some text to find its hash..."
223 // <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