|
| 1 | +import { useQuery } from "@tanstack/react-query"; |
| 2 | +import { Address } from "viem"; |
| 3 | + |
| 4 | +import { useGraphqlBatcher } from "context/GraphqlBatcher"; |
| 5 | +import { isUndefined } from "utils/index"; |
| 6 | +import { sanitizeFilter } from "utils/sanitizeFilter"; |
| 7 | + |
| 8 | +import { graphql } from "src/graphql"; |
| 9 | +import { UserDrawsQuery, UserDrawsCountQuery, Draw_Filter, OrderDirection } from "src/graphql/graphql"; |
| 10 | +export type { UserDrawsQuery, UserDrawsCountQuery }; |
| 11 | + |
| 12 | +const userDrawsQuery = graphql(` |
| 13 | + query UserDraws($jurorId: ID!, $skip: Int, $first: Int, $orderDirection: OrderDirection, $where: Draw_filter) { |
| 14 | + user(id: $jurorId) { |
| 15 | + id |
| 16 | + totalResolvedVotes |
| 17 | + draws(first: $first, skip: $skip, orderBy: blockNumber, orderDirection: $orderDirection, where: $where) { |
| 18 | + id |
| 19 | + voteIDNum |
| 20 | + dispute { |
| 21 | + id |
| 22 | + disputeID |
| 23 | + period |
| 24 | + ruled |
| 25 | + currentRoundIndex |
| 26 | + arbitrated { |
| 27 | + id |
| 28 | + } |
| 29 | + court { |
| 30 | + id |
| 31 | + name |
| 32 | + } |
| 33 | + } |
| 34 | + round { |
| 35 | + id |
| 36 | + } |
| 37 | + vote { |
| 38 | + ... on ClassicVote { |
| 39 | + id |
| 40 | + choice |
| 41 | + commit |
| 42 | + commited |
| 43 | + voted |
| 44 | + localRound { |
| 45 | + ... on ClassicRound { |
| 46 | + id |
| 47 | + winningChoice |
| 48 | + } |
| 49 | + } |
| 50 | + justification { |
| 51 | + id |
| 52 | + choice |
| 53 | + reference |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | +`); |
| 61 | + |
| 62 | +const userDrawsCountQuery = graphql(` |
| 63 | + query UserDrawsCount($jurorId: ID!, $where: Draw_filter) { |
| 64 | + user(id: $jurorId) { |
| 65 | + id |
| 66 | + totalResolvedVotes |
| 67 | + draws(orderBy: blockNumber, where: $where) { |
| 68 | + id |
| 69 | + vote { |
| 70 | + ... on ClassicVote { |
| 71 | + id |
| 72 | + voted |
| 73 | + commited |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | +`); |
| 80 | + |
| 81 | +export const useUserDraws = ( |
| 82 | + jurorAddress?: Address, |
| 83 | + skip = 0, |
| 84 | + first = 5, |
| 85 | + where?: Draw_Filter, |
| 86 | + sortOrder?: OrderDirection |
| 87 | +) => { |
| 88 | + const { graphqlBatcher } = useGraphqlBatcher(); |
| 89 | + const sanitizedWhere = sanitizeFilter(where); |
| 90 | + const isEnabled = !isUndefined(jurorAddress); |
| 91 | + |
| 92 | + return useQuery<UserDrawsQuery>({ |
| 93 | + queryKey: ["useUserDraws", jurorAddress?.toLowerCase(), skip, first, sanitizedWhere, sortOrder], |
| 94 | + enabled: isEnabled, |
| 95 | + queryFn: async () => |
| 96 | + await graphqlBatcher.fetch({ |
| 97 | + id: crypto.randomUUID(), |
| 98 | + document: userDrawsQuery, |
| 99 | + variables: { |
| 100 | + jurorId: jurorAddress?.toLowerCase(), |
| 101 | + skip, |
| 102 | + first, |
| 103 | + where: sanitizedWhere, |
| 104 | + orderDirection: sortOrder ?? "desc", |
| 105 | + }, |
| 106 | + }), |
| 107 | + }); |
| 108 | +}; |
| 109 | + |
| 110 | +export const useUserDrawsCount = (jurorAddress?: Address, where?: Draw_Filter) => { |
| 111 | + const { graphqlBatcher } = useGraphqlBatcher(); |
| 112 | + const sanitizedWhere = sanitizeFilter(where); |
| 113 | + const isEnabled = !isUndefined(jurorAddress); |
| 114 | + |
| 115 | + return useQuery<UserDrawsCountQuery>({ |
| 116 | + queryKey: ["useUserDrawsCount", jurorAddress?.toLowerCase(), sanitizedWhere], |
| 117 | + enabled: isEnabled, |
| 118 | + queryFn: async () => |
| 119 | + await graphqlBatcher.fetch({ |
| 120 | + id: crypto.randomUUID(), |
| 121 | + document: userDrawsCountQuery, |
| 122 | + variables: { |
| 123 | + jurorId: jurorAddress?.toLowerCase(), |
| 124 | + where: sanitizedWhere, |
| 125 | + }, |
| 126 | + }), |
| 127 | + }); |
| 128 | +}; |
0 commit comments