mirror of
https://github.com/multica-ai/multica.git
synced 2026-07-28 05:46:58 +02:00
A complete UX upgrade for chat sending → receiving → recovering.
* StatusPill replaces the orphan spinner — stage-aware copy
("Reading files · 12s", "Searching the web · 14s", "Typing · 24s"),
shimmer text, monotonic timer, derived effective status, > 60s
warning tone, > 5min cancel button.
* WS writethrough on task:queued / task:dispatch / task:cancelled so
pendingTask cache stays in sync with the daemon state machine without
invalidate-refetch latency. broadcastTaskDispatch now includes
chat_session_id when the task is for a chat session — the existing
payload only carried it on the generic task: events, leaving the pill
stuck at "Queued" until completion.
* Failure fallback — FailTask writes a chat_message tagged with
failure_reason (mirrors the issue path's system comment, gated on
retried==nil). Front-end renders an inline note ("Connection failed",
with a Show details collapsible) instead of the previous black hole.
* Elapsed timing — chat_message.elapsed_ms persists task.completed_at -
task.created_at on success/failure rows. UI shows "Replied in 38s" /
"Failed after 12s" beneath assistant bubbles. Format helper shared
between StatusPill and the persisted caption so the live timer and
final reading never disagree.
* Optimistic burst rebalanced — pendingTask seed + created_at moved
before the HTTP roundtrip so the pill appears the instant the user
hits send; handleStop is fire-and-forget so cancel feels immediate
(server confirmation arrives via task:cancelled WS).
* Presence integration — chat avatars use ActorAvatar (status dot +
hover card); OfflineBanner above the input on offline/unstable;
SessionDropdown shows per-row in-flight/unread pip plus a
cross-session aggregate pip on the closed trigger.
* Editor blur on send so the caret stops competing with the StatusPill
/ streaming reply for the user's attention.
* Chat panel isOpen now persists globally; defaults to OPEN for new
users (storage key absence) so the feature is discoverable. Existing
users' prior choice is respected.
* DB: migrations 062 (failure_reason) + 063 (elapsed_ms), both
ADD COLUMN NULL — fast, non-blocking, backwards compatible.
* WS: task:failed chat path now invalidates chatKeys.messages — fixes
a pre-existing bug where the failure bubble required a page refresh
to appear.
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
294 lines
10 KiB
TypeScript
294 lines
10 KiB
TypeScript
"use client";
|
|
|
|
/**
|
|
* ContentEditor — the single rich-text editor for the entire application.
|
|
*
|
|
* Architecture decisions (April 2026 refactor):
|
|
*
|
|
* 1. ONE COMPONENT for both editing and readonly display. The `editable` prop
|
|
* controls the mode. Previously we had RichTextEditor + ReadonlyEditor as
|
|
* separate components with duplicated extension configs — this caused
|
|
* visual inconsistency between edit and display modes.
|
|
*
|
|
* 2. ONE MARKDOWN PIPELINE via @tiptap/markdown. Content is loaded with
|
|
* `contentType: 'markdown'` and saved with `editor.getMarkdown()`.
|
|
* Previously we had a custom `markdownToHtml()` pipeline (Marked library)
|
|
* for loading and regex post-processing for saving — two asymmetric paths
|
|
* that caused roundtrip inconsistencies. The @tiptap/markdown extension
|
|
* (v3.21.0+) handles table cell <p> wrapping and custom mention tokenizers
|
|
* natively, eliminating the need for the HTML detour.
|
|
*
|
|
* 3. PREPROCESSING is minimal: only legacy mention shortcode migration and
|
|
* URL linkification (preprocessMarkdown). No HTML conversion.
|
|
*
|
|
* Tech: Tiptap v3.22.1 (ProseMirror wrapper), @tiptap/markdown for
|
|
* bidirectional Markdown ↔ ProseMirror JSON conversion.
|
|
*/
|
|
|
|
import {
|
|
forwardRef,
|
|
useEffect,
|
|
useImperativeHandle,
|
|
useRef,
|
|
type MouseEvent as ReactMouseEvent,
|
|
} from "react";
|
|
import { useEditor, EditorContent } from "@tiptap/react";
|
|
import { cn } from "@multica/ui/lib/utils";
|
|
import type { UploadResult } from "@multica/core/hooks/use-file-upload";
|
|
import { useWorkspaceSlug } from "@multica/core/paths";
|
|
import { useQueryClient } from "@tanstack/react-query";
|
|
import { createEditorExtensions } from "./extensions";
|
|
import { uploadAndInsertFile } from "./extensions/file-upload";
|
|
import { preprocessMarkdown } from "./utils/preprocess";
|
|
import { openLink, isMentionHref } from "./utils/link-handler";
|
|
import { EditorBubbleMenu } from "./bubble-menu";
|
|
import { useLinkHover, LinkHoverCard } from "./link-hover-card";
|
|
import "katex/dist/katex.min.css";
|
|
import "./content-editor.css";
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Helpers
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/** Blob URLs (blob:http://…) are process-local and expire on reload. Strip them
|
|
* from serialised markdown so they never reach the database. */
|
|
const BLOB_IMAGE_RE = /!\[[^\]]*\]\(blob:[^)]*\)\n?/g;
|
|
|
|
function stripBlobUrls(md: string): string {
|
|
return md.replace(BLOB_IMAGE_RE, "");
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Types
|
|
// ---------------------------------------------------------------------------
|
|
|
|
interface ContentEditorProps {
|
|
defaultValue?: string;
|
|
onUpdate?: (markdown: string) => void;
|
|
placeholder?: string;
|
|
editable?: boolean;
|
|
className?: string;
|
|
debounceMs?: number;
|
|
onSubmit?: () => void;
|
|
onBlur?: () => void;
|
|
onUploadFile?: (file: File) => Promise<UploadResult | null>;
|
|
/** Show the floating formatting toolbar on text selection. Defaults true. */
|
|
showBubbleMenu?: boolean;
|
|
/** When true, bare Enter submits (chat-style). Mod-Enter always submits. */
|
|
submitOnEnter?: boolean;
|
|
/**
|
|
* ID of the issue this editor belongs to. When set, the bubble menu exposes
|
|
* a "Create sub-issue from selection" action that parents the new issue
|
|
* under this ID and replaces the selection with a mention link.
|
|
*/
|
|
currentIssueId?: string;
|
|
/**
|
|
* When true, the @mention extension is not registered. Use for editors
|
|
* where mentioning members/agents has no business meaning (e.g. agent
|
|
* system prompts, where the content is fed to an LLM as plain text).
|
|
*/
|
|
disableMentions?: boolean;
|
|
}
|
|
|
|
interface ContentEditorRef {
|
|
getMarkdown: () => string;
|
|
clearContent: () => void;
|
|
focus: () => void;
|
|
/** Drop focus from the editor — used by chat after send so the caret
|
|
* stops competing with the StatusPill / streaming reply for the user's
|
|
* attention. */
|
|
blur: () => void;
|
|
uploadFile: (file: File) => void;
|
|
/** True when file uploads are still in progress. */
|
|
hasActiveUploads: () => boolean;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Component
|
|
// ---------------------------------------------------------------------------
|
|
|
|
const ContentEditor = forwardRef<ContentEditorRef, ContentEditorProps>(
|
|
function ContentEditor(
|
|
{
|
|
defaultValue = "",
|
|
onUpdate,
|
|
placeholder: placeholderText = "",
|
|
editable = true,
|
|
className,
|
|
debounceMs = 300,
|
|
onSubmit,
|
|
onBlur,
|
|
onUploadFile,
|
|
showBubbleMenu = true,
|
|
submitOnEnter = false,
|
|
currentIssueId,
|
|
disableMentions = false,
|
|
},
|
|
ref,
|
|
) {
|
|
const debounceRef = useRef<ReturnType<typeof setTimeout>>(undefined);
|
|
const onUpdateRef = useRef(onUpdate);
|
|
const onSubmitRef = useRef(onSubmit);
|
|
const onBlurRef = useRef(onBlur);
|
|
const onUploadFileRef = useRef(onUploadFile);
|
|
const prevContentRef = useRef(defaultValue);
|
|
const lastEmittedRef = useRef<string | null>(null);
|
|
|
|
// Current workspace slug kept in a ref so the click handler always sees the
|
|
// latest value without recreating the editor. Used by openLink to prefix
|
|
// legacy /issues/... style paths that lack a workspace slug.
|
|
const workspaceSlug = useWorkspaceSlug();
|
|
const workspaceSlugRef = useRef(workspaceSlug);
|
|
workspaceSlugRef.current = workspaceSlug;
|
|
|
|
// Keep refs in sync without recreating editor
|
|
onUpdateRef.current = onUpdate;
|
|
onSubmitRef.current = onSubmit;
|
|
onBlurRef.current = onBlur;
|
|
onUploadFileRef.current = onUploadFile;
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
const editor = useEditor({
|
|
immediatelyRender: false,
|
|
// Note: in v3.22.1 the default is already false/undefined (same behavior).
|
|
// Explicit for clarity — the real perf win is useEditorState in BubbleMenu.
|
|
shouldRerenderOnTransaction: false,
|
|
editable,
|
|
onCreate: ({ editor: ed }) => {
|
|
lastEmittedRef.current = stripBlobUrls(ed.getMarkdown());
|
|
},
|
|
content: defaultValue ? preprocessMarkdown(defaultValue) : "",
|
|
contentType: defaultValue ? "markdown" : undefined,
|
|
extensions: createEditorExtensions({
|
|
editable,
|
|
placeholder: placeholderText,
|
|
queryClient,
|
|
onSubmitRef,
|
|
onUploadFileRef,
|
|
submitOnEnter,
|
|
disableMentions,
|
|
}),
|
|
onUpdate: ({ editor: ed }) => {
|
|
if (!onUpdateRef.current) return;
|
|
if (debounceRef.current) clearTimeout(debounceRef.current);
|
|
debounceRef.current = setTimeout(() => {
|
|
const md = stripBlobUrls(ed.getMarkdown());
|
|
if (md === lastEmittedRef.current) return;
|
|
lastEmittedRef.current = md;
|
|
onUpdateRef.current?.(md);
|
|
}, debounceMs);
|
|
},
|
|
onBlur: () => {
|
|
onBlurRef.current?.();
|
|
},
|
|
editorProps: {
|
|
handleDOMEvents: {
|
|
click(_view, event) {
|
|
const target = event.target as HTMLElement;
|
|
// Skip links inside NodeView wrappers — they handle their own clicks
|
|
if (target.closest("[data-node-view-wrapper]")) return false;
|
|
|
|
const link = target.closest("a");
|
|
const href = link?.getAttribute("href");
|
|
if (!href || isMentionHref(href)) return false;
|
|
|
|
event.preventDefault();
|
|
openLink(href, workspaceSlugRef.current);
|
|
return true;
|
|
},
|
|
},
|
|
attributes: {
|
|
class: cn(
|
|
"rich-text-editor text-sm outline-none",
|
|
!editable && "readonly",
|
|
className,
|
|
),
|
|
},
|
|
},
|
|
});
|
|
|
|
// Cleanup debounce on unmount
|
|
useEffect(() => {
|
|
return () => {
|
|
if (debounceRef.current) clearTimeout(debounceRef.current);
|
|
};
|
|
}, []);
|
|
|
|
// Readonly content update: when defaultValue changes and editor is readonly,
|
|
// re-set the content (e.g. after editing a comment, the readonly view updates)
|
|
useEffect(() => {
|
|
if (!editor || editable) return;
|
|
if (defaultValue === prevContentRef.current) return;
|
|
prevContentRef.current = defaultValue;
|
|
const processed = defaultValue ? preprocessMarkdown(defaultValue) : "";
|
|
if (processed) {
|
|
editor.commands.setContent(processed, { contentType: "markdown" });
|
|
} else {
|
|
editor.commands.clearContent();
|
|
}
|
|
}, [editor, editable, defaultValue]);
|
|
|
|
useImperativeHandle(ref, () => ({
|
|
getMarkdown: () => stripBlobUrls(editor?.getMarkdown() ?? ""),
|
|
clearContent: () => {
|
|
editor?.commands.clearContent();
|
|
},
|
|
focus: () => {
|
|
editor?.commands.focus();
|
|
},
|
|
blur: () => {
|
|
editor?.commands.blur();
|
|
},
|
|
uploadFile: (file: File) => {
|
|
if (!editor || !onUploadFileRef.current) return;
|
|
const endPos = editor.state.doc.content.size;
|
|
uploadAndInsertFile(editor, file, onUploadFileRef.current, endPos);
|
|
},
|
|
hasActiveUploads: () => {
|
|
if (!editor) return false;
|
|
let uploading = false;
|
|
editor.state.doc.descendants((node) => {
|
|
if (node.attrs.uploading) uploading = true;
|
|
return !uploading;
|
|
});
|
|
return uploading;
|
|
},
|
|
}));
|
|
|
|
// Link hover card — disabled when BubbleMenu is active (has selection)
|
|
const wrapperRef = useRef<HTMLDivElement>(null);
|
|
const hoverDisabled = !editor?.state.selection.empty;
|
|
const hover = useLinkHover(wrapperRef, hoverDisabled);
|
|
|
|
const handleContainerMouseDown = (event: ReactMouseEvent<HTMLDivElement>) => {
|
|
if (!editable || !editor) return;
|
|
|
|
const target = event.target as HTMLElement;
|
|
if (target.closest(".ProseMirror")) return;
|
|
if (target.closest("a, button, input, textarea, [role='button'], [data-node-view-wrapper]")) return;
|
|
|
|
event.preventDefault();
|
|
editor.commands.focus("end");
|
|
};
|
|
|
|
if (!editor) return null;
|
|
|
|
return (
|
|
<div
|
|
ref={wrapperRef}
|
|
className="relative flex min-h-full flex-col"
|
|
onMouseDown={handleContainerMouseDown}
|
|
>
|
|
<EditorContent className="flex-1 min-h-full" editor={editor} />
|
|
{editable && showBubbleMenu && (
|
|
<EditorBubbleMenu editor={editor} currentIssueId={currentIssueId} />
|
|
)}
|
|
<LinkHoverCard {...hover} />
|
|
</div>
|
|
);
|
|
},
|
|
);
|
|
|
|
export { ContentEditor, type ContentEditorProps, type ContentEditorRef };
|