From f3b4640c7ad9f75a3e4545edef2b01773fe0fa59 Mon Sep 17 00:00:00 2001 From: "codegen-sh[bot]" <131295404+codegen-sh[bot]@users.noreply.github.com> Date: Sun, 8 Jun 2025 10:59:16 -0400 Subject: [PATCH] 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 - Fixed imports and removed unused variables - Maintained existing functionality while reducing code duplication * Fix TypeScript errors in createSwitchRegister utility - Updated generic constraint from Record to FieldValues - Added Path import from react-hook-form for proper type safety - Fixed parameter type from keyof TFormData to Path - 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 --- app/components/Collection/CollectionForm.tsx | 8 +- app/components/DocumentCopy.tsx | 18 +--- .../OAuthClient/OAuthClientForm.tsx | 3 +- .../Sharing/Document/PublicAccess.tsx | 12 +-- app/components/Switch.tsx | 11 +-- app/components/TemplatizeDialog/index.tsx | 9 +- app/menus/DocumentMenu.tsx | 38 ++++--- app/scenes/Search/Search.tsx | 4 +- app/scenes/Settings/Application.tsx | 7 +- app/scenes/Settings/Details.tsx | 4 +- app/scenes/Settings/Features.tsx | 27 +++-- app/scenes/Settings/Notifications.tsx | 10 +- app/scenes/Settings/Preferences.tsx | 60 ++++++++--- app/scenes/Settings/Security.tsx | 99 ++++++++++++++----- app/utils/forms.ts | 25 +++++ .../slack/client/components/SlackListItem.tsx | 14 ++- 16 files changed, 225 insertions(+), 124 deletions(-) create mode 100644 app/utils/forms.ts diff --git a/app/components/Collection/CollectionForm.tsx b/app/components/Collection/CollectionForm.tsx index bcd36bb82e..14dbc03974 100644 --- a/app/components/Collection/CollectionForm.tsx +++ b/app/components/Collection/CollectionForm.tsx @@ -22,6 +22,7 @@ import useBoolean from "~/hooks/useBoolean"; import useCurrentTeam from "~/hooks/useCurrentTeam"; import useStores from "~/hooks/useStores"; import { EmptySelectValue } from "~/types"; +import { createSwitchRegister } from "~/utils/forms"; const IconPicker = createLazyComponent(() => import("~/components/IconPicker")); @@ -114,7 +115,7 @@ export const CollectionForm = observer(function CollectionForm_({ }, [setFocus]); const handleIconChange = useCallback( - (icon: string, color: string | null) => { + (icon: string, color: string) => { if (icon !== values.icon) { setFocus("name"); } @@ -131,7 +132,6 @@ export const CollectionForm = observer(function CollectionForm_({ Collections are used to group documents and choose permissions - . )} @@ -197,7 +197,7 @@ export const CollectionForm = observer(function CollectionForm_({ id="commenting" label={t("Commenting")} note={t("Allow commenting on documents within this collection.")} - {...register("commenting")} + {...createSwitchRegister(register, "commenting")} /> )} diff --git a/app/components/DocumentCopy.tsx b/app/components/DocumentCopy.tsx index d800dfd6ed..d725d52cfa 100644 --- a/app/components/DocumentCopy.tsx +++ b/app/components/DocumentCopy.tsx @@ -46,20 +46,6 @@ function DocumentCopy({ document, onSubmit }: Props) { return nodes; }, [policies, collectionTrees, document.isTemplate]); - const handlePublishChange = React.useCallback( - (ev: React.ChangeEvent) => { - setPublish(ev.target.checked); - }, - [] - ); - - const handleRecursiveChange = React.useCallback( - (ev: React.ChangeEvent) => { - setRecursive(ev.target.checked); - }, - [] - ); - const copy = async () => { if (!selectedPath) { toast.message(t("Select a location to copy")); @@ -102,7 +88,7 @@ function DocumentCopy({ document, onSubmit }: Props) { label={t("Publish")} labelPosition="right" checked={publish} - onChange={handlePublishChange} + onChange={setPublish} /> )} @@ -113,7 +99,7 @@ function DocumentCopy({ document, onSubmit }: Props) { label={t("Include nested documents")} labelPosition="right" checked={recursive} - onChange={handleRecursiveChange} + onChange={setRecursive} /> )} diff --git a/app/components/OAuthClient/OAuthClientForm.tsx b/app/components/OAuthClient/OAuthClientForm.tsx index 1bd1260ffd..599f79a571 100644 --- a/app/components/OAuthClient/OAuthClientForm.tsx +++ b/app/components/OAuthClient/OAuthClientForm.tsx @@ -8,6 +8,7 @@ import ImageInput from "~/scenes/Settings/components/ImageInput"; import Button from "~/components/Button"; import Flex from "~/components/Flex"; import Input, { LabelText } from "~/components/Input"; +import { createSwitchRegister } from "~/utils/forms"; import isCloudHosted from "~/utils/isCloudHosted"; import Switch from "../Switch"; @@ -116,7 +117,7 @@ export const OAuthClientForm = observer(function OAuthClientForm_({ /> {isCloudHosted && ( diff --git a/app/components/Sharing/Document/PublicAccess.tsx b/app/components/Sharing/Document/PublicAccess.tsx index 53e78073cf..ae3ecc149f 100644 --- a/app/components/Sharing/Document/PublicAccess.tsx +++ b/app/components/Sharing/Document/PublicAccess.tsx @@ -52,10 +52,10 @@ function PublicAccess({ document, share, sharedParent }: Props) { }, [share?.urlId]); const handleIndexingChanged = React.useCallback( - async (event) => { + async (checked: boolean) => { try { await share?.save({ - allowIndexing: event.currentTarget.checked, + allowIndexing: checked, }); } catch (err) { toast.error(err.message); @@ -65,10 +65,10 @@ function PublicAccess({ document, share, sharedParent }: Props) { ); const handleShowLastModifiedChanged = React.useCallback( - async (event) => { + async (checked: boolean) => { try { await share?.save({ - showLastUpdated: event.currentTarget.checked, + showLastUpdated: checked, }); } catch (err) { toast.error(err.message); @@ -78,10 +78,10 @@ function PublicAccess({ document, share, sharedParent }: Props) { ); const handlePublishedChange = React.useCallback( - async (event) => { + async (checked: boolean) => { try { await share?.save({ - published: event.currentTarget.checked, + published: checked, }); } catch (err) { toast.error(err.message); diff --git a/app/components/Switch.tsx b/app/components/Switch.tsx index efd5a77b30..fa57059048 100644 --- a/app/components/Switch.tsx +++ b/app/components/Switch.tsx @@ -28,7 +28,7 @@ interface Props /** Whether the switch is disabled */ disabled?: boolean; /** Callback when the switch state changes */ - onChange?: (event: React.ChangeEvent) => void; + onChange?: (checked: boolean) => void; } function Switch( @@ -49,15 +49,10 @@ function Switch( const handleCheckedChange = React.useCallback( (checkedState: boolean) => { if (onChange) { - // Create a synthetic event to maintain compatibility with existing code - const syntheticEvent = { - target: { id: props.id, name: props.name, checked: checkedState }, - currentTarget: { checked: checkedState }, - } as React.ChangeEvent; - onChange(syntheticEvent); + onChange(checkedState); } }, - [onChange, props.id, props.name] + [onChange] ); const component = ( diff --git a/app/components/TemplatizeDialog/index.tsx b/app/components/TemplatizeDialog/index.tsx index 59e9e0914a..cd2c8ff3b1 100644 --- a/app/components/TemplatizeDialog/index.tsx +++ b/app/components/TemplatizeDialog/index.tsx @@ -27,13 +27,6 @@ function DocumentTemplatizeDialog({ documentId }: Props) { document.collectionId ?? null ); - const handlePublishChange = React.useCallback( - (ev: React.ChangeEvent) => { - setPublish(ev.target.checked); - }, - [] - ); - const handleSubmit = React.useCallback(async () => { const template = await document?.templatize({ collectionId, @@ -72,7 +65,7 @@ function DocumentTemplatizeDialog({ documentId }: Props) { label={t("Published")} note={t("Enable other members to use the template immediately")} checked={publish} - onChange={handlePublishChange} + onChange={setPublish} /> diff --git a/app/menus/DocumentMenu.tsx b/app/menus/DocumentMenu.tsx index 6cdbde8f70..602abeb92c 100644 --- a/app/menus/DocumentMenu.tsx +++ b/app/menus/DocumentMenu.tsx @@ -234,6 +234,27 @@ const MenuContent: React.FC = observer(function MenuContent_({ onSelectTemplate, }); + const handleEmbedsToggle = React.useCallback( + (checked: boolean) => { + if (checked) { + document.enableEmbeds(); + } else { + document.disableEmbeds(); + } + }, + [document] + ); + + const handleFullWidthToggle = React.useCallback( + (checked: boolean) => { + user.setPreference(UserPreference.FullWidthDocuments, checked); + void user.save(); + document.fullWidth = checked; + void document.save({ fullWidth: checked }); + }, + [user, document] + ); + return !isEmpty(can) ? ( = observer(function MenuContent_({ label={t("Enable embeds")} labelPosition="left" checked={!document.embedsDisabled} - onChange={ - document.embedsDisabled - ? document.enableEmbeds - : document.disableEmbeds - } + onChange={handleEmbedsToggle} /> )} @@ -380,16 +397,7 @@ const MenuContent: React.FC = observer(function MenuContent_({ label={t("Full width")} labelPosition="left" checked={document.fullWidth} - onChange={(ev) => { - const fullWidth = ev.currentTarget.checked; - user.setPreference( - UserPreference.FullWidthDocuments, - fullWidth - ); - void user.save(); - document.fullWidth = fullWidth; - void document.save(); - }} + onChange={handleFullWidthToggle} /> )} diff --git a/app/scenes/Search/Search.tsx b/app/scenes/Search/Search.tsx index 12f1c76270..36b5699d3a 100644 --- a/app/scenes/Search/Search.tsx +++ b/app/scenes/Search/Search.tsx @@ -271,8 +271,8 @@ function Search() { width={26} height={14} label={t("Search titles only")} - onChange={(ev: React.ChangeEvent) => { - handleFilterChange({ titleFilter: ev.target.checked }); + onChange={(checked: boolean) => { + handleFilterChange({ titleFilter: checked }); }} checked={titleFilter} /> diff --git a/app/scenes/Settings/Application.tsx b/app/scenes/Settings/Application.tsx index b358af75b1..a580d35516 100644 --- a/app/scenes/Settings/Application.tsx +++ b/app/scenes/Settings/Application.tsx @@ -5,6 +5,7 @@ import { Controller, useForm } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { useParams } from "react-router-dom"; import { toast } from "sonner"; + import { OAuthClientValidation } from "@shared/validations"; import OAuthClient from "~/models/oauth/OAuthClient"; import Breadcrumb from "~/components/Breadcrumb"; @@ -22,6 +23,7 @@ import Tooltip from "~/components/Tooltip"; import useRequest from "~/hooks/useRequest"; import useStores from "~/hooks/useStores"; import OAuthClientMenu from "~/menus/OAuthClientMenu"; +import { createSwitchRegister } from "~/utils/forms"; import isCloudHosted from "~/utils/isCloudHosted"; import { settingsPath } from "~/utils/routeHelpers"; import { ActionRow } from "./components/ActionRow"; @@ -218,7 +220,10 @@ const Application = observer(function Application({ oauthClient }: Props) { )} border={false} > - + )} diff --git a/app/scenes/Settings/Details.tsx b/app/scenes/Settings/Details.tsx index c34785ec39..b4b94eaed3 100644 --- a/app/scenes/Settings/Details.tsx +++ b/app/scenes/Settings/Details.tsx @@ -274,9 +274,7 @@ function Details() { id={TeamPreference.PublicBranding} name={TeamPreference.PublicBranding} checked={publicBranding} - onChange={(event: React.ChangeEvent) => - setPublicBranding(event.target.checked) - } + onChange={(checked: boolean) => setPublicBranding(checked)} /> )} diff --git a/app/scenes/Settings/Features.tsx b/app/scenes/Settings/Features.tsx index 4b210f7a43..30ecd2a1c9 100644 --- a/app/scenes/Settings/Features.tsx +++ b/app/scenes/Settings/Features.tsx @@ -15,16 +15,23 @@ function Features() { const team = useCurrentTeam(); const { t } = useTranslation(); - const handlePreferenceChange = - (inverted = false) => - async (ev: React.ChangeEvent) => { - team.setPreference( - ev.target.name as TeamPreference, - inverted ? !ev.target.checked : ev.target.checked - ); + const handleSeamlessEditChange = React.useCallback( + async (checked: boolean) => { + team.setPreference(TeamPreference.SeamlessEdit, !checked); await team.save(); toast.success(t("Settings saved")); - }; + }, + [team, t] + ); + + const handleCommentingChange = React.useCallback( + async (checked: boolean) => { + team.setPreference(TeamPreference.Commenting, checked); + await team.save(); + toast.success(t("Settings saved")); + }, + [team, t] + ); return ( }> @@ -46,7 +53,7 @@ function Features() { id={TeamPreference.SeamlessEdit} name={TeamPreference.SeamlessEdit} checked={!team.getPreference(TeamPreference.SeamlessEdit)} - onChange={handlePreferenceChange(true)} + onChange={handleSeamlessEditChange} /> diff --git a/app/scenes/Settings/Notifications.tsx b/app/scenes/Settings/Notifications.tsx index 479c6564eb..26b35f2044 100644 --- a/app/scenes/Settings/Notifications.tsx +++ b/app/scenes/Settings/Notifications.tsx @@ -138,15 +138,13 @@ function Notifications() { }, 500); const handleChange = React.useCallback( - async (ev: React.ChangeEvent) => { - await user.setNotificationEventType( - ev.target.name as NotificationEventType, - ev.target.checked - ); + (eventType: NotificationEventType) => async (checked: boolean) => { + await user.setNotificationEventType(eventType, checked); showSuccessMessage(); }, [user, showSuccessMessage] ); + const showSuccessNotice = window.location.search === "?success"; return ( @@ -196,7 +194,7 @@ function Notifications() { id={option.event} name={option.event} checked={!!setting} - onChange={handleChange} + onChange={handleChange(option.event)} /> ); diff --git a/app/scenes/Settings/Preferences.tsx b/app/scenes/Settings/Preferences.tsx index b6240c357c..c456f000f1 100644 --- a/app/scenes/Settings/Preferences.tsx +++ b/app/scenes/Settings/Preferences.tsx @@ -49,16 +49,50 @@ function Preferences() { [t] ); - const handlePreferenceChange = - (inverted = false) => - async (ev: React.ChangeEvent) => { - user.setPreference( - ev.target.name as UserPreference, - inverted ? !ev.target.checked : ev.target.checked - ); + const handleUseCursorPointerChange = React.useCallback( + async (checked: boolean) => { + user.setPreference(UserPreference.UseCursorPointer, checked); await user.save(); toast.success(t("Preferences saved")); - }; + }, + [user, t] + ); + + const handleCodeBlockLineNumbersChange = React.useCallback( + async (checked: boolean) => { + user.setPreference(UserPreference.CodeBlockLineNumers, checked); + await user.save(); + toast.success(t("Preferences saved")); + }, + [user, t] + ); + + const handleSeamlessEditChange = React.useCallback( + async (checked: boolean) => { + user.setPreference(UserPreference.SeamlessEdit, !checked); + await user.save(); + toast.success(t("Preferences saved")); + }, + [user, t] + ); + + const handleRememberLastPathChange = React.useCallback( + async (checked: boolean) => { + user.setPreference(UserPreference.RememberLastPath, checked); + await user.save(); + toast.success(t("Preferences saved")); + }, + [user, t] + ); + + const handleEnableSmartTextChange = React.useCallback( + async (checked: boolean) => { + user.setPreference(UserPreference.EnableSmartText, checked); + await user.save(); + toast.success(t("Preferences saved")); + }, + [user, t] + ); const handleLanguageChange = React.useCallback( async (language: string) => { @@ -145,7 +179,7 @@ function Preferences() { id={UserPreference.UseCursorPointer} name={UserPreference.UseCursorPointer} checked={user.getPreference(UserPreference.UseCursorPointer)} - onChange={handlePreferenceChange(false)} + onChange={handleUseCursorPointerChange} /> @@ -179,7 +213,7 @@ function Preferences() { team.getPreference(TeamPreference.SeamlessEdit) ) } - onChange={handlePreferenceChange(true)} + onChange={handleSeamlessEditChange} /> diff --git a/app/scenes/Settings/Security.tsx b/app/scenes/Settings/Security.tsx index 4bc5efba7a..973a62facf 100644 --- a/app/scenes/Settings/Security.tsx +++ b/app/scenes/Settings/Security.tsx @@ -88,13 +88,6 @@ function Security() { [team, showSuccessMessage] ); - const handleChange = React.useCallback( - async (ev: React.ChangeEvent) => { - await saveData({ [ev.target.id]: ev.target.checked }); - }, - [saveData] - ); - const handleDefaultRoleChange = React.useCallback( async (newDefaultRole: string) => { await saveData({ defaultUserRole: newDefaultRole }); @@ -102,11 +95,68 @@ function Security() { [saveData] ); - const handlePreferenceChange = React.useCallback( - async (ev: React.ChangeEvent) => { + const handleGuestSigninChange = React.useCallback( + async (checked: boolean) => { + await saveData({ guestSignin: checked }); + }, + [saveData] + ); + + const handleSharingChange = React.useCallback( + async (checked: boolean) => { + await saveData({ sharing: checked }); + }, + [saveData] + ); + + const handleDocumentEmbedsChange = React.useCallback( + async (checked: boolean) => { + await saveData({ documentEmbeds: checked }); + }, + [saveData] + ); + + const handleMemberCollectionCreateChange = React.useCallback( + async (checked: boolean) => { + await saveData({ memberCollectionCreate: checked }); + }, + [saveData] + ); + + const handleMemberTeamCreateChange = React.useCallback( + async (checked: boolean) => { + await saveData({ memberTeamCreate: checked }); + }, + [saveData] + ); + + const handleMembersCanInviteChange = React.useCallback( + async (checked: boolean) => { const preferences = { ...team.preferences, - [ev.target.id]: ev.target.checked, + [TeamPreference.MembersCanInvite]: checked, + }; + await saveData({ preferences }); + }, + [saveData, team.preferences] + ); + + const handleViewersCanExportChange = React.useCallback( + async (checked: boolean) => { + const preferences = { + ...team.preferences, + [TeamPreference.ViewersCanExport]: checked, + }; + await saveData({ preferences }); + }, + [saveData, team.preferences] + ); + + const handleMembersCanDeleteAccountChange = React.useCallback( + async (checked: boolean) => { + const preferences = { + ...team.preferences, + [TeamPreference.MembersCanDeleteAccount]: checked, }; await saveData({ preferences }); }, @@ -114,8 +164,8 @@ function Security() { ); const handleInviteRequiredChange = React.useCallback( - async (ev: React.ChangeEvent) => { - const inviteRequired = ev.target.checked; + async (checked: boolean) => { + const inviteRequired = checked; const newData = { ...data, inviteRequired }; if (inviteRequired) { @@ -141,10 +191,9 @@ function Security() { ), }); - return; + } else { + await saveData(newData); } - - await saveData(newData); }, [data, saveData, t, dialogs, team.signinMethods] ); @@ -204,7 +253,7 @@ function Security() { @@ -218,7 +267,7 @@ function Security() { {isCloudHosted && ( @@ -268,7 +317,11 @@ function Security() { "When enabled, documents can be shared publicly on the internet by any member of the workspace" )} > - + {isCloudHosted && ( @@ -331,7 +384,7 @@ function Security() { )} diff --git a/app/utils/forms.ts b/app/utils/forms.ts new file mode 100644 index 0000000000..a07620fe84 --- /dev/null +++ b/app/utils/forms.ts @@ -0,0 +1,25 @@ +import { UseFormRegister, Path, FieldValues } from "react-hook-form"; + +/** + * Creates a switch register function that adapts react-hook-form's register + * to work with Switch components that use boolean callbacks instead of synthetic events. + * + * @param register - The register function from react-hook-form + * @param fieldName - The name of the form field to register + * @returns An object with the registration props adapted for Switch components + */ +export function createSwitchRegister< + TFormData extends FieldValues = FieldValues +>(register: UseFormRegister, fieldName: Path) { + const { onChange, ...rest } = register(fieldName); + return { + ...rest, + onChange: (checked: boolean) => { + const syntheticEvent = { + target: { name: fieldName, value: checked, checked }, + type: "change", + }; + void onChange(syntheticEvent); + }, + }; +} diff --git a/plugins/slack/client/components/SlackListItem.tsx b/plugins/slack/client/components/SlackListItem.tsx index 7efa2e56d4..e30ca263ed 100644 --- a/plugins/slack/client/components/SlackListItem.tsx +++ b/plugins/slack/client/components/SlackListItem.tsx @@ -26,13 +26,11 @@ type Props = { function SlackListItem({ integration, collection }: Props) { const { t } = useTranslation(); - const handleChange = async (ev: React.ChangeEvent) => { - if (ev.target.checked) { - integration.events = uniq([...integration.events, ev.target.name]); + const handleChange = (eventName: string) => async (checked: boolean) => { + if (checked) { + integration.events = uniq([...integration.events, eventName]); } else { - integration.events = integration.events.filter( - (n) => n !== ev.target.name - ); + integration.events = integration.events.filter((n) => n !== eventName); } await integration.save(); @@ -87,13 +85,13 @@ function SlackListItem({ integration, collection }: Props) { label={t("Document published")} name="documents.publish" checked={integration.events.includes("documents.publish")} - onChange={handleChange} + onChange={handleChange("documents.publish")} />