generated from scaffold-eth/scaffold-eth
-
Notifications
You must be signed in to change notification settings - Fork 695
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3210 from OlympusDAO/voterParticipation
add ability to view voter participation and comments
- Loading branch information
Showing
5 changed files
with
186 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { | ||
Box, | ||
Paper, | ||
Tab, | ||
Table, | ||
TableBody, | ||
TableCell, | ||
TableContainer, | ||
TableHead, | ||
TableRow, | ||
Tabs, | ||
Typography, | ||
} from "@mui/material"; | ||
import React, { useState } from "react"; | ||
import { useParams } from "react-router-dom"; | ||
import { useGetVotes } from "src/views/Governance/hooks/useGetVotes"; | ||
import { VoteRow } from "src/views/Governance/Proposals/VoteRow"; | ||
|
||
// Data for the tables, including a 'reason' field for each row | ||
const tablesData = [ | ||
{ | ||
id: "For", | ||
}, | ||
{ | ||
id: "Against", | ||
}, | ||
{ | ||
id: "Abstain", | ||
}, | ||
]; | ||
|
||
const TabPanel = (props: { children: React.ReactNode; value: number; index: number }) => { | ||
const { children, value, index, ...other } = props; | ||
return ( | ||
<div role="tabpanel" hidden={value !== index} id={`tabpanel-${index}`} aria-labelledby={`tab-${index}`} {...other}> | ||
{value === index && <Box p={3}>{children}</Box>} | ||
</div> | ||
); | ||
}; | ||
|
||
export default function GovernanceTable() { | ||
const { id } = useParams(); | ||
const [tabIndex, setTabIndex] = useState(0); | ||
const { data: voteData } = useGetVotes({ proposalId: id, support: tabIndex + 1 }); | ||
|
||
const handleTabChange = (event: React.SyntheticEvent, newIndex: number) => { | ||
setTabIndex(newIndex); | ||
}; | ||
|
||
return ( | ||
<Box sx={{ width: "100%" }}> | ||
<Tabs | ||
textColor="primary" | ||
aria-label="proposal tabs" | ||
indicatorColor="primary" | ||
value={tabIndex} | ||
onChange={handleTabChange} | ||
//hides the tab underline sliding animation in while <Zoom> is loading | ||
TabIndicatorProps={{ style: { display: "none" } }} | ||
centered | ||
> | ||
{" "} | ||
{tablesData.map((table, index) => ( | ||
<Tab label={table.id} key={index} /> | ||
))} | ||
</Tabs> | ||
|
||
{tablesData.map((table, index) => ( | ||
<TabPanel value={tabIndex} index={index} key={index}> | ||
<TableContainer component={Paper}> | ||
<Table> | ||
<TableHead> | ||
<TableRow> | ||
<TableCell>{tablesData[tabIndex].id}</TableCell> | ||
<TableCell align="right">Votes</TableCell> | ||
</TableRow> | ||
</TableHead> | ||
<TableBody> | ||
{voteData?.length ? ( | ||
voteData.map((row, i) => ( | ||
<TableRow key={i}> | ||
<VoteRow voter={row.voter} reason={row.reason} votes={row.votes} tx={row.transactionHash} /> | ||
</TableRow> | ||
)) | ||
) : ( | ||
<TableRow> | ||
<TableCell colSpan={2} align="center"> | ||
<Typography>No votes yet</Typography> | ||
</TableCell> | ||
</TableRow> | ||
)} | ||
</TableBody> | ||
</Table> | ||
</TableContainer> | ||
</TabPanel> | ||
))} | ||
</Box> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Box, Link, TableCell, Tooltip, Typography } from "@mui/material"; | ||
import { formatEther } from "ethers/lib/utils.js"; | ||
import { abbreviatedNumber } from "src/helpers"; | ||
import { truncateEthereumAddress } from "src/helpers/truncateAddress"; | ||
import { useEnsName } from "wagmi"; | ||
|
||
export const VoteRow = ({ | ||
voter, | ||
reason, | ||
votes, | ||
tx, | ||
}: { | ||
voter: string; | ||
reason?: string; | ||
votes: string; | ||
tx: string; | ||
}) => { | ||
const { data: ensName } = useEnsName({ address: voter as `0x${string}` }); | ||
return ( | ||
<> | ||
<TableCell> | ||
<Link href={`https://etherscan.io/tx/${tx}`} target="_blank" rel="noopener noreferrer"> | ||
<Tooltip title={voter}> | ||
<Box>{ensName || truncateEthereumAddress(voter)}</Box> | ||
</Tooltip> | ||
</Link> | ||
{/* Render the reason if provided, and style it as a comment */} | ||
{reason && ( | ||
<Typography variant="body2" sx={{ color: "gray", fontStyle: "italic", mt: 1 }}> | ||
"{reason}" | ||
</Typography> | ||
)} | ||
</TableCell> | ||
<TableCell align="right">{abbreviatedNumber.format(Number(formatEther(votes) || 0))} gOHM</TableCell> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { useQuery } from "@tanstack/react-query"; | ||
import request, { gql } from "graphql-request"; | ||
import { Environment } from "src/helpers/environment/Environment/Environment"; | ||
|
||
export const useGetVotes = ({ proposalId, support }: { proposalId?: string; support: number }) => { | ||
return useQuery( | ||
["getVotes", proposalId, support], | ||
async () => { | ||
const query = gql` | ||
query MyQuery { | ||
voteCasts(orderBy: votes, orderDirection: desc, where: {proposalId: ${proposalId}, support: ${support} }) { | ||
votes | ||
voter | ||
reason | ||
support | ||
transactionHash | ||
} | ||
} | ||
`; | ||
|
||
type votesResponse = { | ||
voteCasts: { | ||
votes: string; | ||
voter: string; | ||
reason: string; | ||
support: number; | ||
transactionHash: string; | ||
}[]; | ||
}; | ||
|
||
const subgraphUrl = Environment.getGovernanceSubgraphUrl(); | ||
const response = await request<votesResponse>(subgraphUrl, query); | ||
|
||
return response.voteCasts || []; | ||
}, | ||
{ enabled: !!proposalId && !!support }, | ||
); | ||
}; |