1 import { useState } from 'react';
3 import { fromUnixTime, getDate, getHours, getMinutes, getMonth, getUnixTime, getYear } from 'date-fns';
4 import { c } from 'ttag';
6 import type { InputProps } from '@proton/atoms';
7 import { DateInputTwo, TimeInput } from '@proton/components';
9 interface Props extends Omit<InputProps, 'min' | 'max' | 'value' | 'onChange'> {
10 expiration: number | null;
11 handleExpirationChange: (exp: number) => void;
12 clearButton?: boolean;
17 const getMaxDate = () => {
18 const date = new Date();
19 date.setDate(date.getDate() + 90);
23 export const ExpirationTimeDatePicker = ({
25 handleExpirationChange,
30 const initialValue = expiration ? fromUnixTime(expiration) : undefined;
31 const [expDate, setExpDate] = useState<Date | undefined>(initialValue);
32 const [expTime, setExpTime] = useState<Date | undefined>(initialValue);
34 const MIN_DATE = new Date();
35 const date = getMaxDate();
36 const MAX_DATE = date;
38 const [maxTime, setMaxTime] = useState<Date | undefined>(expTime ? date : undefined);
40 const handleChangeDate = (value: Date | undefined) => {
44 const year = getYear(value);
45 const month = getMonth(value);
46 const day = getDate(value);
52 if (year === getYear(MAX_DATE) && month === getMonth(MAX_DATE) && day === getDate(MAX_DATE)) {
53 const date = getMaxDate();
57 setMaxTime(undefined);
62 tempDate = fromUnixTime(expiration);
63 tempDate.setFullYear(year);
64 tempDate.setMonth(month);
65 tempDate.setDate(day);
67 handleExpirationChange(getUnixTime(tempDate));
71 const handleChangeTime = (value: Date) => {
74 const hours = getHours(value);
75 const minutes = getMinutes(value);
78 const tempDate = fromUnixTime(expiration);
79 tempDate.setHours(hours);
80 tempDate.setMinutes(minutes);
81 handleExpirationChange(getUnixTime(tempDate));
88 id="expirationDateInputId"
89 className="flex-1 grow-2"
92 onChange={handleChangeDate}
93 displayWeekNumbers={false}
96 placeholder={c('Title').t`Date`}
97 title={c('Title').t`Select link expiration date`}
99 data-testid="expirationDateInputId"
102 {allowTime && expTime && (
104 id="epirationTimeInputId"
105 className="ml-2 flex-1"
108 onChange={handleChangeTime}
110 title={c('Title').t`Select link expiration time`}
111 data-testid="expirationDateInputId"