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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
import useLocalState from "@/state/state";
import type { Poast } from "@/types/trill";
import Sigil from "@/components/Sigil";
import { useState, useEffect, useRef, type FormEvent } from "react";
import Snippets, { ReplySnippet } from "./Snippets";
import toast from "react-hot-toast";
import Icon from "@/components/Icon";
import { wait } from "@/logic/utils";
function Composer({ isAnon }: { isAnon?: boolean }) {
const { api, composerData, addNotification, setComposerData } = useLocalState(
(s) => ({
api: s.api,
composerData: s.composerData,
addNotification: s.addNotification,
setComposerData: s.setComposerData,
}),
);
const our = api!.airlock.our!;
const [input, setInput] = useState("");
const [isExpanded, setIsExpanded] = useState(false);
const [isLoading, setLoading] = useState(false);
const inputRef = useRef<HTMLInputElement>(null);
useEffect(() => {
if (composerData) {
setIsExpanded(true);
if (
composerData.type === "reply" &&
composerData.post &&
"trill" in composerData.post
) {
const author = composerData.post.trill.author;
setInput(`${author} `);
}
// Auto-focus input when composer opens
setTimeout(() => {
inputRef.current?.focus();
}, 100); // Small delay to ensure the composer is rendered
}
}, [composerData]);
async function poast(e: FormEvent<HTMLFormElement>) {
e.preventDefault();
// TODO
// const parent = replying ? replying : null;
// const tokens = tokenize(input);
// const post: SentPoast = {
// host: parent ? parent.host : our,
// author: our,
// thread: parent ? parent.thread : null,
// parent: parent ? parent.id : null,
// contents: input,
// read: openLock,
// write: openLock,
// tags: input.match(HASHTAGS_REGEX) || [],
// };
// TODO make it user choosable
setLoading(true);
const res =
composerData?.type === "reply" && "trill" in composerData.post
? api!.addReply(
input,
composerData.post.trill.host,
composerData.post.trill.id,
composerData.post.trill.thread || composerData.post.trill.id,
)
: composerData?.type === "quote" && "trill" in composerData.post
? api!.addQuote(input, {
ship: composerData.post.trill.host,
id: composerData.post.trill.id,
})
: !composerData
? api!.addPost(input)
: wait(500);
const ares = await res;
if (ares) {
// // Check for mentions in the post (ship names starting with ~)
const mentions = input.match(/~[a-z-]+/g);
if (mentions) {
mentions.forEach((mention) => {
if (mention !== our) {
// Don't notify self-mentions
addNotification({
type: "mention",
from: our,
message: `You mentioned ${mention} in a post`,
});
}
});
}
// If this is a reply, add notification
if (
composerData?.type === "reply" &&
composerData.post &&
"trill" in composerData.post
) {
if (composerData.post.trill.author !== our) {
addNotification({
type: "reply",
from: our,
message: `You replied to ${composerData.post.trill.author}'s post`,
postId: composerData.post.trill.id,
});
}
}
setInput("");
setComposerData(null); // Clear composer data after successful post
toast.success("post sent");
setIsExpanded(false);
}
}
const placeHolder =
composerData?.type === "reply"
? "Write your reply..."
: composerData?.type === "quote"
? "Add your thoughts..."
: isAnon
? "> be me"
: "What's going on in Urbit";
const clearComposer = (e: React.MouseEvent) => {
e.preventDefault();
setComposerData(null);
setInput("");
setIsExpanded(false);
};
return (
<form
id="composer"
className={`${isExpanded ? "expanded" : ""} ${composerData ? "has-context" : ""}`}
onSubmit={poast}
>
<div className="sigil avatar">
<Sigil patp={our} size={46} />
</div>
<div className="composer-content">
{/* Reply snippets appear above input */}
{composerData && composerData.type === "reply" && (
<div className="composer-context reply-context">
<div className="context-header">
<span className="context-type">
<Icon name="reply" size={14} /> Replying to
</span>
<button
className="clear-context"
onClick={clearComposer}
title="Clear"
type="button"
>
×
</button>
</div>
<ReplySnippet post={composerData.post} />
</div>
)}
{/* Quote context header above input (without snippet) */}
{composerData && composerData.type === "quote" && (
<div className="quote-header">
<div className="context-header">
<span className="context-type">
<Icon name="quote" size={14} /> Quote posting
</span>
<button
className="clear-context"
onClick={clearComposer}
title="Clear"
type="button"
>
×
</button>
</div>
</div>
)}
<div className="composer-input-row">
<input
ref={inputRef}
value={input}
onInput={(e) => setInput(e.currentTarget.value)}
onFocus={() => setIsExpanded(true)}
placeholder={placeHolder}
/>
<button type="submit" disabled={!input.trim()} className="post-btn">
Post
</button>
</div>
{/* Quote snippets appear below input */}
{composerData && composerData.type === "quote" && (
<div className="composer-context quote-context">
<Snippets post={composerData.post} />
</div>
)}
</div>
</form>
);
}
export default Composer;
|