// Copyright 2021 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only /* eslint-disable no-restricted-syntax */ import React, { useState } from 'react'; import { ConfirmationDialog } from '../ConfirmationDialog'; import { Select } from '../Select'; import { LocalizerType } from '../../types/Util'; import { Theme } from '../../util/theme'; const CSS_MODULE = 'module-disappearing-time-dialog'; const DEFAULT_VALUE = 60; export type PropsType = Readonly<{ i18n: LocalizerType; theme?: Theme; initialValue?: number; onSubmit: (value: number) => void; onClose: () => void; }>; const UNITS = ['seconds', 'minutes', 'hours', 'days', 'weeks']; const UNIT_TO_MS = new Map([ ['seconds', 1], ['minutes', 60], ['hours', 60 * 60], ['days', 24 * 60 * 60], ['weeks', 7 * 24 * 60 * 60], ]); const RANGES = new Map([ ['seconds', [1, 60]], ['minutes', [1, 60]], ['hours', [1, 24]], ['days', [1, 7]], ['weeks', [1, 5]], ]); export function DisappearingTimeDialog(props: PropsType): JSX.Element { const { i18n, theme, initialValue = DEFAULT_VALUE, onSubmit, onClose, } = props; let initialUnit = 'seconds'; let initialUnitValue = 1; for (const unit of UNITS) { const ms = UNIT_TO_MS.get(unit) || 1; if (initialValue < ms) { break; } initialUnit = unit; initialUnitValue = Math.floor(initialValue / ms); } const [unitValue, setUnitValue] = useState(initialUnitValue); const [unit, setUnit] = useState(initialUnit); const range = RANGES.get(unit) || [1, 1]; const values: Array = []; for (let i = range[0]; i < range[1]; i += 1) { values.push(i); } return (

{i18n('DisappearingTimeDialog__body')}

{ setUnit(newUnit); const ranges = RANGES.get(newUnit); if (!ranges) { return; } const [min, max] = ranges; setUnitValue(Math.max(min, Math.min(max - 1, unitValue))); }} options={UNITS.map(unitName => { return { value: unitName, text: i18n(`DisappearingTimeDialog__${unitName}`), }; })} />
); }