Skip to content

Commit

Permalink
fix(chat): make mentions interactive with user detail modal
Browse files Browse the repository at this point in the history
  • Loading branch information
SinghaAnirban005 committed Dec 5, 2024
1 parent 43807a7 commit eb828e3
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
20 changes: 20 additions & 0 deletions packages/api/src/EmbeddedChatApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1124,4 +1124,24 @@ export default class EmbeddedChatApi {
const data = response.json();
return data;
}

async userData(username: string) {
const { userId, authToken } = (await this.auth.getCurrentUser()) || {};
const response = await fetch(
`${this.host}/api/v1/users.info?username=${username}`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
"X-Auth-Token": authToken,
"X-User-Id": userId,
},
}
);
const data = response.json();
return data;
}
}



29 changes: 28 additions & 1 deletion packages/markups/src/mentions/UserMention.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
import React, { useContext } from 'react';
import PropTypes from 'prop-types';
import { Box } from '@embeddedchat/ui-elements';
import { useUserStore } from '@embeddedchat/react/src/store';
import useSetExclusiveState from '@embeddedchat/react/src/hooks/useSetExclusiveState';
import RCContext from '@embeddedchat/react/src/context/RCInstance';
import { MarkupInteractionContext } from '../MarkupInteractionContext';
import useMentionStyles from '../elements/elements.styles';

const UserMention = ({ contents }) => {
const { members, username } = useContext(MarkupInteractionContext);
const { RCInstance } = useContext(RCContext);
const setExclusiveState = useSetExclusiveState();
const { setShowCurrentUserInfo, setCurrentUser } = useUserStore((state) => ({
setShowCurrentUserInfo: state.setShowCurrentUserInfo,
setCurrentUser: state.setCurrentUser,
}));

const handleUserInfo = async (uname) => {
const data = await RCInstance.userData(uname);
setCurrentUser({
_id: data.user._id,
username: data.user.username,
name: data.user.name,
});
setExclusiveState(setShowCurrentUserInfo);
};

const hasMember = (user) => {
if (user === 'all' || user === 'here') return true;
Expand All @@ -23,7 +42,15 @@ const UserMention = ({ contents }) => {
return (
<>
{hasMember(contents.value) ? (
<Box is="span" css={styles.mention}>
<Box
is="span"
css={styles.mention}
onClick={
['here', 'all'].includes(contents.value)
? null
: () => handleUserInfo(contents.value)
}
>
{contents.value}
</Box>
) : (
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/views/Markdown/Markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useMemo } from 'react';
import PropTypes from 'prop-types';
import { css } from '@emotion/react';
import { Box } from '@embeddedchat/ui-elements';
import { Markup, MarkupInteractionContext } from '@embeddedchat/markups';
import { Markup, MarkupInteractionContext } from '@embeddedchat/markups/src';
import EmojiReaction from '../EmojiReaction/EmojiReaction';
import { useMemberStore, useUserStore } from '../../store';

Expand Down
8 changes: 6 additions & 2 deletions packages/react/src/views/UserInformation/UserInformation.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const UserInformation = () => {
useEffect(() => {
const getCurrentUserInfo = async () => {
try {
const res = await RCInstance.userInfo(currentUser._id);
const res = await RCInstance.userData(currentUser.username);
if (res?.user) {
setCurrentUserInfo(res.user);
setIsUserInfoFetched(true);
Expand Down Expand Up @@ -123,7 +123,11 @@ const UserInformation = () => {
/>
<UserInfoField
label="Last login"
value={formatTimestamp(currentUserInfo.lastLogin)}
value={
currentUserInfo?.username === 'rocket.cat'
? 'Never'
: formatTimestamp(currentUserInfo.lastLogin)
}
isAdmin={isAdmin}
authenticatedUserId={authenticatedUserId}
currentUserInfo={currentUserInfo}
Expand Down

0 comments on commit eb828e3

Please sign in to comment.