|
| 1 | +import { http, HttpResponse } from 'msw'; |
| 2 | +import { useForm } from 'react-hook-form'; |
| 3 | +import { SourceKind } from '@hyperdx/common-utils/dist/types'; |
| 4 | +import { Box } from '@mantine/core'; |
| 5 | +import type { Meta, StoryObj } from '@storybook/nextjs'; |
| 6 | + |
| 7 | +import { SourceSelectControlled } from './SourceSelect'; |
| 8 | + |
| 9 | +const mockSources = [ |
| 10 | + { |
| 11 | + id: 'source-logs', |
| 12 | + name: 'OpenAPI LB Logs New', |
| 13 | + kind: SourceKind.Log, |
| 14 | + connection: 'conn-1', |
| 15 | + from: { databaseName: 'default', tableName: 'otel_logs' }, |
| 16 | + timestampValueExpression: 'Timestamp', |
| 17 | + }, |
| 18 | + { |
| 19 | + id: 'source-console-logs', |
| 20 | + name: 'Console API LB Logs New', |
| 21 | + kind: SourceKind.Log, |
| 22 | + connection: 'conn-1', |
| 23 | + from: { databaseName: 'default', tableName: 'console_logs' }, |
| 24 | + timestampValueExpression: 'Timestamp', |
| 25 | + }, |
| 26 | + { |
| 27 | + id: 'source-traces', |
| 28 | + name: 'Data Plane Traces', |
| 29 | + kind: SourceKind.Trace, |
| 30 | + connection: 'conn-1', |
| 31 | + from: { databaseName: 'default', tableName: 'otel_traces' }, |
| 32 | + timestampValueExpression: 'Timestamp', |
| 33 | + }, |
| 34 | + { |
| 35 | + id: 'source-sessions', |
| 36 | + name: 'Sessions', |
| 37 | + kind: SourceKind.Session, |
| 38 | + connection: 'conn-1', |
| 39 | + from: { databaseName: 'default', tableName: 'hyperdx_sessions' }, |
| 40 | + timestampValueExpression: 'Timestamp', |
| 41 | + }, |
| 42 | + { |
| 43 | + id: 'source-metrics', |
| 44 | + name: 'System Metrics', |
| 45 | + kind: SourceKind.Metric, |
| 46 | + connection: 'conn-1', |
| 47 | + from: { databaseName: 'default', tableName: 'otel_metrics' }, |
| 48 | + timestampValueExpression: 'TimeUnix', |
| 49 | + }, |
| 50 | +]; |
| 51 | + |
| 52 | +const sourcesHandler = http.get('*/api/sources', () => |
| 53 | + HttpResponse.json(mockSources), |
| 54 | +); |
| 55 | + |
| 56 | +/** |
| 57 | + * `SourceSelectControlled` is the data-source picker used across the search, |
| 58 | + * chart, and dashboard surfaces. It is a `react-hook-form`-controlled Mantine |
| 59 | + * `Select` that lists the team's sources (optionally grouped by section), shows |
| 60 | + * the selected source's signal-kind icon on the left, and can render an |
| 61 | + * adjacent kebab menu for source-management actions. |
| 62 | + * |
| 63 | + * The dropdown marks the currently selected source with a trailing check so it |
| 64 | + * is easy to tell which source is active while scanning the list. |
| 65 | + */ |
| 66 | +const meta: Meta<typeof SourceSelectControlled> = { |
| 67 | + title: 'Components/SourceSelect', |
| 68 | + component: SourceSelectControlled, |
| 69 | + parameters: { |
| 70 | + layout: 'padded', |
| 71 | + msw: { handlers: { sources: sourcesHandler } }, |
| 72 | + }, |
| 73 | + argTypes: { |
| 74 | + size: { |
| 75 | + control: 'select', |
| 76 | + options: ['xs', 'sm', 'md', 'lg'], |
| 77 | + description: 'Mantine input size', |
| 78 | + }, |
| 79 | + }, |
| 80 | +}; |
| 81 | + |
| 82 | +export default meta; |
| 83 | + |
| 84 | +type StoryArgs = { |
| 85 | + size?: string; |
| 86 | + defaultSourceId?: string; |
| 87 | + withMenu?: boolean; |
| 88 | + allowedSourceKinds?: SourceKind[]; |
| 89 | +}; |
| 90 | + |
| 91 | +const SourceSelectWrapper = ({ |
| 92 | + size, |
| 93 | + defaultSourceId = 'source-logs', |
| 94 | + withMenu, |
| 95 | + allowedSourceKinds, |
| 96 | +}: StoryArgs) => { |
| 97 | + const { control } = useForm<{ source: string }>({ |
| 98 | + defaultValues: { source: defaultSourceId }, |
| 99 | + }); |
| 100 | + |
| 101 | + return ( |
| 102 | + <Box style={{ maxWidth: 420 }}> |
| 103 | + <SourceSelectControlled |
| 104 | + control={control} |
| 105 | + name="source" |
| 106 | + size={size} |
| 107 | + allowedSourceKinds={allowedSourceKinds} |
| 108 | + onSchemaPreview={withMenu ? () => {} : undefined} |
| 109 | + onEdit={withMenu ? () => {} : undefined} |
| 110 | + onManageSources={withMenu ? () => {} : undefined} |
| 111 | + onCreate={withMenu ? () => {} : undefined} |
| 112 | + /> |
| 113 | + </Box> |
| 114 | + ); |
| 115 | +}; |
| 116 | + |
| 117 | +type Story = StoryObj<typeof SourceSelectWrapper>; |
| 118 | + |
| 119 | +/** |
| 120 | + * A source is pre-selected. Open the dropdown to see the active source marked |
| 121 | + * with a check, alongside each source's signal-kind icon. |
| 122 | + */ |
| 123 | +export const Default: Story = { |
| 124 | + render: args => <SourceSelectWrapper {...args} />, |
| 125 | + args: { |
| 126 | + size: 'sm', |
| 127 | + defaultSourceId: 'source-logs', |
| 128 | + }, |
| 129 | +}; |
| 130 | + |
| 131 | +/** With the adjacent source-management kebab menu (view schema, edit, etc.). */ |
| 132 | +export const WithManagementMenu: Story = { |
| 133 | + name: 'With Management Menu', |
| 134 | + render: args => <SourceSelectWrapper {...args} />, |
| 135 | + args: { |
| 136 | + size: 'sm', |
| 137 | + defaultSourceId: 'source-traces', |
| 138 | + withMenu: true, |
| 139 | + }, |
| 140 | +}; |
| 141 | + |
| 142 | +/** No source selected yet — shows the generic placeholder icon and text. */ |
| 143 | +export const NoSelection: Story = { |
| 144 | + name: 'No Selection (Placeholder)', |
| 145 | + render: args => <SourceSelectWrapper {...args} />, |
| 146 | + args: { |
| 147 | + size: 'sm', |
| 148 | + defaultSourceId: '', |
| 149 | + }, |
| 150 | +}; |
| 151 | + |
| 152 | +/** Restricted to a single signal kind via `allowedSourceKinds`. */ |
| 153 | +export const LogsOnly: Story = { |
| 154 | + name: 'Logs Only', |
| 155 | + render: args => <SourceSelectWrapper {...args} />, |
| 156 | + args: { |
| 157 | + size: 'sm', |
| 158 | + defaultSourceId: 'source-logs', |
| 159 | + allowedSourceKinds: [SourceKind.Log], |
| 160 | + }, |
| 161 | +}; |
0 commit comments