Add mentions in activity center (#152)

This commit is contained in:
Maria Rushkova 2021-12-10 09:08:15 +01:00 committed by GitHub
parent 032d9ae471
commit 6a0678bb02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 5 deletions

View File

@ -1,4 +1,4 @@
import React, { useMemo, useRef, useState } from "react"; import React, { useEffect, useMemo, useRef, useState } from "react";
import styled from "styled-components"; import styled from "styled-components";
import { useActivities } from "../contexts/activityProvider"; import { useActivities } from "../contexts/activityProvider";
@ -9,6 +9,7 @@ import { Activity } from "../models/Activity";
import { equalDate } from "../utils/equalDate"; import { equalDate } from "../utils/equalDate";
import { buttonStyles } from "./Buttons/buttonStyle"; import { buttonStyles } from "./Buttons/buttonStyle";
import { Mention } from "./Chat/ChatMessageContent";
import { import {
ContentWrapper, ContentWrapper,
DateSeparator, DateSeparator,
@ -63,6 +64,32 @@ function ActivityMessage({
[activity.user, contacts] [activity.user, contacts]
); );
const [elements, setElements] = useState<
(string | React.ReactElement | undefined)[]
>([activity.message?.content]);
useEffect(() => {
if (activity.message) {
const split = activity.message?.content.split(" ");
const newSplit = split.flatMap((element, idx) => {
if (element.startsWith("@")) {
return [
<Mention
key={idx}
id={element}
setMentioned={() => true}
className="activity"
/>,
" ",
];
}
return [element, " "];
});
newSplit.pop();
setElements(newSplit);
}
}, [activity.message?.content]);
return ( return (
<MessageOuterWrapper> <MessageOuterWrapper>
<ActivityDate> <ActivityDate>
@ -116,8 +143,10 @@ function ActivityMessage({
</ContextHeading> </ContextHeading>
)} )}
<ActivityText> <ActivityText>
{activity.message?.content || {activity.message?.content && (
(activity.requestType === "income" && activity.request)} <div>{elements.map((el) => el)}</div>
)}
{activity.requestType === "income" && activity.request}
</ActivityText> </ActivityText>
{type === "mention" && {type === "mention" &&
activity.channel && activity.channel &&

View File

@ -15,9 +15,10 @@ import { textMediumStyles, textSmallStyles } from "../Text";
interface MentionProps { interface MentionProps {
id: string; id: string;
setMentioned: (val: boolean) => void; setMentioned: (val: boolean) => void;
className?: string;
} }
function Mention({ id, setMentioned }: MentionProps) { export function Mention({ id, setMentioned, className }: MentionProps) {
const { contacts } = useMessengerContext(); const { contacts } = useMessengerContext();
const contact = useMemo(() => contacts[id.slice(1)], [id, contacts]); const contact = useMemo(() => contacts[id.slice(1)], [id, contacts]);
const [showMenu, setShowMenu] = useState(false); const [showMenu, setShowMenu] = useState(false);
@ -30,7 +31,7 @@ function Mention({ id, setMentioned }: MentionProps) {
}, [contact.id]); }, [contact.id]);
return ( return (
<MentionBLock onClick={() => setShowMenu(!showMenu)}> <MentionBLock onClick={() => setShowMenu(!showMenu)} className={className}>
{`@${contact.customName ?? contact.id}`} {`@${contact.customName ?? contact.id}`}
{showMenu && <ContactMenu id={id.slice(1)} setShowMenu={setShowMenu} />} {showMenu && <ContactMenu id={id.slice(1)} setShowMenu={setShowMenu} />}
</MentionBLock> </MentionBLock>
@ -207,6 +208,13 @@ const MentionBLock = styled.div`
background: ${({ theme }) => theme.mentionHover}; background: ${({ theme }) => theme.mentionHover};
} }
&.activity {
max-width: 488px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
${textMediumStyles} ${textMediumStyles}
`; `;