: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 { useState } from 'react'
import { IconThumbsDown, IconThumbsDownFilled, IconThumbsUp, IconThumbsUpFilled } from '@posthog/icons'
import { LemonButton, LemonTextArea, Popover } from '@posthog/lemon-ui'
import { useKeyboardHotkeys } from 'lib/hooks/useKeyboardHotkeys'
import { KeyboardShortcut } from 'lib/lemon-ui/KeyboardShortcut/KeyboardShortcut'
import { AccessControlLevel } from '~/types'
import type { ReplayObservationLabelApi } from '../generated/api.schemas'
import { getReplayVisionEditDisabledReason } from '../utils/accessControl'
import { observationLabelLogic } from './observationLabelLogic'
export interface ObservationLabelProps {
observationId: string
initialLabel?: ReplayObservationLabelApi | null
onChange?: (label: ReplayObservationLabelApi | null) => void
/** The observation's scanner's effective access level, for object-level overrides. Falls back to the
* resource default when the caller doesn't have the scanner loaded. */
scannerUserAccessLevel?: AccessControlLevel | null
}
const FEEDBACK_PLACEHOLDER = 'Optional: what did it get right or wrong, and why? The scanner learns from this.'
function useEditAccess(scannerUserAccessLevel?: AccessControlLevel | null): string | null {
// Editing the shared rating mutates team-wide data derived from a recording, so it needs the same
// bar as the backend label write: replay_scanner editor AND session_recording viewer.
return getReplayVisionEditDisabledReason(scannerUserAccessLevel)
}
function FeedbackEditor({
observationId,
initialLabel,
onChange,
scannerUserAccessLevel,
compact,
onBlur,
}: ObservationLabelProps & { compact: boolean; onBlur?: () => void }): JSX.Element {
const logic = observationLabelLogic({ observationId, initialLabel, onChange })
const { saving, saveFailed, feedbackDraft, feedbackSynced } = useValues(logic)
const { setFeedbackDraft } = useActions(logic)
const canEdit = !useEditAccess(scannerUserAccessLevel)
return (
<div className="space-y-1">
<LemonTextArea
placeholder={FEEDBACK_PLACEHOLDER}
value={feedbackDraft}
onChange={setFeedbackDraft}
disabled={!canEdit}
minRows={compact ? 1 : 2}
autoFocus={compact}
onBlur={onBlur}
data-attr="replay-vision-label-feedback"
/>
{canEdit && (
<div className="flex justify-end">
<span
className={`text-xs ${saveFailed ? 'text-danger' : 'text-muted'}`}
data-attr="replay-vision-label-feedback-status"
>
{saveFailed ? 'Not saved' : saving || !feedbackSynced ? 'Saving…' : 'Saved'}
</span>
</div>
)}
</div>
)
}
// Rendered only beside the page's rating question, so table rows add no key listeners.
function RatingHotkeys({ onThumb, disabled }: { onThumb: (isCorrect: boolean) => void; disabled: boolean }): null {
useKeyboardHotkeys({
y: { action: () => onThumb(true), disabled },
n: { action: () => onThumb(false), disabled },
})
return null
}
/**
* Thumbs up/down rating on whether the scanner got this session right. The rating is shared across the
* team (one per observation) and the scanner learns from it. `compact` renders just the buttons; the default
* adds the question, opens the optional feedback editor in a popover after a rating, and hides once rated.
*/
export function ObservationLabelControl({
observationId,
initialLabel,
onChange,
scannerUserAccessLevel,
compact = false,
}: ObservationLabelProps & { compact?: boolean }): JSX.Element | null {
const logic = observationLabelLogic({ observationId, initialLabel, onChange })
const { label, saving, feedbackDraft } = useValues(logic)
const { rate, clearRating } = useActions(logic)
const [feedbackOpen, setFeedbackOpen] = useState(false)
const thumbsUp = label?.is_correct === true
const thumbsDown = label?.is_correct === false
const editDisabledReason = useEditAccess(scannerUserAccessLevel)
// Clicking the active thumb again removes the rating.
const onThumb = (isCorrect: boolean): void => {
if (label?.is_correct === isCorrect) {
clearRating()
setFeedbackOpen(false)
return
}
rate(isCorrect, feedbackDraft)
setFeedbackOpen(!compact)
}
const closeNote = (): void => setFeedbackOpen(false)
const buttons = (
<div className="flex items-center gap-1">
<LemonButton
size="xsmall"
type={thumbsUp ? 'primary' : 'secondary'}
icon={thumbsUp ? <IconThumbsUpFilled /> : <IconThumbsUp />}
loading={saving}
disabledReason={editDisabledReason ?? undefined}
tooltip={
thumbsUp ? (
'Remove rating'
) : (
<>
Scanner got this right
{!compact && (
<>
{' '}
<KeyboardShortcut y />
</>
)}
</>
)
}
onClick={() => onThumb(true)}
data-attr="replay-vision-label-thumbs-up"
/>
<LemonButton
size="xsmall"
type={thumbsDown ? 'primary' : 'secondary'}
icon={thumbsDown ? <IconThumbsDownFilled /> : <IconThumbsDown />}
loading={saving}
disabledReason={editDisabledReason ?? undefined}
tooltip={
thumbsDown ? (
'Remove rating'
) : (
<>
Scanner got this wrong
{!compact && (
<>
{' '}
<KeyboardShortcut n />
</>
)}
</>
)
}
onClick={() => onThumb(false)}
data-attr="replay-vision-label-thumbs-down"
/>
</div>
)
if (compact) {
return <div className="py-2">{buttons}</div>
}
if (label && !feedbackOpen) {
return null
}
return (
<div className="flex flex-wrap items-center justify-between gap-x-3 gap-y-1 rounded bg-surface-secondary px-3 py-2">
<RatingHotkeys onThumb={onThumb} disabled={saving || !!label || !!editDisabledReason} />
<div className="flex flex-col">
<span className="text-sm">Did the scanner get this right?</span>
<span className="text-xs text-muted">Ratings help this scanner improve.</span>
</div>
<Popover
// Waits for the saved label, since the feedback autosave writes onto it.
visible={feedbackOpen && !!label}
onClickOutside={closeNote}
placement="bottom-end"
overlay={
<div className="w-80 p-1 flex flex-col gap-2">
<span className="text-sm font-medium">Add a note</span>
<FeedbackEditor
observationId={observationId}
initialLabel={initialLabel}
onChange={onChange}
scannerUserAccessLevel={scannerUserAccessLevel}
compact
/>
<LemonButton
size="small"
type="secondary"
className="self-end"
onClick={closeNote}
data-attr="replay-vision-label-feedback-done"
>
Done
</LemonButton>
</div>
}
>
{buttons}
</Popover>
</div>
)
}