: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
chore(product-analytics): Add a banner to the dashboard when it's shared publicly (#58361)
Jordan Mryyan5774e0625621
diff --git a/frontend/src/lib/components/Sharing/SharingModal.test.tsx b/frontend/src/lib/components/Sharing/SharingModal.test.tsxindex 7d0a110f1b75..e1caba5d4e45 100644--- a/frontend/src/lib/components/Sharing/SharingModal.test.tsx+++ b/frontend/src/lib/components/Sharing/SharingModal.test.tsx@@ -1,6 +1,9 @@import '@testing-library/jest-dom'import { render, screen, within } from '@testing-library/react'+import { expectLogic } from 'kea-test-utils'++import { eventUsageLogic } from 'lib/utils/eventUsageLogic'import { useAvailableFeatures } from '~/mocks/features'import { useMocks } from '~/mocks/jest'@@ -8,6 +11,7 @@ import { initKeaTests } from '~/test/init'import { InsightShortId, QueryBasedInsightModel } from '~/types'import { AvailableFeature } from '~/types'+import { sharingLogic } from './sharingLogic'import { SharingModal, SharingModalProps } from './SharingModal'const createdAt = '2022-06-28T12:30:51.459746Z'@@ -17,13 +21,15 @@ const insightShortId = 'insight456' as InsightShortIdconst defaultInsightId = 456function mockDashboardSharingConfiguration({+ enabled = true,passwordRequired = false,}: {+ enabled?: booleanpasswordRequired?: boolean}): Record<string, any> {const sharingConfiguration = {created_at: createdAt,- enabled: true,+ enabled,access_token: accessToken,password_required: passwordRequired,}@@ -101,6 +107,50 @@ describe('SharingModal (dashboard)', () => {// Dashboard options smoke checksexpect(screen.getByText(/Show PostHog branding/i)).toBeInTheDocument()})++ it('calls onSharingEnabledChange after the dashboard sharing switch update succeeds', async () => {+ const onSharingEnabledChange = jest.fn()++ initKeaTests()+ useMocks({+ get: mockDashboardSharingConfiguration({ enabled: false }),+ patch: mockDashboardSharingConfiguration({ enabled: true }),+ })++ const logic = sharingLogic({ dashboardId, onSharingEnabledChange })+ eventUsageLogic.mount()+ await expectLogic(logic, () => {+ logic.mount()+ }).toDispatchActions(['loadSharingConfigurationSuccess'])+ expect(onSharingEnabledChange).not.toHaveBeenCalled()++ await expectLogic(logic, () => {+ logic.actions.setIsEnabled(true)+ }).toDispatchActions(['setIsEnabledSuccess'])++ expect(onSharingEnabledChange).toHaveBeenCalledTimes(1)+ expect(onSharingEnabledChange).toHaveBeenCalledWith(true)+ logic.unmount()+ eventUsageLogic.unmount()+ })++ it('does not call onSharingEnabledChange on initial dashboard sharing load', async () => {+ const onSharingEnabledChange = jest.fn()++ initKeaTests()+ useMocks({+ get: mockDashboardSharingConfiguration({ enabled: true }),+ })++ const logic = sharingLogic({ dashboardId, onSharingEnabledChange })++ await expectLogic(logic, () => {+ logic.mount()+ }).toDispatchActions(['loadSharingConfigurationSuccess'])++ expect(onSharingEnabledChange).not.toHaveBeenCalled()+ logic.unmount()+ })})describe('SharingModal (insight)', () => {diff --git a/frontend/src/lib/components/Sharing/SharingModal.tsx b/frontend/src/lib/components/Sharing/SharingModal.tsxindex bac1abed2598..2455cda85026 100644--- a/frontend/src/lib/components/Sharing/SharingModal.tsx+++ b/frontend/src/lib/components/Sharing/SharingModal.tsx@@ -90,6 +90,7 @@ export interface SharingModalBaseProps {*/recordingLinkTimeForm?: ReactNodeuserAccessLevel?: AccessControlLevel+ onSharingEnabledChange?: (enabled: boolean) => void}export interface SharingModalProps extends SharingModalBaseProps {@@ -110,6 +111,7 @@ export function SharingModalContent({previewIframe = false,recordingLinkTimeForm = undefined,userAccessLevel,+ onSharingEnabledChange,}: SharingModalBaseProps): JSX.Element {const logicProps = {dashboardId,@@ -117,6 +119,7 @@ export function SharingModalContent({recordingId,notebookShortId,additionalParams,+ onSharingEnabledChange,}const {whitelabelAvailable,diff --git a/frontend/src/lib/components/Sharing/sharingLogic.ts b/frontend/src/lib/components/Sharing/sharingLogic.tsindex 53b8f411922c..1522ac43801b 100644--- a/frontend/src/lib/components/Sharing/sharingLogic.ts+++ b/frontend/src/lib/components/Sharing/sharingLogic.ts@@ -21,6 +21,7 @@ export interface SharingLogicProps {recordingId?: stringnotebookShortId?: stringadditionalParams?: Record<string, any>+ onSharingEnabledChange?: (enabled: boolean) => void}export interface EmbedConfig {@@ -85,7 +86,11 @@ export const sharingLogic = kea<sharingLogicType>([return await api.sharing.get(await propsToApiParams(props))},setIsEnabled: async (enabled: boolean) => {- return await api.sharing.update(await propsToApiParams(props), { enabled })+ const sharingConfiguration = await api.sharing.update(await propsToApiParams(props), { enabled })+ if (sharingConfiguration) {+ props.onSharingEnabledChange?.(sharingConfiguration.enabled)+ }+ return sharingConfiguration},setPasswordRequired: async (password_required: boolean) => {return await api.sharing.update(await propsToApiParams(props), {diff --git a/frontend/src/scenes/dashboard/Dashboard.tsx b/frontend/src/scenes/dashboard/Dashboard.tsxindex 2fe80e618118..96741f120134 100644--- a/frontend/src/scenes/dashboard/Dashboard.tsx+++ b/frontend/src/scenes/dashboard/Dashboard.tsx@@ -27,6 +27,7 @@ import { AddInsightToDashboardModal } from './addInsightToDashboardModal/AddInsiimport { addInsightToDashboardLogic } from './addInsightToDashboardModalLogic'import { DashboardHeader } from './DashboardHeader'import { DashboardOverridesBanner } from './DashboardOverridesBanner'+import { DashboardPublicAccessBanner } from './DashboardPublicAccessBanner'import { DashboardZoomControl } from './DashboardZoomControl'import { EmptyDashboardComponent } from './EmptyDashboardComponent'@@ -108,6 +109,7 @@ function DashboardScene({ backTo }: { backTo?: { url: string; name: string } }):<SceneContent className={cn('dashboard')}>{placement == DashboardPlacement.Dashboard && <DashboardHeader />}{canEditDashboard && addInsightToDashboardModalVisible && <AddInsightToDashboardModal />}+ <DashboardPublicAccessBanner dashboard={dashboard} placement={placement} />{dashboardFailedToLoad ? (<InsightErrorState title="There was an error loading this dashboard" />diff --git a/frontend/src/scenes/dashboard/DashboardModals.tsx b/frontend/src/scenes/dashboard/DashboardModals.tsxindex 4ae597b0507c..5fecbbe0cc3d 100644--- a/frontend/src/scenes/dashboard/DashboardModals.tsx+++ b/frontend/src/scenes/dashboard/DashboardModals.tsx@@ -9,6 +9,7 @@ import { TerraformExportModal } from 'lib/components/TerraformExporter/Terraformimport { urls } from 'scenes/urls'import { userLogic } from 'scenes/userLogic'+import { dashboardsModel } from '~/models/dashboardsModel'import { DashboardMode, DashboardType, QueryBasedInsightModel } from '~/types'import { DashboardInsightColorsModal } from './DashboardInsightColorsModal'@@ -30,6 +31,7 @@ export function DashboardModals({ dashboard }: { dashboard: DashboardType<QueryBterraformModalOpen,} = useValues(dashboardLogic)const { setTerraformModalOpen } = useActions(dashboardLogic)+ const { updateDashboardSuccess } = useActions(dashboardsModel)const { push } = useActions(router)const { user } = useValues(userLogic)@@ -47,6 +49,7 @@ export function DashboardModals({ dashboard }: { dashboard: DashboardType<QueryBcloseModal={() => push(urls.dashboard(dashboard.id))}dashboardId={dashboard.id}userAccessLevel={dashboard.user_access_level}+ onSharingEnabledChange={(enabled) => updateDashboardSuccess({ ...dashboard, is_shared: enabled })}/>{canEditDashboard && (<>diff --git a/frontend/src/scenes/dashboard/DashboardPublicAccessBanner.test.tsx b/frontend/src/scenes/dashboard/DashboardPublicAccessBanner.test.tsxnew file mode 100644index 000000000000..d0a00632c593--- /dev/null+++ b/frontend/src/scenes/dashboard/DashboardPublicAccessBanner.test.tsx@@ -0,0 +1,91 @@+import '@testing-library/jest-dom'++import { cleanup, render, screen } from '@testing-library/react'++import { AccessControlLevel, DashboardPlacement, DashboardType, QueryBasedInsightModel } from '~/types'++import { DashboardPublicAccessBanner } from './DashboardPublicAccessBanner'++const MOCK_DASHBOARD: DashboardType<QueryBasedInsightModel> = {+ id: 5,+ name: 'Test dashboard',+ description: 'A test dashboard',+ pinned: false,+ tiles: [],+ tags: [],+ created_at: '2020-01-01T00:00:00Z',+ created_by: {+ id: 1,+ first_name: 'Test',+ last_name: 'User',+ email: '[email protected]',+ uuid: 'abc',+ distinct_id: 'test-distinct-id',+ },+ last_accessed_at: '2020-01-01T00:00:00Z',+ is_shared: false,+ deleted: false,+ creation_mode: 'default',+ user_access_level: AccessControlLevel.Editor,+ filters: {},+ variables: {},+}++function makeDashboard(+ overrides: Partial<DashboardType<QueryBasedInsightModel>> = {}+): DashboardType<QueryBasedInsightModel> {+ return { ...MOCK_DASHBOARD, ...overrides }+}++function renderBanner({+ dashboard = makeDashboard({ is_shared: true }),+ placement = DashboardPlacement.Dashboard,+}: {+ dashboard?: DashboardType<QueryBasedInsightModel>+ placement?: DashboardPlacement+} = {}): void {+ render(<DashboardPublicAccessBanner dashboard={dashboard} placement={placement} />)+}++describe('DashboardPublicAccessBanner', () => {+ afterEach(() => {+ cleanup()+ })++ it('shows when a standard dashboard is shared publicly', async () => {+ renderBanner()++ expect(+ screen.getByText(+ 'This dashboard is shared publicly. Updates you make here may be visible to anyone with the public link. Avoid adding sensitive data.'+ )+ ).toBeInTheDocument()+ expect(screen.getAllByText('Manage sharing')).toHaveLength(2)+ })++ it('shows for read-only team users', async () => {+ renderBanner({+ dashboard: makeDashboard({+ is_shared: true,+ user_access_level: AccessControlLevel.Viewer,+ }),+ })++ expect(screen.getByText(/This dashboard is shared publicly/)).toBeInTheDocument()+ })++ it('does not show when the dashboard is not shared', async () => {+ renderBanner({ dashboard: makeDashboard({ is_shared: false }) })++ expect(screen.queryByText(/This dashboard is shared publicly/)).not.toBeInTheDocument()+ })++ it.each([DashboardPlacement.Public, DashboardPlacement.Export])(+ 'does not show for %s placement',+ async (placement) => {+ renderBanner({ placement })++ expect(screen.queryByText(/This dashboard is shared publicly/)).not.toBeInTheDocument()+ }+ )+})diff --git a/frontend/src/scenes/dashboard/DashboardPublicAccessBanner.tsx b/frontend/src/scenes/dashboard/DashboardPublicAccessBanner.tsxnew file mode 100644index 000000000000..d51f39f62b6b--- /dev/null+++ b/frontend/src/scenes/dashboard/DashboardPublicAccessBanner.tsx@@ -0,0 +1,43 @@+import { useActions } from 'kea'+import { router } from 'kea-router'++import { LemonBanner } from '@posthog/lemon-ui'++import { urls } from 'scenes/urls'++import { DashboardPlacement, DashboardType, QueryBasedInsightModel } from '~/types'++const DASHBOARD_PUBLIC_ACCESS_BANNER_PLACEMENTS = [+ DashboardPlacement.Dashboard,+ DashboardPlacement.ProjectHomepage,+ DashboardPlacement.Builtin,+]++export function DashboardPublicAccessBanner({+ dashboard,+ placement,+}: {+ dashboard: DashboardType<QueryBasedInsightModel> | null+ placement: DashboardPlacement+}): JSX.Element | null {+ const { push } = useActions(router)++ if (!dashboard?.is_shared || !DASHBOARD_PUBLIC_ACCESS_BANNER_PLACEMENTS.includes(placement)) {+ return null+ }++ return (+ <LemonBanner+ type="warning"+ className="mb-4"+ dismissKey={`dashboard-public-access-banner-${dashboard.id}`}+ action={{+ children: 'Manage sharing',+ onClick: () => push(urls.dashboardSharing(dashboard.id)),+ }}+ >+ This dashboard is shared publicly. Updates you make here may be visible to anyone with the public link.+ Avoid adding sensitive data.+ </LemonBanner>+ )+}