From 2e50fb0344c943deabe6eb32edbaf41e88d519ab Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Wed, 11 Mar 2026 08:38:12 -0400 Subject: [PATCH] feat: Add toggle for all notifications (#11713) * feat: Add toggle for all notifications * tidy --- app/scenes/Settings/Notifications.tsx | 69 ++++++++++++++-------- server/routes/api/users/schema.ts | 4 +- server/routes/api/users/users.ts | 18 +++++- shared/i18n/locales/en_US/translation.json | 2 +- 4 files changed, 64 insertions(+), 29 deletions(-) diff --git a/app/scenes/Settings/Notifications.tsx b/app/scenes/Settings/Notifications.tsx index bb102577b7..ddaa61432c 100644 --- a/app/scenes/Settings/Notifications.tsx +++ b/app/scenes/Settings/Notifications.tsx @@ -1,4 +1,5 @@ import debounce from "lodash/debounce"; +import { runInAction } from "mobx"; import { observer } from "mobx-react"; import { AcademicCapIcon, @@ -24,19 +25,14 @@ import Notice from "~/components/Notice"; import Scene from "~/components/Scene"; import Switch from "~/components/Switch"; import Text from "~/components/Text"; -import { HStack } from "~/components/primitives/HStack"; -import env from "~/env"; -import useCurrentTeam from "~/hooks/useCurrentTeam"; import useCurrentUser from "~/hooks/useCurrentUser"; -import usePolicy from "~/hooks/usePolicy"; +import { client } from "~/utils/ApiClient"; import isCloudHosted from "~/utils/isCloudHosted"; import SettingRow from "./components/SettingRow"; function Notifications() { const user = useCurrentUser(); - const team = useCurrentTeam(); const { t } = useTranslation(); - const can = usePolicy(team.id); const options = [ { @@ -151,6 +147,8 @@ function Notifications() { }, ]; + const visibleOptions = options.filter((o) => o.visible !== false); + const showSuccessMessage = debounce(() => { toast.success(t("Notifications saved")); }, 500); @@ -163,6 +161,29 @@ function Notifications() { [user, showSuccessMessage] ); + const handleToggleAll = React.useCallback( + async (checked: boolean) => { + runInAction(() => { + const updated = { ...user.notificationSettings }; + for (const option of visibleOptions) { + updated[option.event] = checked; + } + user.notificationSettings = updated; + }); + await client.post( + checked + ? `/users.notificationsSubscribe` + : `/users.notificationsUnsubscribe` + ); + showSuccessMessage(); + }, + [user, visibleOptions, showSuccessMessage] + ); + + const allEnabled = visibleOptions.every((o) => + user.subscribedToEventType(o.event) + ); + const showSuccessNotice = window.location.search === "?success"; return ( @@ -180,17 +201,18 @@ function Notifications() { Manage when and where you receive email notifications. - {env.EMAIL_ENABLED && can.manage && ( - - - The email integration is currently disabled. Please set the - associated environment variables and restart the server to enable - notifications. - - - )} - -

{t("Notifications")}

+ + + {options.map((option) => { const setting = user.subscribedToEventType(option.event); @@ -199,13 +221,14 @@ function Notifications() { - {option.icon} {option.title} - - } + label={option.title} name={option.event} - description={option.description} + description={ + + {option.description} + + } + compact > ; export const UsersNotificationsSubscribeSchema = z.object({ body: z.object({ - eventType: z.enum(NotificationEventType), + eventType: z.enum(NotificationEventType).optional(), }), }); @@ -76,7 +76,7 @@ export type UsersNotificationsSubscribeReq = z.infer< export const UsersNotificationsUnsubscribeSchema = z.object({ body: z.object({ - eventType: z.enum(NotificationEventType), + eventType: z.enum(NotificationEventType).optional(), }), }); diff --git a/server/routes/api/users/users.ts b/server/routes/api/users/users.ts index d7f6ca794e..12d61b863b 100644 --- a/server/routes/api/users/users.ts +++ b/server/routes/api/users/users.ts @@ -2,7 +2,7 @@ import Router from "koa-router"; import type { WhereOptions } from "sequelize"; import { Op, Sequelize } from "sequelize"; import type { UserPreferences } from "@shared/types"; -import { UserRole } from "@shared/types"; +import { NotificationEventType, UserRole } from "@shared/types"; import { UserRoleHelper } from "@shared/utils/UserRoleHelper"; import { settingsPath } from "@shared/utils/routeHelpers"; import { UserValidation } from "@shared/validations"; @@ -680,7 +680,13 @@ router.post( async (ctx: APIContext) => { const { eventType } = ctx.input.body; const { user } = ctx.state.auth; - user.setNotificationEventType(eventType, true); + const eventTypes = eventType + ? [eventType] + : Object.values(NotificationEventType); + + for (const type of eventTypes) { + user.setNotificationEventType(type, true); + } await user.saveWithCtx(ctx); @@ -698,7 +704,13 @@ router.post( async (ctx: APIContext) => { const { eventType } = ctx.input.body; const { user } = ctx.state.auth; - user.setNotificationEventType(eventType, false); + const eventTypes = eventType + ? [eventType] + : Object.values(NotificationEventType); + + for (const type of eventTypes) { + user.setNotificationEventType(type, false); + } await user.saveWithCtx(ctx); diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json index 4bfbfcb80a..a735a35866 100644 --- a/shared/i18n/locales/en_US/translation.json +++ b/shared/i18n/locales/en_US/translation.json @@ -1277,7 +1277,7 @@ "Notifications saved": "Notifications saved", "Unsubscription successful. Your notification settings were updated": "Unsubscription successful. Your notification settings were updated", "Manage when and where you receive email notifications.": "Manage when and where you receive email notifications.", - "The email integration is currently disabled. Please set the associated environment variables and restart the server to enable notifications.": "The email integration is currently disabled. Please set the associated environment variables and restart the server to enable notifications.", + "All notifications": "All notifications", "Preferences saved": "Preferences saved", "Unread count": "Unread count", "Unread indicator": "Unread indicator",