From d6dda01ebd6776702a1297561e1a1250ff839d83 Mon Sep 17 00:00:00 2001
From: Sem <931684+sembrestels@users.noreply.github.com>
Date: Wed, 25 Sep 2024 18:26:53 +0100
Subject: [PATCH] Handle changing votes better in the UI
---
apps/web/src/components/VotingCard.tsx | 137 +++++++++++++++----------
packages/ui/tailwind.config.ts | 2 +-
2 files changed, 82 insertions(+), 57 deletions(-)
diff --git a/apps/web/src/components/VotingCard.tsx b/apps/web/src/components/VotingCard.tsx
index f049e34..547fbd2 100644
--- a/apps/web/src/components/VotingCard.tsx
+++ b/apps/web/src/components/VotingCard.tsx
@@ -54,17 +54,19 @@ const VotingCard = ({
}, [initialAllocation, projects]);
// Array of project addresses that have been voted on
- const votedProjects = Object.keys(votes).filter(
- (grantee) => (votes[grantee as `0x${string}`] ?? 0) > 0,
+ const votedProjects = (Object.keys(votes) as `0x${string}`[]).filter(
+ (grantee) => (votes[grantee] ?? 0) > 0,
);
const totalVotes = Object.values(votes).reduce((a, b) => a + b, 0);
const handleVote = (grantee: `0x${string}`, value: number) => {
- const newValue = Math.max(0, value || 0);
setVotes((prev) => ({
...prev,
- [grantee]: newValue,
+ [grantee]: Math.max(
+ 0,
+ Math.min(value || 0, votingPower - totalVotes + (prev[grantee] || 0)),
+ ),
}));
};
@@ -97,7 +99,6 @@ const VotingCard = ({
{projects.map((project) => {
- const voteCount = votes[project.account] || 0;
return (
{project.name}
-
-
-
- handleVote(
- project.account,
- Number.parseInt(e.target.value),
- )
- }
- className="w-16 bg-gray-600 text-center text-white rounded-none px-3 py-1 input-number-hide-arrows border-none"
- />
-
-
- {totalVotes > 0
- ? Math.round((voteCount / votingPower) * 100)
- : 0}
- %
-
-
+
);
})}
@@ -174,6 +133,72 @@ const VotingCard = ({
);
};
+function VoteControls({
+ project,
+ votes,
+ handleVote,
+ votingPower,
+ maxVotedProjects,
+ votedProjects,
+ totalVotes,
+}: {
+ project: Project;
+ votes: Allocation;
+ handleVote: (grantee: `0x${string}`, value: number) => void;
+ votingPower: number;
+ maxVotedProjects: number;
+ votedProjects: `0x${string}`[];
+ totalVotes: number;
+}) {
+ const voteCount = votes[project.account] || 0;
+ return (
+ <>
+
+
+
+ handleVote(project.account, Number.parseInt(e.target.value))
+ }
+ className="w-16 bg-gray-600 text-center text-white rounded-none px-3 py-1 input-number-hide-arrows border-none focus-visible:ring-0 focus-visible:ring-offset-0"
+ />
+
+
+ {totalVotes > 0 ? Math.round((voteCount / votingPower) * 100) : 0}%
+
+
+ >
+ );
+}
+
function SkeletonVote() {
return (
diff --git a/packages/ui/tailwind.config.ts b/packages/ui/tailwind.config.ts
index 9ded1f9..5172aa1 100644
--- a/packages/ui/tailwind.config.ts
+++ b/packages/ui/tailwind.config.ts
@@ -27,7 +27,7 @@ const config = {
colors: {
border: colors.yellow[500],
input: colors.gray[800],
- ring: colors.gray[800],
+ ring: colors.yellow[800],
background: colors.gray[800],
foreground: colors.gray[100],
primary: {