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.

fix(surveys): memoize survey table columns so row memoization works (#68453)

Paul D'Ambra112009396684
diff --git a/frontend/src/scenes/surveys/components/SurveysTable.tsx b/frontend/src/scenes/surveys/components/SurveysTable.tsx
index deb6977abd67..afd8461b4388 100644
--- a/frontend/src/scenes/surveys/components/SurveysTable.tsx
+++ b/frontend/src/scenes/surveys/components/SurveysTable.tsx
@@ -1,5 +1,6 @@
import { useActions, useValues } from 'kea'
import { router } from 'kea-router'
+import { useMemo } from 'react'
import { LemonButton, LemonDialog, LemonDivider, LemonInput, LemonSelect, LemonTable, Spinner } from '@posthog/lemon-ui'
@@ -63,6 +64,288 @@ export function SurveysTable(): JSX.Element {
const hasMultipleProjects = currentOrganization?.teams && currentOrganization.teams.length > 1
+ // A fresh columns array every render defeats TableRow's React.memo, re-rendering all 100
+ // rows of the table whenever anything in surveysLogic changes.
+ const columns = useMemo<LemonTableColumn<Survey, keyof Survey | undefined>[]>(
+ () => [
+ {
+ dataIndex: 'name',
+ title: 'Name',
+ render: function RenderName(_, survey) {
+ return <LemonTableLink to={urls.survey(survey.id)} title={stringWithWBR(survey.name, 17)} />
+ },
+ },
+ {
+ title: 'Responses',
+ dataIndex: 'id',
+ render: function RenderResponses(_, survey) {
+ return (
+ <>
+ {surveysResponsesCountLoading ? (
+ <Spinner />
+ ) : (
+ <div>{surveysResponsesCount[survey.id] ?? 0}</div>
+ )}
+ </>
+ )
+ },
+ sorter: (surveyA, surveyB) => {
+ const countA = surveysResponsesCount[surveyA.id] ?? 0
+ const countB = surveysResponsesCount[surveyB.id] ?? 0
+ return countA - countB
+ },
+ },
+ {
+ dataIndex: 'type',
+ title: 'Mode',
+ render: function RenderType(_, survey) {
+ return SURVEY_TYPE_LABEL_MAP[survey.type]
+ },
+ },
+ {
+ title: 'Question type',
+ render: function RenderResponses(_, survey) {
+ return survey.questions?.length === 1 ? SurveyQuestionLabel[survey.questions[0].type] : 'Multiple'
+ },
+ },
+ ...(tab === SurveysTabs.Active
+ ? [
+ createdAtColumn<Survey>() as LemonTableColumn<Survey, keyof Survey | undefined>,
+ {
+ title: 'Status',
+ width: 100,
+ render: function Render(_: any, survey: Survey) {
+ return <SurveyStatusTag survey={survey} />
+ },
+ },
+ ]
+ : []),
+ {
+ width: 0,
+ render: function Render(_, survey: Survey) {
+ return (
+ <More
+ overlay={
+ <>
+ <LemonButton fullWidth onClick={() => router.actions.push(urls.survey(survey.id))}>
+ View
+ </LemonButton>
+ <AccessControlAction
+ resourceType={AccessControlResourceType.Survey}
+ minAccessLevel={AccessControlLevel.Editor}
+ userAccessLevel={survey.user_access_level}
+ >
+ <LemonButton
+ fullWidth
+ onClick={() => {
+ if (hasMultipleProjects) {
+ setSurveyToDuplicate(survey)
+ } else {
+ duplicateSurvey(survey)
+ }
+ }}
+ >
+ Duplicate
+ </LemonButton>
+ </AccessControlAction>
+ {!survey.start_date && (
+ <AccessControlAction
+ resourceType={AccessControlResourceType.Survey}
+ minAccessLevel={AccessControlLevel.Editor}
+ userAccessLevel={survey.user_access_level}
+ >
+ <LemonButton
+ fullWidth
+ onClick={() => {
+ const warnings = getSurveyWarnings(survey, teamSdkVersions)
+ LemonDialog.open({
+ title: 'Launch this survey?',
+ content: (
+ <div>
+ <div className="text-sm text-secondary">
+ The survey will immediately start displaying to
+ users matching the display conditions.
+ </div>
+ <SdkVersionWarnings warnings={warnings} />
+ </div>
+ ),
+ primaryButton: {
+ children: 'Launch',
+ type: 'primary',
+ onClick: () => {
+ updateSurvey({
+ id: survey.id,
+ updatePayload: {
+ start_date: dayjs().toISOString(),
+ },
+ intentContext: ProductIntentContext.SURVEY_LAUNCHED,
+ })
+ },
+ size: 'small',
+ },
+ secondaryButton: {
+ children: 'Cancel',
+ type: 'tertiary',
+ size: 'small',
+ },
+ })
+ }}
+ >
+ Launch survey
+ </LemonButton>
+ </AccessControlAction>
+ )}
+ {isSurveyRunning(survey) && (
+ <AccessControlAction
+ resourceType={AccessControlResourceType.Survey}
+ minAccessLevel={AccessControlLevel.Editor}
+ userAccessLevel={survey.user_access_level}
+ >
+ <LemonButton
+ fullWidth
+ onClick={() => {
+ LemonDialog.open({
+ title: 'Stop this survey?',
+ content: (
+ <div className="text-sm text-secondary">
+ The survey will no longer be visible to your users.
+ </div>
+ ),
+ primaryButton: {
+ children: 'Stop',
+ type: 'primary',
+ onClick: () => {
+ updateSurvey({
+ id: survey.id,
+ updatePayload: {
+ end_date: dayjs().toISOString(),
+ },
+ intentContext:
+ ProductIntentContext.SURVEY_COMPLETED,
+ })
+ },
+ size: 'small',
+ },
+ secondaryButton: {
+ children: 'Cancel',
+ type: 'tertiary',
+ size: 'small',
+ },
+ })
+ }}
+ >
+ Stop survey
+ </LemonButton>
+ </AccessControlAction>
+ )}
+ {survey.end_date && !survey.archived && (
+ <AccessControlAction
+ resourceType={AccessControlResourceType.Survey}
+ minAccessLevel={AccessControlLevel.Editor}
+ userAccessLevel={survey.user_access_level}
+ >
+ <LemonButton
+ fullWidth
+ onClick={() =>
+ openResumeSurveyDialog(survey, () =>
+ updateSurvey({
+ id: survey.id,
+ updatePayload: { end_date: null },
+ intentContext: ProductIntentContext.SURVEY_RESUMED,
+ })
+ )
+ }
+ >
+ Resume survey
+ </LemonButton>
+ </AccessControlAction>
+ )}
+ <LemonDivider />
+ {survey.archived && (
+ <AccessControlAction
+ resourceType={AccessControlResourceType.Survey}
+ minAccessLevel={AccessControlLevel.Editor}
+ userAccessLevel={survey.user_access_level}
+ >
+ <LemonButton
+ fullWidth
+ onClick={() => {
+ updateSurvey({
+ id: survey.id,
+ updatePayload: { archived: false },
+ intentContext: ProductIntentContext.SURVEY_UNARCHIVED,
+ })
+ }}
+ >
+ Unarchive
+ </LemonButton>
+ </AccessControlAction>
+ )}
+ {!survey.archived && (
+ <AccessControlAction
+ resourceType={AccessControlResourceType.Survey}
+ minAccessLevel={AccessControlLevel.Editor}
+ userAccessLevel={survey.user_access_level}
+ >
+ <LemonButton
+ fullWidth
+ onClick={() =>
+ openArchiveSurveyDialog(survey, () => {
+ const updatePayload: Partial<Survey> = {
+ archived: true,
+ }
+ if (isSurveyRunning(survey)) {
+ updatePayload.end_date = dayjs().toISOString()
+ }
+ updateSurvey({
+ id: survey.id,
+ updatePayload,
+ intentContext: ProductIntentContext.SURVEY_ARCHIVED,
+ })
+ })
+ }
+ >
+ Archive
+ </LemonButton>
+ </AccessControlAction>
+ )}
+ {canDeleteSurvey(survey) && (
+ <AccessControlAction
+ resourceType={AccessControlResourceType.Survey}
+ minAccessLevel={AccessControlLevel.Editor}
+ userAccessLevel={survey.user_access_level}
+ >
+ <LemonButton
+ status="danger"
+ onClick={() =>
+ openDeleteSurveyDialog(survey, () => deleteSurvey(survey.id))
+ }
+ fullWidth
+ >
+ Delete permanently
+ </LemonButton>
+ </AccessControlAction>
+ )}
+ </>
+ }
+ />
+ )
+ },
+ },
+ ],
+ [
+ surveysResponsesCount,
+ surveysResponsesCountLoading,
+ tab,
+ teamSdkVersions,
+ hasMultipleProjects,
+ updateSurvey,
+ deleteSurvey,
+ duplicateSurvey,
+ setSurveyToDuplicate,
+ ]
+ )
+
const isInitialDataLoad = surveys.length === 0 && hasNextPage
const isTableLoading = dataLoading || isInitialDataLoad
const shouldShowEmptyState = !isTableLoading && surveys.length === 0
@@ -167,281 +450,7 @@ export function SurveysTable(): JSX.Element {
</div>
)
}
- columns={[
- {
- dataIndex: 'name',
- title: 'Name',
- render: function RenderName(_, survey) {
- return <LemonTableLink to={urls.survey(survey.id)} title={stringWithWBR(survey.name, 17)} />
- },
- },
- {
- title: 'Responses',
- dataIndex: 'id',
- render: function RenderResponses(_, survey) {
- return (
- <>
- {surveysResponsesCountLoading ? (
- <Spinner />
- ) : (
- <div>{surveysResponsesCount[survey.id] ?? 0}</div>
- )}
- </>
- )
- },
- sorter: (surveyA, surveyB) => {
- const countA = surveysResponsesCount[surveyA.id] ?? 0
- const countB = surveysResponsesCount[surveyB.id] ?? 0
- return countA - countB
- },
- },
- {
- dataIndex: 'type',
- title: 'Mode',
- render: function RenderType(_, survey) {
- return SURVEY_TYPE_LABEL_MAP[survey.type]
- },
- },
- {
- title: 'Question type',
- render: function RenderResponses(_, survey) {
- return survey.questions?.length === 1
- ? SurveyQuestionLabel[survey.questions[0].type]
- : 'Multiple'
- },
- },
- ...(tab === SurveysTabs.Active
- ? [
- createdAtColumn<Survey>() as LemonTableColumn<Survey, keyof Survey | undefined>,
- {
- title: 'Status',
- width: 100,
- render: function Render(_: any, survey: Survey) {
- return <SurveyStatusTag survey={survey} />
- },
- },
- ]
- : []),
- {
- width: 0,
- render: function Render(_, survey: Survey) {
- return (
- <More
- overlay={
- <>
- <LemonButton
- fullWidth
- onClick={() => router.actions.push(urls.survey(survey.id))}
- >
- View
- </LemonButton>
- <AccessControlAction
- resourceType={AccessControlResourceType.Survey}
- minAccessLevel={AccessControlLevel.Editor}
- userAccessLevel={survey.user_access_level}
- >
- <LemonButton
- fullWidth
- onClick={() => {
- if (hasMultipleProjects) {
- setSurveyToDuplicate(survey)
- } else {
- duplicateSurvey(survey)
- }
- }}
- >
- Duplicate
- </LemonButton>
- </AccessControlAction>
- {!survey.start_date && (
- <AccessControlAction
- resourceType={AccessControlResourceType.Survey}
- minAccessLevel={AccessControlLevel.Editor}
- userAccessLevel={survey.user_access_level}
- >
- <LemonButton
- fullWidth
- onClick={() => {
- const warnings = getSurveyWarnings(survey, teamSdkVersions)
- LemonDialog.open({
- title: 'Launch this survey?',
- content: (
- <div>
- <div className="text-sm text-secondary">
- The survey will immediately start displaying
- to users matching the display conditions.
- </div>
- <SdkVersionWarnings warnings={warnings} />
- </div>
- ),
- primaryButton: {
- children: 'Launch',
- type: 'primary',
- onClick: () => {
- updateSurvey({
- id: survey.id,
- updatePayload: {
- start_date: dayjs().toISOString(),
- },
- intentContext:
- ProductIntentContext.SURVEY_LAUNCHED,
- })
- },
- size: 'small',
- },
- secondaryButton: {
- children: 'Cancel',
- type: 'tertiary',
- size: 'small',
- },
- })
- }}
- >
- Launch survey
- </LemonButton>
- </AccessControlAction>
- )}
- {isSurveyRunning(survey) && (
- <AccessControlAction
- resourceType={AccessControlResourceType.Survey}
- minAccessLevel={AccessControlLevel.Editor}
- userAccessLevel={survey.user_access_level}
- >
- <LemonButton
- fullWidth
- onClick={() => {
- LemonDialog.open({
- title: 'Stop this survey?',
- content: (
- <div className="text-sm text-secondary">
- The survey will no longer be visible to your
- users.
- </div>
- ),
- primaryButton: {
- children: 'Stop',
- type: 'primary',
- onClick: () => {
- updateSurvey({
- id: survey.id,
- updatePayload: {
- end_date: dayjs().toISOString(),
- },
- intentContext:
- ProductIntentContext.SURVEY_COMPLETED,
- })
- },
- size: 'small',
- },
- secondaryButton: {
- children: 'Cancel',
- type: 'tertiary',
- size: 'small',
- },
- })
- }}
- >
- Stop survey
- </LemonButton>
- </AccessControlAction>
- )}
- {survey.end_date && !survey.archived && (
- <AccessControlAction
- resourceType={AccessControlResourceType.Survey}
- minAccessLevel={AccessControlLevel.Editor}
- userAccessLevel={survey.user_access_level}
- >
- <LemonButton
- fullWidth
- onClick={() =>
- openResumeSurveyDialog(survey, () =>
- updateSurvey({
- id: survey.id,
- updatePayload: { end_date: null },
- intentContext: ProductIntentContext.SURVEY_RESUMED,
- })
- )
- }
- >
- Resume survey
- </LemonButton>
- </AccessControlAction>
- )}
- <LemonDivider />
- {survey.archived && (
- <AccessControlAction
- resourceType={AccessControlResourceType.Survey}
- minAccessLevel={AccessControlLevel.Editor}
- userAccessLevel={survey.user_access_level}
- >
- <LemonButton
- fullWidth
- onClick={() => {
- updateSurvey({
- id: survey.id,
- updatePayload: { archived: false },
- intentContext: ProductIntentContext.SURVEY_UNARCHIVED,
- })
- }}
- >
- Unarchive
- </LemonButton>
- </AccessControlAction>
- )}
- {!survey.archived && (
- <AccessControlAction
- resourceType={AccessControlResourceType.Survey}
- minAccessLevel={AccessControlLevel.Editor}
- userAccessLevel={survey.user_access_level}
- >
- <LemonButton
- fullWidth
- onClick={() =>
- openArchiveSurveyDialog(survey, () => {
- const updatePayload: Partial<Survey> = {
- archived: true,
- }
- if (isSurveyRunning(survey)) {
- updatePayload.end_date = dayjs().toISOString()
- }
- updateSurvey({
- id: survey.id,
- updatePayload,
- intentContext: ProductIntentContext.SURVEY_ARCHIVED,
- })
- })
- }
- >
- Archive
- </LemonButton>
- </AccessControlAction>
- )}
- {canDeleteSurvey(survey) && (
- <AccessControlAction
- resourceType={AccessControlResourceType.Survey}
- minAccessLevel={AccessControlLevel.Editor}
- userAccessLevel={survey.user_access_level}
- >
- <LemonButton
- status="danger"
- onClick={() =>
- openDeleteSurveyDialog(survey, () =>
- deleteSurvey(survey.id)
- )
- }
- fullWidth
- >
- Delete permanently
- </LemonButton>
- </AccessControlAction>
- )}
- </>
- }
- />
- )
- },
- },
- ]}
+ columns={columns}
/>
</>
)