From eac47fe413532141ef2035fdd9f85fa8a72f3209 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 6 Jul 2026 14:58:42 +0000 Subject: [PATCH] feat: nudge users to use "@" when typing "[[" to link documents Detects when a user types the "[[" wiki-style link syntax in the editor and shows a toast suggesting they use "@" to link other documents. The hint is surfaced at most once per user, tracked via a new SeenDocumentLinkHint user preference. Co-Authored-By: Claude Opus 4.8 Claude-Session: https://claude.ai/code/session_014y8XoNEbFkSBPunRAp6LmC --- app/components/Editor.tsx | 17 ++++- .../extensions/DocumentLinkHint.test.ts | 68 +++++++++++++++++++ app/editor/extensions/DocumentLinkHint.ts | 41 +++++++++++ app/editor/extensions/index.ts | 2 + app/editor/index.tsx | 2 + shared/types.ts | 3 + 6 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 app/editor/extensions/DocumentLinkHint.test.ts create mode 100644 app/editor/extensions/DocumentLinkHint.ts diff --git a/app/components/Editor.tsx b/app/components/Editor.tsx index 78b3babd72..d1a029412c 100644 --- a/app/components/Editor.tsx +++ b/app/components/Editor.tsx @@ -9,7 +9,7 @@ import { mergeRefs } from "react-merge-refs"; import type { Optional } from "utility-types"; import insertFiles from "@shared/editor/commands/insertFiles"; import EditorContainer from "@shared/editor/components/Styles"; -import { AttachmentPreset } from "@shared/types"; +import { AttachmentPreset, UserPreference } from "@shared/types"; import { ProsemirrorHelper } from "@shared/utils/ProsemirrorHelper"; import { getDataTransferFiles } from "@shared/utils/files"; import { AttachmentValidation } from "@shared/validations"; @@ -50,7 +50,8 @@ function Editor(props: Props, ref: React.RefObject | null) { const { t } = useTranslation(); const embeds = useEmbeds(!shareId); const localRef = React.useRef(); - const preferences = useCurrentUser({ rejectOnEmpty: false })?.preferences; + const user = useCurrentUser({ rejectOnEmpty: false }); + const preferences = user?.preferences; const previousCommentIds = React.useRef(); // Upload progress tracking for delayed toast @@ -133,6 +134,17 @@ function Editor(props: Props, ref: React.RefObject | null) { localRef?.current?.focusAtEnd(); }, [localRef]); + // Nudge the user towards the "@" mention menu the first time they type the + // "[[" document link syntax, then remember that we've done so. + const handleShowDocumentLinkHint = React.useCallback(() => { + if (!user || user.getPreference(UserPreference.SeenDocumentLinkHint)) { + return; + } + toast.message(t("Type @ to link to another document")); + user.setPreference(UserPreference.SeenDocumentLinkHint, true); + void user.save(); + }, [user, t]); + const handleDrop = React.useCallback( (event: React.DragEvent) => { event.preventDefault(); @@ -285,6 +297,7 @@ function Editor(props: Props, ref: React.RefObject | null) { {...props} onClickLink={handleClickLink} onChange={handleChange} + onShowDocumentLinkHint={handleShowDocumentLinkHint} onFileUploadStart={handleFileUploadStart} onFileUploadStop={handleFileUploadStop} onFileUploadProgress={handleFileUploadProgress} diff --git a/app/editor/extensions/DocumentLinkHint.test.ts b/app/editor/extensions/DocumentLinkHint.test.ts new file mode 100644 index 0000000000..3445347d06 --- /dev/null +++ b/app/editor/extensions/DocumentLinkHint.test.ts @@ -0,0 +1,68 @@ +import { inputRules } from "prosemirror-inputrules"; +import type { Node } from "prosemirror-model"; +import { EditorView } from "prosemirror-view"; +import { UserPreference } from "@shared/types"; +import type { UserPreferences } from "@shared/types"; +import { + codeBlock, + createEditorStateWithSelection, + doc, + p, +} from "@shared/test/editor"; +import type { Editor } from "~/editor"; +import DocumentLinkHint from "./DocumentLinkHint"; + +const onShowDocumentLinkHint = vi.fn(); + +function buildExtension(userPreferences?: UserPreferences) { + const extension = new DocumentLinkHint(); + extension.editor = { + props: { onShowDocumentLinkHint, userPreferences }, + } as unknown as Editor; + return extension; +} + +/** + * Mounts an editor with the extension's input rules and simulates typing a + * single character at the end of the given document. + */ +function type(extension: DocumentLinkHint, testDoc: Node, char: string) { + const plugin = inputRules({ rules: extension.inputRules() }); + const pos = testDoc.content.size - 1; + const state = createEditorStateWithSelection(testDoc, pos, [plugin]); + const view = new EditorView(document.createElement("div"), { state }); + view.someProp("handleTextInput", (handler) => + handler(view, pos, pos, char, () => view.state.tr) + ); + view.destroy(); +} + +describe("DocumentLinkHint", () => { + beforeEach(() => { + onShowDocumentLinkHint.mockClear(); + }); + + it("fires the hint when the second bracket completes '[['", () => { + type(buildExtension(), doc(p("[")), "["); + expect(onShowDocumentLinkHint).toHaveBeenCalledTimes(1); + }); + + it("does not fire for a single bracket", () => { + type(buildExtension(), doc(p("")), "["); + expect(onShowDocumentLinkHint).not.toHaveBeenCalled(); + }); + + it("does not fire inside a code block", () => { + type(buildExtension(), doc(codeBlock("[")), "["); + expect(onShowDocumentLinkHint).not.toHaveBeenCalled(); + }); + + it("does not fire once the user has already seen the hint", () => { + type( + buildExtension({ [UserPreference.SeenDocumentLinkHint]: true }), + doc(p("[")), + "[" + ); + expect(onShowDocumentLinkHint).not.toHaveBeenCalled(); + }); +}); diff --git a/app/editor/extensions/DocumentLinkHint.ts b/app/editor/extensions/DocumentLinkHint.ts new file mode 100644 index 0000000000..782d6007c1 --- /dev/null +++ b/app/editor/extensions/DocumentLinkHint.ts @@ -0,0 +1,41 @@ +import { InputRule } from "prosemirror-inputrules"; +import type { EditorState } from "prosemirror-state"; +import Extension from "@shared/editor/lib/Extension"; +import { isInCode } from "@shared/editor/queries/isInCode"; +import { UserPreference } from "@shared/types"; + +/** + * Detects when the user types "[[" – a document linking syntax used by some + * other tools – and nudges them towards using the "@" mention menu to link + * documents instead. The hint is surfaced at most once per user, tracked via + * a user preference. + */ +export default class DocumentLinkHint extends Extension { + get name() { + return "document-link-hint"; + } + + inputRules() { + return [ + new InputRule(/\[\[$/, (state: EditorState) => { + if (isInCode(state)) { + return null; + } + + // Skip firing entirely once the user has already seen the hint. + if ( + this.editor.props.userPreferences?.[ + UserPreference.SeenDocumentLinkHint + ] + ) { + return null; + } + + this.editor.props.onShowDocumentLinkHint?.(); + + // Return null to leave the typed characters untouched. + return null; + }), + ]; + } +} diff --git a/app/editor/extensions/index.ts b/app/editor/extensions/index.ts index 0b87b7a105..291459feca 100644 --- a/app/editor/extensions/index.ts +++ b/app/editor/extensions/index.ts @@ -2,6 +2,7 @@ import type { AnyExtensionClass } from "@shared/editor/lib/types"; import BlockMenuExtension from "~/editor/extensions/BlockMenu"; import ClipboardTextSerializer from "~/editor/extensions/ClipboardTextSerializer"; import DiagramsExtension from "@shared/editor/extensions/Diagrams"; +import DocumentLinkHint from "~/editor/extensions/DocumentLinkHint"; import EmojiMenuExtension from "~/editor/extensions/EmojiMenu"; import FindAndReplaceExtension from "~/editor/extensions/FindAndReplace"; import HoverPreviewsExtension from "~/editor/extensions/HoverPreviews"; @@ -22,6 +23,7 @@ export const withUIExtensions = (nodes: Nodes) => [ BlockMenuExtension, EmojiMenuExtension, MentionMenuExtension, + DocumentLinkHint, FindAndReplaceExtension, HoverPreviewsExtension, SelectionToolbarExtension, diff --git a/app/editor/index.tsx b/app/editor/index.tsx index 0b0af48cdd..4718a6ac53 100644 --- a/app/editor/index.tsx +++ b/app/editor/index.tsx @@ -138,6 +138,8 @@ export type Props = { onDeleteCommentMark?: (commentId: string) => void; /** Callback when comments sidebar should be opened */ onOpenCommentsSidebar?: () => void; + /** Callback when the user types the "[[" document link syntax */ + onShowDocumentLinkHint?: () => void; /** Callback when a file upload begins */ onFileUploadStart?: () => void; /** Callback when a file upload ends */ diff --git a/shared/types.ts b/shared/types.ts index 1e22cd23ad..6ac86d31be 100644 --- a/shared/types.ts +++ b/shared/types.ts @@ -343,6 +343,8 @@ export enum UserPreference { EnableSmartText = "enableSmartText", /** The style of notification badge to display. */ NotificationBadge = "notificationBadge", + /** Whether the user has been shown the hint to use "@" for linking documents. */ + SeenDocumentLinkHint = "seenDocumentLinkHint", } export enum NotificationBadgeType { @@ -363,6 +365,7 @@ export type UserPreferences = { [UserPreference.SortCommentsByOrderInDocument]?: boolean; [UserPreference.EnableSmartText]?: boolean; [UserPreference.NotificationBadge]?: NotificationBadgeType; + [UserPreference.SeenDocumentLinkHint]?: boolean; }; export type SourceMetadata = {