Style mentions in messages (#127)
This commit is contained in:
parent
fe609f0444
commit
045a64a353
|
@ -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]
|
" ",
|
||||||
);
|
];
|
||||||
|
}
|
||||||
|
if (element.startsWith("@")) {
|
||||||
|
return [<Mention key={idx}>{element}</Mention>, " "];
|
||||||
|
}
|
||||||
|
return [element, " "];
|
||||||
});
|
});
|
||||||
const match = matches[0];
|
newSplit.pop();
|
||||||
const link =
|
|
||||||
match.startsWith("http://") || match.startsWith("https://")
|
|
||||||
? match
|
|
||||||
: "https://" + match;
|
|
||||||
setLink(link);
|
setLink(link);
|
||||||
setElements(newSplit);
|
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};
|
||||||
|
|
Loading…
Reference in New Issue