|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import {dateTimeParse} from '@gravity-ui/date-utils'; |
| 4 | +import type {DateTime} from '@gravity-ui/date-utils'; |
| 5 | +import {Button} from '@gravity-ui/uikit'; |
| 6 | +import {action} from '@storybook/addon-actions'; |
| 7 | +import type {Meta, StoryObj} from '@storybook/react'; |
| 8 | + |
| 9 | +import {timeZoneControl} from '../../../demo/utils/zones'; |
| 10 | +import {RelativeRangeDatePicker} from '../../RelativeRangeDatePicker'; |
| 11 | +import type {RelativeRangeDatePickerValue} from '../../RelativeRangeDatePicker'; |
| 12 | +import {RangeDateSelection} from '../RangeDateSelection'; |
| 13 | +import type {ViewportDimensions, ViewportInterval} from '../components/Ruler/Ruler'; |
| 14 | + |
| 15 | +const meta: Meta<typeof RangeDateSelection> = { |
| 16 | + title: 'Components/RangeDateSelection', |
| 17 | + component: RangeDateSelection, |
| 18 | + tags: ['autodocs'], |
| 19 | + args: { |
| 20 | + onUpdate: action('onUpdate'), |
| 21 | + }, |
| 22 | +}; |
| 23 | + |
| 24 | +export default meta; |
| 25 | + |
| 26 | +type Story = StoryObj<typeof RangeDateSelection>; |
| 27 | + |
| 28 | +export const Default = { |
| 29 | + render: (args) => { |
| 30 | + const timeZone = args.timeZone; |
| 31 | + const props = { |
| 32 | + ...args, |
| 33 | + minValue: args.minValue ? dateTimeParse(args.minValue, {timeZone}) : undefined, |
| 34 | + maxValue: args.maxValue ? dateTimeParse(args.maxValue, {timeZone}) : undefined, |
| 35 | + placeholderValue: args.placeholderValue |
| 36 | + ? dateTimeParse(args.placeholderValue, {timeZone}) |
| 37 | + : undefined, |
| 38 | + renderAdditionalRulerContent: |
| 39 | + (args.renderAdditionalRulerContent as unknown as string) === 'fill' |
| 40 | + ? renderAdditionalRulerContent |
| 41 | + : undefined, |
| 42 | + }; |
| 43 | + return <RangeDateSelection {...props} />; |
| 44 | + }, |
| 45 | + argTypes: { |
| 46 | + minValue: { |
| 47 | + control: { |
| 48 | + type: 'text', |
| 49 | + }, |
| 50 | + }, |
| 51 | + maxValue: { |
| 52 | + control: { |
| 53 | + type: 'text', |
| 54 | + }, |
| 55 | + }, |
| 56 | + placeholderValue: { |
| 57 | + control: { |
| 58 | + type: 'text', |
| 59 | + }, |
| 60 | + }, |
| 61 | + timeZone: timeZoneControl, |
| 62 | + renderAdditionalRulerContent: { |
| 63 | + options: ['none', 'fill'], |
| 64 | + control: { |
| 65 | + type: 'radio', |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | +} satisfies Story; |
| 70 | + |
| 71 | +export const WithControls = { |
| 72 | + ...Default, |
| 73 | + render: function WithControls(args) { |
| 74 | + const timeZone = args.timeZone; |
| 75 | + const minValue = args.minValue ? dateTimeParse(args.minValue, {timeZone}) : undefined; |
| 76 | + const maxValue = args.maxValue ? dateTimeParse(args.maxValue, {timeZone}) : undefined; |
| 77 | + const placeholderValue = args.placeholderValue |
| 78 | + ? dateTimeParse(args.placeholderValue, {timeZone}) |
| 79 | + : undefined; |
| 80 | + |
| 81 | + const [value, setValue] = React.useState<RelativeRangeDatePickerValue>({ |
| 82 | + start: { |
| 83 | + type: 'relative', |
| 84 | + value: 'now - 1d', |
| 85 | + }, |
| 86 | + end: { |
| 87 | + type: 'relative', |
| 88 | + value: 'now', |
| 89 | + }, |
| 90 | + }); |
| 91 | + |
| 92 | + const {start, end} = toAbsoluteRange(value, timeZone); |
| 93 | + |
| 94 | + const [, rerender] = React.useState({}); |
| 95 | + React.useEffect(() => { |
| 96 | + const hasRelative = value.start?.type === 'relative' || value.end?.type === 'relative'; |
| 97 | + if (hasRelative) { |
| 98 | + const timer = setInterval(() => { |
| 99 | + rerender({}); |
| 100 | + }, 1000); |
| 101 | + return () => clearInterval(timer); |
| 102 | + } |
| 103 | + return undefined; |
| 104 | + }, [value]); |
| 105 | + |
| 106 | + return ( |
| 107 | + <div> |
| 108 | + <div |
| 109 | + style={{ |
| 110 | + display: 'flex', |
| 111 | + gap: '1rem', |
| 112 | + justifyContent: 'flex-end', |
| 113 | + paddingBlock: '1rem', |
| 114 | + }} |
| 115 | + > |
| 116 | + <RelativeRangeDatePicker |
| 117 | + style={{width: '20rem'}} |
| 118 | + value={value} |
| 119 | + onUpdate={(v) => { |
| 120 | + if (v) { |
| 121 | + setValue(v); |
| 122 | + } |
| 123 | + }} |
| 124 | + format="L LTS" |
| 125 | + withApplyButton |
| 126 | + withPresets |
| 127 | + minValue={minValue} |
| 128 | + maxValue={maxValue} |
| 129 | + placeholderValue={placeholderValue} |
| 130 | + /> |
| 131 | + <div style={{display: 'flex', gap: '2px'}}> |
| 132 | + <Button |
| 133 | + view="flat" |
| 134 | + onClick={() => setValue(getRelativeInterval('now - 30m', 'now'))} |
| 135 | + > |
| 136 | + 30m |
| 137 | + </Button> |
| 138 | + <Button |
| 139 | + view="flat" |
| 140 | + onClick={() => setValue(getRelativeInterval('now - 1h', 'now'))} |
| 141 | + > |
| 142 | + 1h |
| 143 | + </Button> |
| 144 | + <Button |
| 145 | + view="flat" |
| 146 | + onClick={() => setValue(getRelativeInterval('now - 1d', 'now'))} |
| 147 | + > |
| 148 | + 1d |
| 149 | + </Button> |
| 150 | + <Button |
| 151 | + view="flat" |
| 152 | + onClick={() => setValue(getRelativeInterval('now - 1w', 'now'))} |
| 153 | + > |
| 154 | + 1w |
| 155 | + </Button> |
| 156 | + </div> |
| 157 | + </div> |
| 158 | + <RangeDateSelection |
| 159 | + {...args} |
| 160 | + value={{start, end}} |
| 161 | + onUpdate={(value) => { |
| 162 | + setValue({ |
| 163 | + start: {type: 'absolute', value: value.start}, |
| 164 | + end: {type: 'absolute', value: value.end}, |
| 165 | + }); |
| 166 | + }} |
| 167 | + minValue={minValue} |
| 168 | + maxValue={maxValue} |
| 169 | + placeholderValue={placeholderValue} |
| 170 | + /> |
| 171 | + </div> |
| 172 | + ); |
| 173 | + }, |
| 174 | +} satisfies Story; |
| 175 | + |
| 176 | +function getRelativeInterval(start: string, end: string): RelativeRangeDatePickerValue { |
| 177 | + return { |
| 178 | + start: {type: 'relative', value: start}, |
| 179 | + end: {type: 'relative', value: end}, |
| 180 | + }; |
| 181 | +} |
| 182 | + |
| 183 | +function toAbsoluteRange(interval: RelativeRangeDatePickerValue, timeZone?: string) { |
| 184 | + const start: DateTime = |
| 185 | + interval.start?.type === 'relative' |
| 186 | + ? dateTimeParse(interval.start.value, {timeZone})! |
| 187 | + : interval.start!.value; |
| 188 | + |
| 189 | + const end: DateTime = |
| 190 | + interval.end?.type === 'relative' |
| 191 | + ? dateTimeParse(interval.end.value, {roundUp: true, timeZone})! |
| 192 | + : interval.end!.value; |
| 193 | + |
| 194 | + return {start, end}; |
| 195 | +} |
| 196 | + |
| 197 | +function renderAdditionalRulerContent(props: { |
| 198 | + interval: ViewportInterval; |
| 199 | + dimensions: ViewportDimensions; |
| 200 | +}) { |
| 201 | + const {width, height} = props.dimensions; |
| 202 | + return ( |
| 203 | + <React.Fragment> |
| 204 | + {Array.from({length: 12}, (_, i) => ( |
| 205 | + <path |
| 206 | + key={i} |
| 207 | + d={`M${(i * width) / 12},0l${width / 12},0l0,${height}l${-width / 12},0`} |
| 208 | + fill={`hsl(${i * 30}, 100%, 50%)`} |
| 209 | + fillOpacity={0.05} |
| 210 | + /> |
| 211 | + ))} |
| 212 | + </React.Fragment> |
| 213 | + ); |
| 214 | +} |
0 commit comments