frontend client for gemstone. decentralised workplace app

feat: invites from constellation query

serenity 7bcdde0a 73e35f4f

+56
src/queries/get-invites-from-constellation.ts
···
+
import { didSchema, type Did } from "@/lib/types/atproto";
+
import { getConstellationBacklinks } from "@/lib/utils/constellation";
+
import type { Result } from "@/lib/utils/result";
+
import { z } from "zod";
+
+
// TODO: use prism instead of constellation, so that we can get the full record
+
// in the future. that way we can track the status of an invite.
+
export const getInvitesFromConstellation = async (
+
did: Did,
+
): Promise<
+
Result<
+
{
+
invites: Array<{
+
did: `did:${string}:${string}`;
+
collection: "systems.gmstn.development.channel.invite";
+
rkey: string;
+
}>;
+
},
+
unknown
+
>
+
> => {
+
const backlinksResult = await getConstellationBacklinks({
+
subject: did,
+
source: {
+
nsid: "systems.gmstn.development.channel.invite",
+
fieldName: "recipient",
+
},
+
});
+
+
if (!backlinksResult.ok) return { ok: false, error: backlinksResult.error };
+
+
const {
+
success,
+
error,
+
data: records,
+
} = z
+
.array(
+
z.object({
+
did: didSchema,
+
collection: z.literal(
+
"systems.gmstn.development.channel.invite",
+
),
+
rkey: z.string(),
+
}),
+
)
+
.safeParse(backlinksResult.data);
+
+
if (!success) return { ok: false, error };
+
+
return {
+
ok: true,
+
data: {
+
invites: [...records],
+
},
+
};
+
};
+30
src/queries/hooks/useConstellationInvitesQuery.ts
···
+
import { getInvitesFromConstellation } from "@/queries/get-invites-from-constellation";
+
import type { OAuthSession } from "@atproto/oauth-client";
+
import { useQuery } from "@tanstack/react-query";
+
+
export const useConstellationInvitesQuery = (session: OAuthSession) => {
+
const queryKey = ["invites", session.did];
+
return {
+
queryKey,
+
useQuery: () =>
+
useQuery({
+
queryKey,
+
queryFn: async () => {
+
return await invitesQueryFn(session);
+
},
+
}),
+
};
+
};
+
+
const invitesQueryFn = async (session: OAuthSession) => {
+
const invites = await getInvitesFromConstellation(session.did);
+
+
if (!invites.ok) {
+
console.error("invitesQueryFn error.", invites.error);
+
throw new Error(
+
`Something went wrong while getting the user's invite records.}`,
+
);
+
}
+
+
return invites.data;
+
};