a tool for shared writing and social publishing
0

Configure Feed

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

simplify profile comments

Jared Pereira (May 27, 2026, 5:49 PM EDT) ddc8b105 b9f6d9f5

+27 -69
+27 -69
app/(app)/(home-pages)/p/[didOrHandle]/comments/getProfileComments.ts
··· 3 3 import { supabaseServerClient } from "supabase/serverClient"; 4 4 import { Json } from "supabase/database.types"; 5 5 import { PubLeafletComment } from "lexicons/api"; 6 - import { AtUri } from "@atproto/syntax"; 7 6 import { getProfiles, type Profile } from "src/identity"; 8 7 9 8 export type Cursor = { 10 9 indexed_at: string; 11 10 uri: string; 12 11 }; 13 - 14 - export type CommentProfile = Pick< 15 - Profile, 16 - "did" | "handle" | "displayName" | "avatar" 17 - >; 18 12 19 13 export type ProfileComment = { 20 14 uri: string; 21 15 record: Json; 22 16 indexed_at: string; 23 - profile: CommentProfile | null; 17 + profile: Profile | null; 24 18 document: { 25 19 uri: string; 26 20 data: Json; ··· 33 27 parentComment: { 34 28 uri: string; 35 29 record: Json; 36 - profile: CommentProfile | null; 30 + profile: Profile | null; 37 31 } | null; 38 32 }; 39 33 40 - function toCommentProfile(p: Profile | null | undefined): CommentProfile | null { 41 - if (!p) return null; 42 - return { 43 - did: p.did, 44 - handle: p.handle, 45 - displayName: p.displayName, 46 - avatar: p.avatar, 47 - }; 48 - } 49 - 50 34 export async function getProfileComments( 51 35 did: string, 52 36 cursor?: Cursor | null, ··· 81 65 .map((c) => (c.record as PubLeafletComment.Record).reply?.parent) 82 66 .filter((uri): uri is string => !!uri); 83 67 84 - // Fetch parent comments if there are any replies 85 - let parentCommentsMap = new Map< 86 - string, 87 - { uri: string; record: Json } 88 - >(); 89 - 90 - if (parentUris.length > 0) { 91 - const { data: parentComments } = await supabaseServerClient 92 - .from("comments_on_documents") 93 - .select("uri, record") 94 - .in("uri", parentUris); 68 + const { data: parentComments } = parentUris.length 69 + ? await supabaseServerClient 70 + .from("comments_on_documents") 71 + .select("uri, record, profile") 72 + .in("uri", parentUris) 73 + : { data: null }; 95 74 96 - if (parentComments) { 97 - for (const pc of parentComments) { 98 - parentCommentsMap.set(pc.uri, { uri: pc.uri, record: pc.record }); 99 - } 100 - } 75 + const allDids = new Set<string>([did]); 76 + for (const pc of parentComments ?? []) { 77 + if (pc.profile) allDids.add(pc.profile); 101 78 } 102 - 103 - // Gather all author DIDs from both main and parent comments 104 - const allDids = new Set<string>(); 105 - for (const c of rawComments) allDids.add(new AtUri(c.uri).host); 106 - for (const pc of parentCommentsMap.values()) 107 - allDids.add(new AtUri(pc.uri).host); 79 + const profiles = await getProfiles([...allDids]); 108 80 109 - const profiles = await getProfiles(Array.from(allDids)); 81 + const parentMap = new Map( 82 + (parentComments ?? []).map((pc) => [ 83 + pc.uri, 84 + { 85 + uri: pc.uri, 86 + record: pc.record, 87 + profile: pc.profile ? profiles.get(pc.profile) ?? null : null, 88 + }, 89 + ]), 90 + ); 110 91 111 - // Transform to ProfileComment format 112 92 const comments: ProfileComment[] = rawComments.map((comment) => { 113 93 const record = comment.record as PubLeafletComment.Record; 114 94 const doc = comment.documents; 115 95 const pub = doc?.documents_in_publications?.[0]?.publications; 116 - const commenterDid = new AtUri(comment.uri).host; 117 - 118 - const parentRaw = record.reply?.parent 119 - ? parentCommentsMap.get(record.reply.parent) 120 - : undefined; 121 - const parentDid = parentRaw ? new AtUri(parentRaw.uri).host : null; 122 96 123 97 return { 124 98 uri: comment.uri, 125 99 record: comment.record, 126 100 indexed_at: comment.indexed_at, 127 - profile: toCommentProfile(profiles.get(commenterDid)), 128 - document: doc 129 - ? { 130 - uri: doc.uri, 131 - data: doc.data, 132 - } 133 - : null, 134 - publication: pub 135 - ? { 136 - uri: pub.uri, 137 - record: pub.record, 138 - } 139 - : null, 140 - parentComment: parentRaw 141 - ? { 142 - uri: parentRaw.uri, 143 - record: parentRaw.record, 144 - profile: parentDid 145 - ? toCommentProfile(profiles.get(parentDid)) 146 - : null, 147 - } 101 + profile: profiles.get(did) ?? null, 102 + document: doc ? { uri: doc.uri, data: doc.data } : null, 103 + publication: pub ? { uri: pub.uri, record: pub.record } : null, 104 + parentComment: record.reply?.parent 105 + ? parentMap.get(record.reply.parent) ?? null 148 106 : null, 149 107 }; 150 108 });