import React from 'react'
import { type Meta, type StoryObj } from '@storybook/react-vite'
import { getTimeframeDates } from '../../services/TimeframeServiceTyped'
import { DocsTemplate } from '../../../.storybook'
const TimeframeDateDisplay = ({ timeframe, dateType, label }) => {
const resultDate = getTimeframeDates(timeframe, dateType)
return (
{label}
Input Timeframe:
{JSON.stringify(timeframe, null, 2)}
Date Type: {dateType}
Result: {resultDate}
)
}
export default {
title: 'Helper Functions/getTimeframeDates',
component: TimeframeDateDisplay,
parameters: {
viewMode: 'docs',
previewTabs: {
canvas: { hidden: true },
},
docs: {
page: () => (
<>
Core features:
-
Generates start and end dates for various timeframe types
-
Handles historical, trailing, current, quarterly, and
previous timeframes
- Returns dates as formatted strings
,
Supported timeframe types:
-
historical: Fixed periods like 30D, 3M, 1Y
-
trailing: Rolling periods like last 24 hours,
last 7 days
-
current: Current time periods (this month, this
year)
-
quarterly: Quarter-based periods with year
selection
-
previous: Previous time periods (last month,
last year)
,
Common use cases:
- Filtering data by specific date ranges
- Setting date boundaries for charts and reports
- Generating API requests with date parameters
- Creating comparative date ranges for analytics
,
Best practices:
-
Validate timeframes using{' '}
checkForCorrectTimeframe before passing to this
function
-
Always specify whether you need a 'startDate' or 'endDate'
-
Handle any potential null returns with appropriate fallbacks
,
]}
/>
>
),
source: {
code: false,
},
},
},
argTypes: {
label: {
control: 'text',
description: 'Label for the timeframe date calculation',
},
timeframe: {
control: 'object',
description: 'Timeframe object to calculate dates from',
},
dateType: {
control: 'radio',
options: ['startDate', 'endDate'],
description: 'Whether to get start or end date of the timeframe',
},
},
} as Meta
type Story = StoryObj
const Template: Story = {
render: (args) => {
return
},
}
export const Historical30Days: Story = {
...Template,
args: {
label: 'Historical 30 Days (Start Date)',
timeframe: {
display: '30D',
value: 30,
timeValue: 'day',
type: 'historical',
},
dateType: 'startDate',
},
}
export const Historical30DaysEnd: Story = {
...Template,
args: {
label: 'Historical 30 Days (End Date)',
timeframe: {
display: '30D',
value: 30,
timeValue: 'day',
type: 'historical',
},
dateType: 'endDate',
},
}
export const Trailing24Hours: Story = {
...Template,
args: {
label: 'Trailing 24 Hours (Start Date)',
timeframe: {
display: '24HRS',
value: 24,
timeValue: 'hours',
type: 'trailing',
},
dateType: 'startDate',
},
}
export const CurrentMonth: Story = {
...Template,
args: {
label: 'Current Month (Start Date)',
timeframe: {
display: 'This Month',
timeValue: 'month',
type: 'current',
},
dateType: 'startDate',
},
}
export const CurrentMonthEnd: Story = {
...Template,
args: {
label: 'Current Month (End Date)',
timeframe: {
display: 'This Month',
timeValue: 'month',
type: 'current',
},
dateType: 'endDate',
},
}
export const QuarterlyQ1: Story = {
...Template,
args: {
label: 'Quarterly Q1 2025 (Start Date)',
timeframe: {
display: '2025 Q1',
timeValue: 'quarter',
type: 'quarterly',
year: '2025',
quarter: 1,
},
dateType: 'startDate',
},
}
export const PreviousYear: Story = {
...Template,
args: {
label: 'Previous Year (Start Date)',
timeframe: {
display: 'Last Year',
timeValue: 'year',
type: 'previous',
},
dateType: 'startDate',
},
}
export const CustomTimeframe: Story = {
...Template,
args: {
label: 'Custom Date Range (Start Date)',
timeframe: {
display: '2025-01-01 - 2025-01-31',
value: 'custom',
timeValue: 'day',
type: 'historical',
},
dateType: 'startDate',
},
}
export const AllTimeHistorical: Story = {
...Template,
args: {
label: 'All Time Historical (Start Date)',
timeframe: {
display: 'All Time',
value: 'All',
timeValue: 'day',
type: 'historical',
},
dateType: 'startDate',
},
}