From faa659d7804cc6883f94a96cf13084ee99f2b224 Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Thu, 17 Jun 2021 12:49:19 +1000 Subject: [PATCH] PostItem: refactor, memoize --- .../Home/Post/PostItem/PostItem.tsx | 274 ++++++++---------- 1 file changed, 118 insertions(+), 156 deletions(-) diff --git a/pkg/interface/src/views/landscape/components/Home/Post/PostItem/PostItem.tsx b/pkg/interface/src/views/landscape/components/Home/Post/PostItem/PostItem.tsx index 5e0938706..c8ac4f970 100644 --- a/pkg/interface/src/views/landscape/components/Home/Post/PostItem/PostItem.tsx +++ b/pkg/interface/src/views/landscape/components/Home/Post/PostItem/PostItem.tsx @@ -2,10 +2,11 @@ import { Box, Col, Row, Text } from '@tlon/indigo-react'; import { Association, GraphNode, Group, Post } from '@urbit/api'; import { BigInteger } from 'big-integer'; import { History } from 'history'; -import React, { Ref } from 'react'; +import React, { useCallback, useMemo, useState } from 'react'; +import { useHistory } from 'react-router'; import { getPostRoute } from '~/logic/lib/graph'; import { isWriter } from '~/logic/lib/group'; -import { withHovering } from '~/logic/lib/util'; +import { useHovering } from '~/logic/lib/util'; import { Mention } from '~/views/components/MentionText'; import PostInput from '../PostInput'; import PostContent from './PostContent'; @@ -14,14 +15,13 @@ import PostHeader from './PostHeader'; export interface PostItemProps { association: Association; - baseUrl: string + baseUrl: string; bind?: unknown; graphPath: string; group: Group; history: History; hovering?: boolean; index: BigInteger[]; - innerRef?: Ref; isParent?: boolean; isRelativeTime?: boolean; isReply?: boolean; @@ -33,180 +33,142 @@ export interface PostItemProps { isHierarchical?: boolean; } -interface PostItemState { - inReplyMode: boolean; -} +function PostItem(props: PostItemProps) { + const { + node, + group, + association, + index, + vip, + isHierarchical, + isParent, + isThread, + isLast, + isReply, + isRelativeTime, + parentPost + } = props; -class PostItem extends React.Component { - constructor(props) { - super(props); + const [inReplyMode, setInReplyMode] = useState(false); + const toggleReplyMode = useCallback(() => setInReplyMode(m => !m), []); - this.state = { inReplyMode: false }; - this.toggleReplyMode = this.toggleReplyMode.bind(this); - this.navigateToChildren = this.navigateToChildren.bind(this); - this.submitCallback = this.submitCallback.bind(this); - } - - canWrite() { - const { - group, - association, - vip, - index - } = this.props; + const history = useHistory(); + const canWrite = useMemo(() => { if (vip === '') { return true; } - if (index && index.length > 0) { return true; } - return isWriter(group, association.resource); - } + }, [group, association.resource, vip, index]); - toggleReplyMode() { - this.setState({ inReplyMode: !this.state.inReplyMode }); - } + const navigateToChildren = useCallback(() => { + history.push( + getPostRoute(association.resource, index, !isThread && !isHierarchical) + ); + }, [ + isHierarchical, + history.push, + index, + isParent, + isThread, + association.resource + ]); - navigateToChildren() { - const { - isHierarchical, - history, - index, - isParent, - isThread, - association - } = this.props; - if (isParent) { - return; - } + const postExists = Boolean(node.post) && typeof node.post !== 'string'; + const { hovering, bind } = useHovering(); - history.push(getPostRoute(association.resource, index, !isThread && !isHierarchical)); - } - - submitCallback() { - this.toggleReplyMode(); - } - - render() { - const { - node, - graphPath, - association, - index, - innerRef, - isParent = false, - isReply = false, - isRelativeTime = true, - parentPost, - vip, - group, - hovering, - bind, - isThread, - isLast - } = this.props; - - let indexString = ''; - - index.forEach((i) => { - indexString = indexString + '/' + i.toString(); - }); - - const { inReplyMode } = this.state; - - const canComment = this.canWrite(); - const postExists = Boolean(node.post) && typeof node.post !== 'string'; - - return ( + return ( + - - { (postExists) ? ( - <> - - { ((isReply || (parentPost && index.length > 1 && isParent)) && parentPost?.author) ? ( - - Replying to - - - ) : null } - + {postExists ? ( + <> + + {(isReply || (parentPost && index.length > 1 && isParent)) && + parentPost?.author ? ( + + + Replying to + + + + ) : null} + - - ) : ( - - This post has been deleted - - ) } - - { inReplyMode ? ( - - - - - ) : null } - { isThread && !isLast && !inReplyMode ? ( - - - - ) : null } + + ) : ( + + This post has been deleted + + )} - ); - } + {inReplyMode ? ( + + + + + ) : null} + {isThread && !isLast && !inReplyMode ? ( + + + + ) : null} + + ); } -export default withHovering(PostItem) as React.ComponentType; +export default React.memo(PostItem); +