All files / src/hooks useSentiment.ts

93.75% Statements 15/16
100% Branches 4/4
100% Functions 1/1
93.75% Lines 15/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 231x     1x   1x 148x   148x 40x 18x 18x 18x   22x 22x   22x 148x   148x 148x  
import { useState, useEffect } from 'react';
 
// Stores
import { sentimentStore } from '@/stores/sentiment';
 
export const useSentiment = (commentId: string | undefined) => {
  const [sentiment, setSentiment] = useState<string | null>(null);
 
  useEffect(() => {
    if (!commentId) {
      setSentiment(null);
      return;
    }
    // Sync immediately in case the WS fired before this subscription was set up
    setSentiment(sentimentStore.getData(commentId));
    return sentimentStore.subscribe((updatedId, value) => {
      if (updatedId === commentId) setSentiment(value);
    });
  }, [commentId]);
 
  return sentiment;
};