Skip to content

: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.

feat(surveys): tabbed expanded row on survey responses (#59926)

Lucas Faria34d23aba22a8
diff --git a/frontend/src/lib/components/SurveyResponseDisplay/SurveyResponseDisplay.tsx b/frontend/src/lib/components/SurveyResponseDisplay/SurveyResponseDisplay.tsx
index eb53ba2044d0..4e21f9495d22 100644
--- a/frontend/src/lib/components/SurveyResponseDisplay/SurveyResponseDisplay.tsx
+++ b/frontend/src/lib/components/SurveyResponseDisplay/SurveyResponseDisplay.tsx
@@ -1,12 +1,17 @@
import { useValues } from 'kea'
import { router } from 'kea-router'
-import { IconArchive } from '@posthog/icons'
-import { LemonTag, Link } from '@posthog/lemon-ui'
+import { IconArchive, IconCode, IconGlobe, IconLaptop, IconPin } from '@posthog/icons'
+import { LemonDivider, LemonTag, Link } from '@posthog/lemon-ui'
+import { TZLabel } from 'lib/components/TZLabel'
+import ViewRecordingButton, { ViewRecordingButtonVariant } from 'lib/components/ViewRecordingButton/ViewRecordingButton'
import { IconLink } from 'lib/lemon-ui/icons'
+import { countryCodeToFlag } from 'lib/utils/geography/country'
+import { PersonDisplay } from 'scenes/persons/PersonDisplay'
+import { getThumbIcon } from 'scenes/surveys/hooks/useSurveyResponseColumns'
import { surveyLogic } from 'scenes/surveys/surveyLogic'
-import { getSurveyResponseValue } from 'scenes/surveys/utils'
+import { getSurveyResponseValue, isScaleTwoRating } from 'scenes/surveys/utils'
import { urls } from 'scenes/urls'
import { SurveyEventProperties as SurveyEventPropertyNames, SurveyQuestion } from '~/types'
@@ -14,9 +19,62 @@ import { SurveyEventProperties as SurveyEventPropertyNames, SurveyQuestion } fro
interface SurveyResponseDisplayProps {
eventProperties: Record<string, any>
eventUuid?: string
+ distinctId?: string
+ timestamp?: string | null
+ personProperties?: Record<string, any>
}
-export function SurveyResponseDisplay({ eventProperties, eventUuid }: SurveyResponseDisplayProps): JSX.Element {
+function prettyUrl(url: string): string {
+ try {
+ const parsed = new URL(url)
+ return `${parsed.host}${parsed.pathname}`.replace(/\/$/, '') || parsed.host
+ } catch {
+ return url.replace(/^https?:\/\//, '')
+ }
+}
+
+function formatAnswer(value: unknown): string {
+ if (Array.isArray(value)) {
+ return value.join(', ')
+ }
+ if (value === null || value === undefined) {
+ return ''
+ }
+ return String(value)
+}
+
+function AnswerDisplay({ question, value }: { question: SurveyQuestion; value: unknown }): JSX.Element {
+ if (isScaleTwoRating(question) && (value == '1' || value == '2')) {
+ return (
+ <span className="text-sm font-medium flex items-center gap-1.5">
+ {getThumbIcon(value)}
+ Thumbs {value == '1' ? 'up' : 'down'}
+ </span>
+ )
+ }
+ return (
+ <p className="text-sm font-medium text-default whitespace-pre-wrap leading-relaxed m-0 break-words">
+ {formatAnswer(value)}
+ </p>
+ )
+}
+
+function MetaItem({ icon, children }: { icon?: JSX.Element; children: React.ReactNode }): JSX.Element {
+ return (
+ <span className="flex items-center gap-1 min-w-0">
+ {icon && <span className="shrink-0 flex items-center text-muted">{icon}</span>}
+ <span className="truncate">{children}</span>
+ </span>
+ )
+}
+
+export function SurveyResponseDisplay({
+ eventProperties,
+ eventUuid,
+ distinctId,
+ timestamp,
+ personProperties,
+}: SurveyResponseDisplayProps): JSX.Element {
const surveyId = eventProperties[SurveyEventPropertyNames.SURVEY_ID]
const { location } = useValues(router)
@@ -26,6 +84,28 @@ export function SurveyResponseDisplay({ eventProperties, eventUuid }: SurveyResp
const isArchived = eventUuid ? (archivedResponseUuids?.has(eventUuid) ?? false) : false
const surveyName = survey?.name || eventProperties['$survey_name']
+ const iteration = eventProperties[SurveyEventPropertyNames.SURVEY_ITERATION]
+ const isPartial =
+ eventProperties[SurveyEventPropertyNames.SURVEY_COMPLETED] === false ||
+ eventProperties[SurveyEventPropertyNames.SURVEY_PARTIALLY_COMPLETED] === true
+
+ const sessionId = typeof eventProperties.$session_id === 'string' ? eventProperties.$session_id : undefined
+ const currentUrl = typeof eventProperties.$current_url === 'string' ? eventProperties.$current_url : null
+ const browser = typeof eventProperties.$browser === 'string' ? eventProperties.$browser : null
+ const os = typeof eventProperties.$os === 'string' ? eventProperties.$os : null
+ const deviceType = typeof eventProperties.$device_type === 'string' ? eventProperties.$device_type : null
+ const deviceLine = [browser, os, deviceType].filter(Boolean).join(' · ')
+ const city = typeof eventProperties.$geoip_city_name === 'string' ? eventProperties.$geoip_city_name : null
+ const country = typeof eventProperties.$geoip_country_name === 'string' ? eventProperties.$geoip_country_name : null
+ const countryCode =
+ typeof eventProperties.$geoip_country_code === 'string' ? eventProperties.$geoip_country_code : null
+ const locationLine = [city, country].filter(Boolean).join(', ')
+ const lib = typeof eventProperties.$lib === 'string' ? eventProperties.$lib : null
+ const libVersion = typeof eventProperties.$lib_version === 'string' ? eventProperties.$lib_version : null
+ const libraryLine = [lib, libVersion].filter(Boolean).join(' ')
+
+ const hasFooter = currentUrl || deviceLine || locationLine || libraryLine
+ const hasHeaderMeta = distinctId || timestamp || sessionId || isPartial || isArchived || iteration != null
const responses: { questionIndex: number; question: SurveyQuestion; value: any }[] = []
@@ -40,7 +120,7 @@ export function SurveyResponseDisplay({ eventProperties, eventUuid }: SurveyResp
}
return (
- <div className="flex flex-col gap-2 pb-2">
+ <div className="flex flex-col gap-4 pb-2">
<div className="flex flex-row gap-2 flex-wrap items-center">
{surveyName && <h3 className="mb-0 mr-2">{surveyName}</h3>}
{surveyId && !isOnSurveyPage && (
@@ -51,26 +131,79 @@ export function SurveyResponseDisplay({ eventProperties, eventUuid }: SurveyResp
)}
</div>
- {isArchived && (
- <div>
- <LemonTag type="muted" icon={<IconArchive />}>
- Response archived
- </LemonTag>
+ {hasHeaderMeta && (
+ <div className="flex flex-wrap items-center gap-x-3 gap-y-1.5 text-xs text-secondary min-w-0">
+ {distinctId && (
+ <PersonDisplay
+ person={{ distinct_id: distinctId, properties: personProperties }}
+ withIcon="xs"
+ noEllipsis={false}
+ />
+ )}
+ {timestamp && <TZLabel time={timestamp} />}
+ {sessionId && (
+ <ViewRecordingButton
+ sessionId={sessionId}
+ timestamp={timestamp ?? undefined}
+ variant={ViewRecordingButtonVariant.Link}
+ checkRecordingExists
+ />
+ )}
+ {isPartial && (
+ <LemonTag type="warning" size="small">
+ Partial
+ </LemonTag>
+ )}
+ {isArchived && (
+ <LemonTag type="muted" size="small" icon={<IconArchive />}>
+ Response archived
+ </LemonTag>
+ )}
+ {iteration !== undefined && iteration !== null && (
+ <MetaItem>
+ <span className="text-muted">Iteration</span> {String(iteration)}
+ </MetaItem>
+ )}
</div>
)}
{responses.length > 0 && (
- <div className="flex flex-col gap-3">
+ <div className="flex flex-col gap-4">
{responses.map(({ questionIndex, question, value }) => (
- <div key={questionIndex} className="flex flex-col gap-1">
- <span className="text-xs text-secondary font-semibold">{question.question}</span>
- <span className="text-sm whitespace-pre-wrap">
- {Array.isArray(value) ? value.join(', ') : value}
- </span>
+ <div key={questionIndex} className="flex flex-col gap-1 min-w-0">
+ <span className="text-xs text-secondary">{question.question}</span>
+ <AnswerDisplay question={question} value={value} />
</div>
))}
</div>
)}
+
+ {hasFooter && (
+ <>
+ <LemonDivider className="my-0" />
+ <div className="flex flex-wrap items-center gap-x-4 gap-y-1.5 text-xs text-secondary min-w-0">
+ {currentUrl && (
+ <Link
+ to={currentUrl}
+ target="_blank"
+ title={currentUrl}
+ className="flex items-center gap-1 max-w-xs min-w-0"
+ >
+ <IconGlobe className="shrink-0 text-muted" />
+ <span className="truncate">{prettyUrl(currentUrl)}</span>
+ </Link>
+ )}
+ {deviceLine && <MetaItem icon={<IconLaptop />}>{deviceLine}</MetaItem>}
+ {locationLine && (
+ <MetaItem icon={<IconPin />}>
+ {countryCode && <span className="mr-1">{countryCodeToFlag(countryCode)}</span>}
+ {locationLine}
+ </MetaItem>
+ )}
+ {libraryLine && <MetaItem icon={<IconCode />}>{libraryLine}</MetaItem>}
+ </div>
+ </>
+ )}
</div>
)
}
diff --git a/frontend/src/scenes/activity/explore/EventDetails.tsx b/frontend/src/scenes/activity/explore/EventDetails.tsx
index 9718cc3158a0..e7b8776612b0 100644
--- a/frontend/src/scenes/activity/explore/EventDetails.tsx
+++ b/frontend/src/scenes/activity/explore/EventDetails.tsx
@@ -103,6 +103,9 @@ export function EventDetails({ event, tableProps }: EventDetailsProps): JSX.Elem
<SurveyResponseDisplay
eventProperties={properties}
eventUuid={'uuid' in event && event.uuid ? event.uuid : undefined}
+ distinctId={'distinct_id' in event ? event.distinct_id : undefined}
+ timestamp={'timestamp' in event ? event.timestamp : undefined}
+ personProperties={'person' in event ? event.person?.properties : undefined}
/>
</div>
)
diff --git a/frontend/src/scenes/surveys/SurveyViewRedesign/SurveyResponseExpandedRow.tsx b/frontend/src/scenes/surveys/SurveyViewRedesign/SurveyResponseExpandedRow.tsx
index fb2d2479454b..a73b96af83dd 100644
--- a/frontend/src/scenes/surveys/SurveyViewRedesign/SurveyResponseExpandedRow.tsx
+++ b/frontend/src/scenes/surveys/SurveyViewRedesign/SurveyResponseExpandedRow.tsx
@@ -1,26 +1,6 @@
-import { useValues } from 'kea'
+import { EventDetails } from 'scenes/activity/explore/EventDetails'
-import { IconArchive, IconCode, IconGlobe, IconLaptop, IconPin } from '@posthog/icons'
-import { LemonDivider, LemonTag, Link } from '@posthog/lemon-ui'
-
-import { TZLabel } from 'lib/components/TZLabel'
-import ViewRecordingButton, { ViewRecordingButtonVariant } from 'lib/components/ViewRecordingButton/ViewRecordingButton'
-import { countryCodeToFlag } from 'lib/utils/geography/country'
-import { PersonDisplay } from 'scenes/persons/PersonDisplay'
-import { getThumbIcon } from 'scenes/surveys/hooks/useSurveyResponseColumns'
-import { surveyLogic } from 'scenes/surveys/surveyLogic'
-import { getSurveyResponseValue, isScaleTwoRating } from 'scenes/surveys/utils'
-
-import { EventType, SurveyEventProperties, SurveyQuestion } from '~/types'
-
-function prettyUrl(url: string): string {
- try {
- const parsed = new URL(url)
- return `${parsed.host}${parsed.pathname}`.replace(/\/$/, '') || parsed.host
- } catch {
- return url.replace(/^https?:\/\//, '')
- }
-}
+import { EventType } from '~/types'
function findEventInRow(result: unknown): EventType | null {
if (!Array.isArray(result)) {
@@ -40,147 +20,10 @@ function findEventInRow(result: unknown): EventType | null {
return null
}
-function formatAnswer(value: unknown): string {
- if (Array.isArray(value)) {
- return value.join(', ')
- }
- if (value === null || value === undefined) {
- return ''
- }
- return String(value)
-}
-
-function AnswerDisplay({ question, value }: { question: SurveyQuestion; value: unknown }): JSX.Element {
- if (isScaleTwoRating(question) && (value == '1' || value == '2')) {
- return (
- <span className="text-sm font-medium flex items-center gap-1.5">
- {getThumbIcon(value)}
- Thumbs {value == '1' ? 'up' : 'down'}
- </span>
- )
- }
- return (
- <p className="text-sm font-medium text-default whitespace-pre-wrap leading-relaxed m-0 break-words">
- {formatAnswer(value)}
- </p>
- )
-}
-
-function MetaItem({ icon, children }: { icon?: JSX.Element; children: React.ReactNode }): JSX.Element {
- return (
- <span className="flex items-center gap-1 min-w-0">
- {icon && <span className="shrink-0 flex items-center text-muted">{icon}</span>}
- <span className="truncate">{children}</span>
- </span>
- )
-}
-
export function SurveyResponseExpandedRow({ result }: { result: unknown }): JSX.Element | null {
const event = findEventInRow(result)
- const { survey, archivedResponseUuids } = useValues(surveyLogic)
-
if (!event) {
return null
}
-
- const properties = event.properties ?? {}
- const currentUrl = typeof properties.$current_url === 'string' ? properties.$current_url : null
- const sessionId = typeof properties.$session_id === 'string' ? properties.$session_id : undefined
- const browser = typeof properties.$browser === 'string' ? properties.$browser : null
- const os = typeof properties.$os === 'string' ? properties.$os : null
- const deviceType = typeof properties.$device_type === 'string' ? properties.$device_type : null
- const deviceLine = [browser, os, deviceType].filter(Boolean).join(' · ')
- const city = typeof properties.$geoip_city_name === 'string' ? properties.$geoip_city_name : null
- const country = typeof properties.$geoip_country_name === 'string' ? properties.$geoip_country_name : null
- const countryCode = typeof properties.$geoip_country_code === 'string' ? properties.$geoip_country_code : null
- const locationLine = [city, country].filter(Boolean).join(', ')
- const lib = typeof properties.$lib === 'string' ? properties.$lib : null
- const libVersion = typeof properties.$lib_version === 'string' ? properties.$lib_version : null
- const libraryLine = [lib, libVersion].filter(Boolean).join(' ')
- const iteration = properties[SurveyEventProperties.SURVEY_ITERATION]
- const isPartial =
- properties[SurveyEventProperties.SURVEY_COMPLETED] === false ||
- properties[SurveyEventProperties.SURVEY_PARTIALLY_COMPLETED] === true
- const isArchived = event.uuid ? archivedResponseUuids?.has(event.uuid) : false
-
- const responses: { questionIndex: number; question: SurveyQuestion; value: unknown }[] = []
- if (survey?.questions) {
- survey.questions.forEach((q, index) => {
- const value = getSurveyResponseValue(properties, index, q.id)
- if (value !== undefined) {
- responses.push({ questionIndex: index, question: q, value })
- }
- })
- }
-
- return (
- <div className="mx-auto w-full max-w-[min(56rem,calc(100vw-2rem))] min-w-0 px-4 py-4 flex flex-col gap-5">
- <div className="flex flex-wrap items-center gap-x-3 gap-y-1.5 text-xs text-secondary min-w-0">
- <PersonDisplay
- person={{ distinct_id: event.distinct_id, properties: event.person?.properties }}
- withIcon="xs"
- noEllipsis={false}
- />
- {event.timestamp && <TZLabel time={event.timestamp} />}
- {sessionId && (
- <ViewRecordingButton
- sessionId={sessionId}
- timestamp={event.timestamp}
- variant={ViewRecordingButtonVariant.Link}
- checkRecordingExists
- />
- )}
- {isPartial && (
- <LemonTag type="warning" size="small">
- Partial
- </LemonTag>
- )}
- {isArchived && (
- <LemonTag type="muted" size="small" icon={<IconArchive />}>
- Archived
- </LemonTag>
- )}
- {iteration !== undefined && iteration !== null && (
- <MetaItem>
- <span className="text-muted">Iteration</span> {String(iteration)}
- </MetaItem>
- )}
- </div>
-
- {responses.length > 0 && (
- <div className="flex flex-col gap-4">
- {responses.map(({ questionIndex, question, value }) => (
- <div key={questionIndex} className="flex flex-col gap-1 min-w-0">
- <span className="text-xs text-secondary">{question.question}</span>
- <AnswerDisplay question={question} value={value} />
- </div>
- ))}
- </div>
- )}
-
- <LemonDivider className="my-0" />
-
- <div className="flex flex-wrap items-center gap-x-4 gap-y-1.5 text-xs text-secondary min-w-0">
- {currentUrl && (
- <Link
- to={currentUrl}
- target="_blank"
- title={currentUrl}
- className="flex items-center gap-1 max-w-xs min-w-0"
- >
- <IconGlobe className="shrink-0 text-muted" />
- <span className="truncate">{prettyUrl(currentUrl)}</span>
- </Link>
- )}
- {deviceLine && <MetaItem icon={<IconLaptop />}>{deviceLine}</MetaItem>}
- {locationLine && (
- <MetaItem icon={<IconPin />}>
- {countryCode && <span className="mr-1">{countryCodeToFlag(countryCode)}</span>}
- {locationLine}
- </MetaItem>
- )}
- {libraryLine && <MetaItem icon={<IconCode />}>{libraryLine}</MetaItem>}
- </div>
- </div>
- )
+ return <EventDetails event={event} />
}