Skip to content

Commit

Permalink
Merge branch 'develop' into feat/#485-files-menu
Browse files Browse the repository at this point in the history
  • Loading branch information
umangutkarsh authored Mar 3, 2024
2 parents c9ba1f2 + da8b930 commit a468e2e
Show file tree
Hide file tree
Showing 11 changed files with 314 additions and 13 deletions.
20 changes: 20 additions & 0 deletions packages/api/src/EmbeddedChatApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,26 @@ export default class EmbeddedChatApi {
}
}

async getMentionedMessages() {
try {
const { userId, authToken } = (await this.auth.getCurrentUser()) || {};
const response = await fetch(
`${this.host}/api/v1/chat.getMentionedMessages?roomId=${this.rid}`,
{
headers: {
"Content-Type": "application/json",
"X-Auth-Token": authToken,
"X-User-Id": userId,
},
method: "GET",
}
);
return await response.json();
} catch (err) {
console.error(err);
}
}

async pinMessage(mid: string) {
try {
const { userId, authToken } = (await this.auth.getCurrentUser()) || {};
Expand Down
52 changes: 40 additions & 12 deletions packages/react/src/components/Attachments/PinnedAttachment.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,46 @@
import React from 'react';
import React, { useContext } from 'react';
import PropTypes from 'prop-types';
import { Box } from '../Box';
import { Avatar } from '../Avatar';
import RCContext from '../../context/RCInstance';

const PinnedAttachment = ({ attachment }) => (
<Box
style={{
borderInlineStart: '1px solid currentColor',
paddingLeft: '0.8rem',
}}
>
<Box>{attachment?.author_name}</Box>
<Box>{attachment?.text}</Box>
</Box>
);
const PinnedAttachment = ({ attachment }) => {
const { RCInstance } = useContext(RCContext);
const getUserAvatarUrl = (authorIcon) => {
const host = RCInstance.getHost();
const URL = `${host}${authorIcon}`;
return URL;
};
return (
<Box
style={{
borderInlineStart: '1px solid currentColor',
paddingLeft: '0.8rem',
}}
>
<Box
style={{
display: 'flex',
gap: '0.3rem',
}}
>
<Avatar
url={getUserAvatarUrl(attachment?.author_icon)}
alt="avatar"
size="1.2em"
/>
<Box>{attachment?.author_name}</Box>
</Box>
<Box
style={{
marginTop: '0.7rem',
}}
>
{attachment?.text}
</Box>
</Box>
);
};

export default PinnedAttachment;

Expand Down
14 changes: 14 additions & 0 deletions packages/react/src/components/ChatHeader/ChatHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
useChannelStore,
useToastStore,
useThreadsMessageStore,
useMentionsStore,
useFileStore,
} from '../../store';
import { DynamicHeader } from '../DynamicHeader';
Expand Down Expand Up @@ -75,6 +76,7 @@ const ChatHeader = ({
(state) => state.setShowAllThreads
);
const setShowAllFiles = useFileStore((state) => state.setShowAllFiles);
const setShowMentions = useMentionsStore((state) => state.setShowMentions);
const toastPosition = useToastStore((state) => state.position);

const handleGoBack = async () => {
Expand Down Expand Up @@ -147,6 +149,11 @@ const ChatHeader = ({
setShowAllFiles(true);
setShowSearch(false);
}, [setShowAllFiles, setShowSearch]);

const showMentions = useCallback(async () => {
setShowMentions(true);
setShowSearch(false);
}, [setShowMentions, setShowSearch]);

useEffect(() => {
const setMessageAllowed = async () => {
Expand Down Expand Up @@ -230,6 +237,12 @@ const ChatHeader = ({
label: 'Threads',
icon: 'thread',
},
{
id: 'mentions',
action: showMentions,
label: 'Mentions',
icon: 'at',
},
{
id: 'members',
action: showChannelMembers,
Expand Down Expand Up @@ -287,6 +300,7 @@ const ChatHeader = ({
setFullScreen,
showAllFiles,
showAllThreads,
showMentions,
showChannelMembers,
showChannelinformation,
showPinnedMessage,
Expand Down
17 changes: 17 additions & 0 deletions packages/react/src/components/Icon/icons/At.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/react/src/components/Icon/icons/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import VideoRecorder from './VideoRecoder';
import DisabledRecorder from './DisableRecorder';
import Clipboard from './Clipboard';
import Clip from './Clip';
import At from './At';

const icons = {
file: File,
Expand Down Expand Up @@ -84,6 +85,7 @@ const icons = {
'pin-filled': PinFilled,
clipboard: Clipboard,
clip: Clip,
at: At,
};

export default icons;
5 changes: 5 additions & 0 deletions packages/react/src/components/Menu/Menu.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export const Menu = {
label: 'Threads',
icon: 'thread',
},
{
id: 'mentions',
label: 'Mentions',
icon: 'at',
},
{
id: 'members',
label: 'Members',
Expand Down
6 changes: 5 additions & 1 deletion packages/react/src/components/MessageList/MessageList.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import {
useSearchMessageStore,
useChannelStore,
useUserStore,
useThreadsMessageStore,
useFileStore,
useMentionsStore,
useThreadsMessageStore,
} from '../../store';
import RoomMembers from '../RoomMembers/RoomMember';
import MessageReportWindow from '../ReportMessage/MessageReportWindow';
Expand All @@ -17,6 +18,7 @@ import SearchMessage from '../SearchMessage/SearchMessage';
import Roominfo from '../RoomInformation/RoomInformation';
import AllThreads from '../AllThreads/AllThreads';
import { Files } from '../Files';
import UserMentions from '../UserMentions/UserMentions';
import { Message } from '../Message';

const MessageList = ({ messages }) => {
Expand All @@ -31,6 +33,7 @@ const MessageList = ({ messages }) => {
(state) => state.showAllThreads
);
const showAllFiles = useFileStore((state) => state.showAllFiles);
const showMentions = useMentionsStore((state) => state.showMentions);

const isMessageNewDay = (current, previous) =>
!previous || !isSameDay(new Date(current.ts), new Date(previous.ts));
Expand Down Expand Up @@ -62,6 +65,7 @@ const MessageList = ({ messages }) => {
{showChannelinfo && <Roominfo />}
{showAllThreads && <AllThreads />}
{showAllFiles && <Files />}
{showMentions && <UserMentions />}
</>
);
};
Expand Down
180 changes: 180 additions & 0 deletions packages/react/src/components/UserMentions/UserMentions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
import React, { useState, useEffect } from 'react';
import { css } from '@emotion/react';
import { isSameDay, format } from 'date-fns';
import classes from './UserMentions.module.css';
import { Icon } from '../Icon';
import { Box } from '../Box';
import { Attachments } from '../Attachments';
import { ActionButton } from '../ActionButton';
import { useMessageStore, useUserStore, useMentionsStore } from '../../store';
import { MessageBody } from '../Message/MessageBody';
import { MessageMetrics } from '../Message/MessageMetrics';
import { useRCContext } from '../../context/RCInstance';
import { Markdown } from '../Markdown';
import { MessageDivider } from '../Message/MessageDivider';
import MessageAvatarContainer from '../Message/MessageAvatarContainer';
import MessageBodyContainer from '../Message/MessageBodyContainer';
import MessageHeader from '../Message/MessageHeader';

const MessageCss = css`
display: flex;
flex-direction: row;
align-items: flex-start;
padding-top: 0.5rem;
-webkit-padding-before: 0.5rem;
padding-block-start: 0.5rem;
padding-bottom: 0.25rem;
-webkit-padding-after: 0.25rem;
padding-block-end: 0.25rem;
padding-left: 1.25rem;
padding-right: 1.25rem;
padding-inline: 1.25rem;
&:hover {
background: #f2f3f5;
}
`;

const UserMentions = () => {
const showAvatar = useUserStore((state) => state.showAvatar);
const setShowMentions = useMentionsStore((state) => state.setShowMentions);
const { RCInstance } = useRCContext();
const [mentionedMessages, setMentionedMessages] = useState([]);
const [isLoaded, setIsLoaded] = useState(false);

const openThread = useMessageStore((state) => state.openThread);

const toggleShowMentions = () => {
setShowMentions(false);
};
const handleOpenThread = (msg) => () => {
openThread(msg);
toggleShowMentions(false);
};
const isMessageNewDay = (current, previous) =>
!previous || !isSameDay(new Date(current.ts), new Date(previous.ts));

useEffect(() => {
const fetchMentionedMsgs = async () => {
const response = await RCInstance.getMentionedMessages();
if (response && response.messages) {
setMentionedMessages(response.messages);
setIsLoaded(true);
}
};
fetchMentionedMsgs();
}, [RCInstance, setMentionedMessages]);

return (
<Box className={classes.component}>
<Box className={classes.wrapContainer}>
<Box style={{ padding: '16px' }}>
<Box
css={css`
display: flex;
`}
>
<h3 style={{ display: 'contents' }}>
<Icon
name="at"
size="1.25rem"
style={{ padding: '0px 20px 20px 0px' }}
/>
<Box
css={css`
width: 100%;
color: #4a4a4a;
`}
>
Mentions
</Box>
<ActionButton onClick={toggleShowMentions} ghost size="small">
<Icon name="cross" size="1.25rem" />
</ActionButton>
</h3>
</Box>
</Box>

{isLoaded && (
<Box
style={{
flex: '1',
overflow: 'auto',
display: 'flex',
flexDirection: 'column',
justifyContent:
mentionedMessages.length === 0 ? 'center' : 'initial',
alignItems: mentionedMessages.length === 0 ? 'center' : 'initial',
}}
>
{mentionedMessages.length === 0 ? (
<Box
style={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
color: '#4a4a4a',
}}
>
<Icon
name="magnifier"
size="3rem"
style={{ padding: '0.5rem' }}
/>
<span style={{ fontSize: '1.2rem', fontWeight: 'bold' }}>
No mentions found
</span>
</Box>
) : (
mentionedMessages.map((message, index, arr) => {
const newDay =
index === 0 || isMessageNewDay(message, arr[index - 1]);
return (
<React.Fragment key={message._id}>
{newDay ? (
<MessageDivider>
{format(new Date(message.ts), 'MMMM d, yyyy')}
</MessageDivider>
) : null}
<Box css={MessageCss}>
{showAvatar && (
<MessageAvatarContainer
message={message}
sequential={false}
isStarred={false}
/>
)}
<MessageBodyContainer>
<MessageHeader message={message} />

<MessageBody>
{message.attachments &&
message.attachments.length > 0 ? (
<>
<Markdown body={message} isReaction={false} />
<Attachments attachments={message.attachments} />
</>
) : (
<Markdown body={message} isReaction={false} />
)}
</MessageBody>

{!message.t && message.tcount && (
<MessageMetrics
message={message}
handleOpenThread={handleOpenThread}
/>
)}
</MessageBodyContainer>
</Box>
</React.Fragment>
);
})
)}
</Box>
)}
</Box>
</Box>
);
};

export default UserMentions;
Loading

0 comments on commit a468e2e

Please sign in to comment.