Chat mention thing

This commit is contained in:
MyNameIsBatman 2021-06-30 00:35:05 -03:00
parent e00dd44888
commit 0b0f877794
3 changed files with 54 additions and 3 deletions

View File

@ -11,7 +11,7 @@
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<title>React App</title>
<title>Nitro</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>

View File

@ -15,6 +15,18 @@
}
}
.chat-mention {
padding: 1px 5px;
font-weight: bold;
background: rgba(0, 0, 0, .2);
cursor: pointer;
border-radius: 100px;
&:hover {
color: blue;
}
}
.bubble-container {
position: absolute;
width: fit-content;

View File

@ -1,10 +1,12 @@
import { FC, MouseEvent, useCallback, useEffect, useRef, useState } from 'react';
import { GetSessionDataManager } from '../../../../../api';
import { ChatWidgetMessageViewProps } from './ChatWidgetMessageView.types';
export const ChatWidgetMessageView: FC<ChatWidgetMessageViewProps> = props =>
{
const { chat = null, makeRoom = null } = props;
const [ isVisible, setIsVisible ] = useState(false);
const [ isVisible, setIsVisible ] = useState(true);
const [ messageParts, setMessageParts ] = useState<{text: string, className?: string, style?: any, onClick?: () => void}[]>(null);
const elementRef = useRef<HTMLDivElement>();
const onClick = useCallback((event: MouseEvent) =>
@ -12,6 +14,38 @@ export const ChatWidgetMessageView: FC<ChatWidgetMessageViewProps> = props =>
}, []);
useEffect(() =>
{
if(messageParts) return;
const userNameMention = '@' + GetSessionDataManager().userName;
const matches = [...chat.text.matchAll(new RegExp(userNameMention + '\\b', 'gi'))];
if(matches.length > 0)
{
const prevText = chat.text.substr(0, matches[0].index);
const postText = chat.text.substring(matches[0].index + userNameMention.length, chat.text.length);
setMessageParts(
[
{ text: prevText },
{ text: userNameMention, className: 'chat-mention', onClick: () => {alert('I clicked in the mention')}},
{ text: postText }
]
);
}
else
{
setMessageParts(
[
{ text: chat.text }
]
);
}
}, [ chat ]);
useEffect(() =>
{
const element = elementRef.current;
@ -62,7 +96,12 @@ export const ChatWidgetMessageView: FC<ChatWidgetMessageViewProps> = props =>
{ (chat.imageUrl && (chat.imageUrl !== '')) && <div className="user-image" style={ { backgroundImage: 'url(' + chat.imageUrl + ')' } } /> }
</div>
<div className="chat-content">
<b className="username mr-1">{ chat.username }</b><span className="message"> { chat.text }</span>
<b className="username mr-1">{ chat.username }</b><span className="message"> {
messageParts && messageParts.map((part, index) =>
{
return <span key={ index } className={ part.className } style={ part.style } onClick={ part.onClick }>{ part.text }</span>
})
}</span>
</div>
<div className="pointer"></div>
</div>