chore: make dataTestIds for timer options human readable

pull/3281/head
Audric Ackermann 6 months ago
parent c83bed4a30
commit cd49729779
No known key found for this signature in database

@ -1,10 +1,14 @@
import { isEmpty } from 'lodash';
import { DisappearTimeOptionDataTestId } from 'react';
import { TimerOptionsArray } from '../../../../../session/disappearing_messages/timerOptions';
import {
TimerOptionsArray,
TimerSeconds,
} from '../../../../../session/disappearing_messages/timerOptions';
import { PanelButtonGroup, PanelLabel } from '../../../../buttons/PanelButton';
import { PanelRadioButton } from '../../../../buttons/PanelRadioButton';
import { Localizer } from '../../../../basic/Localizer';
import { assertUnreachable } from '../../../../../types/sqlSharedTypes';
type TimerOptionsProps = {
options: TimerOptionsArray | null;
@ -14,6 +18,56 @@ type TimerOptionsProps = {
disabled?: boolean;
};
function toMinutes(seconds: Extract<TimerSeconds, 300 | 1800>) {
const ret = Math.floor(seconds / 60);
if (ret !== 5 && ret !== 30) {
throw new Error('invalid toMinutes');
}
return ret;
}
function toHours(seconds: Extract<TimerSeconds, 3600 | 21600 | 43200>) {
const ret = Math.floor(seconds / 3600);
if (ret !== 1 && ret !== 6 && ret !== 12) {
throw new Error('invalid toHours');
}
return ret;
}
function toDays(seconds: Extract<TimerSeconds, 86400 | 604800 | 1209600>) {
const ret = Math.floor(seconds / 86400);
if (ret !== 1 && ret !== 7 && ret !== 14) {
throw new Error('invalid toDays');
}
return ret;
}
function getDataTestIdFromTimerSeconds(seconds: TimerSeconds): DisappearTimeOptionDataTestId {
switch (seconds) {
case 0:
case 5:
case 10:
case 30:
case 60:
return `time-option-${seconds}-seconds`;
case 300:
case 1800:
return `time-option-${toMinutes(seconds)}-minutes`;
case 3600:
case 21600:
case 43200:
return `time-option-${toHours(seconds)}-hours`;
case 86400:
case 604800:
case 1209600:
return `time-option-${toDays(seconds)}-days`;
default:
assertUnreachable(seconds, 'getDataTestIdFromTimerSeconds: unhandled case');
// tsc is a bit dumb sometimes and expects a return here
throw new Error('getDataTestIdFromTimerSeconds: unhandled case');
}
}
export const TimeOptions = (props: TimerOptionsProps) => {
const { options, selected, setSelected, hasOnlyOneMode, disabled } = props;
@ -30,8 +84,9 @@ export const TimeOptions = (props: TimerOptionsProps) => {
)}
<PanelButtonGroup>
{options.map(option => {
// we want "time-option-3600-seconds", etc as accessibility id
const parentDataTestId: DisappearTimeOptionDataTestId = `time-option-${option.value}-seconds`;
// we want "time-option-1-hours", etc as accessibility id
const parentDataTestId = getDataTestIdFromTimerSeconds(option.value);
return (
<PanelRadioButton
key={option.name}

16
ts/react.d.ts vendored

@ -18,14 +18,14 @@ declare module 'react' {
| 'time-option-10-seconds'
| 'time-option-30-seconds'
| 'time-option-60-seconds'
| 'time-option-300-seconds'
| 'time-option-1800-seconds'
| 'time-option-3600-seconds'
| 'time-option-21600-seconds'
| 'time-option-43200-seconds'
| 'time-option-86400-seconds'
| 'time-option-604800-seconds'
| 'time-option-1209600-seconds';
| 'time-option-5-minutes'
| 'time-option-30-minutes'
| 'time-option-1-hours'
| 'time-option-6-hours'
| 'time-option-12-hours'
| 'time-option-1-days'
| 'time-option-7-days'
| 'time-option-14-days';
type SessionDataTestId =
| 'group-member-status-text'
| 'loading-spinner'

@ -4,7 +4,7 @@ import {
formatNonAbbreviatedExpireTimer,
} from '../../util/i18n/formatting/expirationTimer';
type TimerSeconds =
export type TimerSeconds =
| 0
| 5
| 10

Loading…
Cancel
Save