import type { IMessage } from '@rocket.chat/core-typings';
import { css } from '@rocket.chat/css-in-js';
import { Box, ModalBackdrop, Skeleton } from '@rocket.chat/fuselage';
import { useLocalStorage } from '@rocket.chat/fuselage-hooks';
import {
Contextualbar,
ContextualbarHeader,
ContextualbarAction,
ContextualbarActions,
ContextualbarClose,
ContextualbarBack,
ContextualbarInnerContent,
ContextualbarDialog,
} from '@rocket.chat/ui-client';
import {
useLayoutContextualBarExpanded,
useToastMessageDispatch,
useTranslation,
useUserId,
useRoomToolbox,
} from '@rocket.chat/ui-contexts';
import { createPortal } from 'react-dom';
import ThreadChat from './components/ThreadChat';
import ThreadSkeleton from './components/ThreadSkeleton';
import ThreadTitle from './components/ThreadTitle';
import { useThreadMainMessageQuery } from './hooks/useThreadMainMessageQuery';
import { useToggleFollowingThreadMutation } from './hooks/useToggleFollowingThreadMutation';
import { useGoToThreadList } from '../../hooks/useGoToThreadList';
import ChatProvider from '../../providers/ChatProvider';
export type ThreadProps = {
tmid: IMessage['_id'];
};
const Thread = ({ tmid }: ThreadProps) => {
const goToThreadList = useGoToThreadList({ replace: true });
const { closeTab } = useRoomToolbox();
const mainMessageQueryResult = useThreadMainMessageQuery(tmid, {
onDelete: () => {
closeTab();
},
});
const t = useTranslation();
const dispatchToastMessage = useToastMessageDispatch();
const canExpand = useLayoutContextualBarExpanded();
const [expanded, setExpanded] = useLocalStorage('expand-threads', false);
const uid = useUserId();
const following = uid ? (mainMessageQueryResult.data?.replies?.includes(uid) ?? false) : false;
const toggleFollowingMutation = useToggleFollowingThreadMutation({
onError: (error) => {
dispatchToastMessage({ type: 'error', message: error });
},
});
const handleBackdropClick = () => {
closeTab();
};
const handleGoBack = () => {
goToThreadList();
};
const handleToggleExpand = () => {
setExpanded((expanded) => !expanded);
};
const handleToggleFollowing = () => {
const rid = mainMessageQueryResult.data?.rid;
if (!rid) {
return;
}
toggleFollowingMutation.mutate({ rid, tmid, follow: !following });
};
const handleClose = () => {
closeTab();
};
const isExpanded = canExpand && expanded;
const portalTarget = isExpanded ? document.getElementById('main-content') : null;
const threadContent = (
{(mainMessageQueryResult.isLoading && ) ||
(mainMessageQueryResult.isSuccess && ) ||
null}
{canExpand && (
)}
{(mainMessageQueryResult.isLoading && ) ||
(mainMessageQueryResult.isSuccess && (
)) ||
null}
);
return (
{portalTarget ? (
createPortal(
<>
{threadContent}
>,
portalTarget,
)
) : (
{threadContent}
)}
);
};
export default Thread;