Style mentions in messages (#127)

This commit is contained in:
Szymon Szlachtowicz 2021-11-15 12:00:57 +01:00 committed by GitHub
parent fe609f0444
commit 045a64a353
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 28 deletions

View File

@ -6,10 +6,6 @@ import { useFetchMetadata } from "../../contexts/fetchMetadataProvider";
import { ChatMessage } from "../../models/ChatMessage"; import { ChatMessage } from "../../models/ChatMessage";
import { Metadata } from "../../models/Metadata"; import { Metadata } from "../../models/Metadata";
import { ImageMenu } from "../Form/ImageMenu"; import { ImageMenu } from "../Form/ImageMenu";
/* eslint-disable no-useless-escape */
const regEx =
/(?:(?:http|https):\/\/)?(?:[-a-z0-9]+\.)+[a-z]+(?::\d+)?(?:(?:\/[-\+~%/\.\w]+)?\/?(?:[&?][-\+=&;%@\.\w]+)?(?:#[\w-]+)?)?/gi;
/* eslint-enable no-useless-escape */
type ChatMessageContentProps = { type ChatMessageContentProps = {
message: ChatMessage; message: ChatMessage;
@ -31,30 +27,26 @@ export function ChatMessageContent({
const [openGraph, setOpenGraph] = useState<Metadata | undefined>(undefined); const [openGraph, setOpenGraph] = useState<Metadata | undefined>(undefined);
useEffect(() => { useEffect(() => {
const split = content.split(regEx); let link;
const newSplit: (string | React.ReactElement)[] = [split[0]]; const split = content.split(" ");
const matches = content.match(regEx); const newSplit = split.flatMap((element, idx) => {
if (matches) { if (element.startsWith("http://") || element.startsWith("https://")) {
matches.forEach((match, idx) => { link = element;
const link = return [
match.startsWith("http://") || match.startsWith("https://") <Link key={idx} onClick={() => setLinkOpen(element)}>
? match {element}
: "https://" + match;
newSplit.push(
<Link key={idx} onClick={() => setLinkOpen(link)}>
{match}
</Link>, </Link>,
split[idx + 1] " ",
); ];
}); }
const match = matches[0]; if (element.startsWith("@")) {
const link = return [<Mention key={idx}>{element}</Mention>, " "];
match.startsWith("http://") || match.startsWith("https://") }
? match return [element, " "];
: "https://" + match; });
setLink(link); newSplit.pop();
setElements(newSplit); setLink(link);
} setElements(newSplit);
}, [content]); }, [content]);
useEffect(() => { useEffect(() => {
@ -166,7 +158,12 @@ const ContentWrapper = styled.div`
flex-direction: column; flex-direction: column;
`; `;
const Link = styled.div` const Mention = styled.span`
color: blue;
font-weight: 500;
`;
const Link = styled.a`
text-decoration: underline; text-decoration: underline;
cursor: pointer; cursor: pointer;
color: ${({ theme }) => theme.memberNameColor}; color: ${({ theme }) => theme.memberNameColor};