frontend client for gemstone. decentralised workplace app

refactor: abstract channels query hook as well

serenity b22da76c 02039883

Changed files
+71 -13
src
components
queries
+8 -13
src/components/Settings/ChannelSettings.tsx
···
import { Text } from "@/components/primitives/Text";
import { ChannelInfo } from "@/components/Settings/ChannelInfo";
import { useFacet } from "@/lib/facet";
-
import type { AtUri } from "@/lib/types/atproto";
-
import { stringToAtUri } from "@/lib/utils/atproto";
import { useOAuthSessionGuaranteed } from "@/providers/OAuthProvider";
import { useCurrentPalette } from "@/providers/ThemeProvider";
-
import { getChannelRecordsFromPds } from "@/queries/get-channels-from-pds";
-
import type { OAuthSession } from "@atproto/oauth-client";
-
import { useQuery } from "@tanstack/react-query";
-
import { MessagesSquare } from "lucide-react-native";
-
import { View } from "react-native";
export const ChannelSettings = () => {
const { atoms, typography } = useFacet();
const { semantic } = useCurrentPalette();
const session = useOAuthSessionGuaranteed();
-
const { isLoading, data: channels } = useQuery({
-
queryKey: ["channels", session.did],
-
queryFn: async () => {
-
return await channelsQueryFn(session);
-
},
-
});
return isLoading ? (
<Loading />
···
import { Text } from "@/components/primitives/Text";
import { ChannelInfo } from "@/components/Settings/ChannelInfo";
import { useFacet } from "@/lib/facet";
+
import { fade, lighten } from "@/lib/facet/src/lib/colors";
import { useOAuthSessionGuaranteed } from "@/providers/OAuthProvider";
import { useCurrentPalette } from "@/providers/ThemeProvider";
+
import { useChannelsQuery } from "@/queries/hooks/useChannelsQuery";
+
import { MessagesSquare, Plus } from "lucide-react-native";
+
import { useState } from "react";
+
import { Modal, Pressable, View } from "react-native";
export const ChannelSettings = () => {
const { atoms, typography } = useFacet();
const { semantic } = useCurrentPalette();
const session = useOAuthSessionGuaranteed();
+
const [showAddModal, setShowAddModal] = useState(false);
+
const { useQuery } = useChannelsQuery(session);
+
const { isLoading, data: channels } = useQuery();
return isLoading ? (
<Loading />
+63
src/queries/hooks/useChannelsQuery.ts
···
···
+
import type { AtUri } from "@/lib/types/atproto";
+
import { stringToAtUri } from "@/lib/utils/atproto";
+
import { getChannelRecordsFromPds } from "@/queries/get-channels-from-pds";
+
import type { OAuthSession } from "@atproto/oauth-client";
+
import { useQuery } from "@tanstack/react-query";
+
+
export const useChannelsQuery = (session: OAuthSession) => {
+
const queryKey = ["channels", session.did];
+
return {
+
queryKey,
+
useQuery: () =>
+
useQuery({
+
queryKey,
+
queryFn: async () => {
+
return await channelsQueryFn(session);
+
},
+
}),
+
};
+
};
+
+
const channelsQueryFn = async (session: OAuthSession) => {
+
const channels = await getChannelRecordsFromPds({
+
pdsEndpoint: session.serverMetadata.issuer,
+
did: session.did,
+
});
+
+
if (!channels.ok) {
+
console.error("channelsQueryFn error.", channels.error);
+
throw new Error(
+
`Something went wrong while getting the user's channel records.}`,
+
);
+
}
+
+
const results = channels.data
+
.map((record) => {
+
const convertResult = stringToAtUri(record.uri);
+
if (!convertResult.ok) {
+
console.error(
+
"Could not convert",
+
record,
+
"into at:// URI object.",
+
convertResult.error,
+
);
+
return;
+
}
+
if (!convertResult.data.collection || !convertResult.data.rKey) {
+
console.error(
+
record,
+
"did not convert to a full at:// URI with collection and rkey.",
+
);
+
return;
+
}
+
const uri: Required<AtUri> = {
+
authority: convertResult.data.authority,
+
collection: convertResult.data.collection,
+
rKey: convertResult.data.rKey,
+
};
+
return { cid: record.cid, uri, value: record.value };
+
})
+
.filter((atUri) => atUri !== undefined);
+
+
return results;
+
};