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

145 lines
4.2 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2021 Signal Messenger, LLC
2021-08-05 12:35:33 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
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';
2022-05-23 18:37:53 +00:00
import { isConversationMuted } from '../../../util/isConversationMuted';
2021-08-05 12:35:33 +00:00
import { getMuteOptions } from '../../../util/getMuteOptions';
import { parseIntOrThrow } from '../../../util/parseIntOrThrow';
import { useUniqueId } from '../../../hooks/useUniqueId';
2021-08-05 12:35:33 +00:00
type PropsType = {
id: string;
2021-08-05 12:35:33 +00:00
conversationType: ConversationTypeType;
dontNotifyForMentionsIfMuted: boolean;
i18n: LocalizerType;
muteExpiresAt: undefined | number;
setDontNotifyForMentionsIfMuted: (
conversationId: string,
2021-08-05 12:35:33 +00:00
dontNotifyForMentionsIfMuted: boolean
) => unknown;
setMuteExpiration: (
conversationId: string,
muteExpiresAt: undefined | number
) => unknown;
2021-08-05 12:35:33 +00:00
};
2022-11-18 00:45:19 +00:00
export function ConversationNotificationsSettings({
id,
2022-03-22 20:45:34 +00:00
conversationType,
dontNotifyForMentionsIfMuted,
i18n,
muteExpiresAt,
setMuteExpiration,
setDontNotifyForMentionsIfMuted,
2022-11-18 00:45:19 +00:00
}: PropsType): JSX.Element {
const muteNotificationsSelectId = useUniqueId();
const mentionsSelectId = useUniqueId();
2022-03-22 20:45:34 +00:00
const muteOptions = useMemo(
() => [
2022-05-23 18:37:53 +00:00
...(isConversationMuted({ muteExpiresAt })
2022-03-22 20:45:34 +00:00
? []
: [
{
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
2022-03-22 20:45:34 +00:00
const onMuteChange = (rawValue: string) => {
const ms = parseIntOrThrow(
rawValue,
'NotificationSettings: mute ms was not an integer'
);
setMuteExpiration(id, ms);
2022-03-22 20:45:34 +00:00
};
2021-08-05 12:35:33 +00:00
2022-03-22 20:45:34 +00:00
const onChangeDontNotifyForMentionsIfMuted = (rawValue: string) => {
setDontNotifyForMentionsIfMuted(id, rawValue === 'yes');
2022-03-22 20:45:34 +00:00
};
2021-11-11 22:43:05 +00:00
2022-03-22 20:45:34 +00:00
return (
<div className="conversation-details-panel">
<PanelSection>
<PanelRow
icon={
<ConversationDetailsIcon
ariaLabel={i18n('muteNotificationsTitle')}
icon={IconType.mute}
/>
}
label={
<label htmlFor={muteNotificationsSelectId}>
{i18n('muteNotificationsTitle')}
</label>
}
2022-03-22 20:45:34 +00:00
right={
<Select
id={muteNotificationsSelectId}
options={muteOptions}
onChange={onMuteChange}
value={-1}
/>
2022-03-22 20:45:34 +00:00
}
/>
{conversationType === 'group' && (
2021-08-05 12:35:33 +00:00
<PanelRow
icon={
<ConversationDetailsIcon
2022-03-22 20:45:34 +00:00
ariaLabel={i18n(
'ConversationNotificationsSettings__mentions__label'
)}
icon={IconType.mention}
2021-08-05 12:35:33 +00:00
/>
}
label={
<label htmlFor={mentionsSelectId}>
{i18n('ConversationNotificationsSettings__mentions__label')}
</label>
}
2022-03-22 20:45:34 +00:00
info={i18n('ConversationNotificationsSettings__mentions__info')}
2021-08-05 12:35:33 +00:00
right={
<Select
id={mentionsSelectId}
2022-03-22 20:45:34 +00:00
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'}
2021-08-05 12:35:33 +00:00
/>
}
/>
2022-03-22 20:45:34 +00:00
)}
</PanelSection>
</div>
);
2022-11-18 00:45:19 +00:00
}