import React, { type ComponentProps } from 'react' import type { Meta, StoryObj } from '@storybook/react-vite' import CsvExport from './CsvExport' import { DocsTemplate } from '../../../.storybook' import type SingleCsv from './SingleCsv' export default { title: 'Components/CsvExport', component: CsvExport, parameters: { docs: { page: () => ( The CsvExport component enables users to download reports in the CSV (Comma-Separated Values) file format. By clicking on the button, users can retrieve the report data as a downloadable file, allowing for easy analysis and compatibility with various spreadsheet software. } infoBullets={[ Place the CsvExport component in relevant areas, such as report or data visualization pages, where users might want to export the data for further analysis or sharing. , This component is typically used in conjunction with other components that display data, such as tables or charts. , The CsvExport component should be used sparingly on its own. It is currently part of the{' '} PageHeader {' '} and{' '} TableHeader {' '} components. Since those components are standards that should be used with tables and charts, it should be a rare occurence to use this component on its own. , ]} /> ), }, }, } as Meta type Story = StoryObj const Template: Story = { render: (args) => , } const singleCsvDataDownloadOptions = [ { linkName: 'CSV dataset 1', csvName: 'CSV dataset 1', hiddenClass: 'revandunits', csvData: [ { Name: 'John Doe', Age: 33, Email: 'johndoe@pattern.com' }, { Name: 'Jane Doe', Age: 31, Email: 'janedoe@pattern.com' }, { Name: 'Tom', Age: 35, Email: 'tom@pattern.com' }, ], callout: (element) => element.click(), }, ] const multiCsvDataDownloadOptions = [ { linkName: 'CSV dataset 1', csvName: 'CSV dataset 1', hiddenClass: 'revandunits', csvData: [ { Name: 'John Doe', Age: 33, Email: 'johndoe@pattern.com' }, { Name: 'Jane Doe', Age: 31, Email: 'janedoe@pattern.com' }, { Name: 'Tom', Age: 35, Email: 'tom@pattern.com' }, ], callout: (element) => element.click(), }, { linkName: 'CSV dataset 2', csvName: 'CSV dataset 2', hiddenClass: 'revandunits', csvData: [ { Name: 'Jane Doe', Age: 31, Email: 'janedoe@pattern.com' }, { Name: 'John Doe', Age: 33, Email: 'johndoe@pattern.com' }, { Name: 'Tom', Age: 35, Email: 'tom@pattern.com' }, ], callout: (element) => element.click(), }, ] // Using an open source CSV download API for demo const url = 'https://dl.dropboxusercontent.com/s/eui3rl43yf2m8nv/nuevo_test.csv' type CsvOption = ComponentProps['csv'] const singleCsvApiDownloadOptions: CsvOption[] = [ { linkName: 'API Data 1', csvName: 'API Data 1', csvFormat: { api: () => fetch(url).then((response) => response.blob()), params: {}, }, }, ] const multiCsvApiDownloadOptions: CsvOption[] = [ { linkName: 'API Data 1', csvName: 'API Data 1', csvFormat: { api: () => fetch(url).then((response) => { return response.blob() }), params: {}, }, }, { linkName: 'API Data 2', csvName: 'API Data 2', csvFormat: { api: () => fetch(url).then((response) => { return response.blob() }), params: {}, }, }, ] export const SingleCsvDataDownload: Story = { ...Template, args: { csvDownloadOptions: singleCsvDataDownloadOptions, show: true, initialDisplay: true, }, name: 'Single Download Using JSON Data', } export const MultipleCsvDataDownload: Story = { ...Template, args: { csvDownloadOptions: multiCsvDataDownloadOptions, show: true, initialDisplay: true, }, name: 'Multiple Download Using JSON Data', } export const SingleCsvApiDownload: Story = { ...Template, args: { csvDownloadOptions: singleCsvApiDownloadOptions, show: true, initialDisplay: true, }, name: 'Single Download Using an API', } export const MultipleCsvApiDownload: Story = { ...Template, args: { csvDownloadOptions: multiCsvApiDownloadOptions, show: true, initialDisplay: true, }, name: 'Multiple Download Using an API', }