import React from 'react'
import { type Meta, type StoryObj } from '@storybook/react-vite'
import { checkForCorrectTimeframe } from '../../services/TimeframeServiceTyped'
import { DocsTemplate } from '../../../.storybook'
const TimeframeValidationDisplay = ({ timeframe, label }) => {
const isValid = checkForCorrectTimeframe(timeframe)
return (
{label}
{JSON.stringify(timeframe, null, 2)}
{isValid ? 'Valid ✓' : 'Invalid ✗'}
)
}
export default {
title: 'Helper Functions/checkForCorrectTimeframe',
component: TimeframeValidationDisplay,
parameters: {
viewMode: 'docs',
previewTabs: {
canvas: { hidden: true },
},
docs: {
page: () => (
<>
Core features:
-
Validates that timeframe objects contain all required
properties
-
Dynamically adjusts required properties based on the
timeframe type
- Handles null/undefined timeframe objects safely
,
Required properties by timeframe type:
-
All types:
display, timeValue,{' '}
type
-
Historical and trailing types: additional
value{' '}
property
-
Current and quarterly types: no
value property
required
,
Common use cases:
-
Validating timeframe objects before passing to date
calculation functions
- Form validation for timeframe selection components
-
API request validation: Ensures that data sent to or
received from APIs is correctly structured.
-
Data integrity checks: Validates data consistency and
correctness.
,
Best practices:
-
Use this function to validate timeframes before calling{' '}
getTimeframeDates
-
Implement validation early in your component lifecycle to
prevent downstream errors
-
Handle invalid timeframes gracefully by providing fallbacks
,
]}
/>
>
),
source: {
code: false,
},
},
},
argTypes: {
label: {
control: 'text',
description: 'Label for the timeframe',
},
timeframe: {
control: 'object',
description: 'Timeframe object to validate',
},
},
} as Meta
type Story = StoryObj
const Template: Story = {
render: (args) => {
return
},
}
export const ValidHistoricalTimeframe: Story = {
...Template,
args: {
label: 'Valid Historical Timeframe',
timeframe: {
display: '30D',
value: 30,
timeValue: 'day',
type: 'historical',
},
},
}
export const ValidTrailingTimeframe: Story = {
...Template,
args: {
label: 'Valid Trailing Timeframe',
timeframe: {
display: '24HRS',
value: 24,
timeValue: 'hours',
type: 'trailing',
},
},
}
export const ValidCurrentTimeframe: Story = {
...Template,
args: {
label: 'Valid Current Timeframe',
timeframe: {
display: 'This Month',
timeValue: 'month',
type: 'current',
},
},
}
export const ValidQuarterlyTimeframe: Story = {
...Template,
args: {
label: 'Valid Quarterly Timeframe',
timeframe: {
display: '2025 Q1',
timeValue: 'quarter',
type: 'quarterly',
year: '2025',
quarter: 1,
},
},
}
export const InvalidTimeframeMissingTimeValue: Story = {
...Template,
args: {
label: 'Invalid Timeframe (Missing timeValue)',
timeframe: {
display: '30D',
type: 'historical',
value: 30,
},
},
}
export const InvalidHistoricalTimeframeMissingValue: Story = {
...Template,
args: {
label: 'Invalid Historical Timeframe (Missing value)',
timeframe: {
display: '30D',
timeValue: 'day',
type: 'historical',
},
},
}
export const NullTimeframe: Story = {
...Template,
args: {
label: 'Null Timeframe',
timeframe: null,
},
}