You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NotificationsSection.tsx 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import React, { useCallback, useMemo } from 'react';
  2. import { Divider } from 'react-native-paper';
  3. import { useDispatch, useSelector } from 'react-redux';
  4. import { IReduxState } from '../../../app/types';
  5. import { updateSettings } from '../../../base/settings/actions';
  6. import Switch from '../../../base/ui/components/native/Switch';
  7. import { getNotificationsTabProps } from '../../functions.any';
  8. import FormRow from './FormRow';
  9. import FormSection from './FormSection';
  10. import styles from './styles';
  11. const NotificationsSection = () => {
  12. const dispatch = useDispatch();
  13. const {
  14. soundsIncomingMessage,
  15. soundsParticipantJoined,
  16. soundsParticipantKnocking,
  17. soundsParticipantLeft,
  18. soundsReactions,
  19. soundsTalkWhileMuted,
  20. enableReactions,
  21. enabledNotifications,
  22. disabledSounds,
  23. moderatorMutedSoundsReactions
  24. } = useSelector((state: IReduxState) => getNotificationsTabProps(state));
  25. const sounds = useMemo(() => {
  26. const partialSounds = [
  27. {
  28. label: 'settings.reactions',
  29. state: soundsReactions,
  30. name: 'soundsReactions',
  31. disabled: Boolean(moderatorMutedSoundsReactions
  32. || disabledSounds.includes('REACTION_SOUND'))
  33. },
  34. {
  35. label: 'settings.incomingMessage',
  36. state: soundsIncomingMessage,
  37. name: 'soundsIncomingMessage'
  38. },
  39. {
  40. label: 'settings.participantJoined',
  41. state: soundsParticipantJoined,
  42. name: 'soundsParticipantJoined'
  43. },
  44. {
  45. label: 'settings.participantLeft',
  46. state: soundsParticipantLeft,
  47. name: 'soundsParticipantLeft'
  48. },
  49. {
  50. label: 'settings.talkWhileMuted',
  51. state: soundsTalkWhileMuted,
  52. name: 'soundsTalkWhileMuted'
  53. },
  54. {
  55. label: 'settings.participantKnocking',
  56. state: soundsParticipantKnocking,
  57. name: 'soundsParticipantKnocking'
  58. }
  59. ];
  60. if (!enableReactions) {
  61. partialSounds.shift();
  62. }
  63. return partialSounds;
  64. }, [ soundsReactions,
  65. soundsIncomingMessage,
  66. soundsParticipantJoined,
  67. soundsParticipantLeft,
  68. soundsTalkWhileMuted,
  69. soundsParticipantKnocking,
  70. enableReactions ]);
  71. const onSoundToggled = useCallback((name: string) => (enabled?: boolean) => {
  72. dispatch(updateSettings({ [name]: enabled }));
  73. }, [ dispatch, updateSettings ]);
  74. const onNotificationToggled = useCallback((name: string) => (enabled?: boolean) => {
  75. dispatch(updateSettings({
  76. userSelectedNotifications: {
  77. ...enabledNotifications,
  78. [name]: Boolean(enabled)
  79. }
  80. }
  81. )
  82. );
  83. }, [ dispatch, updateSettings, enabledNotifications ]);
  84. return (
  85. <>
  86. <FormSection
  87. label = 'settings.playSounds'>
  88. {
  89. sounds.map(({ label, state, name, disabled }) => (
  90. <FormRow
  91. key = { label }
  92. label = { label }>
  93. <Switch
  94. checked = { Boolean(state) }
  95. disabled = { disabled }
  96. onChange = { onSoundToggled(name) } />
  97. </FormRow>
  98. ))
  99. }
  100. </FormSection>
  101. {
  102. Object.keys(enabledNotifications).length > 0 && (
  103. <>
  104. {/* @ts-ignore */}
  105. <Divider style = { styles.fieldSeparator } />
  106. <FormSection
  107. label = 'notify.displayNotifications'>
  108. {
  109. Object.keys(enabledNotifications).map(name => (
  110. <FormRow
  111. key = { name }
  112. label = { name }>
  113. <Switch
  114. checked = { Boolean(enabledNotifications[name]) }
  115. onChange = { onNotificationToggled(name) } />
  116. </FormRow>)
  117. )
  118. }
  119. </FormSection>
  120. </>
  121. )
  122. }
  123. </>
  124. );
  125. };
  126. export default NotificationsSection;