2021-07-20 08:02:02 +03:00
// This displays a clickable user name (or whatever children element you provide), and displays a simple tooltip of created time. OnClick a modal with more information about the user is displayed.
2022-09-07 10:00:28 +03:00
import { useState , ReactNode , FC } from 'react' ;
2023-01-11 03:39:12 +03:00
import { Divider , Modal , Typography , Row , Col , Space , Tooltip } from 'antd' ;
2024-03-07 08:13:13 +03:00
import { format , formatDistanceToNow } from 'date-fns' ;
2021-08-18 03:38:10 +03:00
import { uniq } from 'lodash' ;
2022-09-07 10:00:28 +03:00
import { BanUserButton } from './BanUserButton' ;
import { ModeratorUserButton } from './ModeratorUserButton' ;
2021-07-20 08:02:02 +03:00
2023-01-10 07:57:29 +03:00
import { User , UserConnectionInfo } from '../../types/chat' ;
2022-09-07 10:00:28 +03:00
import { formatDisplayDate } from './UserTable' ;
2023-01-10 07:57:29 +03:00
import { formatUAstring } from '../../utils/format' ;
2021-07-20 08:02:02 +03:00
2022-09-07 10:00:28 +03:00
export type UserPopoverProps = {
2021-07-20 08:02:02 +03:00
user : User ;
connectionInfo? : UserConnectionInfo | null ;
children : ReactNode ;
2022-09-07 10:00:28 +03:00
} ;
2021-07-20 08:02:02 +03:00
2022-09-07 10:00:28 +03:00
export const UserPopover : FC < UserPopoverProps > = ( { user , connectionInfo , children } ) = > {
2022-11-02 00:50:23 +03:00
const [ isModalOpen , setIsModalOpen ] = useState ( false ) ;
2021-07-20 08:02:02 +03:00
const handleShowModal = ( ) = > {
2022-11-02 00:50:23 +03:00
setIsModalOpen ( true ) ;
2021-07-20 08:02:02 +03:00
} ;
const handleCloseModal = ( ) = > {
2022-11-02 00:50:23 +03:00
setIsModalOpen ( false ) ;
2021-07-20 08:02:02 +03:00
} ;
const { displayName , createdAt , previousNames , nameChangedAt , disabledAt } = user ;
const { connectedAt , messageCount , userAgent } = connectionInfo || { } ;
let lastNameChangeDate = null ;
const nameList = previousNames && [ . . . previousNames ] ;
if ( previousNames && previousNames . length > 1 && nameChangedAt ) {
lastNameChangeDate = new Date ( nameChangedAt ) ;
// reverse prev names for display purposes
nameList . reverse ( ) ;
}
const dateObject = new Date ( createdAt ) ;
const createdAtDate = format ( dateObject , 'PP pp' ) ;
const lastNameChangeDuration = lastNameChangeDate
? formatDistanceToNow ( lastNameChangeDate )
: null ;
return (
< >
< Tooltip
title = {
< >
Created at : { createdAtDate } .
< br / > Click for more info .
< / >
}
placement = "bottomLeft"
>
< button
type = "button"
aria - label = "Display more details about this user"
className = "user-item-container"
onClick = { handleShowModal }
>
{ children }
< / button >
< / Tooltip >
< Modal
destroyOnClose
width = { 600 }
cancelText = "Close"
okButtonProps = { { style : { display : 'none' } } }
title = { ` User details: ${ displayName } ` }
2022-11-02 00:50:23 +03:00
open = { isModalOpen }
2021-07-20 08:02:02 +03:00
onOk = { handleCloseModal }
onCancel = { handleCloseModal }
>
< div className = "user-details" >
< Typography.Title level = { 4 } > { displayName } < / Typography.Title >
< p className = "created-at" > User created at { createdAtDate } . < / p >
< Row gutter = { 16 } >
{ connectionInfo && (
< Col md = { lastNameChangeDate ? 12 : 24 } >
< Typography.Title level = { 5 } >
This user is currently connected to Chat .
< / Typography.Title >
< ul className = "connection-info" >
< li >
< strong > Active for : < / strong > { formatDistanceToNow ( new Date ( connectedAt ) ) }
< / li >
< li >
< strong > Messages sent : < / strong > { messageCount }
< / li >
< li >
< strong > User Agent : < / strong >
< br / >
{ formatUAstring ( userAgent ) }
< / li >
< / ul >
< / Col >
) }
{ lastNameChangeDate && (
< Col md = { connectionInfo ? 12 : 24 } >
< Typography.Title level = { 5 } > This user is also seen as : < / Typography.Title >
< ul className = "previous-names-list" >
2021-08-18 03:38:10 +03:00
{ uniq ( nameList ) . map ( ( name , index ) = > (
2021-07-20 08:02:02 +03:00
< li className = { index === 0 ? 'latest' : '' } >
< span className = "user-name-item" > { name } < / span >
{ index === 0 ? ` (Changed ${ lastNameChangeDuration } ago) ` : '' }
< / li >
) ) }
< / ul >
< / Col >
) }
< / Row >
< Divider / >
2022-01-13 00:52:37 +03:00
< Space direction = "horizontal" >
{ disabledAt ? (
< >
This user was banned on < code > { formatDisplayDate ( disabledAt ) } < / code > .
< br / >
< br / >
2022-09-07 10:00:28 +03:00
< BanUserButton
2022-01-13 00:52:37 +03:00
label = "Unban this user"
user = { user }
isEnabled = { false }
onClick = { handleCloseModal }
/ >
< / >
) : (
2022-09-07 10:00:28 +03:00
< BanUserButton
2022-01-13 00:52:37 +03:00
label = "Ban this user"
2021-07-20 08:02:02 +03:00
user = { user }
2022-01-13 00:52:37 +03:00
isEnabled
2021-07-20 08:02:02 +03:00
onClick = { handleCloseModal }
/ >
2022-01-13 00:52:37 +03:00
) }
2022-09-07 10:00:28 +03:00
< ModeratorUserButton user = { user } onClick = { handleCloseModal } / >
2022-01-13 00:52:37 +03:00
< / Space >
2021-07-20 08:02:02 +03:00
< / div >
< / Modal >
< / >
) ;
2022-09-07 10:00:28 +03:00
} ;
2021-07-20 08:02:02 +03:00
UserPopover . defaultProps = {
connectionInfo : null ,
} ;