import moment from "moment"; import React, { FC, useCallback, useEffect, useMemo, useState } from "react"; import { Meta, StoryObj } from "@storybook/react"; import { useTranslation } from "react-i18next"; import i18nextStoryDecorator from "../../.storybook/decorators/i18nextStoryDecorator"; import { Cron, getNextSynchronization } from "."; import { InputSize, InputType, TextInput } from "../text-input"; import { Button, ButtonSize } from "../button"; import type { CronProps } from "./Cron.types"; import { defaultCronString } from "./Cron.constants"; type CronType = FC<{ locale: string; timezone: string } & CronProps>; type Story = StoryObj; const locales = [ "az", "bg", "cs", "de", "el-GR", "en-GB", "en-US", "es", "fi", "fr", "hy-AM", "it", "lv", "nl", "pl", "pt", "pt-BR", "ro", "ru", "sk", "sl", "vi", "tr", "uk-UA", "ar-SA", "lo-LA", "ja-JP", "zh-CN", "ko-KR", ]; const TzNames = [ "-12:00", "-11:00", "-10:00", "-09:30", "-09:00", "-08:00", "-07:00", "-06:00", "-05:00", "-04:00", "-03:30", "-03:00", "-02:00", "-01:00", "00:00", "+01:00", "+02:00", "+03:00", "+03:30", "+04:00", "+04:30", "+05:00", "+05:30", "+05:45", "+06:00", "+06:30", "+07:00", "+08:00", "+08:45", "+09:00", "+09:30", "+10:00", "+10:30", "+11:00", "+12:00", "+12:45", "+13:00", "+14:00", ]; const meta: Meta = { title: "Components/Cron", component: Cron, argTypes: { value: { description: "Cron value", }, setValue: { description: "Set the cron value, similar to onChange.", }, onError: { description: "Triggered when the cron component detects an error with the value.", }, locale: { control: "select", options: locales }, timezone: { control: "select", options: TzNames }, }, decorators: [i18nextStoryDecorator], }; const DefaultTemplate = ({ defaultValue, locale, timezone, }: Record) => { const { i18n } = useTranslation(); const [input, setInput] = useState(() => defaultValue); const [cron, setCron] = useState(defaultValue); const [error, setError] = useState(); useEffect(() => { i18n.changeLanguage(locale); }, [i18n, locale]); const onError = useCallback((exception?: Error) => { setError(exception); }, []); const setValue = (value: string) => { setInput(value); setCron(value); }; const onClick = () => { setCron(input); }; useEffect(() => { setValue(defaultValue); }, [defaultValue]); const date = useMemo( () => getNextSynchronization(cron, timezone), [cron, timezone], ); return (
) => setInput(e.target.value) } style={{ flexGrow: 1, }} />

Cron string: {cron}

Error message: {error?.message ?? "undefined"}

{date && (

Next synchronization: {" "} {`${date?.setLocale(locale).toFormat("DDDD TTTT")}`}

)}
); }; export default meta; export const Default: Story = { args: { locale: "en-GB", timezone: moment.tz(moment.tz.guess()).format("Z"), }, render: ({ value: defaultValue = defaultCronString, locale, timezone }) => { return ( ); }, };