feat: Integrate forum layout support across various components and enhance room handling logic
This commit is contained in:
@@ -0,0 +1,223 @@
|
||||
// @refresh reset
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { Box, Spinner, Text } from 'folds';
|
||||
import { Room } from 'matrix-js-sdk';
|
||||
import { useMatrixClient } from '../../hooks/useMatrixClient';
|
||||
import { formatForumTimeAgo } from './forumTime';
|
||||
import { loadPostThread } from './forumFeed';
|
||||
import { appendReplyToThread } from './forumTopicHelpers';
|
||||
import type { ForumPost } from './types';
|
||||
import { ForumAuthorIdentity } from './ForumAuthorIdentity';
|
||||
import { ForumMessageBody } from './ForumMessageBody';
|
||||
import { ForumMessageActions } from './ForumMessageActions';
|
||||
import { ForumRichReplyComposer } from './ForumRichReplyComposer';
|
||||
import * as theme from './forumTheme.css';
|
||||
|
||||
function ForumThreadReply({
|
||||
reply,
|
||||
room,
|
||||
threadRootId,
|
||||
roomId,
|
||||
depth,
|
||||
onReplySent,
|
||||
}: {
|
||||
reply: ForumPost;
|
||||
room: Room | null;
|
||||
threadRootId: string;
|
||||
roomId: string;
|
||||
depth: number;
|
||||
onReplySent: (reply: ForumPost) => void;
|
||||
}) {
|
||||
const [replyOpen, setReplyOpen] = useState(false);
|
||||
|
||||
return (
|
||||
<article className={theme.ForumThreadReply} data-event-id={reply.eventId}>
|
||||
<div className={theme.ForumThreadReplyInner}>
|
||||
<header className={theme.ForumPostHeader}>
|
||||
<div className={theme.ForumPostHeaderMain}>
|
||||
<ForumAuthorIdentity room={room} sender={reply.sender} displayName={reply.senderDisplayName} />
|
||||
<time className={theme.ForumPostTime} dateTime={new Date(reply.timestamp).toISOString()}>
|
||||
{formatForumTimeAgo(reply.timestamp)}
|
||||
</time>
|
||||
</div>
|
||||
</header>
|
||||
{reply.replyToDeleted && (
|
||||
<p className={theme.ForumReplyToDeleted} role="note">
|
||||
Replying to a deleted message
|
||||
</p>
|
||||
)}
|
||||
<ForumMessageBody body={reply.body} bodyHtml={reply.bodyHtml} />
|
||||
{!replyOpen && (
|
||||
<div className={theme.ForumReplyActionsFooter}>
|
||||
<ForumMessageActions onReply={() => setReplyOpen(true)} />
|
||||
</div>
|
||||
)}
|
||||
{replyOpen && (
|
||||
<div className={theme.ForumMessageActionPanels}>
|
||||
<ForumRichReplyComposer
|
||||
roomId={roomId}
|
||||
threadRootId={threadRootId}
|
||||
replyToEventId={reply.eventId}
|
||||
onSent={(sentReply) => {
|
||||
setReplyOpen(false);
|
||||
onReplySent(sentReply);
|
||||
}}
|
||||
onCancel={() => setReplyOpen(false)}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
{reply.replies.length > 0 && (
|
||||
<div className={theme.ForumThreadChildren}>
|
||||
{reply.replies.map((child) => (
|
||||
<ForumThreadReply
|
||||
key={child.eventId}
|
||||
reply={child}
|
||||
room={room}
|
||||
threadRootId={threadRootId}
|
||||
roomId={roomId}
|
||||
depth={depth + 1}
|
||||
onReplySent={onReplySent}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</article>
|
||||
);
|
||||
}
|
||||
|
||||
type ForumThreadDetailProps = {
|
||||
roomId: string;
|
||||
rootEventId: string;
|
||||
titleHint?: string;
|
||||
initialPost?: ForumPost | null;
|
||||
onReplySent?: (reply: ForumPost) => void;
|
||||
};
|
||||
|
||||
export function ForumThreadDetail({
|
||||
roomId,
|
||||
rootEventId,
|
||||
titleHint,
|
||||
initialPost,
|
||||
onReplySent,
|
||||
}: ForumThreadDetailProps) {
|
||||
const mx = useMatrixClient();
|
||||
const [post, setPost] = useState<ForumPost | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [rootReplyOpen, setRootReplyOpen] = useState(false);
|
||||
const room = mx.getRoom(roomId);
|
||||
|
||||
const handleReplySent = (reply: ForumPost) => {
|
||||
setPost((current) => {
|
||||
if (!current || !reply.replyToEventId) return current;
|
||||
return appendReplyToThread(current, reply.replyToEventId, reply);
|
||||
});
|
||||
onReplySent?.(reply);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
|
||||
if (initialPost?.eventId === rootEventId) {
|
||||
setPost(initialPost);
|
||||
setError(null);
|
||||
setLoading(false);
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}
|
||||
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
loadPostThread(mx, roomId, rootEventId)
|
||||
.then((loaded) => {
|
||||
if (cancelled) return;
|
||||
setPost(loaded);
|
||||
if (!loaded) setError('Post not found.');
|
||||
})
|
||||
.catch((err) => {
|
||||
if (!cancelled) {
|
||||
setError(err instanceof Error ? err.message : 'Could not load this thread.');
|
||||
}
|
||||
})
|
||||
.finally(() => {
|
||||
if (!cancelled) setLoading(false);
|
||||
});
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [mx, roomId, rootEventId, initialPost]);
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<Box alignItems="Center" gap="200" style={{ padding: '1rem' }}>
|
||||
<Spinner size="200" />
|
||||
<Text size="T300" priority="300">
|
||||
Loading…
|
||||
</Text>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
if (error || !post) {
|
||||
return (
|
||||
<Text as="p" className={theme.ForumListLoading} size="T300" priority="300">
|
||||
{error || 'Post not found.'}
|
||||
</Text>
|
||||
);
|
||||
}
|
||||
|
||||
const title = post.title || titleHint || '(untitled post)';
|
||||
|
||||
return (
|
||||
<article className={theme.ForumTopicDetailRoot} data-event-id={post.eventId}>
|
||||
<header className={theme.ForumTopicDetailHead}>
|
||||
<div className={theme.ForumTopicDetailHeadMain}>
|
||||
<h1 className={theme.ForumDetailTitle}>{title}</h1>
|
||||
<div className={theme.ForumTopicDetailAuthor}>
|
||||
<ForumAuthorIdentity room={room} sender={post.sender} displayName={post.senderDisplayName} />
|
||||
<span className={theme.ForumPostTime}>· {formatForumTimeAgo(post.timestamp)}</span>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
<div className={theme.ForumTopicDetailOp}>
|
||||
<ForumMessageBody body={post.body} bodyHtml={post.bodyHtml} />
|
||||
{!rootReplyOpen && (
|
||||
<div className={theme.ForumReplyActionsFooter}>
|
||||
<ForumMessageActions onReply={() => setRootReplyOpen(true)} />
|
||||
</div>
|
||||
)}
|
||||
{rootReplyOpen && (
|
||||
<div className={theme.ForumMessageActionPanels}>
|
||||
<ForumRichReplyComposer
|
||||
roomId={roomId}
|
||||
threadRootId={rootEventId}
|
||||
replyToEventId={post.eventId}
|
||||
onSent={(sentReply) => {
|
||||
setRootReplyOpen(false);
|
||||
handleReplySent(sentReply);
|
||||
}}
|
||||
onCancel={() => setRootReplyOpen(false)}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
{post.replies.length > 0 && (
|
||||
<div className={theme.ForumThread} id="topicReplyThread">
|
||||
{post.replies.map((reply) => (
|
||||
<ForumThreadReply
|
||||
key={reply.eventId}
|
||||
reply={reply}
|
||||
room={room}
|
||||
threadRootId={rootEventId}
|
||||
roomId={roomId}
|
||||
depth={0}
|
||||
onReplySent={handleReplySent}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</article>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user