11import { useParams } from "react-router" ;
22import CoreBase from "./base" ;
33import {
4- createComment ,
54 createVote ,
65 getConversation ,
76 getNextComment ,
87} from "../../components/api/conversation" ;
9- import { useEffect , useState } from "react" ;
8+ import { useEffect , useMemo , useState } from "react" ;
109import {
1110 CheckIcon ,
1211 ForwardIcon ,
@@ -19,7 +18,7 @@ import { useQuery } from "@tanstack/react-query";
1918import { getApi } from "../../components/api/base" ;
2019import CommentConfig from "../../components/admin/comments/CommentConfig" ;
2120
22- type Comment = { content : string ; id : string } ;
21+ type Comment = { content : string ; id : string ; num_votes : number } ;
2322
2423const VotingSection = ( {
2524 comment,
@@ -68,29 +67,28 @@ const VotingSection = ({
6867
6968const ConversationPage = ( ) => {
7069 const { conversationId } = useParams < { conversationId : string } > ( ) ;
71- const [ conversation , setConversation ] = useState < Conversation | null > ( null ) ;
72- const [ currentComment , setCurrentComment ] = useState < Comment | null > ( null ) ;
73- const [ commentNumber , setCommentNumber ] = useState < number > ( 0 ) ;
7470
71+ const conversation = useQuery < Conversation > ( {
72+ queryKey : [ "current-conversation" , conversationId ] ,
73+ queryFn : ( ) => getConversation ( conversationId ?? "" ) ,
74+ } ) ;
75+
76+ const currentComment = useQuery < { comment : Comment ; num_votes : number } > ( {
77+ queryKey : [ "current-comment" , conversationId ] ,
78+ queryFn : ( ) => getNextComment ( conversationId ?? "" ) ,
79+ } ) ;
80+
81+ const amountOfVotedComments = useMemo ( ( ) => {
82+ return currentComment . data ?. num_votes ?? 0 ;
83+ } , [ currentComment ] ) ;
7584 const nextComment = async ( ) => {
7685 if ( ! conversationId ) return ;
7786
78- try {
79- const { comment, num_votes } = await getNextComment ( conversationId ) ;
80- setCurrentComment ( comment ) ;
81- setCommentNumber ( num_votes + 1 ) ;
82- } catch ( error : any ) {
83- if ( error . status === 404 ) {
84- setCurrentComment ( null ) ;
85- }
86- }
87+ await currentComment . refetch ( ) ;
8788 } ;
88-
8989 const fetchConversation = async ( ) => {
9090 if ( ! conversationId ) return ;
91-
92- const data = await getConversation ( conversationId ) ;
93- setConversation ( data ) ;
91+ await conversation . refetch ( ) ;
9492 } ;
9593
9694 // dialog logic
@@ -139,14 +137,20 @@ const ConversationPage = () => {
139137 } ) ;
140138 return (
141139 < CoreBase >
142- < main >
140+ < main className = "w-[95%] mx-auto" >
143141 < section className = "p-8" >
144- < h1 className = "text-3xl font-bold mb-4" > { conversation ?. name } </ h1 >
145- < p className = "mb-4" > { conversation ?. description } </ p >
142+ < h1 className = "text-3xl font-bold mb-4" > { conversation . data ?. name } </ h1 >
143+ < p className = "mb-4" > { conversation . data ?. description } </ p >
146144 </ section >
147- < section className = "p-8 bg-background md:w-1/2" >
145+ < section
146+ className = "p-8 bg-background w-full flex flex-col"
147+ aria-labelledby = "active-comment-header"
148+ >
148149 < div className = "flex justify-between items-center" >
149- < h2 className = "font-semibold text-secondary mb-4" >
150+ < h2
151+ id = "active-comment-header"
152+ className = "font-semibold text-secondary mb-4"
153+ >
150154 Active Comments
151155 </ h2 >
152156 < Button
@@ -157,17 +161,20 @@ const ConversationPage = () => {
157161 </ Button >
158162 </ div >
159163
160- { currentComment ? (
164+ { currentComment . isSuccess ? (
161165 < VotingSection
162- comment = { currentComment }
163- commentNumber = { commentNumber }
166+ comment = { currentComment . data ?. comment }
167+ commentNumber = { amountOfVotedComments + 1 }
164168 onVote = { onVote }
165169 />
166170 ) : (
167171 < div className = "flex flex-col items-center justify-center p-4 bg-white rounded-xl" >
168172 < p className = "text-gray-500" > No more comments to review.</ p >
169173 </ div >
170174 ) }
175+ < p className = "text-center pt-6" >
176+ { amountOfVotedComments + 1 } of { comments . data ?. length } comments
177+ </ p >
171178 </ section >
172179 </ main >
173180 { ! ! conversationId && (
0 commit comments