signal-desktop/ts/components/conversation/conversation-details/ConversationNotificationsSettings.tsx

128 lines
3.9 KiB
TypeScript
Raw Normal View History

2021-08-05 12:35:33 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { FunctionComponent } from 'react';
import React, { useMemo } from 'react';
2021-08-05 12:35:33 +00:00
import type { ConversationTypeType } from '../../../state/ducks/conversations';
import type { LocalizerType } from '../../../types/Util';
2021-08-05 12:35:33 +00:00
import { PanelSection } from './PanelSection';
import { PanelRow } from './PanelRow';
import { ConversationDetailsIcon, IconType } from './ConversationDetailsIcon';
2021-08-05 12:35:33 +00:00
import { Select } from '../../Select';
import { isMuted } from '../../../util/isMuted';
import { getMuteOptions } from '../../../util/getMuteOptions';
import { parseIntOrThrow } from '../../../util/parseIntOrThrow';
type PropsType = {
conversationType: ConversationTypeType;
dontNotifyForMentionsIfMuted: boolean;
i18n: LocalizerType;
muteExpiresAt: undefined | number;
setDontNotifyForMentionsIfMuted: (
dontNotifyForMentionsIfMuted: boolean
) => unknown;
setMuteExpiration: (muteExpiresAt: undefined | number) => unknown;
};
2021-11-11 22:43:05 +00:00
export const ConversationNotificationsSettings: FunctionComponent<PropsType> =
({
conversationType,
dontNotifyForMentionsIfMuted,
i18n,
muteExpiresAt,
setMuteExpiration,
setDontNotifyForMentionsIfMuted,
}) => {
const muteOptions = useMemo(
() => [
...(isMuted(muteExpiresAt)
? []
: [
{
disabled: true,
text: i18n('notMuted'),
value: -1,
},
]),
...getMuteOptions(muteExpiresAt, i18n).map(
({ disabled, name, value }) => ({
disabled,
text: name,
value,
})
),
],
[i18n, muteExpiresAt]
2021-08-05 12:35:33 +00:00
);
2021-11-11 22:43:05 +00:00
const onMuteChange = (rawValue: string) => {
const ms = parseIntOrThrow(
rawValue,
'NotificationSettings: mute ms was not an integer'
);
setMuteExpiration(ms);
};
2021-08-05 12:35:33 +00:00
2021-11-11 22:43:05 +00:00
const onChangeDontNotifyForMentionsIfMuted = (rawValue: string) => {
setDontNotifyForMentionsIfMuted(rawValue === 'yes');
};
return (
<div className="conversation-details-panel">
<PanelSection>
2021-08-05 12:35:33 +00:00
<PanelRow
icon={
<ConversationDetailsIcon
2021-11-11 22:43:05 +00:00
ariaLabel={i18n('muteNotificationsTitle')}
icon={IconType.mute}
2021-08-05 12:35:33 +00:00
/>
}
2021-11-11 22:43:05 +00:00
label={i18n('muteNotificationsTitle')}
2021-08-05 12:35:33 +00:00
right={
<Select
2021-11-11 22:43:05 +00:00
options={muteOptions}
onChange={onMuteChange}
value={-1}
2021-08-05 12:35:33 +00:00
/>
}
/>
2021-11-11 22:43:05 +00:00
{conversationType === 'group' && (
<PanelRow
icon={
<ConversationDetailsIcon
ariaLabel={i18n(
'ConversationNotificationsSettings__mentions__label'
)}
icon={IconType.mention}
/>
}
label={i18n('ConversationNotificationsSettings__mentions__label')}
info={i18n('ConversationNotificationsSettings__mentions__info')}
right={
<Select
options={[
{
text: i18n(
'ConversationNotificationsSettings__mentions__select__always-notify'
),
value: 'no',
},
{
text: i18n(
'ConversationNotificationsSettings__mentions__select__dont-notify-for-mentions-if-muted'
),
value: 'yes',
},
]}
onChange={onChangeDontNotifyForMentionsIfMuted}
value={dontNotifyForMentionsIfMuted ? 'yes' : 'no'}
/>
}
/>
)}
</PanelSection>
</div>
);
};