use-project-notification-preferences.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { useCallback, useEffect, useState } from 'react'
  2. import { useProjectContext } from '@/shared/context/project-context'
  3. import { getJSON, postJSON } from '@/infrastructure/fetch-json'
  4. import { debugConsole } from '@/utils/debugging'
  5. import type {
  6. GlobalNotificationPreferencesSchema,
  7. NotificationPreferencesSchema,
  8. } from '../../../../../modules/notifications/app/src/types.js'
  9. import { sendMB } from '@/infrastructure/event-tracking'
  10. import { useIdeReactContext } from '@/features/ide-react/context/ide-react-context'
  11. import { type PermissionsLevel } from '@/features/ide-react/types/permissions'
  12. export type SettableNotificationLevel = 'all' | 'replies' | 'off'
  13. export type NotificationLevel = SettableNotificationLevel | 'global-off'
  14. /**
  15. * Map UI notification level to backend preferences
  16. */
  17. function levelToPreferences(
  18. level: SettableNotificationLevel
  19. ): NotificationPreferencesSchema {
  20. switch (level) {
  21. case 'all':
  22. return {
  23. commentOnOwnProject: true,
  24. commentOnInvitedProject: true,
  25. repliesOnAuthoredThread: true,
  26. repliesOnParticipatingThread: true,
  27. commentResolvedOnAuthoredThread: true,
  28. commentResolvedOnParticipatingThread: true,
  29. commentReopenedOnAuthoredThread: true,
  30. commentReopenedOnParticipatingThread: true,
  31. trackedChangesOnOwnProject: true,
  32. trackedChangesOnInvitedProject: true,
  33. trackChangesAcceptedOnAuthoredChange: true,
  34. }
  35. case 'replies':
  36. return {
  37. commentOnOwnProject: false,
  38. commentOnInvitedProject: false,
  39. repliesOnAuthoredThread: true,
  40. repliesOnParticipatingThread: true,
  41. commentResolvedOnAuthoredThread: true,
  42. commentResolvedOnParticipatingThread: true,
  43. commentReopenedOnAuthoredThread: true,
  44. commentReopenedOnParticipatingThread: true,
  45. trackedChangesOnOwnProject: false,
  46. trackedChangesOnInvitedProject: false,
  47. trackChangesAcceptedOnAuthoredChange: true,
  48. }
  49. case 'off':
  50. return {
  51. commentOnOwnProject: false,
  52. commentOnInvitedProject: false,
  53. repliesOnAuthoredThread: false,
  54. repliesOnParticipatingThread: false,
  55. commentResolvedOnAuthoredThread: false,
  56. commentResolvedOnParticipatingThread: false,
  57. commentReopenedOnAuthoredThread: false,
  58. commentReopenedOnParticipatingThread: false,
  59. trackedChangesOnOwnProject: false,
  60. trackedChangesOnInvitedProject: false,
  61. trackChangesAcceptedOnAuthoredChange: false,
  62. }
  63. }
  64. }
  65. /**
  66. * Map backend preferences to UI notification level, considering the user's
  67. * role so that only the relevant key variant is inspected.
  68. */
  69. function preferencesToLevel(
  70. preferences: GlobalNotificationPreferencesSchema,
  71. permissionsLevel: PermissionsLevel
  72. ): NotificationLevel {
  73. if (preferences.muteAllNotifications) {
  74. return 'global-off'
  75. }
  76. const isOwner = permissionsLevel === 'owner'
  77. const projectComments = isOwner
  78. ? preferences.commentOnOwnProject
  79. : preferences.commentOnInvitedProject
  80. const projectTrackedChanges = isOwner
  81. ? preferences.trackedChangesOnOwnProject
  82. : preferences.trackedChangesOnInvitedProject
  83. const anyProjectNotifications = projectComments || projectTrackedChanges
  84. const anyParticipantNotifications =
  85. preferences.repliesOnAuthoredThread ||
  86. preferences.repliesOnParticipatingThread ||
  87. preferences.trackChangesAcceptedOnAuthoredChange ||
  88. preferences.commentResolvedOnAuthoredThread ||
  89. preferences.commentResolvedOnParticipatingThread ||
  90. preferences.commentReopenedOnAuthoredThread ||
  91. preferences.commentReopenedOnParticipatingThread
  92. if (!anyProjectNotifications && !anyParticipantNotifications) {
  93. return 'off'
  94. }
  95. if (!anyProjectNotifications && anyParticipantNotifications) {
  96. return 'replies'
  97. }
  98. return 'all'
  99. }
  100. export function useProjectNotificationPreferences() {
  101. const { projectId } = useProjectContext()
  102. const { permissionsLevel } = useIdeReactContext()
  103. const [notificationLevel, setNotificationLevel] =
  104. useState<NotificationLevel>('all')
  105. const [isLoading, setIsLoading] = useState(true)
  106. // Load preferences on mount
  107. useEffect(() => {
  108. getJSON<GlobalNotificationPreferencesSchema>(
  109. `/notifications/preferences/project/${projectId}`
  110. )
  111. .then(prefs => {
  112. setNotificationLevel(preferencesToLevel(prefs, permissionsLevel))
  113. })
  114. .catch(debugConsole.error)
  115. .finally(() => setIsLoading(false))
  116. }, [projectId, permissionsLevel])
  117. const setLevel = useCallback(
  118. (level: SettableNotificationLevel) => {
  119. setNotificationLevel(level)
  120. const preferences = levelToPreferences(level)
  121. sendMB('setting-changed', {
  122. changedSetting: 'projectEmailNotifications',
  123. changedSettingVal: level,
  124. projectRole: permissionsLevel,
  125. })
  126. postJSON(`/notifications/preferences/project/${projectId}`, {
  127. body: preferences,
  128. }).catch(debugConsole.error)
  129. },
  130. [projectId, permissionsLevel]
  131. )
  132. return {
  133. notificationLevel,
  134. setNotificationLevel: setLevel,
  135. isLoading,
  136. }
  137. }