From be08f13f77e4944e7ff33b6b19dd58cb899c184e Mon Sep 17 00:00:00 2001 From: Jared Pereira Date: Fri, 12 Dec 2025 12:51:42 +0000 Subject: [PATCH] fix comment action import --- app/p/[didOrHandle]/getProfileComments.ts | 133 ------------------------------------------------------------------------------------------------------------------------------------- app/p/[didOrHandle]/(profile)/comments/CommentsContent.tsx | 2 +- app/p/[didOrHandle]/(profile)/comments/getProfileComments.ts | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ app/p/[didOrHandle]/(profile)/comments/page.tsx | 8 ++++++-- 4 file(s) changed, 140 insertion(s)(+), 136 deletion(s)(-) diff --git a/app/p/[didOrHandle]/getProfileComments.ts b/app/p/[didOrHandle]/getProfileComments.ts deleted file mode 100644 --- a/app/p/[didOrHandle]/getProfileComments.ts +++ /dev/null @@ -1,133 +0,0 @@ -"use server"; - -import { supabaseServerClient } from "supabase/serverClient"; -import { Json } from "supabase/database.types"; -import { PubLeafletComment } from "lexicons/api"; - -export type Cursor = { - indexed_at: string; - uri: string; -}; - -export type ProfileComment = { - uri: string; - record: Json; - indexed_at: string; - bsky_profiles: { record: Json; handle: string | null } | null; - document: { - uri: string; - data: Json; - } | null; - publication: { - uri: string; - record: Json; - } | null; - // For replies, include the parent comment info - parentComment: { - uri: string; - record: Json; - bsky_profiles: { record: Json; handle: string | null } | null; - } | null; -}; - -export async function getProfileComments( - did: string, - cursor?: Cursor | null, -): Promise<{ comments: ProfileComment[]; nextCursor: Cursor | null }> { - const limit = 20; - - let query = supabaseServerClient - .from("comments_on_documents") - .select( - `*, - bsky_profiles(record, handle), - documents(uri, data, documents_in_publications(publications(*)))`, - ) - .eq("profile", did) - .order("indexed_at", { ascending: false }) - .order("uri", { ascending: false }) - .limit(limit); - - if (cursor) { - query = query.or( - `indexed_at.lt.${cursor.indexed_at},and(indexed_at.eq.${cursor.indexed_at},uri.lt.${cursor.uri})`, - ); - } - - const { data: rawComments } = await query; - - if (!rawComments || rawComments.length === 0) { - return { comments: [], nextCursor: null }; - } - - // Collect parent comment URIs for replies - const parentUris = rawComments - .map((c) => (c.record as PubLeafletComment.Record).reply?.parent) - .filter((uri): uri is string => !!uri); - - // Fetch parent comments if there are any replies - let parentCommentsMap = new Map< - string, - { - uri: string; - record: Json; - bsky_profiles: { record: Json; handle: string | null } | null; - } - >(); - - if (parentUris.length > 0) { - const { data: parentComments } = await supabaseServerClient - .from("comments_on_documents") - .select(`uri, record, bsky_profiles(record, handle)`) - .in("uri", parentUris); - - if (parentComments) { - for (const pc of parentComments) { - parentCommentsMap.set(pc.uri, { - uri: pc.uri, - record: pc.record, - bsky_profiles: pc.bsky_profiles, - }); - } - } - } - - // Transform to ProfileComment format - const comments: ProfileComment[] = rawComments.map((comment) => { - const record = comment.record as PubLeafletComment.Record; - const doc = comment.documents; - const pub = doc?.documents_in_publications?.[0]?.publications; - - return { - uri: comment.uri, - record: comment.record, - indexed_at: comment.indexed_at, - bsky_profiles: comment.bsky_profiles, - document: doc - ? { - uri: doc.uri, - data: doc.data, - } - : null, - publication: pub - ? { - uri: pub.uri, - record: pub.record, - } - : null, - parentComment: record.reply?.parent - ? parentCommentsMap.get(record.reply.parent) || null - : null, - }; - }); - - const nextCursor = - comments.length === limit - ? { - indexed_at: comments[comments.length - 1].indexed_at, - uri: comments[comments.length - 1].uri, - } - : null; - - return { comments, nextCursor }; -} diff --git a/app/p/[didOrHandle]/(profile)/comments/CommentsContent.tsx b/app/p/[didOrHandle]/(profile)/comments/CommentsContent.tsx --- a/app/p/[didOrHandle]/(profile)/comments/CommentsContent.tsx +++ b/app/p/[didOrHandle]/(profile)/comments/CommentsContent.tsx @@ -12,7 +12,7 @@ getProfileComments, type ProfileComment, type Cursor, -} from "../getProfileComments"; +} from "./getProfileComments"; import { timeAgo } from "src/utils/timeAgo"; import { getPublicationURL } from "app/lish/createPub/getPublicationURL"; diff --git a/app/p/[didOrHandle]/(profile)/comments/getProfileComments.ts b/app/p/[didOrHandle]/(profile)/comments/getProfileComments.ts new file mode 100644 --- /dev/null +++ b/app/p/[didOrHandle]/(profile)/comments/getProfileComments.ts @@ -0,0 +1,133 @@ +"use server"; + +import { supabaseServerClient } from "supabase/serverClient"; +import { Json } from "supabase/database.types"; +import { PubLeafletComment } from "lexicons/api"; + +export type Cursor = { + indexed_at: string; + uri: string; +}; + +export type ProfileComment = { + uri: string; + record: Json; + indexed_at: string; + bsky_profiles: { record: Json; handle: string | null } | null; + document: { + uri: string; + data: Json; + } | null; + publication: { + uri: string; + record: Json; + } | null; + // For replies, include the parent comment info + parentComment: { + uri: string; + record: Json; + bsky_profiles: { record: Json; handle: string | null } | null; + } | null; +}; + +export async function getProfileComments( + did: string, + cursor?: Cursor | null, +): Promise<{ comments: ProfileComment[]; nextCursor: Cursor | null }> { + const limit = 20; + + let query = supabaseServerClient + .from("comments_on_documents") + .select( + `*, + bsky_profiles(record, handle), + documents(uri, data, documents_in_publications(publications(*)))`, + ) + .eq("profile", did) + .order("indexed_at", { ascending: false }) + .order("uri", { ascending: false }) + .limit(limit); + + if (cursor) { + query = query.or( + `indexed_at.lt.${cursor.indexed_at},and(indexed_at.eq.${cursor.indexed_at},uri.lt.${cursor.uri})`, + ); + } + + const { data: rawComments } = await query; + + if (!rawComments || rawComments.length === 0) { + return { comments: [], nextCursor: null }; + } + + // Collect parent comment URIs for replies + const parentUris = rawComments + .map((c) => (c.record as PubLeafletComment.Record).reply?.parent) + .filter((uri): uri is string => !!uri); + + // Fetch parent comments if there are any replies + let parentCommentsMap = new Map< + string, + { + uri: string; + record: Json; + bsky_profiles: { record: Json; handle: string | null } | null; + } + >(); + + if (parentUris.length > 0) { + const { data: parentComments } = await supabaseServerClient + .from("comments_on_documents") + .select(`uri, record, bsky_profiles(record, handle)`) + .in("uri", parentUris); + + if (parentComments) { + for (const pc of parentComments) { + parentCommentsMap.set(pc.uri, { + uri: pc.uri, + record: pc.record, + bsky_profiles: pc.bsky_profiles, + }); + } + } + } + + // Transform to ProfileComment format + const comments: ProfileComment[] = rawComments.map((comment) => { + const record = comment.record as PubLeafletComment.Record; + const doc = comment.documents; + const pub = doc?.documents_in_publications?.[0]?.publications; + + return { + uri: comment.uri, + record: comment.record, + indexed_at: comment.indexed_at, + bsky_profiles: comment.bsky_profiles, + document: doc + ? { + uri: doc.uri, + data: doc.data, + } + : null, + publication: pub + ? { + uri: pub.uri, + record: pub.record, + } + : null, + parentComment: record.reply?.parent + ? parentCommentsMap.get(record.reply.parent) || null + : null, + }; + }); + + const nextCursor = + comments.length === limit + ? { + indexed_at: comments[comments.length - 1].indexed_at, + uri: comments[comments.length - 1].uri, + } + : null; + + return { comments, nextCursor }; +} diff --git a/app/p/[didOrHandle]/(profile)/comments/page.tsx b/app/p/[didOrHandle]/(profile)/comments/page.tsx --- a/app/p/[didOrHandle]/(profile)/comments/page.tsx +++ b/app/p/[didOrHandle]/(profile)/comments/page.tsx @@ -1,5 +1,5 @@ import { idResolver } from "app/(home-pages)/reader/idResolver"; -import { getProfileComments } from "../../getProfileComments"; +import { getProfileComments } from "./getProfileComments"; import { ProfileCommentsContent } from "./CommentsContent"; export default async function ProfileCommentsPage(props: { @@ -19,6 +19,10 @@ const { comments, nextCursor } = await getProfileComments(did); return ( - + ); } -- tangled.sh