: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 type { Meta, StoryObj } from '@storybook/react'
import { useState } from 'react'
import { TileFilters } from '~/queries/schema/schema-general'
import { AccessControlLevel, DashboardTile, InsightColor, InsightShortId, InsightModel } from '~/types'
import EXAMPLE_DATA_TABLE_NODE_EVENTS_QUERY from '../../../../mocks/fixtures/api/projects/team_id/insights/dataTableEvents.json'
import EXAMPLE_DATA_TABLE_NODE_HOGQL_QUERY from '../../../../mocks/fixtures/api/projects/team_id/insights/dataTableHogQL.json'
import EXAMPLE_FUNNEL from '../../../../mocks/fixtures/api/projects/team_id/insights/funnelLeftToRight.json'
import EXAMPLE_LIFECYCLE from '../../../../mocks/fixtures/api/projects/team_id/insights/lifecycle.json'
import EXAMPLE_RETENTION from '../../../../mocks/fixtures/api/projects/team_id/insights/retention.json'
import EXAMPLE_STICKINESS from '../../../../mocks/fixtures/api/projects/team_id/insights/stickiness.json'
import EXAMPLE_TRENDS from '../../../../mocks/fixtures/api/projects/team_id/insights/trendsLine.json'
import EXAMPLE_TRENDS_BREAKDOWN_MANY from '../../../../mocks/fixtures/api/projects/team_id/insights/trendsLineBreakdownMany.json'
import EXAMPLE_TRENDS_MULTI from '../../../../mocks/fixtures/api/projects/team_id/insights/trendsLineMulti.json'
import EXAMPLE_TRENDS_PIE from '../../../../mocks/fixtures/api/projects/team_id/insights/trendsPie.json'
import EXAMPLE_TRENDS_TABLE from '../../../../mocks/fixtures/api/projects/team_id/insights/trendsTable.json'
import EXAMPLE_TRENDS_HORIZONTAL_BAR from '../../../../mocks/fixtures/api/projects/team_id/insights/trendsValue.json'
import EXAMPLE_TRENDS_WORLD_MAP from '../../../../mocks/fixtures/api/projects/team_id/insights/trendsWorldMap.json'
import { ApiError } from '../../../api-error'
import { InsightCard as InsightCardComponent } from './index'
import type { InsightCardProps } from './InsightCard'
const defaultTile = {
id: 1,
filters_overrides: {
properties: [
{
key: '$browser',
operator: 'exact',
type: 'event',
value: ['Chrome'],
},
],
} as TileFilters,
} as DashboardTile
const examples = [
EXAMPLE_TRENDS,
EXAMPLE_TRENDS_MULTI,
EXAMPLE_TRENDS_BREAKDOWN_MANY,
EXAMPLE_TRENDS_HORIZONTAL_BAR,
EXAMPLE_TRENDS_TABLE,
EXAMPLE_TRENDS_PIE,
EXAMPLE_TRENDS_WORLD_MAP,
EXAMPLE_FUNNEL,
EXAMPLE_RETENTION,
EXAMPLE_STICKINESS,
EXAMPLE_LIFECYCLE,
EXAMPLE_DATA_TABLE_NODE_HOGQL_QUERY,
EXAMPLE_DATA_TABLE_NODE_EVENTS_QUERY,
] as unknown as InsightModel[]
const meta: Meta<InsightCardProps> = {
title: 'Components/Cards/Insight Card',
component: InsightCardComponent,
parameters: {
mockDate: '2023-07-01',
},
argTypes: {
insight: {
name: 'Insight',
control: { type: 'object' },
},
loading: {
control: { type: 'boolean' },
},
apiErrored: {
control: { type: 'boolean' },
},
timedOut: {
control: { type: 'boolean' },
},
highlighted: {
control: { type: 'boolean' },
},
tile: defaultTile,
},
args: {
insight: {
name: '',
description: '',
} as unknown as InsightModel,
},
}
export default meta
type Story = StoryObj<InsightCardProps>
export const InsightCard: Story = {
render: (args) => {
const [insightColor, setInsightColor] = useState<InsightColor | null>(null)
const [wasItemRemoved, setWasItemRemoved] = useState(false)
return (
<div className="grid gap-4 grid-cols-2 min-w-[50rem]">
{!wasItemRemoved && (
<InsightCardComponent
tile={args.tile}
insight={
{
...EXAMPLE_TRENDS,
name: args.insight.name,
description: args.insight.description,
} as unknown as InsightModel
}
ribbonColor={insightColor}
loading={args.loading}
apiErrored={args.apiErrored}
highlighted={args.highlighted}
timedOut={args.timedOut}
showResizeHandles={args.showResizeHandles}
updateColor={setInsightColor}
removeFromDashboard={() => setWasItemRemoved(true)}
rename={() => {}}
duplicate={() => {}}
placement="SavedInsightGrid"
/>
)}
<InsightCardComponent
tile={args.tile}
insight={
{
...EXAMPLE_TRENDS,
name: 'Wow, this name is really super duper ginormously off the charts long! How do we even manage to fit it in an insight card without it breaking?!',
description:
'Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.',
tags: ['every', 'green', 'bus', 'drives', 'fast', 'face'],
} as unknown as InsightModel
}
rename={() => {}}
duplicate={() => {}}
placement="SavedInsightGrid"
loading={args.loading}
apiErrored={args.apiErrored}
highlighted={args.highlighted}
timedOut={args.timedOut}
showResizeHandles={args.showResizeHandles}
/>
<InsightCardComponent
tile={args.tile}
insight={
{
...EXAMPLE_TRENDS,
name: '',
description: '',
last_modified_by: null,
} as unknown as InsightModel
}
rename={() => {}}
duplicate={() => {}}
placement="SavedInsightGrid"
loading={args.loading}
apiErrored={args.apiErrored}
highlighted={args.highlighted}
timedOut={args.timedOut}
showResizeHandles={args.showResizeHandles}
/>
<InsightCardComponent
tile={args.tile}
insight={
{
...EXAMPLE_FUNNEL,
short_id: 'funnel_empty' as InsightShortId,
query: {
...EXAMPLE_FUNNEL.query,
source: {
...EXAMPLE_FUNNEL.query.source,
series: EXAMPLE_FUNNEL.query.source.series.slice(0, 1),
},
},
name: 'What a pitiful funnel',
} as unknown as InsightModel
}
rename={() => {}}
duplicate={() => {}}
placement="SavedInsightGrid"
loading={args.loading}
apiErrored={args.apiErrored}
highlighted={args.highlighted}
timedOut={args.timedOut}
showResizeHandles={args.showResizeHandles}
/>
<InsightCardComponent
tile={args.tile}
insight={
{
...EXAMPLE_FUNNEL,
name: 'What a plentiful funnel',
} as unknown as InsightModel
}
rename={() => {}}
duplicate={() => {}}
placement="SavedInsightGrid"
loading={args.loading}
apiErrored={args.apiErrored}
highlighted={args.highlighted}
timedOut={args.timedOut}
showResizeHandles={args.showResizeHandles}
/>
{examples.map((e) => (
<InsightCardComponent
tile={args.tile}
key={e.id}
insight={e as unknown as InsightModel}
rename={() => {}}
duplicate={() => {}}
placement="SavedInsightGrid"
loading={args.loading}
apiErrored={args.apiErrored}
highlighted={args.highlighted}
timedOut={args.timedOut}
showResizeHandles={args.showResizeHandles}
/>
))}
</div>
)
},
}
export const ErrorStates: Story = {
render: () => {
const errors = [
{
name: 'Invalid query',
error: new ApiError('The query is invalid.', 400, undefined, {
detail: 'The query is invalid.',
code: 'invalid_query',
queryId: 'invalid-query-id',
}),
},
{
name: 'Rate limit',
error: new ApiError('Too many requests.', 429, new Headers({ 'Retry-After': '120' }), {
detail: 'Too many requests.',
code: 'rate_limited',
queryId: 'rate-limit-query-id',
}),
},
{
name: 'Permission denied',
error: new ApiError('You do not have permission to run this query.', 403, undefined, {
detail: 'You do not have permission to run this query.',
code: 'permission_denied',
queryId: 'permission-query-id',
}),
},
{
name: 'Table access denied',
error: new ApiError("You don't have access to table `events`.", 400, undefined, {
detail: "You don't have access to table `events`.",
code: 'table_access_denied',
queryId: 'table-access-query-id',
}),
},
{
name: 'Temporary server failure',
error: new ApiError('The query service is temporarily unavailable.', 503, undefined, {
detail: 'The query service is temporarily unavailable.',
code: 'service_unavailable',
queryId: 'temporary-query-id',
}),
},
{
name: 'Unknown server failure',
error: new ApiError('The query service failed.', 500, undefined, {
detail: 'The query service failed.',
code: 'server_error',
queryId: 'server-query-id',
}),
},
]
return (
<div className="grid gap-4 grid-cols-2 min-w-[60rem]">
{errors.map(({ name, error }) => (
<InsightCardComponent
key={name}
tile={defaultTile}
insight={{ ...EXAMPLE_TRENDS, name } as unknown as InsightModel}
apiErrored
apiError={error}
refresh={() => {}}
queryId={error.data?.queryId}
rename={() => {}}
duplicate={() => {}}
placement="SavedInsightGrid"
/>
))}
<InsightCardComponent
tile={defaultTile}
insight={{ ...EXAMPLE_TRENDS, name: 'Timed out' } as unknown as InsightModel}
timedOut
refresh={() => {}}
rename={() => {}}
duplicate={() => {}}
placement="SavedInsightGrid"
/>
</div>
)
},
}
export const CapacityCooldown: Story = {
parameters: {
mockDate: '2026-01-01T12:00:00Z',
testOptions: { viewportWidths: ['narrow', 'medium'] },
},
render: () => {
const [error] = useState(
() => new ApiError('', 503, new Headers({ 'Retry-After': '45' }), { queryId: 'capacity-example-query' })
)
return (
<div className="w-full max-w-160">
<InsightCardComponent
tile={defaultTile}
insight={{ ...EXAMPLE_TRENDS, name: 'Page views', result: null } as unknown as InsightModel}
apiErrored
apiError={error}
refresh={() => {}}
rename={() => {}}
duplicate={() => {}}
placement="SavedInsightGrid"
/>
</div>
)
},
}
// Access Control Stories
export const AccessControlNoAccess: Story = {
render: () => {
return (
<div className="grid gap-4 grid-cols-2 min-w-[50rem]">
<InsightCardComponent
tile={defaultTile}
insight={
{
...EXAMPLE_TRENDS,
name: 'Sales Analysis - Restricted',
description: 'This insight contains sensitive sales data.',
user_access_level: 'none',
} as unknown as InsightModel
}
rename={() => {}}
duplicate={() => {}}
placement="SavedInsightGrid"
/>
</div>
)
},
}
export const AccessControlViewerAccess: Story = {
render: () => {
const [insightColor, setInsightColor] = useState<InsightColor | null>(null)
return (
<div className="grid gap-4 grid-cols-2 min-w-[50rem]">
<InsightCardComponent
tile={defaultTile}
insight={
{
...EXAMPLE_TRENDS,
name: 'User Engagement Metrics - View Only',
description: 'You can view this insight but cannot edit it.',
user_access_level: AccessControlLevel.Viewer,
} as unknown as InsightModel
}
ribbonColor={insightColor}
updateColor={setInsightColor}
rename={() => {}}
duplicate={() => {}}
placement="SavedInsightGrid"
/>
</div>
)
},
}
export const AccessControlEditorAccess: Story = {
render: () => {
const [insightColor, setInsightColor] = useState<InsightColor | null>(null)
const [wasItemRemoved, setWasItemRemoved] = useState(false)
return (
<div className="grid gap-4 grid-cols-2 min-w-[50rem]">
{!wasItemRemoved && (
<InsightCardComponent
tile={defaultTile}
insight={
{
...EXAMPLE_TRENDS,
name: 'Product Analytics - Full Access',
description: 'You can view, edit, and manage this insight.',
user_access_level: AccessControlLevel.Editor,
} as unknown as InsightModel
}
ribbonColor={insightColor}
updateColor={setInsightColor}
removeFromDashboard={() => setWasItemRemoved(true)}
rename={() => {}}
duplicate={() => {}}
placement="SavedInsightGrid"
/>
)}
</div>
)
},
}
export const AccessControlManagerAccess: Story = {
render: () => {
const [insightColor, setInsightColor] = useState<InsightColor | null>(null)
const [wasItemRemoved, setWasItemRemoved] = useState(false)
return (
<div className="grid gap-4 grid-cols-2 min-w-[50rem]">
{!wasItemRemoved && (
<InsightCardComponent
tile={defaultTile}
insight={
{
...EXAMPLE_TRENDS,
name: 'Executive Dashboard - Manager Access',
description: 'You have full management permissions for this insight.',
user_access_level: AccessControlLevel.Manager,
} as unknown as InsightModel
}
ribbonColor={insightColor}
updateColor={setInsightColor}
removeFromDashboard={() => setWasItemRemoved(true)}
deleteWithUndo={async () => {}}
rename={() => {}}
duplicate={() => {}}
placement="SavedInsightGrid"
/>
)}
</div>
)
},
}
export const AccessControlLegacyInsight: Story = {
render: () => {
const [insightColor, setInsightColor] = useState<InsightColor | null>(null)
const [wasItemRemoved, setWasItemRemoved] = useState(false)
return (
<div className="grid gap-4 grid-cols-2 min-w-[50rem]">
{!wasItemRemoved && (
<InsightCardComponent
tile={defaultTile}
insight={
{
...EXAMPLE_TRENDS,
name: 'Legacy Insight - No Access Control',
description: 'This insight was created before access control was implemented.',
// user_access_level is intentionally undefined to test fallback behavior
} as unknown as InsightModel
}
ribbonColor={insightColor}
updateColor={setInsightColor}
removeFromDashboard={() => setWasItemRemoved(true)}
deleteWithUndo={async () => {}}
rename={() => {}}
duplicate={() => {}}
placement="SavedInsightGrid"
/>
)}
</div>
)
},
}
export const AccessControlMixedPermissions: Story = {
render: () => {
const [insightColor, setInsightColor] = useState<InsightColor | null>(null)
return (
<div className="grid gap-4 grid-cols-3 min-w-[75rem]">
{/* No Access */}
<InsightCardComponent
tile={defaultTile}
insight={
{
...EXAMPLE_TRENDS,
name: 'Restricted Data',
user_access_level: 'none',
} as unknown as InsightModel
}
rename={() => {}}
duplicate={() => {}}
placement="SavedInsightGrid"
/>
{/* Viewer Access */}
<InsightCardComponent
tile={defaultTile}
insight={
{
...EXAMPLE_FUNNEL,
name: 'View Only Funnel',
user_access_level: AccessControlLevel.Viewer,
} as unknown as InsightModel
}
ribbonColor={insightColor}
updateColor={setInsightColor}
rename={() => {}}
duplicate={() => {}}
placement="SavedInsightGrid"
/>
{/* Editor Access */}
<InsightCardComponent
tile={defaultTile}
insight={
{
...EXAMPLE_RETENTION,
name: 'Editable Retention',
user_access_level: AccessControlLevel.Editor,
} as unknown as InsightModel
}
ribbonColor={insightColor}
updateColor={setInsightColor}
removeFromDashboard={() => {}}
deleteWithUndo={async () => {}}
rename={() => {}}
duplicate={() => {}}
placement="SavedInsightGrid"
/>
</div>
)
},
}
export const WarehouseSyncWarning: Story = {
render: () => {
const insight = {
...EXAMPLE_TRENDS,
name: 'Monthly revenue by plan',
warnings: [
{
type: 'warehouse_sync',
message:
'Last sync of `invoices` (from Stripe) failed. Results reflect data from 5 days ago. Check the data warehouse source for details.',
schema_name: 'invoices',
source_id: 'source-1',
source_type: 'Stripe',
status: 'Failed',
table_name: 'stripe_invoices',
},
{
type: 'warehouse_sync',
message:
'`hubspot_deals` (from Hubspot) last synced 3 days ago, more than twice its configured sync interval. Results may be out of date.',
schema_name: 'deals',
source_id: 'source-2',
source_type: 'Hubspot',
status: 'Completed',
table_name: 'hubspot_deals',
},
],
} as unknown as InsightModel
return (
<div className="flex gap-4 min-w-[50rem]">
<div className="w-[32rem]">
<InsightCardComponent
tile={defaultTile}
insight={insight}
rename={() => {}}
duplicate={() => {}}
placement="SavedInsightGrid"
/>
</div>
{/* A narrow tile, so the tag has to share the title row with a truncated name. */}
<div className="w-[16rem]">
<InsightCardComponent
tile={defaultTile}
insight={insight}
rename={() => {}}
duplicate={() => {}}
placement="SavedInsightGrid"
/>
</div>
</div>
)
},
}