Skip to content

Commit

Permalink
Merge pull request #113 from BreadchainCoop/development
Browse files Browse the repository at this point in the history
customise project order on page
  • Loading branch information
subject026 authored Sep 2, 2024
2 parents 822e4ac + cd11f3b commit 373aee9
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 26 deletions.
78 changes: 68 additions & 10 deletions src/app/governance/GovernancePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,33 @@ import { useMinRequiredVotingPower } from "./useMinRequiredVotingPower";
import { InfoCallout } from "./components/InfoCallout";
import { useDistributions } from "./useDistributions";
import { useModal } from "../core/context/ModalContext";
import { ProjectMeta, projectsMeta } from "../projectsMeta";

export type Project = {
address: Hex;
points: number;
};

type ProjectWithMeta = Prettify<
ProjectMeta & { account: Hex; currentDistribution: number }
>;

type Prettify<T> = {
[K in keyof T]: T[K];
} & {};

export type CurrentProjectsLoading = { status: "LOADING"; data: null };
export type CurrentProjectsSuccess = {
status: "SUCCESS";
data: ProjectWithMeta[];
sortedData: ProjectWithMeta[];
};
export type CurrentProjectsError = { status: "ERROR"; data: null };
export type CurrentProjectsState =
| CurrentProjectsLoading
| CurrentProjectsSuccess
| CurrentProjectsError;

export function GovernancePage() {
const { user, isSafe } = useConnectedUser();
const { currentVotingDistribution } = useCurrentVotingDistribution();
Expand All @@ -49,25 +70,62 @@ export function GovernancePage() {
}
}, [modalState, setModal]);

const currentProjects = useMemo<CurrentProjectsState>(() => {
if (currentVotingDistribution.status === "LOADING")
return { status: "LOADING", data: null };

if (currentVotingDistribution.status === "ERROR")
return { status: "ERROR", data: null };
let data;
try {
data = currentVotingDistribution.data[0].map(
(account, i): ProjectWithMeta => {
if (!projectsMeta[account])
throw new Error("no metadata found for project!");
return {
account,
...projectsMeta[account],
currentDistribution: currentVotingDistribution.data[1][i],
};
}
);
} catch (err) {
console.log(err);
return {
status: "ERROR",
data: null,
};
}
return {
status: "SUCCESS",
data,
sortedData: data.toSorted((a, b) => a.order - b.order),
};
}, [currentVotingDistribution]);

useEffect(() => {
if (projects.length) return;
if (currentVotingDistribution.status === "SUCCESS") {
if (currentProjects.status === "SUCCESS") {
setProjects(
currentVotingDistribution.data[0].map((address) => ({
address,
currentProjects.data.map((project) => ({
address: project.account,
points: 0,
}))
);
}
}, [currentVotingDistribution, projects]);
}, [currentProjects, projects]);

function updateValue(value: number, address: Hex) {
console.log("value: ", value);
console.log("address: ", address);
const updatedProjects = projects.map((project) => {
if (project.address === address) {
return { ...project, points: value };
}
return project;
});
console.dir({ projects });
console.dir({ updatedProjects });
setProjects(updatedProjects);
}

Expand Down Expand Up @@ -106,7 +164,7 @@ export function GovernancePage() {
: false;

if (
currentVotingDistribution.status === "ERROR" ||
currentProjects.status === "ERROR" ||
cycleDates.status === "ERROR" ||
cycleLength.status === "ERROR"
)
Expand All @@ -117,7 +175,7 @@ export function GovernancePage() {
);

if (
!projects.length ||
currentProjects.status === "LOADING" ||
currentVotingDistribution.status === "LOADING" ||
cycleDates.status === "LOADING" ||
cycleLength.status === "LOADING"
Expand Down Expand Up @@ -151,7 +209,7 @@ export function GovernancePage() {
/>

<div className="max-w-md m-auto col-span-12 row-start-3 row-span-1 lg:row-start-3 lg:col-start-9 lg:col-span-4 h-full flex flex-col gap-4">
<ResultsPanel distribution={currentVotingDistribution} />
<ResultsPanel projects={currentProjects} />
<InfoCallout />
</div>

Expand All @@ -169,9 +227,9 @@ export function GovernancePage() {
/>
</div>
<div className="col-span-12 row-start-5 lg:col-start-1 lg:col-span-8 lg:row-start-3 grid grid-cols-1 gap-3">
{currentVotingDistribution.data[0].map((address, i) => {
{currentProjects.sortedData.map((project, i) => {
return (
<ProjectRow key={address} address={address}>
<ProjectRow key={project.account} address={project.account}>
{!isRecasting && castVote && castVote.length > 0 ? (
<VoteDisplay
points={castVote[i]}
Expand All @@ -181,7 +239,7 @@ export function GovernancePage() {
<VoteForm
value={projects[i].points}
updateValue={updateValue}
address={address}
address={project.account}
totalPoints={totalPoints}
user={user}
userCanVote={userCanVote}
Expand Down
31 changes: 15 additions & 16 deletions src/app/governance/components/ResultsPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import { Hex } from "viem";

import { projectsMeta } from "@/app/projectsMeta";
import { formatVotePercentage } from "@/app/core/util/formatter";
import { CurrentVotingDistributionState } from "../useCurrentVotingDistribution";
import { CardBox } from "@/app/core/components/CardBox";
import { CurrentProjectsSuccess } from "../GovernancePage";

export function ResultsPanel({
distribution,
projects,
}: {
distribution: CurrentVotingDistributionState;
projects: CurrentProjectsSuccess;
}) {
const totalPoints =
distribution.status === "SUCCESS"
? distribution.data[1].reduce((acc, points) => acc + points, 0)
: 0;
const totalPoints = projects.sortedData.reduce(
(acc, points) => acc + points.currentDistribution,
0
);

return (
<section className="grid grid-cols-1 gap-4">
Expand All @@ -24,15 +24,14 @@ export function ResultsPanel({
results
</h3>
<div className="grid grid-cols-1 gap-4">
{distribution.status === "SUCCESS" &&
distribution.data[0].map((account, i) => (
<ResultsProject
key={`project_result_${account}`}
address={account}
projectPoints={distribution.data[1][i]}
totalPoints={totalPoints}
/>
))}
{projects.sortedData.map((project, i) => (
<ResultsProject
key={`project_result_${project.account}`}
address={project.account}
projectPoints={projects.sortedData[i].currentDistribution}
totalPoints={totalPoints}
/>
))}
</div>
</div>
</div>
Expand Down

0 comments on commit 373aee9

Please sign in to comment.