Skip to content

Commit b417176

Browse files
committed
feat(VoteButtons): refactor vote ID handling and improve type safety
1 parent cffe1de commit b417176

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

components/AIReplySection/VoteButtons.tsx

+10-4
Original file line numberDiff line numberDiff line change
@@ -80,28 +80,33 @@ type Props = {
8080
};
8181

8282
// One browser refresh represents one voter
83-
const aiReplyVoterId = Math.random().toString(36).substring(2);
83+
const aiReplyVoterId = Math.random()
84+
.toString(36)
85+
.substring(2);
8486

8587
function VoteButtons({ aiResponseId }: Props) {
8688
const classes = useStyles();
8789
const [
8890
votePopoverAnchorEl,
8991
setVotePopoverAnchorEl,
90-
] = useState<EventTarget | null>(null);
92+
] = useState<HTMLElement | null>(null);
9193
const [currentVote, setCurrentVote] = useState<number>(0);
9294
const [comment, setComment] = useState('');
9395

96+
// Creates and updates score using the same ID
97+
const scoreId = `${aiResponseId}__${aiReplyVoterId}`;
98+
9499
const handleVoteClick = async (
95100
event: React.MouseEvent<HTMLElement>,
96101
vote: number
97102
) => {
98-
const buttonElem = event.target;
103+
const buttonElem = event.target as HTMLElement;
99104
// If clicking same vote again, set to 0 (no vote)
100105
const newVote = vote === currentVote ? 0 : vote;
101106

102107
// Send vote immediately, no ned to wait
103108
langfuseWeb.score({
104-
id: `${aiResponseId}__${aiReplyVoterId}`,
109+
id: scoreId,
105110
traceId: aiResponseId,
106111
name: 'user-feedback',
107112
value: newVote,
@@ -124,6 +129,7 @@ function VoteButtons({ aiResponseId }: Props) {
124129
if (currentVote === 0 || !comment.trim()) return;
125130

126131
await langfuseWeb.score({
132+
id: scoreId,
127133
traceId: aiResponseId,
128134
name: 'user-feedback',
129135
value: currentVote,

0 commit comments

Comments
 (0)