2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
2021-10-20 23:46:41 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import React, { useMemo, useState } from 'react';
|
2023-02-23 21:32:19 +00:00
|
|
|
import uuid from 'uuid';
|
2021-10-20 23:46:41 +00:00
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { LocalizerType } from '../../../types/Util';
|
2021-10-20 23:46:41 +00:00
|
|
|
import { getMuteOptions } from '../../../util/getMuteOptions';
|
|
|
|
import { parseIntOrThrow } from '../../../util/parseIntOrThrow';
|
2023-02-23 21:32:19 +00:00
|
|
|
import { CircleCheckbox, Variant } from '../../CircleCheckbox';
|
2021-10-20 23:46:41 +00:00
|
|
|
import { Modal } from '../../Modal';
|
|
|
|
import { Button, ButtonVariant } from '../../Button';
|
|
|
|
|
|
|
|
type PropsType = {
|
|
|
|
i18n: LocalizerType;
|
2022-12-06 17:31:44 +00:00
|
|
|
id: string;
|
2021-10-20 23:46:41 +00:00
|
|
|
muteExpiresAt: undefined | number;
|
|
|
|
onClose: () => unknown;
|
2022-12-06 17:31:44 +00:00
|
|
|
setMuteExpiration: (
|
|
|
|
conversationId: string,
|
|
|
|
muteExpiresAt: undefined | number
|
|
|
|
) => unknown;
|
2021-10-20 23:46:41 +00:00
|
|
|
};
|
|
|
|
|
2022-11-18 00:45:19 +00:00
|
|
|
export function ConversationNotificationsModal({
|
2021-10-20 23:46:41 +00:00
|
|
|
i18n,
|
2022-12-06 17:31:44 +00:00
|
|
|
id,
|
2021-10-20 23:46:41 +00:00
|
|
|
muteExpiresAt,
|
|
|
|
onClose,
|
|
|
|
setMuteExpiration,
|
2022-11-18 00:45:19 +00:00
|
|
|
}: PropsType): JSX.Element {
|
2021-10-20 23:46:41 +00:00
|
|
|
const muteOptions = useMemo(
|
|
|
|
() =>
|
2023-02-23 21:32:19 +00:00
|
|
|
getMuteOptions(muteExpiresAt, i18n)
|
|
|
|
.map(({ disabled, name, value }) => ({
|
|
|
|
disabled,
|
|
|
|
text: name,
|
|
|
|
value,
|
|
|
|
}))
|
|
|
|
.filter(x => x.value > 0),
|
2021-10-20 23:46:41 +00:00
|
|
|
[i18n, muteExpiresAt]
|
|
|
|
);
|
|
|
|
|
|
|
|
const [muteExpirationValue, setMuteExpirationValue] = useState(muteExpiresAt);
|
|
|
|
|
|
|
|
const onMuteChange = () => {
|
|
|
|
const ms = parseIntOrThrow(
|
|
|
|
muteExpirationValue,
|
|
|
|
'NotificationSettings: mute ms was not an integer'
|
|
|
|
);
|
2022-12-06 17:31:44 +00:00
|
|
|
setMuteExpiration(id, ms);
|
2021-10-20 23:46:41 +00:00
|
|
|
onClose();
|
|
|
|
};
|
|
|
|
|
2023-02-23 21:32:19 +00:00
|
|
|
const htmlIds = useMemo(() => {
|
|
|
|
return Array.from({ length: muteOptions.length }, () => uuid());
|
|
|
|
}, [muteOptions.length]);
|
|
|
|
|
2021-10-20 23:46:41 +00:00
|
|
|
return (
|
|
|
|
<Modal
|
2022-09-27 20:24:21 +00:00
|
|
|
modalName="ConversationNotificationsModal"
|
2021-10-20 23:46:41 +00:00
|
|
|
hasXButton
|
|
|
|
onClose={onClose}
|
|
|
|
i18n={i18n}
|
2023-03-30 00:03:25 +00:00
|
|
|
title={i18n('icu:muteNotificationsTitle')}
|
2022-09-29 22:40:09 +00:00
|
|
|
modalFooter={
|
|
|
|
<>
|
|
|
|
<Button onClick={onClose} variant={ButtonVariant.Secondary}>
|
2023-03-30 00:03:25 +00:00
|
|
|
{i18n('icu:cancel')}
|
2022-09-29 22:40:09 +00:00
|
|
|
</Button>
|
|
|
|
<Button onClick={onMuteChange} variant={ButtonVariant.Primary}>
|
2023-03-30 00:03:25 +00:00
|
|
|
{i18n('icu:mute')}
|
2022-09-29 22:40:09 +00:00
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
}
|
2021-10-20 23:46:41 +00:00
|
|
|
>
|
2023-02-23 21:32:19 +00:00
|
|
|
{muteOptions.map((option, i) => (
|
|
|
|
<label
|
|
|
|
className="Preferences__settings-radio__label"
|
|
|
|
key={htmlIds[i]}
|
|
|
|
htmlFor={htmlIds[i]}
|
|
|
|
>
|
|
|
|
<CircleCheckbox
|
|
|
|
id={htmlIds[i]}
|
2021-10-20 23:46:41 +00:00
|
|
|
checked={muteExpirationValue === option.value}
|
2023-02-23 21:32:19 +00:00
|
|
|
variant={Variant.Small}
|
2021-10-20 23:46:41 +00:00
|
|
|
disabled={option.disabled}
|
|
|
|
isRadio
|
|
|
|
moduleClassName="ConversationDetails__radio"
|
|
|
|
name="mute"
|
|
|
|
onChange={value => value && setMuteExpirationValue(option.value)}
|
|
|
|
/>
|
2023-02-23 21:32:19 +00:00
|
|
|
{option.text}
|
|
|
|
</label>
|
|
|
|
))}
|
2021-10-20 23:46:41 +00:00
|
|
|
</Modal>
|
|
|
|
);
|
2022-11-18 00:45:19 +00:00
|
|
|
}
|