Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions apps/web/src/components/work-item/CommentEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ import {
import { createMentionExtension, type MentionMember } from './editorMention';

export interface CommentEditorProps {
onSubmit: (contentHtml: string, access: 'INTERNAL' | 'EXTERNAL') => void | Promise<void>;
/**
* Called when the user submits. Return (or resolve to) `false` — or throw —
* to signal the submit failed; the editor then keeps the typed text instead
* of clearing it, so nothing is lost on a network/permission error.
*/
onSubmit: (
contentHtml: string,
access: 'INTERNAL' | 'EXTERNAL',
) => void | boolean | Promise<void | boolean>;
isSubmitting?: boolean;
initialHtml?: string;
placeholder?: string;
Expand Down Expand Up @@ -105,11 +113,21 @@ export function CommentEditor({

if (!editor) return null;

const handleSubmit = () => {
const handleSubmit = async () => {
if (isSubmitting) return;
const html = editor.getHTML().trim();
if (html === '<p></p>' || html === '') return;
void onSubmit(html, access);
try {
// Only clear once the submit has actually succeeded — otherwise a failed
// post/edit would silently discard the user's text.
const result = await onSubmit(html, access);
if (result === false) return;
} catch {
return;
}
// The await above may outlive the editor (parent unmounts / cancels the
// edit); clearing a destroyed editor throws.
if (editor.isDestroyed) return;
editor.commands.clearContent();
setIsEmpty(true);
};
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Expand Down
17 changes: 12 additions & 5 deletions apps/web/src/pages/IssueDetailPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,11 @@ export function IssueDetailPage() {
return t('common.time.justNow', 'just now');
};

const postComment = async (contentHtml: string, access: 'INTERNAL' | 'EXTERNAL' = 'INTERNAL') => {
if (!workspaceSlug || !contentHtml.trim()) return;
const postComment = async (
contentHtml: string,
access: 'INTERNAL' | 'EXTERNAL' = 'INTERNAL',
): Promise<boolean> => {
if (!workspaceSlug || !contentHtml.trim()) return false;
setErrorMessage(null);
setPostingComment(true);
try {
Expand All @@ -440,19 +443,21 @@ export function IssueDetailPage() {
access,
);
setComments((prev) => [...prev, created]);
return true;
} catch (err) {
setErrorMessage(
err instanceof Error
? err.message
: t('workItem.detail.postCommentFailed', 'Failed to post comment.'),
);
return false;
} finally {
setPostingComment(false);
}
};

const updateComment = async (commentId: string, contentHtml: string) => {
if (!workspaceSlug || !contentHtml.trim()) return;
const updateComment = async (commentId: string, contentHtml: string): Promise<boolean> => {
if (!workspaceSlug || !contentHtml.trim()) return false;
setErrorMessage(null);
setUpdatingCommentId(commentId);
try {
Expand All @@ -465,12 +470,14 @@ export function IssueDetailPage() {
);
setComments((prev) => prev.map((c) => (c.id === commentId ? updated : c)));
setEditingCommentId(null);
return true;
} catch (err) {
setErrorMessage(
err instanceof Error
? err.message
: t('workItem.detail.updateCommentFailed', 'Failed to update comment.'),
);
return false;
} finally {
setUpdatingCommentId(null);
}
Expand Down Expand Up @@ -745,7 +752,7 @@ export function IssueDetailPage() {
<div className="mt-2">
<CommentEditor
initialHtml={c.comment}
onSubmit={(html) => void updateComment(c.id, html)}
onSubmit={(html) => updateComment(c.id, html)}
isSubmitting={updatingCommentId === c.id}
onCancel={() => setEditingCommentId(null)}
showShortcutHint
Expand Down
Loading