import React, { useState, useEffect } from 'react'; import PropTypes from 'prop-types'; import { Link } from 'react-router-dom'; import { UserType } from './Types'; import { info, fetchUserUri } from '../api'; import Avatar from './Avatar'; import Icon from './Icon'; import SearchBox from './SearchBox'; import './UserInfo.css'; let isMounted; export default function UserInfo(props) { const [user, setUser] = useState({ uname: props.user, uid: 0 }); const { onUpdate } = props; useEffect(() => { isMounted = true; if (!user.avatar) { info(user.uname).then(response => { if (isMounted) { setUser(response.data); onUpdate && onUpdate(response.data); } }); } return () => { isMounted = false; }; }, [onUpdate, user, user.avatar]); return ( <>
Was online recently
{ user.uid > 0 && <> PM Recommendations Photos }
{props.children} ); } function Summary({ user }) { const readUrl = `/${user.uname}/friends`; const readersUrl = `/${user.uname}/readers`; const blUrl = `/${user.uname}/bl`; let read = user.read && I read: {user.read.length}; let readers = user.readers && My readers: {user.readers.length}; let mybl = user.statsMyBL && My blacklist: {user.statsMyBL}; let presentItems = [read, readers, mybl].filter(Boolean); return (
{presentItems.length > 0 && presentItems.reduce((prev, curr) => [prev, ' ', curr])}
); } Summary.propTypes = { user: UserType.isRequired }; const UserSummary = React.memo(Summary); export function UserLink(props) { const [user, setUser] = useState(props.user); useEffect(() => { isMounted = true; if (user.uri) { fetchUserUri(user.uri).then(response => { if (isMounted) { setUser(response.data); } }); } return () => { isMounted = false; }; }, [user.uid, user.uri]); return ( user.uid ? {user.uname} : {user.uname} ); } UserInfo.propTypes = { user: PropTypes.string.isRequired, onUpdate: PropTypes.func, children: PropTypes.node }; UserLink.propTypes = { user: UserType.isRequired };