import React, { useEffect, useMemo, useState } from 'react'; import { defineMessages, useIntl } from 'react-intl'; import { Select } from 'soapbox/components/ui'; const messages = defineMessages({ minutes: { id: 'intervals.full.minutes', defaultMessage: '{number, plural, one {# minute} other {# minutes}}' }, hours: { id: 'intervals.full.hours', defaultMessage: '{number, plural, one {# hour} other {# hours}}' }, days: { id: 'intervals.full.days', defaultMessage: '{number, plural, one {# day} other {# days}}' }, }); interface IDurationSelector { onDurationChange(expiresIn: number): void } const DurationSelector = ({ onDurationChange }: IDurationSelector) => { const intl = useIntl(); const [days, setDays] = useState(2); const [hours, setHours] = useState(0); const [minutes, setMinutes] = useState(0); const value = useMemo(() => { const now: any = new Date(); const future: any = new Date(); now.setDate(now.getDate() + days); now.setMinutes(now.getMinutes() + minutes); now.setHours(now.getHours() + hours); return Math.round((now - future) / 1000); }, [days, hours, minutes]); useEffect(() => { if (days === 7) { setHours(0); setMinutes(0); } }, [days]); useEffect(() => { onDurationChange(value); }, [value]); return (
); }; export default DurationSelector;