frontend client for gemstone. decentralised workplace app
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: invites from constellation query

serenity 7bcdde0a 73e35f4f

+86
+56
src/queries/get-invites-from-constellation.ts
··· 1 + import { didSchema, type Did } from "@/lib/types/atproto"; 2 + import { getConstellationBacklinks } from "@/lib/utils/constellation"; 3 + import type { Result } from "@/lib/utils/result"; 4 + import { z } from "zod"; 5 + 6 + // TODO: use prism instead of constellation, so that we can get the full record 7 + // in the future. that way we can track the status of an invite. 8 + export const getInvitesFromConstellation = async ( 9 + did: Did, 10 + ): Promise< 11 + Result< 12 + { 13 + invites: Array<{ 14 + did: `did:${string}:${string}`; 15 + collection: "systems.gmstn.development.channel.invite"; 16 + rkey: string; 17 + }>; 18 + }, 19 + unknown 20 + > 21 + > => { 22 + const backlinksResult = await getConstellationBacklinks({ 23 + subject: did, 24 + source: { 25 + nsid: "systems.gmstn.development.channel.invite", 26 + fieldName: "recipient", 27 + }, 28 + }); 29 + 30 + if (!backlinksResult.ok) return { ok: false, error: backlinksResult.error }; 31 + 32 + const { 33 + success, 34 + error, 35 + data: records, 36 + } = z 37 + .array( 38 + z.object({ 39 + did: didSchema, 40 + collection: z.literal( 41 + "systems.gmstn.development.channel.invite", 42 + ), 43 + rkey: z.string(), 44 + }), 45 + ) 46 + .safeParse(backlinksResult.data); 47 + 48 + if (!success) return { ok: false, error }; 49 + 50 + return { 51 + ok: true, 52 + data: { 53 + invites: [...records], 54 + }, 55 + }; 56 + };
+30
src/queries/hooks/useConstellationInvitesQuery.ts
··· 1 + import { getInvitesFromConstellation } from "@/queries/get-invites-from-constellation"; 2 + import type { OAuthSession } from "@atproto/oauth-client"; 3 + import { useQuery } from "@tanstack/react-query"; 4 + 5 + export const useConstellationInvitesQuery = (session: OAuthSession) => { 6 + const queryKey = ["invites", session.did]; 7 + return { 8 + queryKey, 9 + useQuery: () => 10 + useQuery({ 11 + queryKey, 12 + queryFn: async () => { 13 + return await invitesQueryFn(session); 14 + }, 15 + }), 16 + }; 17 + }; 18 + 19 + const invitesQueryFn = async (session: OAuthSession) => { 20 + const invites = await getInvitesFromConstellation(session.did); 21 + 22 + if (!invites.ok) { 23 + console.error("invitesQueryFn error.", invites.error); 24 + throw new Error( 25 + `Something went wrong while getting the user's invite records.}`, 26 + ); 27 + } 28 + 29 + return invites.data; 30 + };