diff --git a/src/components/node/NodeRelatedItem/index.tsx b/src/components/node/NodeRelatedItem/index.tsx index ebdfbdf8..982f62e9 100644 --- a/src/components/node/NodeRelatedItem/index.tsx +++ b/src/components/node/NodeRelatedItem/index.tsx @@ -1,10 +1,10 @@ -import React, { FC, memo, useCallback, useState } from 'react'; +import React, { FC, memo, useCallback, useState, useMemo } from 'react'; import * as styles from './styles.scss'; import classNames from 'classnames'; import { INode } from '~/redux/types'; import { URLS, PRESETS } from '~/constants/urls'; import { RouteComponentProps, withRouter } from 'react-router'; -import { getURL } from '~/utils/dom'; +import { getURL, stringToColour } from '~/utils/dom'; type IProps = RouteComponentProps & { item: Partial; @@ -28,6 +28,15 @@ const NodeRelatedItemUnconnected: FC = memo(({ item, history }) => { const [is_loaded, setIsLoaded] = useState(false); 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)) || '', + [] + ); + return (
= memo(({ item, history }) => { >
- {!item.thumbnail &&
{getTitleLetters(item.title)}
} + {!item.thumbnail && ( +
+ {getTitleLetters(item.title)} +
+ )} { + let hash = 0; + + for (let i = 0; i < str.length; i++) { + hash = str.charCodeAt(i) + ((hash << 5) - hash); + } + + let colour = '#'; + + for (let i = 0; i < 3; i++) { + let value = (hash >> (i * 8)) & 0xff; + colour += ('00' + value.toString(16)).substr(-2); + } + + return colour; +}; + +export const darken = (col: string, amt: number) => { + var usePound = false; + + if (col[0] == '#') { + col = col.slice(1); + usePound = true; + } + + var num = parseInt(col, 16); + + var r = (num >> 16) + amt; + + if (r > 255) r = 255; + else if (r < 0) r = 0; + + var b = ((num >> 8) & 0x00ff) + amt; + + if (b > 255) b = 255; + else if (b < 0) b = 0; + + var g = (num & 0x0000ff) + amt; + + if (g > 255) g = 255; + else if (g < 0) g = 0; + + return (usePound ? '#' : '') + (g | (b << 8) | (r << 16)).toString(16); +};