Skip to content

Commit

Permalink
fix: proposals list ordering (#977)
Browse files Browse the repository at this point in the history
* feat: order correctly by end time instead of created time

Not all proposal types have an edit, so not all proposals don't currently have a created time. We can use endTime or startTime as a proxy for created time.

* feat: accepted or rejected status
  • Loading branch information
baiirun authored Jan 10, 2025
1 parent eb6d2e6 commit 41b15b8
Showing 1 changed file with 86 additions and 9 deletions.
95 changes: 86 additions & 9 deletions apps/web/partials/governance/governance-proposals-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ const SubstreamActiveProposal = Schema.extend(
type SubstreamActiveProposal = Schema.Schema.Type<typeof SubstreamActiveProposal>;

interface NetworkResult {
proposals: {
activeProposals: {
nodes: SubstreamActiveProposal[];
};
completedProposals: {
nodes: SubstreamActiveProposal[];
};
}
Expand Down Expand Up @@ -164,13 +167,15 @@ const getFetchActiveProposalsQuery = (
first: number,
skip: number,
connectedAddress: string | undefined
) => `query {
proposals(
) => `
activeProposals: proposals(
first: ${first}
offset: ${skip}
orderBy: EDIT_BY_EDIT_ID__CREATED_AT_DESC
orderBy: END_TIME_DESC
filter: {
spaceId: { equalTo: "${spaceId}" }
status: { equalTo: PROPOSED }
endTime: { greaterThanOrEqualTo: ${Math.floor(Date.now() / 1000)} }
or: [
{ type: { equalTo: ADD_EDIT } }
{ type: { equalTo: ADD_SUBSPACE } }
Expand Down Expand Up @@ -215,7 +220,73 @@ const getFetchActiveProposalsQuery = (
}
}
}
}`;
`;

const getFetchCompletedProposalsQuery = (
spaceId: string,
first: number,
skip: number,
connectedAddress: string | undefined
) => `
completedProposals: proposals(
first: ${first}
offset: ${skip}
orderBy: END_TIME_DESC
filter: {
spaceId: { equalTo: "${spaceId}" }
status: { in: [ACCEPTED, REJECTED] }
or: [
{ type: { equalTo: ADD_EDIT } }
{ type: { equalTo: ADD_SUBSPACE } }
{ type: { equalTo: REMOVE_SUBSPACE } }
]
}
) {
nodes {
id
edit {
id
name
createdAt
createdAtBlock
}
type
onchainProposalId
createdById
startTime
endTime
status
proposalVotes {
totalCount
nodes {
vote
accountId
}
}
userVotes: proposalVotes(
filter: {
accountId: { equalTo: "${connectedAddress ?? ''}" }
}
) {
nodes {
vote
accountId
}
}
}
}
`;

const allProposalsQuery = (spaceId: string, first: number, skip: number, connectedAddress: string | undefined) => `
query {
${getFetchActiveProposalsQuery(spaceId, first, skip, connectedAddress)}
${getFetchCompletedProposalsQuery(spaceId, first, skip, connectedAddress)}
}
`;

async function fetchActiveProposals({
spaceId,
Expand All @@ -232,7 +303,7 @@ async function fetchActiveProposals({

const graphqlFetchEffect = graphql<NetworkResult>({
endpoint: Environment.getConfig().api,
query: getFetchActiveProposalsQuery(spaceId, first, offset, connectedAddress),
query: allProposalsQuery(spaceId, first, offset, connectedAddress),
});

const graphqlFetchWithErrorFallbacks = Effect.gen(function* (awaited) {
Expand All @@ -256,14 +327,20 @@ async function fetchActiveProposals({
error.message
);
return {
proposals: {
activeProposals: {
nodes: [],
},
completedProposals: {
nodes: [],
},
};
default:
console.error(`${error._tag}: Unable to fetch proposals, spaceId: ${spaceId} page: ${page}`);
return {
proposals: {
activeProposals: {
nodes: [],
},
completedProposals: {
nodes: [],
},
};
Expand All @@ -274,7 +351,7 @@ async function fetchActiveProposals({
});

const result = await Effect.runPromise(graphqlFetchWithErrorFallbacks);
const proposals = result.proposals.nodes;
const proposals = [...result.activeProposals.nodes, ...result.completedProposals.nodes];
const profilesForProposals = await fetchProfilesByAddresses(proposals.map(p => p.createdById));

return proposals.map(p => {
Expand Down

0 comments on commit 41b15b8

Please sign in to comment.