|
| 1 | +import { Connect } from '@graphprotocol/hypergraph'; |
| 2 | +import { type UseQueryResult, useQuery } from '@tanstack/react-query'; |
| 3 | +import { gql, request } from 'graphql-request'; |
| 4 | + |
| 5 | +const publicSpacesQueryDocument = gql` |
| 6 | +query Spaces($accountAddress: String!) { |
| 7 | + spaces(filter: { |
| 8 | + member: { is: $accountAddress } |
| 9 | + }) { |
| 10 | + id |
| 11 | + type |
| 12 | + mainVotingAddress |
| 13 | + personalAddress |
| 14 | + entity { |
| 15 | + name |
| 16 | + } |
| 17 | + } |
| 18 | +} |
| 19 | +`; |
| 20 | + |
| 21 | +type SpaceQueryResult = { |
| 22 | + id: string; |
| 23 | + type: string; |
| 24 | + mainVotingAddress: string; |
| 25 | + personalAddress: string; |
| 26 | + entity: { |
| 27 | + name: string; |
| 28 | + }; |
| 29 | +}; |
| 30 | + |
| 31 | +type PublicSpacesQueryResult = { |
| 32 | + spaces: SpaceQueryResult[]; |
| 33 | +}; |
| 34 | + |
| 35 | +export type PublicSpaceData = { |
| 36 | + id: string; |
| 37 | + type: string; |
| 38 | + mainVotingAddress: string; |
| 39 | + personalAddress: string; |
| 40 | + name: string; |
| 41 | +}; |
| 42 | + |
| 43 | +export const usePublicSpaces = (url: string): UseQueryResult<PublicSpaceData[], Error> => { |
| 44 | + return useQuery<PublicSpaceData[]>({ |
| 45 | + queryKey: ['public-spaces'], |
| 46 | + queryFn: async () => { |
| 47 | + const accountAddress = Connect.loadAccountAddress(localStorage); |
| 48 | + if (!accountAddress) return []; |
| 49 | + const result = await request<PublicSpacesQueryResult>(url, publicSpacesQueryDocument, { |
| 50 | + accountAddress, |
| 51 | + }); |
| 52 | + return result?.spaces |
| 53 | + ? result.spaces.map((space: SpaceQueryResult) => ({ |
| 54 | + id: space.id, |
| 55 | + name: space.entity.name, |
| 56 | + type: space.type, |
| 57 | + mainVotingAddress: space.mainVotingAddress, |
| 58 | + personalAddress: space.personalAddress, |
| 59 | + })) |
| 60 | + : []; |
| 61 | + }, |
| 62 | + }); |
| 63 | +}; |
0 commit comments