Files
outline/app/scenes/Settings/Notifications.tsx
codegen-sh[bot]andTom Moor f3b4640c7a Update Switch component onChange handler to use boolean callback (#9385)
* Update Switch component onChange handler to use boolean callback

- Remove synthetic event handling from Switch component
- Update onChange signature to (checked: boolean) => void
- Update all call points across the codebase:
  - PublicAccess.tsx: Updated handlers for indexing, showLastModified, and published switches
  - Security.tsx: Created individual handlers for all security preferences
  - Preferences.tsx: Created individual handlers for user preferences
  - CollectionForm.tsx: Added createSwitchRegister helper for react-hook-form compatibility
  - TemplatizeDialog: Updated publish handler
  - DocumentMenu.tsx: Added handlers for embeds and full-width toggles
  - Search.tsx: Updated SearchTitlesFilter handler
  - Application.tsx: Added createSwitchRegister helper
  - Details.tsx: Updated public branding handler
  - Features.tsx: Created individual handlers for seamless edit and commenting
  - OAuthClientForm.tsx: Added createSwitchRegister helper

- Maintained backward compatibility with react-hook-form
- Improved type safety and code clarity

* Fix ESLint floating promise errors

- Add void operator to onChange call in CollectionForm.tsx
- Add void operator to document.save call in DocumentMenu.tsx

These changes fix the @typescript-eslint/no-floating-promises errors
while maintaining the existing functionality.

* Fix Switch component onChange handlers in remaining files

- Updated DocumentCopy.tsx handlers to use boolean parameter
- Updated Notifications.tsx to use closure pattern for event types
- Updated SlackListItem.tsx to use closure pattern for event names
- All TypeScript errors resolved

* Refactor createSwitchRegister into utils/forms

- Created shared utility function in app/utils/forms.ts
- Removed duplicate implementations from CollectionForm, Application, and OAuthClientForm
- Updated all usage points to use the shared utility
- Improved TypeScript typing with Record<string, unknown>
- Fixed imports and removed unused variables
- Maintained existing functionality while reducing code duplication

* Fix TypeScript errors in createSwitchRegister utility

- Updated generic constraint from Record<string, unknown> to FieldValues
- Added Path import from react-hook-form for proper type safety
- Fixed parameter type from keyof TFormData to Path<TFormData>
- Improved type compatibility with react-hook-form's UseFormRegister

Resolves TypeScript compilation errors in CI pipeline.

* Remove unnecessary handlePublishChange callbacks

- Removed handlePublishChange wrapper in DocumentCopy.tsx
- Removed handlePublishChange wrapper in TemplatizeDialog/index.tsx
- Updated Switch components to use setPublish directly
- Simplified code by leveraging boolean callback from Switch component

Since Switch now passes boolean directly, no need for intermediate callbacks.

* Address review feedback: simplify callbacks and fix fullWidth behavior

1. DocumentCopy.tsx:
   - Remove handleRecursiveChange callback wrapper
   - Use setRecursive directly with Switch component

2. DocumentMenu.tsx:
   - Add void user.save() to persist user preference
   - Add document.fullWidth = checked for optimistic update behavior

Both changes leverage the boolean callback from Switch component properly.

* Update Security.tsx

* i18n

---------

Co-authored-by: codegen-sh[bot] <131295404+codegen-sh[bot]@users.noreply.github.com>
Co-authored-by: Tom Moor <tom@getoutline.com>
2025-06-08 10:59:16 -04:00

207 lines
5.9 KiB
TypeScript

import debounce from "lodash/debounce";
import { observer } from "mobx-react";
import {
AcademicCapIcon,
CheckboxIcon,
CollectionIcon,
CommentIcon,
DocumentIcon,
DoneIcon,
EditIcon,
EmailIcon,
PublishIcon,
StarredIcon,
UserIcon,
} from "outline-icons";
import * as React from "react";
import { useTranslation, Trans } from "react-i18next";
import { toast } from "sonner";
import { NotificationEventType } from "@shared/types";
import Flex from "~/components/Flex";
import Heading from "~/components/Heading";
import Notice from "~/components/Notice";
import Scene from "~/components/Scene";
import Switch from "~/components/Switch";
import Text from "~/components/Text";
import env from "~/env";
import useCurrentTeam from "~/hooks/useCurrentTeam";
import useCurrentUser from "~/hooks/useCurrentUser";
import usePolicy from "~/hooks/usePolicy";
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 = [
{
event: NotificationEventType.PublishDocument,
icon: <PublishIcon />,
title: t("Document published"),
description: t(
"Receive a notification whenever a new document is published"
),
},
{
event: NotificationEventType.UpdateDocument,
icon: <EditIcon />,
title: t("Document updated"),
description: t(
"Receive a notification when a document you are subscribed to is edited"
),
},
{
event: NotificationEventType.CreateComment,
icon: <CommentIcon />,
title: t("Comment posted"),
description: t(
"Receive a notification when a document you are subscribed to or a thread you participated in receives a comment"
),
},
{
event: NotificationEventType.MentionedInComment,
icon: <EmailIcon />,
title: t("Mentioned"),
description: t(
"Receive a notification when someone mentions you in a document or comment"
),
},
{
event: NotificationEventType.ResolveComment,
icon: <DoneIcon />,
title: t("Resolved"),
description: t(
"Receive a notification when a comment thread you were involved in is resolved"
),
},
{
event: NotificationEventType.CreateCollection,
icon: <CollectionIcon />,
title: t("Collection created"),
description: t(
"Receive a notification whenever a new collection is created"
),
},
{
event: NotificationEventType.InviteAccepted,
icon: <UserIcon />,
title: t("Invite accepted"),
description: t(
"Receive a notification when someone you invited creates an account"
),
},
{
event: NotificationEventType.AddUserToDocument,
icon: <DocumentIcon />,
title: t("Invited to document"),
description: t(
"Receive a notification when a document is shared with you"
),
},
{
event: NotificationEventType.AddUserToCollection,
icon: <CollectionIcon />,
title: t("Invited to collection"),
description: t(
"Receive a notification when you are given access to a collection"
),
},
{
event: NotificationEventType.ExportCompleted,
icon: <CheckboxIcon checked />,
title: t("Export completed"),
description: t(
"Receive a notification when an export you requested has been completed"
),
},
{
visible: isCloudHosted,
icon: <AcademicCapIcon />,
event: NotificationEventType.Onboarding,
title: t("Getting started"),
description: t("Tips on getting started with features and functionality"),
},
{
visible: isCloudHosted,
icon: <StarredIcon />,
event: NotificationEventType.Features,
title: t("New features"),
description: t("Receive an email when new features of note are added"),
},
];
const showSuccessMessage = debounce(() => {
toast.success(t("Notifications saved"));
}, 500);
const handleChange = React.useCallback(
(eventType: NotificationEventType) => async (checked: boolean) => {
await user.setNotificationEventType(eventType, checked);
showSuccessMessage();
},
[user, showSuccessMessage]
);
const showSuccessNotice = window.location.search === "?success";
return (
<Scene title={t("Notifications")} icon={<EmailIcon />}>
<Heading>{t("Notifications")}</Heading>
{showSuccessNotice && (
<Notice>
<Trans>
Unsubscription successful. Your notification settings were updated
</Trans>
</Notice>
)}
<Text as="p" type="secondary">
<Trans>Manage when and where you receive email notifications.</Trans>
</Text>
{env.EMAIL_ENABLED && can.manage && (
<Notice>
<Trans>
The email integration is currently disabled. Please set the
associated environment variables and restart the server to enable
notifications.
</Trans>
</Notice>
)}
<h2>{t("Notifications")}</h2>
{options.map((option) => {
const setting = user.subscribedToEventType(option.event);
return (
<SettingRow
key={option.event}
visible={option.visible}
label={
<Flex align="center" gap={4}>
{option.icon} {option.title}
</Flex>
}
name={option.event}
description={option.description}
>
<Switch
key={option.event}
id={option.event}
name={option.event}
checked={!!setting}
onChange={handleChange(option.event)}
/>
</SettingRow>
);
})}
</Scene>
);
}
export default observer(Notifications);