Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(community): preserve mention/channel-ref pills in composer drafts

Guscad3fee4d682
diff --git a/src/web/src/components/community/composer.tsx b/src/web/src/components/community/composer.tsx
index 69df9e8ba..31db56b99 100644
--- a/src/web/src/components/community/composer.tsx
+++ b/src/web/src/components/community/composer.tsx
@@ -3,7 +3,7 @@
import { forwardRef, useEffect, useImperativeHandle, useRef, useState } from "react"
import { createPortal } from "react-dom"
import { AtSign, FileIcon, ImageIcon, PlusCircle, Smile, Upload, Users, X } from "lucide-react"
-import { useEditor, EditorContent } from "@tiptap/react"
+import { useEditor, EditorContent, type JSONContent } from "@tiptap/react"
import StarterKit from "@tiptap/starter-kit"
import Placeholder from "@tiptap/extension-placeholder"
import { DOMParser as PMDOMParser } from "@tiptap/pm/model"
@@ -331,7 +331,10 @@ export const Composer = forwardRef<ComposerHandle, ComposerProps>(function Compo
emitDirtyTransition()
const key = draftKeyRef.current
if (key && !isForumPostBody) {
- writeComposerDraft(key, editor.isEmpty ? "" : editor.getText({ blockSeparator: "\n\n" }))
+ // Persist the ProseMirror doc (JSON), not plain text — so @mention /
+ // channel-ref pill nodes survive the round-trip and restore as pills,
+ // not inert `@label` text.
+ writeComposerDraft(key, editor.isEmpty ? null : editor.getJSON())
}
},
})
@@ -343,14 +346,19 @@ export const Composer = forwardRef<ComposerHandle, ComposerProps>(function Compo
// trips exactly like `getText({blockSeparator:"\n\n"})` serialized it.
useEffect(() => {
if (!editor || isForumPostBody || !draftKey) return
- const text = readComposerDraft(draftKey)
- if (!text) return
+ const doc = readComposerDraft(draftKey)
+ if (!doc) return
restoringDraftRef.current = true
try {
- // `buildPasteDom` produces the same `<p>`/`<br>` block structure the
- // paste path uses; its innerHTML round-trips through tiptap's own HTML
- // parser, so the restored paragraphs/hard-breaks match what was saved.
- editor.commands.setContent(buildPasteDom(text, document).innerHTML)
+ // Restore the stored ProseMirror doc directly — mention / channel-ref
+ // pill nodes come back as pills (a plain-text restore would flatten them
+ // to inert `@label` text). emitUpdate:false so the programmatic set isn't
+ // treated as typing; errorOnInvalidContent:true so a stale/incompatible
+ // doc throws into the catch below instead of silently blanking.
+ editor.commands.setContent(doc as JSONContent, { emitUpdate: false, errorOnInvalidContent: true })
+ } catch {
+ // Corrupt/incompatible stored doc — drop it rather than crash the editor.
+ clearComposerDraft(draftKey)
} finally {
restoringDraftRef.current = false
}
diff --git a/src/web/src/lib/community/composer-draft.test.ts b/src/web/src/lib/community/composer-draft.test.ts
index 4255f0c2b..f08a2e8d5 100644
--- a/src/web/src/lib/community/composer-draft.test.ts
+++ b/src/web/src/lib/community/composer-draft.test.ts
@@ -6,6 +6,24 @@ import {
clearComposerDraft,
} from "./composer-draft"
+// A minimal ProseMirror-shaped doc with a mention pill node, to prove the
+// draft round-trips the structure (not just plain text) — this is the pill
+// regression the JSON switch fixes.
+const DOC_WITH_PILL = {
+ type: "doc",
+ content: [
+ {
+ type: "paragraph",
+ content: [
+ { type: "text", text: "hey " },
+ { type: "mention", attrs: { id: "u1", label: "Alice#0001" } },
+ { type: "text", text: " see " },
+ { type: "channelRef", attrs: { id: "c1", label: "general", serverId: "s1", serverName: "demo" } },
+ ],
+ },
+ ],
+}
+
describe("composer-draft", () => {
let storage: Record<string, string>
@@ -27,37 +45,47 @@ describe("composer-draft", () => {
expect(draftStorageKey("srv/chan")).toBe("c-composer-draft:srv/chan")
})
- it("returns empty string when no draft is stored", () => {
- expect(readComposerDraft("srv/chan")).toBe("")
+ it("returns null when no draft is stored", () => {
+ expect(readComposerDraft("srv/chan")).toBeNull()
+ })
+
+ it("round-trips a ProseMirror doc losslessly (pills preserved)", () => {
+ writeComposerDraft("srv/chan", DOC_WITH_PILL)
+ expect(readComposerDraft("srv/chan")).toEqual(DOC_WITH_PILL)
})
- it("round-trips written text", () => {
- writeComposerDraft("srv/chan", "hello\n\nworld")
- expect(readComposerDraft("srv/chan")).toBe("hello\n\nworld")
+ it("persists as JSON under the key", () => {
+ writeComposerDraft("srv/chan", DOC_WITH_PILL)
+ expect(storage["c-composer-draft:srv/chan"]).toBe(JSON.stringify(DOC_WITH_PILL))
})
it("scopes drafts independently per conversation", () => {
- writeComposerDraft("srv/a", "draft A")
- writeComposerDraft("srv/b", "draft B")
- expect(readComposerDraft("srv/a")).toBe("draft A")
- expect(readComposerDraft("srv/b")).toBe("draft B")
+ writeComposerDraft("srv/a", { type: "doc", content: [{ type: "paragraph" }] })
+ writeComposerDraft("srv/b", DOC_WITH_PILL)
+ expect(readComposerDraft("srv/b")).toEqual(DOC_WITH_PILL)
+ expect(readComposerDraft("srv/a")).not.toEqual(DOC_WITH_PILL)
})
- it("removes the key when the draft is empty or whitespace-only", () => {
- writeComposerDraft("srv/chan", "something")
- writeComposerDraft("srv/chan", " \n ")
+ it("removes the key when the draft is null (empty editor)", () => {
+ writeComposerDraft("srv/chan", DOC_WITH_PILL)
+ writeComposerDraft("srv/chan", null)
expect(storage["c-composer-draft:srv/chan"]).toBeUndefined()
- expect(readComposerDraft("srv/chan")).toBe("")
+ expect(readComposerDraft("srv/chan")).toBeNull()
})
it("clear removes a stored draft", () => {
- writeComposerDraft("srv/chan", "text")
+ writeComposerDraft("srv/chan", DOC_WITH_PILL)
clearComposerDraft("srv/chan")
- expect(readComposerDraft("srv/chan")).toBe("")
+ expect(readComposerDraft("srv/chan")).toBeNull()
+ })
+
+ it("returns null on corrupt stored JSON instead of throwing", () => {
+ storage["c-composer-draft:srv/chan"] = "not-json{{{"
+ expect(readComposerDraft("srv/chan")).toBeNull()
})
- it("returns empty string when window is undefined (SSR)", () => {
+ it("returns null when window is undefined (SSR)", () => {
vi.stubGlobal("window", undefined)
- expect(readComposerDraft("srv/chan")).toBe("")
+ expect(readComposerDraft("srv/chan")).toBeNull()
})
})
diff --git a/src/web/src/lib/community/composer-draft.ts b/src/web/src/lib/community/composer-draft.ts
index d39f4e49b..9d15c1365 100644
--- a/src/web/src/lib/community/composer-draft.ts
+++ b/src/web/src/lib/community/composer-draft.ts
@@ -1,12 +1,18 @@
// Per-channel-per-server composer draft cache. The community composer remounts
// on every channel/DM switch (`ChannelView`/`DmView` are keyed by id), so its
-// in-editor text is otherwise lost on navigate. These helpers persist the raw
-// message text (only text — attachments/replies are not cached) under a key
-// scoped to the conversation, so returning restores the draft.
+// in-editor text is otherwise lost on navigate. These helpers persist the
+// editor's ProseMirror document (as JSON) under a key scoped to the
+// conversation, so returning restores the draft.
+//
+// Why JSON, not plain text: the composer holds @mention and /channel-ref pills
+// as ProseMirror nodes. Plain text flattens them to literal `@label` / `/label`
+// strings that restore as inert text, not pills. The JSON doc preserves those
+// nodes (with their attrs) losslessly, so a restored draft renders identical
+// pills. Attachments/replies are still not cached — text + pills only.
//
// Pure wrappers over localStorage (guarded for SSR/quota) rather than the
-// `useLocalStorage` hook: the composer's text lives in the tiptap editor, not
-// React state, so it hydrates/persists imperatively at mount/update/send.
+// `useLocalStorage` hook: the composer's content lives in the tiptap editor,
+// not React state, so it hydrates/persists imperatively at mount/update/send.
const PREFIX = "c-composer-draft:"
@@ -14,23 +20,29 @@ export function draftStorageKey(scope: string): string {
return `${PREFIX}${scope}`
}
-export function readComposerDraft(scope: string): string {
- if (typeof window === "undefined") return ""
+// The stored shape is the editor's ProseMirror JSON document. Typed as unknown
+// here so this module stays editor-agnostic; the composer casts it to tiptap's
+// JSONContent at the call site.
+export function readComposerDraft(scope: string): unknown | null {
+ if (typeof window === "undefined") return null
try {
- return localStorage.getItem(draftStorageKey(scope)) ?? ""
+ const raw = localStorage.getItem(draftStorageKey(scope))
+ if (!raw) return null
+ return JSON.parse(raw) as unknown
} catch {
- return ""
+ return null
}
}
-export function writeComposerDraft(scope: string, text: string): void {
+export function writeComposerDraft(scope: string, doc: unknown | null): void {
if (typeof window === "undefined") return
const key = draftStorageKey(scope)
try {
- // An empty (or whitespace-only) draft is the absence of a draft — remove
- // the key so a stale entry never resurfaces and localStorage stays clean.
- if (text.trim() === "") localStorage.removeItem(key)
- else localStorage.setItem(key, text)
+ // An empty draft (null — the composer passes null when the editor is empty)
+ // is the absence of a draft: remove the key so a stale entry never
+ // resurfaces and localStorage stays clean.
+ if (doc == null) localStorage.removeItem(key)
+ else localStorage.setItem(key, JSON.stringify(doc))
} catch {
/* quota exceeded — silently ignore, same as useLocalStorage */
}