···88 ShTangledActorProfile,
99 shTangledActorProfileSchema,
1010} from "@/lib/types/lexicons/sh/tangled/actor/profile";
1111+import { useQuery } from "@tanstack/react-query";
11121213/**
1314 * Gets the avatar of the specified DID from their repository. Specifically, queries for sh.tangled.actor.profile self records first, then as a fallback, queries for app.bsky.actor.profile.
···1718 * @param {string} params.did - The DID you want to fetch. This should be a "did:plc:..." or a "did:web:...". This is NOT checked at runtime.
1819 * @param {URL} params.repoUrl - The URL of the DID's home repository (PDS). Omit any trailing slashes. This is automatically trimmed at runtime.
1920 */
2020-export const getAvatar = async ({
2121- did,
2222- repoUrl,
2323-}: {
2424- did: string;
2525- repoUrl: URL;
2626-}) => {
2121+const getAvatar = async ({ did, repoUrl }: { did: string; repoUrl: URL }) => {
2722 const repoUrlString = repoUrl.toString();
2823 const cleanedUrl = repoUrlString.endsWith("/")
2924 ? repoUrlString.substring(0, repoUrlString.length - 1)
···134129135130 return ok(bskyProfile);
136131};
132132+133133+export const avatarQueryKey = (did: string) => ["avatar", did];
134134+135135+/**
136136+ * Hook that wraps useQuery with the query function being the getAvatar query. See below for the query function definition itself.
137137+ * Gets the avatar of the specified DID from their repository. Specifically, queries for sh.tangled.actor.profile self records first, then as a fallback, queries for app.bsky.actor.profile.
138138+ * If both return invalid avatar blob URLs, then it returns "#" as a fallback.
139139+ * You should **NOT** use '#' as the value to an <img> element's `src` attribute. Instead, call the function early then set some other fallback in the component return.
140140+ * @param {Object} params - The supplied params object.
141141+ * @param {string} params.did - The DID you want to fetch. This should be a "did:plc:..." or a "did:web:...". This is NOT checked at runtime.
142142+ * @param {URL} params.repoUrl - The URL of the DID's home repository (PDS). Omit any trailing slashes. This is automatically trimmed at runtime.
143143+ */
144144+export const useAvatarQuery = ({
145145+ did,
146146+ repoUrl,
147147+}: {
148148+ did: string;
149149+ repoUrl: URL;
150150+}) => {
151151+ return useQuery({
152152+ queryKey: avatarQueryKey(did),
153153+ queryFn: () => getAvatar({ did, repoUrl }),
154154+ });
155155+};