Add link highliting (#24)

This commit is contained in:
Szymon Szlachtowicz 2021-09-30 14:18:29 +02:00 committed by GitHub
parent d260c51939
commit b2f8b24fc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 75 additions and 30 deletions

View File

@ -0,0 +1,39 @@
import React, { useEffect, useState } from "react";
/* 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 = {
content: string;
};
export function ChatMessageContent({ content }: ChatMessageContentProps) {
const [elements, setElements] = useState<(string | React.ReactElement)[]>([
content,
]);
useEffect(() => {
const split = content.split(regEx);
const newSplit: (string | React.ReactElement)[] = [split[0]];
const matches = content.match(regEx);
if (matches) {
matches.forEach((match, idx) => {
const link =
match.startsWith("http://") || match.startsWith("https://")
? match
: "https://" + match;
newSplit.push(
<a key={idx} href={link}>
{match}
</a>,
split[idx + 1]
);
});
setElements(newSplit);
}
}, [content]);
return <>{elements.map((el) => el)}</>;
}

View File

@ -5,6 +5,8 @@ import { ChatMessage } from "../../models/ChatMessage";
import { Theme } from "../../styles/themes"; import { Theme } from "../../styles/themes";
import { UserIcon } from "../Icons/UserIcon"; import { UserIcon } from "../Icons/UserIcon";
import { ChatMessageContent } from "./ChatMessageContent";
type ChatMessagesProps = { type ChatMessagesProps = {
messages: ChatMessage[]; messages: ChatMessage[];
theme: Theme; theme: Theme;
@ -18,38 +20,42 @@ export function ChatMessages({ messages, theme }: ChatMessagesProps) {
ref.current.scrollTop = ref.current.scrollHeight; ref.current.scrollTop = ref.current.scrollHeight;
} }
}, [messages]); }, [messages]);
return ( return (
<MessagesWrapper ref={ref}> <MessagesWrapper ref={ref}>
{messages.map((message, idx) => ( {messages.map((message, idx) => {
<MessageOuterWrapper> return (
{(idx === 0 || <MessageOuterWrapper key={idx}>
messages[idx - 1].date.getDay() != messages[idx].date.getDay()) && ( {(idx === 0 ||
<DateSeparator> messages[idx - 1].date.getDay() !=
{message.date.getDay() === today messages[idx].date.getDay()) && (
? "Today" <DateSeparator>
: message.date.toLocaleDateString()} {message.date.getDay() === today
</DateSeparator> ? "Today"
)} : message.date.toLocaleDateString()}
<MessageWrapper key={idx}> </DateSeparator>
<Icon> )}
<UserIcon theme={theme} /> <MessageWrapper>
</Icon> <Icon>
<UserIcon theme={theme} />
</Icon>
<ContentWrapper> <ContentWrapper>
<MessageHeaderWrapper> <MessageHeaderWrapper>
<UserNameWrapper theme={theme}> <UserNameWrapper theme={theme}>
{message.sender.slice(0, 10)} {message.sender.slice(0, 10)}
</UserNameWrapper> </UserNameWrapper>
<TimeWrapper theme={theme}> <TimeWrapper theme={theme}>
{message.date.toLocaleString()} {message.date.toLocaleString()}
</TimeWrapper> </TimeWrapper>
</MessageHeaderWrapper> </MessageHeaderWrapper>
<MessageText theme={theme}>{message.content}</MessageText> <MessageText theme={theme}>
</ContentWrapper> <ChatMessageContent content={message.content} />
</MessageWrapper> </MessageText>
</MessageOuterWrapper> </ContentWrapper>
))} </MessageWrapper>
</MessageOuterWrapper>
);
})}
</MessagesWrapper> </MessagesWrapper>
); );
} }

View File

@ -69,7 +69,7 @@ export function useMessenger(chatId: string, chatIdList: string[]) {
const messenger = await Messenger.create(identity, { const messenger = await Messenger.create(identity, {
bootstrap: getBootstrapNodes.bind({}, [ bootstrap: getBootstrapNodes.bind({}, [
"fleets", "fleets",
"wakuv2.prod", "wakuv2.test",
"waku-websocket", "waku-websocket",
]), ]),
}); });