import React, { FC, memo, useCallback, useEffect, useMemo, useRef, useState } from 'react'; import styles from './styles.module.scss'; import classNames from 'classnames'; import { INode } from '~/redux/types'; import { PRESETS, URLS } from '~/constants/urls'; import { RouteComponentProps, withRouter } from 'react-router'; import { getURL, stringToColour } from '~/utils/dom'; import { Avatar } from '~/components/common/Avatar'; type IProps = RouteComponentProps & { item: Partial; }; type CellSize = 'small' | 'medium' | 'large'; const getTitleLetters = (title?: string): string => { const words = (title && title.split(' ')) || []; if (!words.length) return ''; return words.length > 1 ? words .slice(0, 2) .map(word => word[0]) .join('') .toUpperCase() : words[0].substr(0, 2).toUpperCase(); }; const NodeRelatedItemUnconnected: FC = memo(({ item, history }) => { const [is_loaded, setIsLoaded] = useState(false); const [width, setWidth] = useState(0); const ref = useRef(null); const onClick = useCallback(() => history.push(URLS.NODE_URL(item.id)), [item, history]); const thumb = useMemo( () => (item.thumbnail ? getURL({ url: item.thumbnail }, PRESETS.avatar) : ''), [item] ); const backgroundColor = useMemo( () => (!thumb && item.title && stringToColour(item.title)) || '', [] ); useEffect(() => { if (!ref.current) return; const cb = () => setWidth(ref.current!.getBoundingClientRect().width); window.addEventListener('resize', cb); cb(); return () => window.removeEventListener('resize', cb); }, [ref.current]); const size = useMemo(() => { if (width > 90) return 'large'; if (width > 76) return 'medium'; return 'small'; }, [width]); const image = useMemo(() => getURL({ url: item.thumbnail }, PRESETS.avatar), [item.thumbnail]); return (
{!item.thumbnail && size === 'small' && (
{getTitleLetters(item.title)}
)} {!item.thumbnail && size !== 'small' && (
{item.title}
)} loader setIsLoaded(true)} />
); }); const NodeRelatedItem = withRouter(NodeRelatedItemUnconnected); export { NodeRelatedItem };