@@ -256,21 +256,57 @@ export const MarketDetail = ({ market }: MarketDetailProps) => {
256256
257257 useEffect ( ( ) => {
258258 let active = true ;
259+ const supabase = createClient ( ) ;
259260
260261 const loadComments = async ( ) => {
261262 try {
262263 setCommentsMessage ( "" ) ;
263- const backendComments = await darkonnetApi . listComments ( market . id ) ;
264- if ( active ) setComments ( flattenComments ( backendComments , activeCommentWalletAddress ) ) ;
264+ const { data : backendComments , error : fetchError } = await supabase
265+ . from ( "comments" )
266+ . select ( "*" )
267+ . eq ( "market_id" , market . id )
268+ . order ( "created_at" , { ascending : true } ) ;
269+
270+ if ( fetchError ) throw fetchError ;
271+ if ( active && backendComments ) {
272+ const mapped : MarketComment [ ] = backendComments . map ( c => ( {
273+ id : c . id ,
274+ author : c . display_name ,
275+ walletAddress : c . wallet_address ,
276+ createdAt : new Date ( c . created_at ) . getTime ( ) ,
277+ time : relativeTime ( c . created_at ) ,
278+ text : c . body ,
279+ likes : c . liked_by ?. length || 0 ,
280+ liked : Boolean ( activeCommentWalletAddress && c . liked_by ?. includes ( activeCommentWalletAddress ) ) ,
281+ parentId : c . parent_id ,
282+ } ) ) ;
283+ setComments ( mapped ) ;
284+ }
265285 } catch ( err ) {
266286 if ( active ) setCommentsMessage ( err instanceof Error ? err . message : "Unable to load backend comments." ) ;
267287 }
268288 } ;
269289
270290 loadComments ( ) ;
271291
292+ const channel = supabase
293+ . channel ( `market-detail:${ market . id } ` )
294+ . on (
295+ "postgres_changes" ,
296+ { event : "*" , schema : "public" , table : "comments" , filter : `market_id=eq.${ market . id } ` } ,
297+ ( ) => {
298+ loadComments ( ) ;
299+ } ,
300+ )
301+ . on ( "postgres_changes" , { event : "UPDATE" , schema : "public" , table : "markets" , filter : `market_id=eq.${ market . id } ` } , payload => {
302+ // Handle market status/metadata updates if needed
303+ console . log ( "Market updated:" , payload . new ) ;
304+ } )
305+ . subscribe ( ) ;
306+
272307 return ( ) => {
273308 active = false ;
309+ supabase . removeChannel ( channel ) ;
274310 } ;
275311 } , [ activeCommentWalletAddress , market . id ] ) ;
276312
@@ -315,14 +351,17 @@ export const MarketDetail = ({ market }: MarketDetailProps) => {
315351 return ;
316352 }
317353
354+ const supabase = createClient ( ) ;
318355 try {
319- await darkonnetApi . createComment ( {
320- marketId : market . id ,
321- walletAddress : commentWalletAddress ,
322- displayName : currentProfileName ,
356+ const { error : postError } = await supabase . from ( "comments" ) . insert ( {
357+ market_id : market . id ,
358+ wallet_address : commentWalletAddress . toLowerCase ( ) ,
359+ display_name : currentProfileName ,
323360 body : text ,
324361 } ) ;
325- setComments ( flattenComments ( await darkonnetApi . listComments ( market . id ) , activeCommentWalletAddress ) ) ;
362+
363+ if ( postError ) throw postError ;
364+
326365 setCommentDraft ( "" ) ;
327366 setCommentSort ( "new" ) ;
328367 setCommentsMessage ( "" ) ;
@@ -346,15 +385,18 @@ export const MarketDetail = ({ market }: MarketDetailProps) => {
346385 const parentId = parentComment ?. parentId ?? targetId ;
347386 const replyAuthor = currentProfileName ;
348387
388+ const supabase = createClient ( ) ;
349389 try {
350- await darkonnetApi . createComment ( {
351- marketId : market . id ,
352- walletAddress : commentWalletAddress ,
353- displayName : replyAuthor ,
390+ const { error : postError } = await supabase . from ( "comments" ) . insert ( {
391+ market_id : market . id ,
392+ wallet_address : commentWalletAddress . toLowerCase ( ) ,
393+ display_name : replyAuthor ,
354394 body : text ,
355- parentId,
395+ parent_id : parentId ,
356396 } ) ;
357397
398+ if ( postError ) throw postError ;
399+
358400 if ( parentAuthor === currentProfileName ) {
359401 addNotification ( {
360402 title : "Reply On Your Comment" ,
@@ -363,7 +405,6 @@ export const MarketDetail = ({ market }: MarketDetailProps) => {
363405 } ) ;
364406 }
365407
366- setComments ( flattenComments ( await darkonnetApi . listComments ( market . id ) , activeCommentWalletAddress ) ) ;
367408 setReplyDrafts ( prev => ( { ...prev , [ targetId ] : "" } ) ) ;
368409 setReplyingTo ( null ) ;
369410 setExpandedThreads ( prev => ( { ...prev , [ parentId ] : true } ) ) ;
@@ -383,39 +424,25 @@ export const MarketDetail = ({ market }: MarketDetailProps) => {
383424 }
384425
385426 const nextLiked = ! comment . liked ;
386- setComments ( prev =>
387- prev . map ( comment =>
388- comment . id === commentId
389- ? {
390- ...comment ,
391- liked : nextLiked ,
392- likes : nextLiked ? comment . likes + 1 : Math . max ( 0 , comment . likes - 1 ) ,
393- }
394- : comment ,
395- ) ,
396- ) ;
427+ const supabase = createClient ( ) ;
397428
398429 try {
399- await darkonnetApi . setCommentLike ( {
400- marketId : market . id ,
401- commentId,
402- walletAddress : activeCommentWalletAddress ,
403- liked : nextLiked ,
404- } ) ;
405- setComments ( flattenComments ( await darkonnetApi . listComments ( market . id ) , activeCommentWalletAddress ) ) ;
430+ const { data : currentComment } = await supabase . from ( "comments" ) . select ( "liked_by" ) . eq ( "id" , commentId ) . single ( ) ;
431+
432+ let likedBy = currentComment ?. liked_by || [ ] ;
433+ if ( nextLiked ) {
434+ if ( ! likedBy . includes ( activeCommentWalletAddress ) ) {
435+ likedBy . push ( activeCommentWalletAddress ) ;
436+ }
437+ } else {
438+ likedBy = likedBy . filter ( ( w : string ) => w !== activeCommentWalletAddress ) ;
439+ }
440+
441+ const { error : updateError } = await supabase . from ( "comments" ) . update ( { liked_by : likedBy } ) . eq ( "id" , commentId ) ;
442+
443+ if ( updateError ) throw updateError ;
406444 setCommentsMessage ( "" ) ;
407445 } catch ( err ) {
408- setComments ( prev =>
409- prev . map ( comment =>
410- comment . id === commentId
411- ? {
412- ...comment ,
413- liked : ! nextLiked ,
414- likes : nextLiked ? Math . max ( 0 , comment . likes - 1 ) : comment . likes + 1 ,
415- }
416- : comment ,
417- ) ,
418- ) ;
419446 setCommentsMessage ( err instanceof Error ? err . message : "Unable to update comment like." ) ;
420447 }
421448 } ;
0 commit comments