An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
fix(analytics): remove feedback control impression (#985)
Sushant Tiwariccdb89d8d173
diff --git a/components/__tests__/message-actions.test.tsx b/components/__tests__/message-actions.test.tsxindex feba43f5..b0fd9264 100644--- a/components/__tests__/message-actions.test.tsx+++ b/components/__tests__/message-actions.test.tsx@@ -1,5 +1,3 @@-import React from 'react'-import { fireEvent, render, screen, waitFor } from '@testing-library/react'import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'@@ -30,122 +28,19 @@ describe('MessageActions', () => {global.fetch = originalFetch})- test('records one feedback control impression when the row becomes visible', () => {- const { rerender } = render(- <React.StrictMode>- <MessageActions- message="Answer"- messageId="visible-1"- traceId="trace-1"- chatId="chat-1"- isGuest- status="streaming"- visible={false}- />- </React.StrictMode>- )-- expect(captureClient).not.toHaveBeenCalled()-- rerender(- <React.StrictMode>- <MessageActions- message="Answer"- messageId="visible-1"- traceId="trace-1"- chatId="chat-1"- isGuest- status="ready"- visible- />- </React.StrictMode>- )-- expect(captureClient).toHaveBeenCalledTimes(1)- expect(captureClient).toHaveBeenCalledWith('feedback_control_shown', {- hasTraceId: true,- hasFeedback: false,- chatId: 'chat-1',- isGuest: true- })-- rerender(- <React.StrictMode>- <MessageActions- className="parent-state-changed"- message="Answer"- messageId="visible-1"- traceId="trace-1"- chatId="chat-1"- isGuest- status="ready"- visible- />- </React.StrictMode>- )-- expect(captureClient).toHaveBeenCalledTimes(1)- })-- test('does not record a second impression when the same message remounts', () => {- const props = {- message: 'Answer',- messageId: 'remount-1',- traceId: 'trace-1',- chatId: 'chat-1',- status: 'ready' as const,- visible: true- }-- const first = render(<MessageActions {...props} />)- expect(captureClient).toHaveBeenCalledTimes(1)-- // Reopening a conversation remounts every past answer.- first.unmount()- render(<MessageActions {...props} />)-- expect(captureClient).toHaveBeenCalledTimes(1)- })-- test('reports a message that already carries a score as such', () => {+ test('does not record an event when the feedback control is shown', () => {render(<MessageActionsmessage="Answer"- messageId="scored-1"+ messageId="feedback-shown-1"traceId="trace-1"- feedbackScore={-1}- chatId="chat-1"- status="ready"- visible- />- )-- expect(captureClient).toHaveBeenCalledWith('feedback_control_shown', {- hasTraceId: true,- hasFeedback: true,- chatId: 'chat-1',- isGuest: false- })- })-- test('records a missing trace id for a newly presented message', () => {- render(- <MessageActions- message="First answer"- messageId="no-trace-1"chatId="chat-1"status="ready"visible/>)- expect(captureClient).toHaveBeenCalledTimes(1)- expect(captureClient).toHaveBeenLastCalledWith('feedback_control_shown', {- hasTraceId: false,- hasFeedback: false,- chatId: 'chat-1',- isGuest: false- })+ expect(captureClient).not.toHaveBeenCalled()})test('records a thumbs up click and successful feedback response', async () => {diff --git a/components/message-actions.tsx b/components/message-actions.tsxindex 431d40e7..29e242e6 100644--- a/components/message-actions.tsx+++ b/components/message-actions.tsx@@ -1,6 +1,6 @@'use client'-import { useEffect, useMemo, useState } from 'react'+import { useMemo, useState } from 'react'import Link from 'next/link'import { UseChatHelpers } from '@ai-sdk/react'@@ -33,9 +33,6 @@ import {import { ChatShare } from './chat-share'import { RetryButton } from './retry-button'-// Module scope so the impression survives remounts of the same message.-const reportedFeedbackControls = new Set<string>()-interface MessageActionsProps {message: stringmessageId: string@@ -84,25 +81,6 @@ export function MessageActions({const isLoading = status === 'submitted' || status === 'streaming'const showSaveButton = libraryAvailable && (!isGuest || isCloudDeployment)- useEffect(() => {- if (!visible || isLoading || reportedFeedbackControls.has(messageId)) {- return- }-- // A control that was never offered is otherwise indistinguishable from one- // that was offered and ignored, so the impression is what makes the click- // counts readable. It has to be one per message rather than one per mount:- // reopening a conversation remounts every past answer, and an answer that- // already carries a score cannot produce another one.- reportedFeedbackControls.add(messageId)- captureClient('feedback_control_shown', {- hasTraceId: Boolean(traceId),- hasFeedback: feedbackScore !== null,- chatId,- isGuest- })- }, [chatId, feedbackScore, isGuest, isLoading, messageId, traceId, visible])-// Keep the element mounted during loading to preserve layout; otherwise skip rendering.if (!visible && !isLoading) {return null