aboutsummaryrefslogblamecommitdiff
path: root/vnext/src/ui/UserInfo.js
blob: f71dfcdc76200f00809a9bb3a09ee747163b1391 (plain) (tree)
1
2
3
4
5
6
7
8
9
                                                         
 
                                           
 

                                               
 
             
 
   
                      
                                                                                               
   
                                                                 
                                      
                     
              
      
                     
                        
                                      
                                                   
             
                             
                      

                             





                                                                     
                                       


                                   
                                                       

                                                                
                                                                                

                                                                            
                                                                            

                                                                   

                       
                      
           
     
 
 


                                               
                            





                                                                                                                 
                                     
                                                                           
                                   
              
     
 
                                 
 



                                                
                                 
                                                
                     
                        
                                  
                                                                   
                                
                               
                                                                         
                                                
                      
                 
                            



                                               
                  
         
                      

                             
                  




                                                                                
     
 
import { memo, useState, useEffect, useRef } from 'react'
import { Link } from 'react-router-dom'

import { info, fetchUserUri } from '../api'

import Avatar from './Avatar'
import Icon from './Icon'
import defaultAvatar from '../assets/av-96.png'

let isMounted

/**
 * User info component
 * @param {{uname: string, onUpdate?: Function, children?: import('react').ReactElement}} props
 */
export default function UserInfo({ uname, onUpdate, children }) {
    const [user, setUser] = useState({
        uname: uname,
        uid: 0
    })
    useEffect(() => {
        isMounted = true
        info(uname).then(response => {
            if (isMounted) {
                onUpdate && onUpdate(response.data)
                setUser(response.data)
            }
        }).catch(console.log)
        return () => {
            isMounted = false
        }
    }, [onUpdate, uname])
    return (
        <>
            <div className="userinfo">
                <Avatar user={user}>
                    <div className="msg-ts">Was online recently</div>
                </Avatar>
            </div>
            <UserSummary user={user} />
            <div className="l">
                {
                    user.uid > 0 &&
                    <>
                        <Link to={`/pm/${user.uname}`}>
                            <Icon name="ei-envelope" size="s" />
                            <span className="desktop">PM</span>
                        </Link>
                        <Link to={`/${user.uname}/?show=recomm`} rel="nofollow">
                            <Icon name="ei-heart" size="s" />
                            <span className="desktop">Recommendations</span>
                        </Link>
                        <Link to={`/${user.uname}/?media=1`} rel="nofollow">
                            <Icon name="ei-camera" size="s" />
                            <span className="desktop">Photos</span>
                        </Link>
                    </>
                }
            </div>
            {children}
        </>
    )
}

/**
 * User summary component
 * @param {{user: import('../api').User}} props
 */
function Summary({ user }) {
    const readUrl = `/${user.uname}/friends`
    const readersUrl = `/${user.uname}/readers`
    const blUrl = `/${user.uname}/bl`
    let read = user.read && <Link key={readUrl} to={readUrl}>I read: {user.read.length}</Link>
    let readers = user.readers && <Link key={readersUrl} to={readersUrl}>My readers: {user.readers.length}</Link>
    let mybl = user.statsMyBL && <Link key={blUrl} to={blUrl}>My blacklist: {user.statsMyBL}</Link>
    let presentItems = [read, readers, mybl].filter(Boolean)
    return (
        <div className="msg-summary">
            {presentItems.length > 0 && presentItems.reduce((prev, curr) =>
                [prev, ' ', curr])}
        </div>
    )
}

const UserSummary = memo(Summary)


/**
 * Link to user
 * @param {{ user: import('../api').User}} props
 */
export function UserLink(props) {
    const [user, setUser] = useState(props.user)
    const userRef = useRef(user)
    useEffect(() => {
        isMounted = true
        if (userRef.current.uri) {
            fetchUserUri(userRef.current.uri).then(remote_user => {
                if (isMounted) {
                    setUser({
                        uid: 0,
                        uname: remote_user.preferredUsername,
                        avatar: remote_user.icon && remote_user.icon.url,
                        uri: userRef.current.uri
                    })
                }
            }).catch(() => {
                setUser({
                    uid: 0,
                    uname: userRef.current.uri,
                    uri: userRef.current.uri,
                    avatar: defaultAvatar
                })
            })
        }
        return () => {
            isMounted = false
        }
    }, [props.user])
    return (
        user.uid ?
            <Link key={user.uid} to={`/${user.uname}/`} className="info-avatar">
                <img src={user.avatar} />{user.uname}
            </Link>
            : <a href={user.uri} className="info-avatar">
                <img src={user.avatar || defaultAvatar} />{user.uname}
            </a>
    )
}