All files / src/components/RangeDateSelection/hooks useRangeDateSelectionState.ts

33.73% Statements 28/83
44% Branches 22/50
20% Functions 2/10
33.73% Lines 28/83

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 2591x                                 2x 2x                                                                                                                 3x     3x   3x         3x 3x   3x                 3x               3x       3x   3x             3x     3x       3x 3x 3x         3x   3x 3x       3x         3x             3x                                   3x                                     3x                                         3x                                                               3x                                  
import React from 'react';
 
import type {DateTime} from '@gravity-ui/date-utils';
import {useControlledState} from '@gravity-ui/uikit';
 
import type {RangeValue, ValueBase} from '../../types';
import {SECOND, YEAR} from '../../utils/constants';
import {constrainValue, createPlaceholderValue} from '../../utils/dates';
import {useDefaultTimeZone} from '../../utils/useDefaultTimeZone';
import {alignDateTime} from '../utils/date';
 
function getViewportIntervalFromDates(
    start: DateTime,
    end: DateTime,
    numberOfIntervals = 4,
    placeInViewport = 0.5,
) {
    const diff = end.diff(start);
    return {
        start: start.subtract(diff * (numberOfIntervals - 1) * placeInViewport),
        end: end.add(diff * (numberOfIntervals - 1) * (1 - placeInViewport)),
    };
}
 
export interface RangeDateSelectionState {
    viewportInterval: RangeValue<DateTime>;
    value: RangeValue<DateTime>;
    setValue: (value: RangeValue<DateTime>) => void;
    timeZone: string;
    setDraggingValue: (value: RangeValue<DateTime> | null) => void;
    canResize: boolean;
    startDragging: () => void;
    move: (delta: number, options?: {visualMinDuration?: number}) => void;
    scale: (scale: number, options?: {visualMinDuration?: number; fixedPoint?: number}) => void;
    moveStart: (delta: number, options?: {visualMinDuration?: number}) => void;
    moveEnd: (delta: number, options?: {visualMinDuration?: number}) => void;
    endDragging: () => void;
    align: number;
    isDragging: boolean;
}
 
export interface RangeDateSelectionOptions extends ValueBase<RangeValue<DateTime>> {
    /** The minimum allowed date that a user may select. */
    minValue?: DateTime;
    /** The maximum allowed date that a user may select. */
    maxValue?: DateTime;
    /** Minimum duration of the selection in milliseconds. Defaults to 1 second. */
    minDuration?: number;
    /** Maximum duration of the selection in milliseconds. Defaults to 15 years. */
    maxDuration?: number;
    /** Alignment of the selection in milliseconds. Defaults to 1 second. */
    align?: number;
    /** A placeholder date that controls the default values of each segment when the user first interacts with them. Defaults to today's date at midnight. */
    placeholderValue?: DateTime;
    /**
     * Which timezone use to show values. Example: 'default', 'system', 'Europe/Amsterdam'.
     * @default The timezone of the `value` or `defaultValue` or `placeholderValue`, 'default' otherwise.
     */
    timeZone?: string;
    /**
     * The number of selection intervals that fit into the underlying ruler.
     * @default 4
     */
    numberOfIntervals?: number;
    /**
     * Place of the selection on the underlying ruler.
     * Value must be between 0 and 1, where 0 is the start of the ruler and 1 is the end.
     * @default 0.5 - center of the ruler
     */
    placeOnRuler?: number;
}
 
export function useRangeDateSelectionState(
    props: RangeDateSelectionOptions,
): RangeDateSelectionState {
    const inputTimeZone = useDefaultTimeZone(
        props.value?.start || props.defaultValue?.start || props.placeholderValue,
    );
    const timeZone = props.timeZone || inputTimeZone;
 
    const placeholderValue = createPlaceholderValue({
        placeholderValue: props.placeholderValue,
        timeZone,
    });
 
    const [isDragging, setIsDragging] = React.useState(false);
    const [currentValue, setCurrentValue] = React.useState<RangeValue<DateTime> | null>(null);
 
    const [value, setValue] = useControlledState(
        props.value,
        props.defaultValue ?? {
            start: placeholderValue.startOf('day').timeZone(inputTimeZone),
            end: placeholderValue.endOf('day').timeZone(inputTimeZone),
        },
        props.onUpdate,
    );
 
    const setInterval = (newValue: RangeValue<DateTime>) => {
        setCurrentValue(null);
        setValue({
            start: alignDateTime(newValue.start, props.align).timeZone(inputTimeZone),
            end: alignDateTime(newValue.end, props.align).timeZone(inputTimeZone),
        });
    };
 
    const displayValue = {
        start: value.start.timeZone(timeZone),
        end: value.end.timeZone(timeZone),
    };
    const interval = isDragging ? displayValue : (currentValue ?? displayValue);
 
    const viewportInterval = getViewportIntervalFromDates(
        interval.start,
        interval.end,
        props.numberOfIntervals,
        props.placeOnRuler,
    );
 
    const minValue = props.minValue?.isAfter(viewportInterval.start)
        ? props.minValue
        : viewportInterval.start;
    const maxValue = props.maxValue?.isBefore(viewportInterval.end)
        ? props.maxValue
        : viewportInterval.end;
 
    const align = props.align ?? SECOND;
    const minDuration = Math.max(props.minDuration ?? SECOND, align);
    const maxDuration = Math.max(
        Math.min(props.maxDuration ?? 15 * YEAR, maxValue.diff(minValue)),
        minDuration,
    );
 
    const canResize = minDuration < maxDuration;
 
    const draggingState = React.useRef<RangeValue<DateTime> | null>(null);
    const setDraggingValue = (v: RangeValue<DateTime> | null) => {
        draggingState.current = v;
        setCurrentValue(v);
    };
    const startDragging = () => {
        setIsDragging(true);
        setDraggingValue(displayValue);
    };
 
    const endDragging = () => {
        setIsDragging(false);
        if (draggingState.current) {
            setInterval(draggingState.current);
        }
    };
 
    const moveInterval = (delta: number) => {
        if (!draggingState.current) {
            return;
        }
        const {start, end} = draggingState.current;
        let newStart = start.add(delta, 'millisecond');
        let newEnd = end.add(delta, 'millisecond');
        const diff = newEnd.diff(newStart);
        if (newStart.valueOf() < minValue.valueOf()) {
            newStart = minValue;
            newEnd = constrainValue(newStart.add(diff, 'millisecond'), newStart, maxValue);
        } else if (newEnd.valueOf() > maxValue.valueOf()) {
            newEnd = maxValue;
            newStart = constrainValue(newEnd.subtract(diff, 'millisecond'), minValue, newEnd);
        }
        setDraggingValue({start: newStart, end: newEnd});
    };
 
    const moveStart = (
        delta: number,
        {visualMinDuration = minDuration}: {visualMinDuration?: number} = {},
    ) => {
        if (!draggingState.current) {
            return;
        }
        const {start, end} = draggingState.current;
        let newStart = start.add(delta, 'millisecond');
        const deltaMin = Math.max(visualMinDuration, minDuration);
        const deltaMax = Math.min(maxDuration, end.diff(minValue));
        newStart = constrainValue(
            newStart,
            end.subtract(deltaMax, 'millisecond'),
            end.subtract(deltaMin, 'millisecond'),
        );
        setDraggingValue({start: newStart, end});
    };
 
    const moveEnd = (
        delta: number,
        {visualMinDuration = minDuration}: {visualMinDuration?: number} = {},
    ) => {
        if (!draggingState.current) {
            return;
        }
        const {start, end} = draggingState.current;
        let newEnd = end.add(delta, 'millisecond');
        const deltaMin = Math.max(visualMinDuration, minDuration);
        const deltaMax = Math.min(maxDuration, maxValue.diff(start));
 
        newEnd = constrainValue(
            end.add(delta, 'millisecond'),
            start.add(deltaMin, 'millisecond'),
            start.add(deltaMax, 'millisecond'),
        );
 
        setDraggingValue({start, end: newEnd});
    };
 
    const zoomInterval = (
        scale: number,
        {
            visualMinDuration = minDuration,
            fixedPoint = 0.5,
        }: {visualMinDuration?: number; fixedPoint?: number} = {},
    ) => {
        if (!draggingState.current) {
            return;
        }
        const {start, end} = draggingState.current;
        const len = end.diff(start);
        let newDiff = len * scale;
        newDiff = align * Math.round(newDiff / align);
        newDiff = Math.min(
            Math.max(newDiff, Math.max(visualMinDuration, minDuration)),
            maxDuration,
        );
        let newStart = start.subtract(fixedPoint * (newDiff - len));
        let newEnd = newStart.add(newDiff, 'millisecond');
        const diff = newEnd.diff(newStart);
        if (newStart.valueOf() < minValue.valueOf()) {
            newStart = minValue;
            newEnd = constrainValue(newStart.add(diff, 'millisecond'), newStart, maxValue);
        } else if (newEnd.valueOf() > maxValue.valueOf()) {
            newEnd = maxValue;
            newStart = constrainValue(newEnd.subtract(diff, 'millisecond'), minValue, newEnd);
        }
 
        setDraggingValue({start: newStart, end: newEnd});
    };
 
    return {
        viewportInterval,
        value: currentValue ?? displayValue,
        setValue: setInterval,
        setDraggingValue,
        canResize,
        startDragging,
        move: moveInterval,
        scale: zoomInterval,
        moveStart,
        moveEnd,
        endDragging,
        timeZone,
        align,
        isDragging,
    };
}