Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | 11x 11x 11x 11x 11x 11x 11x 11x 109x 23x 109x 109x 109x 20x 89x 89x 109x 11x 32x 11x 47x 11x 11x 11x 11x 17x 6x 29x 11x 79x 14x 134x 79x 79x 79x 11x 11x 11x 22x 10x 31x 11x 33x 33x 2x 1x 1x 11x 270x 65x 261x | import { communityAdminsKey, communityDiscussionsKey, communityEventsKey, communityGroupsKey, communityGuidesKey, communityKey, communityMembersKey, communityNearbyUsersKey, communityPlacesKey, QueryType, subCommunitiesKey, threadKey, } from "features/queryKeys"; import useUsers from "features/userQueries/useUsers"; import { RpcError } from "grpc-web"; import { useRouter } from "next/router"; import { Community, ListAdminsRes, ListCommunitiesRes, ListDiscussionsRes, ListEventsRes, ListGroupsRes, ListGuidesRes, ListMembersRes, ListNearbyUsersRes, ListPlacesRes, } from "proto/communities_pb"; import { Discussion } from "proto/discussions_pb"; import { GetThreadRes } from "proto/threads_pb"; import { useEffect } from "react"; import { useInfiniteQuery, UseInfiniteQueryOptions, useMutation, useQuery, useQueryClient, UseQueryOptions, } from "react-query"; import { routeToCommunity } from "routes"; import { service } from "service"; export const useCommunity = ( id: number, communitySlug?: string, options?: Omit< UseQueryOptions<Community.AsObject, RpcError>, "queryKey" | "queryFn" | "enabled" > ) => { const queryResult = useQuery<Community.AsObject, RpcError>( communityKey(id), () => id ? service.communities.getCommunity(id) : Promise.reject(new Error("Invalid community id")), { ...options, enabled: !!id, } ); const router = useRouter(); useEffect(() => { if (!queryResult.isSuccess) { return; } const { slug, communityId } = queryResult.data; // guarantee the most recent slug is used if the community was loaded from url params // if no slug was provided in the url, then also redirect to page with slug in url Iif (!id && slug !== communitySlug && typeof window !== "undefined") { router.push(routeToCommunity(communityId, slug)); } }, [queryResult, router, id, communitySlug]); return { ...queryResult, id, }; }; //0 for communityId lists all communities export const useListSubCommunities = (communityId?: number) => useInfiniteQuery<ListCommunitiesRes.AsObject, RpcError>( subCommunitiesKey(communityId || 0), ({ pageParam }) => service.communities.listCommunities(communityId || 0, pageParam), { enabled: communityId !== undefined, getNextPageParam: (lastPage) => lastPage.nextPageToken ? lastPage.nextPageToken : undefined, } ); export const useListGroups = (communityId?: number) => useInfiniteQuery<ListGroupsRes.AsObject, RpcError>( communityGroupsKey(communityId!), ({ pageParam }) => service.communities.listGroups(communityId!, pageParam), { enabled: !!communityId, getNextPageParam: (lastPage) => lastPage.nextPageToken ? lastPage.nextPageToken : undefined, } ); export const useListPlaces = (communityId?: number) => useInfiniteQuery<ListPlacesRes.AsObject, RpcError>( communityPlacesKey(communityId!), ({ pageParam }) => service.communities.listPlaces(communityId!, pageParam), { enabled: !!communityId, getNextPageParam: (lastPage) => lastPage.nextPageToken ? lastPage.nextPageToken : undefined, } ); export const useListGuides = (communityId?: number) => useInfiniteQuery<ListGuidesRes.AsObject, RpcError>( communityGuidesKey(communityId!), ({ pageParam }) => service.communities.listGuides(communityId!, pageParam), { enabled: !!communityId, getNextPageParam: (lastPage) => lastPage.nextPageToken ? lastPage.nextPageToken : undefined, } ); export const useListDiscussions = (communityId: number) => useInfiniteQuery<ListDiscussionsRes.AsObject, RpcError>( communityDiscussionsKey(communityId), ({ pageParam }) => service.communities.listDiscussions(communityId, pageParam), { enabled: !!communityId, getNextPageParam: (lastPage) => lastPage.nextPageToken ? lastPage.nextPageToken : undefined, } ); export const useListAdmins = (communityId: number, type: QueryType) => { const query = useInfiniteQuery<ListAdminsRes.AsObject, RpcError>( communityAdminsKey(communityId, type), ({ pageParam }) => service.communities.listAdmins(communityId, pageParam), { enabled: !!communityId, getNextPageParam: (lastPage) => lastPage.nextPageToken ? lastPage.nextPageToken : undefined, } ); const adminIds = query.data?.pages.flatMap((page) => page.adminUserIdsList); const { data: adminUsers, isLoading: isAdminUsersLoading } = useUsers( adminIds ?? [] ); return { ...query, adminIds, adminUsers, isLoading: query.isLoading || isAdminUsersLoading, }; }; export const useListMembers = (communityId?: number) => useInfiniteQuery<ListMembersRes.AsObject, RpcError>( communityMembersKey(communityId!), ({ pageParam }) => service.communities.listMembers(communityId!, pageParam), { enabled: !!communityId, getNextPageParam: (lastPage) => lastPage.nextPageToken ? lastPage.nextPageToken : undefined, } ); export const useListNearbyUsers = (communityId?: number) => useInfiniteQuery<ListNearbyUsersRes.AsObject, RpcError>( communityNearbyUsersKey(communityId!), ({ pageParam }) => service.communities.listNearbyUsers(communityId!, pageParam), { enabled: !!communityId, getNextPageParam: (lastPage) => lastPage.nextPageToken ? lastPage.nextPageToken : undefined, } ); interface UseListCommunityEventsInput { communityId: number; pageSize?: number; type: QueryType; } export function useListCommunityEvents({ communityId, pageSize, type, }: UseListCommunityEventsInput) { return useInfiniteQuery<ListEventsRes.AsObject, RpcError>({ queryKey: communityEventsKey(communityId, type), queryFn: ({ pageParam }) => service.events.listCommunityEvents(communityId, pageParam, pageSize), getNextPageParam: (lastPage) => lastPage.nextPageToken || undefined, }); } // Discussions export interface CreateDiscussionInput { title: string; content: string; ownerCommunityId: number; } export const useNewDiscussionMutation = (onSuccess?: () => void) => { const queryClient = useQueryClient(); return useMutation<Discussion.AsObject, RpcError, CreateDiscussionInput>( ({ title, content, ownerCommunityId }) => service.discussions.createDiscussion(title, content, ownerCommunityId), { onSuccess(_, { ownerCommunityId }) { onSuccess?.(); queryClient.invalidateQueries( communityDiscussionsKey(ownerCommunityId) ); }, } ); }; export const useThread = ( threadId: number, options?: Omit< UseInfiniteQueryOptions<GetThreadRes.AsObject, RpcError>, "queryKey" | "queryFn" | "getNextPageParam" > ) => useInfiniteQuery<GetThreadRes.AsObject, RpcError>({ queryKey: threadKey(threadId), queryFn: ({ pageParam }) => service.threads.getThread(threadId, pageParam), getNextPageParam: (lastPage) => lastPage.nextPageToken || undefined, ...options, }); |