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 23 | 1x 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;
};
|