Connect, collaborate, and grow with a community of TypeScript developers
[Fix] comment sharing (#1114)
__aadamw9fdd41da7f72
diff --git a/apps/web/src/app/[locale]/challenge/_components/comments/comment.tsx b/apps/web/src/app/[locale]/challenge/_components/comments/comment.tsxindex 199fe64f..153d69f0 100644--- a/apps/web/src/app/[locale]/challenge/_components/comments/comment.tsx+++ b/apps/web/src/app/[locale]/challenge/_components/comments/comment.tsx@@ -107,6 +107,7 @@ export function Comment({rootId,type,parentCommentId: comment.id,+ preselectedReplyId: hasPreselectedReply ? Number(replyId) : undefined,});const toggleReplies = () => setShowReplies(!showReplies);diff --git a/apps/web/src/app/[locale]/challenge/_components/comments/comments.hooks.tsx b/apps/web/src/app/[locale]/challenge/_components/comments/comments.hooks.tsxindex 97dbf302..04194767 100644--- a/apps/web/src/app/[locale]/challenge/_components/comments/comments.hooks.tsx+++ b/apps/web/src/app/[locale]/challenge/_components/comments/comments.hooks.tsx@@ -4,10 +4,10 @@ import {useQuery,useQueryClient,} from '@tanstack/react-query';-import { useReducer } from 'react';+import { useEffect, useReducer } from 'react';import { commentErrors, sortKeys } from './comments.constants';-import { getPaginatedComments } from './getCommentRouteData';+import { getAllComments, getPaginatedComments } from './getCommentRouteData';import type { CommentRoot } from '@repo/db/types';import {addComment as addCommentAction,@@ -168,6 +168,7 @@ export function useComments({ type, rootId, initialPage }: UseCommentsProps) {interface UseCommentRepliesProps extends DefaultCommentsProps {parentCommentId: number;enabled: boolean;+ preselectedReplyId?: number;}const REPLIES_PAGESIZE = 5;@@ -177,43 +178,59 @@ export function useCommentsReplies({type,parentCommentId,enabled,+ preselectedReplyId,}: UseCommentRepliesProps) {const queryClient = useQueryClient();const rootQueryKey = [getRootQueryKey(rootId, type)];const queryKey = [...rootQueryKey, `comment-${parentCommentId}-replies`];+ const { data: replies } = useQuery({+ queryKey,+ queryFn: () => getAllComments({ rootId, rootType: type, parentId: parentCommentId }),+ staleTime: 5000,+ enabled,+ });+const {data,fetchNextPage,isFetching: isFetchingMoreReplies,hasNextPage: hasMoreReplies,+ refetch,status,} = useInfiniteQuery({- enabled,- placeholderData: keepPreviousData,- initialPageParam: 1,- queryKey,- queryFn: async ({ pageParam }) => {- // `page` is the start index of the current page- const page = Number(pageParam);+ initialPageParam: 0,+ queryKey: [...queryKey, 'paginated'],+ queryFn: ({ pageParam }) => {+ // `cursor` is the start index of the current page+ const cursor = Number(pageParam);- const comments = await getPaginatedComments({- rootId,- page,- take: REPLIES_PAGESIZE,- rootType: type,- parentId: parentCommentId,- });+ let take = REPLIES_PAGESIZE;+ if (preselectedReplyId && cursor === 0) {+ const preselectedReplyIndex = replies!.findIndex(+ (reply) => preselectedReplyId === reply.id,+ );+ take = Math.ceil((preselectedReplyIndex + 1) / REPLIES_PAGESIZE) * REPLIES_PAGESIZE;+ }+ // `end` is exclusive, and therefore also the next cursor+ const end = cursor + take;return {// if the current page is the last, don't return the next cursor- page: comments.hasMore ? page + 1 : undefined,- replies: comments.comments,+ cursor: end < replies!.length ? end : undefined,+ replies: replies!.slice(cursor, end),};},- getNextPageParam: (_, pages) => pages.at(-1)?.page,+ enabled: Boolean(replies?.length),+ getNextPageParam: (_, pages) => pages.at(-1)?.cursor,});+ useEffect(() => {+ if (replies) {+ refetch();+ }+ }, [replies, refetch]);+const addReplyComment = async (text: string) => {try {const res = await replyComment(diff --git a/apps/web/src/app/[locale]/challenge/_components/comments/getCommentRouteData.ts b/apps/web/src/app/[locale]/challenge/_components/comments/getCommentRouteData.tsindex 1ea327cf..c718ef03 100644--- a/apps/web/src/app/[locale]/challenge/_components/comments/getCommentRouteData.ts+++ b/apps/web/src/app/[locale]/challenge/_components/comments/getCommentRouteData.ts@@ -187,3 +187,64 @@ export async function getPaginatedComments({comments,};}++export async function getAllComments({+ rootId,+ rootType,+ parentId = null,+ sortKey = 'createdAt',+ sortOrder = 'desc',+}: {+ rootId: number;+ rootType: CommentRoot;+ parentId?: number | null;+ sortKey?: SortKey;+ sortOrder?: SortOrder;+}) {+ const session = await auth();++ const comments = await prisma.comment.findMany({+ where: {+ rootType,+ parentId,+ ...(rootType === 'CHALLENGE' ? { rootChallengeId: rootId } : { rootSolutionId: rootId }),+ visible: true,+ },+ orderBy: orderBy(sortKey, sortOrder),+ include: {+ user: {+ select: {+ id: true,+ name: true,+ image: true,+ },+ },+ _count: {+ select: {+ replies: true,+ vote: true,+ },+ },+ vote: {+ select: {+ userId: true,+ },+ where: {+ userId: session?.user.id || '',+ },+ },+ rootChallenge: {+ select: {+ name: true,+ },+ },+ rootSolution: {+ select: {+ title: true,+ },+ },+ },+ });++ return comments;+}diff --git a/apps/web/src/app/[locale]/challenge/_components/comments/index.tsx b/apps/web/src/app/[locale]/challenge/_components/comments/index.tsxindex 4b8a8f19..7dffe09e 100644--- a/apps/web/src/app/[locale]/challenge/_components/comments/index.tsx+++ b/apps/web/src/app/[locale]/challenge/_components/comments/index.tsx@@ -37,6 +37,7 @@ export function Comments({ preselectedCommentMetadata, rootId, type, expanded =} = useComments({type,rootId,+ initialPage: preselectedCommentMetadata?.page,});const handleChangePage = (page: number) => {