From b811da0695152c1cfacc7b0502cf030d2a33cb96 Mon Sep 17 00:00:00 2001 From: Alf Eaton Date: Tue, 9 Dec 2025 13:23:21 +0000 Subject: [PATCH] [workbench] Improve asynchronous tool use (#30184) GitOrigin-RevId: d1bd33469b557c29968049af99b9c3c85731151d --- .../editor-navigation-toolbar-root.tsx | 9 ++++++--- .../components/online-users-widget.tsx | 3 ++- .../components/toolbar-header.tsx | 3 ++- .../modes/file-tree-create-new-doc.tsx | 2 +- .../context/editor-manager-context.tsx | 17 ++++++++++------- .../online-users/online-users-widget.tsx | 3 ++- .../components/toolbar/online-users.tsx | 4 ++-- .../pdf-preview/hooks/use-compile-triggers.ts | 2 +- .../js/shared/context/local-compile-context.tsx | 6 +++--- .../js/shared/hooks/use-detach-action.js | 2 +- .../frontend/stories/online-users.stories.tsx | 10 ++++++++-- .../components/online-users-widget.test.tsx | 2 +- .../components/toolbar-header.test.tsx | 2 +- 13 files changed, 40 insertions(+), 25 deletions(-) diff --git a/services/web/frontend/js/features/editor-navigation-toolbar/components/editor-navigation-toolbar-root.tsx b/services/web/frontend/js/features/editor-navigation-toolbar/components/editor-navigation-toolbar-root.tsx index cb4ac6f194..a605677929 100644 --- a/services/web/frontend/js/features/editor-navigation-toolbar/components/editor-navigation-toolbar-root.tsx +++ b/services/web/frontend/js/features/editor-navigation-toolbar/components/editor-navigation-toolbar-root.tsx @@ -20,7 +20,10 @@ const EditorNavigationToolbarRoot = React.memo( openShareProjectModal, }: { onlineUsersArray: OnlineUser[] - openDoc: (doc: Doc, { gotoLine }: { gotoLine: number }) => void + openDoc: ( + doc: Doc, + { gotoLine }: { gotoLine: number } + ) => Promise openShareProjectModal: () => void }) { const { @@ -93,9 +96,9 @@ const EditorNavigationToolbarRoot = React.memo( }, [setLeftMenuShown]) const goToUser = useCallback( - (user: OnlineUser) => { + async (user: OnlineUser) => { if (user.doc && typeof user.row === 'number') { - openDoc(user.doc, { gotoLine: user.row + 1 }) + return await openDoc(user.doc, { gotoLine: user.row + 1 }) } }, [openDoc] diff --git a/services/web/frontend/js/features/editor-navigation-toolbar/components/online-users-widget.tsx b/services/web/frontend/js/features/editor-navigation-toolbar/components/online-users-widget.tsx index a433c4bdc0..991e3d00ef 100644 --- a/services/web/frontend/js/features/editor-navigation-toolbar/components/online-users-widget.tsx +++ b/services/web/frontend/js/features/editor-navigation-toolbar/components/online-users-widget.tsx @@ -11,13 +11,14 @@ import { getBackgroundColorForUserId } from '@/shared/utils/colors' import OLTooltip from '@/shared/components/ol/ol-tooltip' import MaterialIcon from '@/shared/components/material-icon' import { OnlineUser } from '@/features/ide-react/context/online-users-context' +import { Doc } from '@ol-types/doc' function OnlineUsersWidget({ onlineUsers, goToUser, }: { onlineUsers: OnlineUser[] - goToUser: (user: OnlineUser) => void + goToUser: (user: OnlineUser) => Promise }) { const { t } = useTranslation() diff --git a/services/web/frontend/js/features/editor-navigation-toolbar/components/toolbar-header.tsx b/services/web/frontend/js/features/editor-navigation-toolbar/components/toolbar-header.tsx index 5dfaccde59..2393680516 100644 --- a/services/web/frontend/js/features/editor-navigation-toolbar/components/toolbar-header.tsx +++ b/services/web/frontend/js/features/editor-navigation-toolbar/components/toolbar-header.tsx @@ -19,6 +19,7 @@ import TryNewEditorButton from '../try-new-editor-button' import { OnlineUser } from '@/features/ide-react/context/online-users-context' import { Cobranding } from '../../../../../types/cobranding' import { canUseNewEditor } from '@/features/ide-redesign/utils/new-editor-utils' +import { Doc } from '@ol-types/doc' const [publishModalModules] = importOverleafModules('publishModal') as { import: { default: ElementType } @@ -50,7 +51,7 @@ export type ToolbarHeaderProps = { toggleHistoryOpen: () => void unreadMessageCount: number onlineUsers: OnlineUser[] - goToUser: (user: OnlineUser) => void + goToUser: (user: OnlineUser) => Promise isRestrictedTokenMember: boolean | undefined hasPublishPermissions: boolean chatVisible: boolean diff --git a/services/web/frontend/js/features/file-tree/components/file-tree-create/modes/file-tree-create-new-doc.tsx b/services/web/frontend/js/features/file-tree/components/file-tree-create/modes/file-tree-create-new-doc.tsx index 204f5af6da..22961d311e 100644 --- a/services/web/frontend/js/features/file-tree/components/file-tree-create/modes/file-tree-create-new-doc.tsx +++ b/services/web/frontend/js/features/file-tree/components/file-tree-create/modes/file-tree-create-new-doc.tsx @@ -31,7 +31,7 @@ export default function FileTreeCreateNewDoc() { }) if (doc) { - openDoc(doc) + return await openDoc(doc) } }, [finishCreatingDoc, name, openDoc] diff --git a/services/web/frontend/js/features/ide-react/context/editor-manager-context.tsx b/services/web/frontend/js/features/ide-react/context/editor-manager-context.tsx index 58ea500768..c7be4acc4e 100644 --- a/services/web/frontend/js/features/ide-react/context/editor-manager-context.tsx +++ b/services/web/frontend/js/features/ide-react/context/editor-manager-context.tsx @@ -54,11 +54,14 @@ export type EditorManager = { getCurrentDocValue: () => string | null getCurrentDocumentId: () => DocId | null setIgnoringExternalUpdates: (value: boolean) => void - openDocWithId: (docId: string, options?: OpenDocOptions) => void + openDocWithId: ( + docId: string, + options?: OpenDocOptions + ) => Promise openDoc: (document: Doc, options?: OpenDocOptions) => Promise openDocs: OpenDocuments openFileWithId: (fileId: string) => void - openInitialDoc: (docId?: string) => void + openInitialDoc: (docId?: string) => Promise isLoading: boolean jumpToLine: (options: GotoLineOptions) => void debugTimers: React.MutableRefObject> @@ -486,12 +489,12 @@ export const EditorManagerProvider: FC = ({ ) const openDocWithId = useCallback( - (docId: string, options: OpenDocOptions = {}) => { + async (docId: string, options: OpenDocOptions = {}) => { const doc = findDocEntityById(fileTreeData, docId) if (!doc) { return } - openDoc(doc, options) + return await openDoc(doc, options) }, [fileTreeData, openDoc] ) @@ -513,11 +516,11 @@ export const EditorManagerProvider: FC = ({ ) const openInitialDoc = useCallback( - (fallbackDocId?: string) => { + async (fallbackDocId?: string) => { const docId = customLocalStorage.getItem(currentDocumentIdStorageKey) || fallbackDocId if (docId) { - openDocWithId(docId) + return await openDocWithId(docId) } }, [currentDocumentIdStorageKey, openDocWithId] @@ -580,7 +583,7 @@ export const EditorManagerProvider: FC = ({ } const handleProjectJoined = () => { - openDoc(doc, { forceReopen: true }) + return openDoc(doc, { forceReopen: true }) } eventEmitter.once('project:joined', handleProjectJoined) diff --git a/services/web/frontend/js/features/ide-redesign/components/online-users/online-users-widget.tsx b/services/web/frontend/js/features/ide-redesign/components/online-users/online-users-widget.tsx index f582885949..ae3f1d58b3 100644 --- a/services/web/frontend/js/features/ide-redesign/components/online-users/online-users-widget.tsx +++ b/services/web/frontend/js/features/ide-redesign/components/online-users/online-users-widget.tsx @@ -14,6 +14,7 @@ import { import classNames from 'classnames' import { useCallback, useMemo } from 'react' import { useTranslation } from 'react-i18next' +import { Doc } from '@ol-types/doc' // Should be kept in sync with $max-user-circles-displayed CSS constant const MAX_USER_CIRCLES_DISPLAYED = 5 @@ -26,7 +27,7 @@ export const OnlineUsersWidget = ({ goToUser, }: { onlineUsers: OnlineUser[] - goToUser: (user: OnlineUser) => void + goToUser: (user: OnlineUser) => Promise }) => { const hasOverflow = onlineUsers.length > MAX_USER_CIRCLES_DISPLAYED const usersBeforeOverflow = useMemo( diff --git a/services/web/frontend/js/features/ide-redesign/components/toolbar/online-users.tsx b/services/web/frontend/js/features/ide-redesign/components/toolbar/online-users.tsx index 4d937fa89c..5defd8af2b 100644 --- a/services/web/frontend/js/features/ide-redesign/components/toolbar/online-users.tsx +++ b/services/web/frontend/js/features/ide-redesign/components/toolbar/online-users.tsx @@ -11,9 +11,9 @@ export const OnlineUsers = () => { const { onlineUsersArray } = useOnlineUsersContext() const goToUser = useCallback( - (user: OnlineUser) => { + async (user: OnlineUser) => { if (user.doc && typeof user.row === 'number') { - openDoc(user.doc, { gotoLine: user.row + 1 }) + return await openDoc(user.doc, { gotoLine: user.row + 1 }) } }, [openDoc] diff --git a/services/web/frontend/js/features/pdf-preview/hooks/use-compile-triggers.ts b/services/web/frontend/js/features/pdf-preview/hooks/use-compile-triggers.ts index 4e46134ba8..e637474398 100644 --- a/services/web/frontend/js/features/pdf-preview/hooks/use-compile-triggers.ts +++ b/services/web/frontend/js/features/pdf-preview/hooks/use-compile-triggers.ts @@ -33,7 +33,7 @@ export const startCompileKeypress = ( } export default function useCompileTriggers( - startCompile: (...args: any[]) => void, + startCompile: (...args: any[]) => Promise, setChangedAt: (...args: any[]) => void ) { const handleKeyDown = useCallback( diff --git a/services/web/frontend/js/shared/context/local-compile-context.tsx b/services/web/frontend/js/shared/context/local-compile-context.tsx index ddc7ea0202..dd6d86b7c6 100644 --- a/services/web/frontend/js/shared/context/local-compile-context.tsx +++ b/services/web/frontend/js/shared/context/local-compile-context.tsx @@ -120,7 +120,7 @@ export type CompileContext = { setAnimateCompileDropdownArrow: (value: boolean) => void recompileFromScratch: () => void setCompiling: (value: boolean) => void - startCompile: (options?: any) => void + startCompile: (options?: any) => Promise stopCompile: () => void setChangedAt: (value: any) => void clearCache: () => void @@ -700,14 +700,14 @@ export const LocalCompileProvider: FC = ({ const startCompile = useCallback( (options: any) => { setCompiledOnce(true) - compiler.compile(options) + return compiler.compile(options) }, [compiler, setCompiledOnce] ) // stop a compile manually const stopCompile = useCallback(() => { - compiler.stopCompile() + return compiler.stopCompile() }, [compiler]) // clear the compile cache diff --git a/services/web/frontend/js/shared/hooks/use-detach-action.js b/services/web/frontend/js/shared/hooks/use-detach-action.js index ed7f22095f..241bc23cb2 100644 --- a/services/web/frontend/js/shared/hooks/use-detach-action.js +++ b/services/web/frontend/js/shared/hooks/use-detach-action.js @@ -21,7 +21,7 @@ export default function useDetachAction( if (role === senderRole) { broadcastEvent(eventName, { args }) } else { - actionFunction(...args) + return actionFunction(...args) } }, [role, senderRole, eventName, actionFunction, broadcastEvent] diff --git a/services/web/frontend/stories/online-users.stories.tsx b/services/web/frontend/stories/online-users.stories.tsx index 7d2796cb2e..6816035de4 100644 --- a/services/web/frontend/stories/online-users.stories.tsx +++ b/services/web/frontend/stories/online-users.stories.tsx @@ -40,7 +40,10 @@ export const OnlineUsersRedesign = ({ users }: { users: number }) => { padding: '20px', }} > - {}} /> + {}) as any} + /> ) } @@ -54,7 +57,10 @@ export const OnlineUsersOld = ({ users }: { users: number }) => { padding: '20px', }} > - {}} /> + {}) as any} + /> ) } diff --git a/services/web/test/frontend/features/editor-navigation-toolbar/components/online-users-widget.test.tsx b/services/web/test/frontend/features/editor-navigation-toolbar/components/online-users-widget.test.tsx index 42c7a04502..acdc82fa7a 100644 --- a/services/web/test/frontend/features/editor-navigation-toolbar/components/online-users-widget.test.tsx +++ b/services/web/test/frontend/features/editor-navigation-toolbar/components/online-users-widget.test.tsx @@ -20,7 +20,7 @@ describe('', function () { email: 'another_test_email', }, ], - goToUser: () => {}, + goToUser: (async () => {}) as any, } describe('with less than 4 users', function () { diff --git a/services/web/test/frontend/features/editor-navigation-toolbar/components/toolbar-header.test.tsx b/services/web/test/frontend/features/editor-navigation-toolbar/components/toolbar-header.test.tsx index 9b2217744e..e3dac691a5 100644 --- a/services/web/test/frontend/features/editor-navigation-toolbar/components/toolbar-header.test.tsx +++ b/services/web/test/frontend/features/editor-navigation-toolbar/components/toolbar-header.test.tsx @@ -14,7 +14,7 @@ describe('', function () { toggleHistoryOpen: () => {}, unreadMessageCount: 0, onlineUsers: [], - goToUser: () => {}, + goToUser: (async () => {}) as any, projectName: 'test project', renameProject: () => {}, openShareModal: () => {},