-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMicroblogComposeClient.tsx
More file actions
363 lines (329 loc) · 12.7 KB
/
MicroblogComposeClient.tsx
File metadata and controls
363 lines (329 loc) · 12.7 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
'use client';
import Link from 'next/link';
import { useEffect, useMemo, useState } from 'react';
import { useRouter } from 'next/navigation';
import ImageFieldInput from './ImageFieldInput';
import TxStatus, { type TxPhase } from './TxStatus';
import { fnCreate } from '../lib/app';
import { chainWithRpcOverride, requestWalletAddress } from '../lib/clients';
import { getReadRpcUrl } from '../lib/manifest';
import { submitWriteTx } from '../lib/tx';
import { listOwnedProfiles, loadMicroblogRuntime, profileHandle, profileLabel, type ProfileRecord } from '../lib/microblog';
type ComposeState = {
loading: boolean;
runtimeError: string | null;
connectError: string | null;
submitError: string | null;
};
const PROFILE_STORAGE_PREFIX = 'TH_MICROBLOG_PROFILE_ID:';
export default function MicroblogComposeClient() {
const router = useRouter();
const [state, setState] = useState<ComposeState>({
loading: true,
runtimeError: null,
connectError: null,
submitError: null
});
const [runtime, setRuntime] = useState<any | null>(null);
const [account, setAccount] = useState<string | null>(null);
const [profiles, setProfiles] = useState<ProfileRecord[]>([]);
const [selectedProfileId, setSelectedProfileId] = useState<string>('');
const [body, setBody] = useState('');
const [image, setImage] = useState('');
const [imageUploadBusy, setImageUploadBusy] = useState(false);
const [txStatus, setTxStatus] = useState<string | null>(null);
const [txPhase, setTxPhase] = useState<TxPhase>('idle');
const [txHash, setTxHash] = useState<string | null>(null);
useEffect(() => {
let cancelled = false;
(async () => {
try {
const loadedRuntime = await loadMicroblogRuntime();
if (cancelled) return;
setRuntime(loadedRuntime);
try {
const cached = localStorage.getItem('TH_ACCOUNT');
if (cached && !cancelled) setAccount(cached);
} catch {
// ignore
}
} catch (error: any) {
if (cancelled) return;
setState((prev) => ({ ...prev, runtimeError: String(error?.message ?? error), loading: false }));
return;
}
if (!cancelled) setState((prev) => ({ ...prev, loading: false }));
})();
return () => {
cancelled = true;
};
}, []);
useEffect(() => {
let cancelled = false;
if (!runtime || !account) {
setProfiles([]);
setSelectedProfileId('');
return;
}
setState((prev) => ({ ...prev, loading: true, connectError: null }));
void (async () => {
try {
const ownedProfiles = await listOwnedProfiles(runtime, account);
if (cancelled) return;
setProfiles(ownedProfiles);
let preferred = '';
try {
const stored = localStorage.getItem(`${PROFILE_STORAGE_PREFIX}${account.toLowerCase()}`) ?? '';
if (stored && ownedProfiles.some((entry) => String(entry.id) === stored)) preferred = stored;
} catch {
// ignore
}
if (!preferred && ownedProfiles[0]) preferred = String(ownedProfiles[0].id);
setSelectedProfileId(preferred);
} catch (error: any) {
if (cancelled) return;
setState((prev) => ({ ...prev, connectError: String(error?.message ?? error) }));
} finally {
if (!cancelled) setState((prev) => ({ ...prev, loading: false }));
}
})();
return () => {
cancelled = true;
};
}, [runtime, account]);
useEffect(() => {
if (!account || !selectedProfileId) return;
try {
localStorage.setItem(`${PROFILE_STORAGE_PREFIX}${account.toLowerCase()}`, selectedProfileId);
} catch {
// ignore
}
}, [account, selectedProfileId]);
const selectedProfile = useMemo(
() => profiles.find((entry) => String(entry.id) === selectedProfileId) ?? null,
[profiles, selectedProfileId]
);
const walletChain = useMemo(
() => (runtime ? chainWithRpcOverride(runtime.chain, getReadRpcUrl(runtime.manifest) || undefined) : null),
[runtime]
);
async function connectWallet() {
if (!walletChain) return;
setState((prev) => ({ ...prev, connectError: null }));
try {
const nextAccount = await requestWalletAddress(walletChain);
setAccount(nextAccount);
try {
localStorage.setItem('TH_ACCOUNT', nextAccount);
} catch {
// ignore
}
} catch (error: any) {
setState((prev) => ({ ...prev, connectError: String(error?.message ?? error) }));
}
}
async function submit() {
if (!runtime || !walletChain || !selectedProfile || !body.trim() || imageUploadBusy) return;
setState((prev) => ({ ...prev, submitError: null }));
setTxStatus(null);
setTxPhase('idle');
setTxHash(null);
try {
const result = await submitWriteTx({
manifest: runtime.manifest,
deployment: runtime.deployment,
chain: walletChain,
publicClient: runtime.publicClient,
address: runtime.appAddress,
abi: runtime.abi,
functionName: fnCreate('Post'),
contractArgs: [
{
authorProfile: selectedProfile.id,
body: body.trim(),
image: image.trim()
}
],
setStatus: setTxStatus,
onPhase: setTxPhase,
onHash: setTxHash
});
setTxStatus(`Posted (${result.hash.slice(0, 10)}…).`);
router.push('/');
router.refresh();
} catch (error: any) {
setState((prev) => ({ ...prev, submitError: String(error?.message ?? error) }));
setTxStatus(null);
setTxPhase('failed');
}
}
if (state.loading && !runtime) {
return (
<section className="card">
<h2>Loading composer…</h2>
<p className="muted">Resolving the active deployment and wallet state.</p>
</section>
);
}
if (state.runtimeError) {
return (
<section className="card">
<div className="eyebrow">/compose/error</div>
<h2>Unable to load composer</h2>
<p className="muted">{state.runtimeError}</p>
</section>
);
}
return (
<div className="pageStack">
<section className="card heroPanel">
<div className="heroSplit">
<div>
<div className="heroTopline">
<span className="eyebrow">/post/compose</span>
<div className="chipRow">
<span className="badge">normalized author identity</span>
<span className="badge">profile-linked posts</span>
</div>
</div>
<h2 className="displayTitle">
Compose as a profile
<br />
<span>not as a copied handle string.</span>
</h2>
<p className="lead">
Posts now store <span className="badge">authorProfile</span> as an on-chain reference to <span className="badge">Profile</span>,
so handle and avatar changes flow through existing posts automatically.
</p>
<div className="actionGroup">
<Link className="btn" href="/">Back to feed</Link>
<Link className="btn" href="/Profile/">Browse profiles</Link>
</div>
</div>
<div className="heroDataPanel">
<div className="eyebrow">/identity</div>
<div className="heroStatGrid">
<div className="heroStat">
<div className="heroStatValue">{account ? 1 : 0}</div>
<div className="heroStatLabel">Wallet linked</div>
</div>
<div className="heroStat">
<div className="heroStatValue">{profiles.length}</div>
<div className="heroStatLabel">Owned profiles</div>
</div>
</div>
<div className="heroMeta">
<span className="badge">posts reference profiles</span>
<span className="badge">profile changes propagate</span>
</div>
</div>
</div>
</section>
{!account ? (
<section className="card">
<div className="eyebrow">/wallet</div>
<h3>Connect a wallet to compose</h3>
<p className="muted">Posting now requires selecting one of your on-chain profiles. Connect the wallet that owns the profile first.</p>
<div className="actionGroup">
<button className="btn primary" onClick={() => void connectWallet()}>Connect wallet</button>
</div>
{state.connectError ? <p className="muted">{state.connectError}</p> : null}
</section>
) : null}
{account && !profiles.length ? (
<section className="card">
<div className="eyebrow">/profiles/empty</div>
<h3>No owned profiles found</h3>
<p className="muted">Create a profile first. Once it exists on-chain under this wallet, you can compose posts as that profile.</p>
<div className="actionGroup">
<Link className="btn primary" href="/Profile/?mode=new">Create profile</Link>
</div>
{state.connectError ? <p className="muted">{state.connectError}</p> : null}
</section>
) : null}
{account && profiles.length ? (
<section className="card" style={{ display: 'grid', gap: 18 }}>
<div>
<h2>Compose Post</h2>
<p className="muted">Choose the on-chain profile identity for this post, then write the post body and optional image.</p>
</div>
<div className="formGrid">
<div className="fieldGroup">
<label className="label">Profile</label>
<select
className="select"
value={selectedProfileId}
onChange={(event) => setSelectedProfileId(event.target.value)}
>
{profiles.map((entry) => (
<option key={String(entry.id)} value={String(entry.id)}>
{profileLabel(entry.record)}
</option>
))}
</select>
</div>
<div className="fieldGroup">
<label className="label">Current identity</label>
<div className="recordPreviewCell" style={{ minHeight: 110 }}>
{selectedProfile ? (
<div style={{ display: 'grid', gap: 10 }}>
<div className="chipRow">
<span className="badge">profile #{String(selectedProfile.id)}</span>
{profileHandle(selectedProfile.record) ? <span className="badge">@{profileHandle(selectedProfile.record)}</span> : null}
</div>
<strong>{profileLabel(selectedProfile.record)}</strong>
{String(selectedProfile.record?.bio ?? '').trim() ? (
<p className="muted" style={{ margin: 0 }}>{String(selectedProfile.record.bio)}</p>
) : null}
</div>
) : (
<span className="muted">Select a profile.</span>
)}
</div>
</div>
<div className="fieldGroup">
<label className="label">Body <span className="badge">required</span></label>
<textarea
className="input"
value={body}
onChange={(event) => setBody(event.target.value)}
placeholder="Share something on-chain. Hashtags like #tokenhost or #microblog will be indexed automatically."
rows={6}
style={{ resize: 'vertical', minHeight: 160 }}
/>
</div>
<div className="fieldGroup">
<label className="label">Image</label>
<ImageFieldInput
manifest={runtime?.manifest ?? null}
value={image}
onChange={setImage}
onBusyChange={setImageUploadBusy}
/>
</div>
</div>
<div className="actionGroup">
<button
className="btn primary"
onClick={() => void submit()}
disabled={
!selectedProfile ||
!body.trim() ||
imageUploadBusy ||
txPhase === 'submitting' ||
txPhase === 'submitted' ||
txPhase === 'confirming'
}
>
{imageUploadBusy ? 'Waiting for image upload…' : 'Publish post'}
</button>
<Link className="btn" href="/">Cancel</Link>
</div>
{txStatus ? <div className="muted">{txStatus}</div> : null}
<TxStatus phase={txPhase} hash={txHash} chainId={Number(runtime?.deployment?.chainId ?? NaN)} error={state.submitError} />
{state.submitError ? <div className="pre">{state.submitError}</div> : null}
</section>
) : null}
</div>
);
}