Changeset View
Changeset View
Standalone View
Standalone View
web/sidebar/community-drawer-item-community.react.js
// @flow | // @flow | ||||
import classnames from 'classnames'; | import classnames from 'classnames'; | ||||
import * as React from 'react'; | import * as React from 'react'; | ||||
import { useResolvedThreadInfo } from 'lib/utils/entity-helpers.js'; | |||||
import css from './community-drawer-item.css'; | import css from './community-drawer-item.css'; | ||||
import type { DrawerItemProps } from './community-drawer-item.react.js'; | import type { DrawerItemProps } from './community-drawer-item.react.js'; | ||||
import CommunityDrawerItem from './community-drawer-item.react.js'; | import { | ||||
getChildren, | |||||
getExpandButton, | |||||
} from './community-drawer-utils.react.js'; | |||||
import ThreadAvatar from '../components/thread-avatar.react.js'; | |||||
function CommunityDrawerItemCommunity(props: DrawerItemProps): React.Node { | function CommunityDrawerItemCommunity(props: DrawerItemProps): React.Node { | ||||
const { | |||||
itemData: { threadInfo, itemChildren, hasSubchannelsButton, labelStyle }, | |||||
expanded, | |||||
toggleExpanded, | |||||
paddingLeft, | |||||
expandable = true, | |||||
handler: Handler, | |||||
} = props; | |||||
const children = React.useMemo( | |||||
() => | |||||
getChildren({ | |||||
expanded, | |||||
hasSubchannelsButton, | |||||
itemChildren, | |||||
paddingLeft, | |||||
threadInfo, | |||||
expandable, | |||||
handler: Handler, | |||||
}), | |||||
[ | |||||
expanded, | |||||
hasSubchannelsButton, | |||||
itemChildren, | |||||
paddingLeft, | |||||
threadInfo, | |||||
expandable, | |||||
Handler, | |||||
], | |||||
); | |||||
const itemExpandButton = React.useMemo( | |||||
() => | |||||
getExpandButton({ | |||||
expandable, | |||||
childrenLength: itemChildren?.length, | |||||
hasSubchannelsButton, | |||||
expanded, | |||||
}), | |||||
[expandable, itemChildren?.length, hasSubchannelsButton, expanded], | |||||
); | |||||
const [handler, setHandler] = React.useState({ | |||||
// eslint-disable-next-line no-unused-vars | |||||
onClick: event => {}, | |||||
isActive: false, | |||||
}); | |||||
const onClick = React.useCallback( | |||||
(event: SyntheticEvent<HTMLElement>) => { | |||||
toggleExpanded?.(threadInfo.id); | |||||
handler.onClick(event); | |||||
}, | |||||
[threadInfo.id, toggleExpanded, handler], | |||||
); | |||||
const classes = classnames({ | const classes = classnames({ | ||||
[css.communityBase]: true, | [css.communityBase]: true, | ||||
[css.communityExpanded]: props.expanded, | [css.communityExpanded]: props.expanded, | ||||
}); | }); | ||||
const { uiName } = useResolvedThreadInfo(threadInfo); | |||||
const titleLabel = classnames({ | |||||
[css[labelStyle]]: true, | |||||
[css.activeTitle]: handler.isActive, | |||||
}); | |||||
const style = React.useMemo(() => ({ paddingLeft }), [paddingLeft]); | |||||
return ( | return ( | ||||
<div className={classes}> | <div className={classes}> | ||||
<CommunityDrawerItem {...props} /> | <Handler setHandler={setHandler} threadInfo={threadInfo} /> | ||||
<a onClick={onClick} className={css.threadEntry} style={style}> | |||||
{itemExpandButton} | |||||
<div className={css.titleWrapper}> | |||||
<ThreadAvatar size="micro" threadInfo={threadInfo} /> | |||||
<div className={titleLabel}>{uiName}</div> | |||||
</div> | |||||
</a> | |||||
<div className={css.threadListContainer}>{children}</div> | |||||
</div> | </div> | ||||
); | ); | ||||
} | } | ||||
const MemoizedCommunityDrawerItemCommunity: React.ComponentType<DrawerItemProps> = | const MemoizedCommunityDrawerItemCommunity: React.ComponentType<DrawerItemProps> = | ||||
React.memo(CommunityDrawerItemCommunity); | React.memo(CommunityDrawerItemCommunity); | ||||
export default MemoizedCommunityDrawerItemCommunity; | export default MemoizedCommunityDrawerItemCommunity; |