-
Notifications
You must be signed in to change notification settings - Fork 3
/
useGroupedProposals.ts
77 lines (70 loc) · 1.96 KB
/
useGroupedProposals.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { gql, request } from "graphql-request";
import { useQuery } from "wagmi";
import { useSnapshotConfig } from "./useSnapshotConfig";
// Snapshot docs here: https://docs.snapshot.org/tools/graphql-api
const PROPOSALS_QUERY = (snapshotSpace: string, startTimestamp: number) => gql`
query {
proposals(
first: 100
skip: 0
where: { space_in: ["${snapshotSpace}"], created_gte: ${startTimestamp} }
orderBy: "created"
orderDirection: desc
) {
id
title
start
end
snapshot
state
}
}
`;
export type Proposal = {
id: string;
title: string;
/** This is a **unix timestamp** (seconds, not ms). */
start: number;
/** This is a **unix timestamp** (seconds, not ms). */
end: number;
/** Block number as a string */
snapshot: string;
state: "closed" | "open" | "pending";
};
type ProposalsQueryResult = {
proposals: Proposal[];
};
/**
*
* @returns Groups of proposals. Each group contains proposals that have the
* same start and end.
*/
export const useGroupedProposals = () => {
const snapshot = useSnapshotConfig();
const fetch = async () => {
const result = await request<ProposalsQueryResult>(
snapshot.apiEndpoint,
PROPOSALS_QUERY(snapshot.space, snapshot.startTimestamp),
);
const proposalGroups: Proposal[][] = [];
// group all proposals that have the same start and end
result.proposals.forEach((proposal) => {
const group = proposalGroups.find(
(group) =>
group[0].start === proposal.start && group[0].end === proposal.end,
);
if (group) {
group.push(proposal);
} else {
proposalGroups.push([proposal]);
}
});
return proposalGroups;
};
return useQuery([snapshot.apiEndpoint, snapshot.space, "proposals"], fetch, {
cacheTime: 604_800_000, // 7 days
staleTime: 3_600_000, // 1 hour
refetchInterval: 3_600_000, // 1 hour
refetchIntervalInBackground: false,
});
};