import { h, FunctionComponent } from 'preact'; import { useSelector, useDispatch, shallowEqual } from 'react-redux'; import { useCallback } from 'preact/hooks'; import b from 'bem-react-helper'; import { useIntl } from 'react-intl'; import { Comment as CommentInterface } from 'common/types'; import { getHandleClickProps } from 'common/accessibility'; import { StoreState } from 'store'; import { setCollapse } from 'store/thread/actions'; import { getThreadIsCollapsed } from 'store/thread/getters'; import InView from 'components/root/in-view/in-view'; import { ConnectedComment as Comment } from 'components/comment/connected-comment'; import { CommentForm } from 'components/comment-form'; interface Props { id: CommentInterface['id']; childs?: CommentInterface['id'][]; level: number; mix?: string; getPreview(text: string): Promise; } const commentSelector = (id: string) => (state: StoreState) => { const { theme, comments } = state; const { allComments, childComments } = comments; const comment = allComments[id]; const childs = childComments[id]; const collapsed = getThreadIsCollapsed(comment)(state); return { comment, childs, collapsed, theme }; }; export const Thread: FunctionComponent = ({ id, level, mix, getPreview }) => { const dispatch = useDispatch(); const intl = useIntl(); const { collapsed, comment, childs, theme } = useSelector(commentSelector(id), shallowEqual); const collapse = useCallback(() => { dispatch(setCollapse(id, !collapsed)); }, [id, collapsed, dispatch]); if (comment.hidden) return null; const indented = level > 0; const repliesCount = childs ? childs.length : 0; return (
{(inviewProps) => ( )} {!collapsed && childs && !!childs.length && childs.map((currentId) => ( ))} {level < 6 && (
)}
); };