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 = new Date(); const future = new Date(); future.setUTCDate(now.getUTCDate() + days); future.setUTCMinutes(now.getUTCMinutes() + minutes); future.setUTCHours(now.getUTCHours() + hours); return Math.round((future.getTime() - now.getTime()) / 1000); }, [days, hours, minutes]); useEffect(() => { if (days === 7) { setHours(0); setMinutes(0); } }, [days]); useEffect(() => { onDurationChange(value); }, [value]); return (
); }; export default DurationSelector;