:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { useActions, useValues } from 'kea'
import { useEffect, useMemo, useRef } from 'react'
import { feedbackPromptLogic } from './feedbackPromptLogic'
import { maxThreadLogic } from './maxThreadLogic'
export interface UseFeedbackResult {
isPromptVisible: boolean
isDetailedFeedbackVisible: boolean
isThankYouVisible: boolean
traceId: string | null
}
// Duplicated for the sandbox runtime in products/posthog_ai/frontend/hooks/useFeedbackPrompt.ts; this copy is deleted with the LangGraph runtime.
export function useFeedback(conversationId: string | null): UseFeedbackResult {
const { threadGrouped, streamingActive, traceId, retryCount, cancelCount } = useValues(maxThreadLogic)
// Only use feedback logic when we have a valid conversationId
const feedbackLogicProps = useMemo(() => (conversationId ? { conversationId } : null), [conversationId])
const feedbackLogic = feedbackLogicProps
? feedbackPromptLogic(feedbackLogicProps)
: feedbackPromptLogic.build({ conversationId: '' })
const { isPromptVisible, isDetailedFeedbackVisible, isThankYouVisible } = useValues(feedbackLogic)
const { checkShouldShowPrompt, implicitDismissPrompt, implicitDismissDetailedFeedback } = useActions(feedbackLogic)
const prevMessageCountRef = useRef(threadGrouped.length)
const prevStreamingActiveRef = useRef(streamingActive)
useEffect(() => {
if (!conversationId) {
return
}
const humanMessageCount = threadGrouped.filter((m) => m.type === 'human').length
const wasStreaming = prevStreamingActiveRef.current
const prevCount = prevMessageCountRef.current
// Trigger feedback check when streaming completes
if (wasStreaming && !streamingActive && humanMessageCount > 0) {
checkShouldShowPrompt(humanMessageCount, retryCount, cancelCount)
}
// If prompt is visible and user sends a new message (count increased), trigger implicit dismiss
if (isPromptVisible && humanMessageCount > prevCount && streamingActive) {
implicitDismissPrompt()
}
// If detailed feedback form is visible and user sends a new message, submit "bad" rating and dismiss
if (isDetailedFeedbackVisible && humanMessageCount > prevCount && streamingActive) {
implicitDismissDetailedFeedback()
}
prevMessageCountRef.current = humanMessageCount
prevStreamingActiveRef.current = streamingActive
}, [
threadGrouped,
streamingActive,
checkShouldShowPrompt,
isPromptVisible,
isDetailedFeedbackVisible,
implicitDismissPrompt,
implicitDismissDetailedFeedback,
conversationId,
retryCount,
cancelCount,
])
return {
isPromptVisible,
isDetailedFeedbackVisible,
isThankYouVisible,
traceId,
}
}