import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { Box, Text, color, Scroll } from 'folds'; import classNames from 'classnames'; import { useSetAtom } from 'jotai'; import { Icon, Icons } from '../../components/icons'; import { useMatrixClient } from '../../hooks/useMatrixClient'; import { usePowerLevels, PowerLevelsContextProvider } from '../../hooks/usePowerLevels'; import { Page, PageContent } from '../../components/page'; import { ContainerColor } from '../../styles/ContainerColor.css'; import { LobbyHeader } from '../lobby/LobbyHeader'; import { activeThreadIdAtomFamily } from '../../state/activeThread'; import { useForumBoardContext } from './ForumBoardContext'; import { ForumFilterBar } from './ForumFilterBar'; import { ForumPostList } from './ForumPostList'; import { ForumNewPostBox } from './ForumNewPostBox'; import { ForumThreadDetail } from './ForumThreadDetail'; import type { ForumPost, ForumPublishedPost } from './types'; import * as theme from './forumTheme.css'; /** matrixsso-style feed in the Cinny main pane (Paarrot theme + folds icons). */ export function ForumSpaceShell() { const mx = useMatrixClient(); const [optimisticPost, setOptimisticPost] = useState(null); const { forumSpace, sections, threads, query, loadingSections, loadingThreads, error, selectedPostId, activeTopicRoomId, setQueryParam, setCategory, selectPost, loadMorePosts, hasMorePosts, loadingMore, insertThreadFromNewPost, recordThreadReply, } = useForumBoardContext(); const spacePowerLevels = usePowerLevels(forumSpace); const activeTopicRoom = activeTopicRoomId ? mx.getRoom(activeTopicRoomId) : null; const topicSelected = Boolean(query.topic); const setActiveThreadId = useSetAtom( activeThreadIdAtomFamily(activeTopicRoomId ?? forumSpace.roomId), ); useEffect(() => { if (selectedPostId && activeTopicRoomId) { setActiveThreadId(selectedPostId); return () => setActiveThreadId(undefined); } setActiveThreadId(undefined); return undefined; }, [selectedPostId, activeTopicRoomId, setActiveThreadId]); useEffect(() => { if (optimisticPost && optimisticPost.eventId !== selectedPostId) { setOptimisticPost(null); } }, [optimisticPost, selectedPostId]); const selectedThread = useMemo( () => threads.find((t) => t.eventId === selectedPostId), [threads, selectedPostId], ); const getTopicRoom = useCallback( (roomId: string | undefined) => (roomId ? mx.getRoom(roomId) : null), [mx], ); const handlePostPublished = useCallback( (published: ForumPublishedPost) => { const post = insertThreadFromNewPost(published); if (post) { setOptimisticPost(post); } selectPost(published.eventId, published.topicRoomId); }, [insertThreadFromNewPost, selectPost], ); const handleReplySent = useCallback(() => { if (selectedPostId) { recordThreadReply(selectedPostId); } }, [recordThreadReply, selectedPostId]); const showThread = Boolean(selectedPostId && activeTopicRoom); const threadInitialPost = optimisticPost?.eventId === selectedPostId ? optimisticPost : null; const detailDropRef = useRef(null); return (
{error && ( {error} )}
); }