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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014y8XoNEbFkSBPunRAp6LmC
This commit is contained in:
Claude committed 2026-07-06 14:58:42 +00:00
1 parent 9faf89e372
commit eac47fe413
6 files changed
+131 -2

No files matched your search

+15 -2
View File
@@ -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<SharedEditor> | null) {
const { t } = useTranslation();
const embeds = useEmbeds(!shareId);
const localRef = React.useRef<SharedEditor>();
const preferences = useCurrentUser({ rejectOnEmpty: false })?.preferences;
const user = useCurrentUser({ rejectOnEmpty: false });
const preferences = user?.preferences;
const previousCommentIds = React.useRef<string[]>();
// Upload progress tracking for delayed toast
@@ -133,6 +134,17 @@ function Editor(props: Props, ref: React.RefObject<SharedEditor> | 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<HTMLDivElement>) => {
event.preventDefault();
@@ -285,6 +297,7 @@ function Editor(props: Props, ref: React.RefObject<SharedEditor> | null) {
{...props}
onClickLink={handleClickLink}
onChange={handleChange}
onShowDocumentLinkHint={handleShowDocumentLinkHint}
onFileUploadStart={handleFileUploadStart}
onFileUploadStop={handleFileUploadStop}
onFileUploadProgress={handleFileUploadProgress}
@@ -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();
});
});
+41
View File
@@ -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;
}),
];
}
}
+2
View File
@@ -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,
+2
View File
@@ -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 */
+3
View File
@@ -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 = {