Repository navigation
Expand file tree
/
Copy pathEditor.tsx
More file actions
122 lines (112 loc) · 3.56 KB
/
Copy pathEditor.tsx
File metadata and controls
122 lines (112 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"use client";
import { useEffect, useState } from "react";
import {
AaToggle,
MdTextarea,
RichToolbar,
useRichText,
} from "@/components/RichText";
import type { DiscussionEditorProps } from "./types";
/**
* Markdown-first discussion composer. Storage is always markdown; the toolbar
* wraps markdown tokens around the textarea selection. Keeps the collapsed
* trigger behaviour and the onSubmit(markdown) + submitLabel contract so
* DiscussionArea works unchanged.
*/
export function DiscussionEditor({
onSubmit,
onCancel,
initialContent = "",
autoExpand = false,
placeholder = "Join the conversation...",
submitLabel = "Comment",
disabled = false,
}: DiscussionEditorProps) {
const [isExpanded, setIsExpanded] = useState(autoExpand);
const [isSubmitting, setIsSubmitting] = useState(false);
const { text, setText, toolbar, setToolbar, ref, exec } =
useRichText(initialContent);
// Focus the textarea once expanded.
useEffect(() => {
if (isExpanded) ref.current?.focus();
}, [isExpanded, ref]);
const reset = () => {
setText(initialContent);
setToolbar(false);
if (!autoExpand) setIsExpanded(false);
};
const handleSubmit = async () => {
const markdown = text.trim();
if (!markdown || isSubmitting) return;
setIsSubmitting(true);
try {
await onSubmit(markdown);
reset();
} finally {
setIsSubmitting(false);
}
};
// Collapsed state — the trigger keeps the existing label text.
if (!isExpanded) {
return (
<button
type="button"
onClick={() => setIsExpanded(true)}
disabled={disabled}
className="w-full rounded-lg border border-hairline bg-inset px-4 py-3 text-left text-muted transition-colors hover:border-strong disabled:cursor-not-allowed disabled:opacity-50"
>
{placeholder}
</button>
);
}
const isEmpty = text.trim().length === 0;
return (
<div className="overflow-hidden rounded-lg border border-hairline bg-surface focus-within:border-strong">
<div className="flex flex-col gap-2 p-2.5">
{toolbar && (
<RichToolbar
exec={exec}
onSwitchToMarkdown={() => setToolbar(false)}
compact
/>
)}
<MdTextarea
ref={ref}
value={text}
onValueChange={setText}
placeholder="What are your thoughts?"
minRows={2}
className="px-1"
/>
</div>
<div className="flex items-center justify-between border-t border-hairline bg-inset px-3 py-2">
<AaToggle on={toolbar} onToggle={() => setToolbar(!toolbar)} />
<div className="flex items-center gap-2">
<button
type="button"
onClick={() => {
reset();
onCancel?.();
}}
disabled={isSubmitting}
className="px-4 py-1.5 text-sm text-muted transition-colors hover:text-fg disabled:opacity-50"
>
Cancel
</button>
{/* Stable hook for tests: the submit label ("Reply") is also the
label of every comment's expand-reply button, so picking this one
out by text alone is ambiguous once a thread has nesting. */}
<button
type="button"
data-testid="discussion-submit"
onClick={handleSubmit}
disabled={isSubmitting || isEmpty}
className="primary-button px-4 py-1.5 text-sm"
>
{isSubmitting ? "Submitting..." : submitLabel}
</button>
</div>
</div>
</div>
);
}