Skip to content

Commit

Permalink
logic-integrate/remove-store
Browse files Browse the repository at this point in the history
  • Loading branch information
umangutkarsh committed Feb 17, 2024
1 parent 4442351 commit 2e9f9a7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 47 deletions.
87 changes: 53 additions & 34 deletions packages/react/src/components/ChatInput/ChatInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import {
useMessageStore,
loginModalStore,
useChannelStore,
useMemberStore
} from '../../store';
import ChatInputFormattingToolbar from './ChatInputFormattingToolbar';
import useAttachmentWindowStore from '../../store/attachmentwindow';
import MembersList from '../Mentions/MembersList';
import mentionmemberStore from '../../store/mentionmemberStore';
import { searchToMentionUser } from '../../lib/searchToMentionUser';
import TypingUsers from '../TypingUsers';
import createPendingMessage from '../../lib/createPendingMessage';
Expand All @@ -22,6 +22,7 @@ import { Box } from '../Box';
import { Icon } from '../Icon';
import { CommandsList } from '../CommandList';
import { ActionButton } from '../ActionButton';
import { Divider } from '../Divider';
import useComponentOverrides from '../../theme/useComponentOverrides';
import { useToastBarDispatch } from '../../hooks/useToastBarDispatch';

Expand All @@ -45,12 +46,21 @@ const ChatInput = ({ scrollToBottom }) => {
(state) => state.setIsUserAuthenticated
);

const isChannelPrivate = useChannelStore((state) => state.isChannelPrivate);

const members = useMemberStore((state) => state.members);
const setMembersHandler = useMemberStore((state) => state.setMembersHandler);

useEffect(() => {
RCInstance.auth.onAuthChange((user) => {
if (user) {
RCInstance.getCommandsList()
.then((data) => setCommands(data.commands || []))
.catch(console.error);

RCInstance.getChannelMembers(isChannelPrivate)
.then((channelMembers) => setMembersHandler(channelMembers.members || []))
.catch(console.error);
}
});
}, [RCInstance]);
Expand All @@ -68,28 +78,19 @@ const ChatInput = ({ scrollToBottom }) => {

const inputRef = useRef(null);
const typingRef = useRef();
const messageRef = useRef();
const messageRef = useRef(null);

const [disableButton, setDisableButton] = useState(true);

const roomMembers = mentionmemberStore((state) => state.roomMembers);
const setRoomMembers = mentionmemberStore((state) => state.setRoomMembers);

const [filteredMembers, setFilteredMembers] = useState([]);

const [mentionIndex, setmentionIndex] = useState(-1);
const [startReading, setStartReading] = useState(false);
const showMembersList = mentionmemberStore((state) => state.showMembersList);
const setshowMembersList = mentionmemberStore(
(state) => state.toggleShowMembers
);
const [showMembersList, setshowMembersList] = useState(false);

const setIsLoginModalOpen = loginModalStore(
(state) => state.setIsLoginModalOpen
);
const isChannelPrivate = useChannelStore((state) => state.isChannelPrivate);
const setIsChannelPrivate = useChannelStore(
(state) => state.setIsChannelPrivate
);

const {
editMessage,
Expand Down Expand Up @@ -217,16 +218,6 @@ const ChatInput = ({ scrollToBottom }) => {
toggle();
setData(event.target.files[0]);
};
const getAllChannelMembers = useCallback(async () => {
try {
const channelMembers = await RCInstance.getChannelMembers(
isChannelPrivate
);
setRoomMembers(channelMembers.members);
} catch (e) {
console.error(e);
}
}, [RCInstance, setRoomMembers, isChannelPrivate]);

useEffect(() => {
if (editMessage.msg) {
Expand All @@ -235,9 +226,6 @@ const ChatInput = ({ scrollToBottom }) => {
messageRef.current.value = '';
}
}, [editMessage]);
useEffect(() => {
getAllChannelMembers();
}, [getAllChannelMembers]);

const username = useUserStore((state) => state.username);
const timerRef = useRef();
Expand Down Expand Up @@ -285,6 +273,33 @@ const ChatInput = ({ scrollToBottom }) => {
}
}, []);

const handleMemberClick = (selectedItem) => {
setshowMembersList(false);

let insertionText;
if (selectedItem === 'all') {
insertionText = `@all `;
}

else if (selectedItem === 'here') {
insertionText = `@here `;
}

else {
insertionText = `${messageRef.current.value.substring(
0,
messageRef.current.value.lastIndexOf('@')
)}@${selectedItem.username} `;
}

messageRef.current.value = insertionText;

const cursorPosition = insertionText.length;
messageRef.current.setSelectionRange(cursorPosition, cursorPosition);
messageRef.current.focus();
};


const showCommands = useCallback(
async (e) => {
const cursor = e.target.selectionStart;
Expand Down Expand Up @@ -320,7 +335,7 @@ const ChatInput = ({ scrollToBottom }) => {
}
searchToMentionUser(
messageRef.current.value,
roomMembers,
members,
startReading,
setStartReading,
setFilteredMembers,
Expand Down Expand Up @@ -400,7 +415,7 @@ const ChatInput = ({ scrollToBottom }) => {
let selectedMember = null;
if (mentionIndex === filteredMembers.length) selectedMember = 'all';
else if (mentionIndex === filteredMembers.length + 1)
selectedMember = 'everyone';
selectedMember = 'here';
else selectedMember = filteredMembers[mentionIndex].username;
messageRef.current.value = `${messageRef.current.value.substring(
0,
Expand Down Expand Up @@ -434,10 +449,14 @@ const ChatInput = ({ scrollToBottom }) => {
`}
>
{showMembersList ? (
<MembersList
mentionIndex={mentionIndex}
filteredMembers={filteredMembers}
/>
<>
<MembersList
mentionIndex={mentionIndex}
filteredMembers={filteredMembers}
onMemberClick={handleMemberClick}
/>
<Divider />
</>
) : (
<></>
)}
Expand Down Expand Up @@ -465,8 +484,8 @@ const ChatInput = ({ scrollToBottom }) => {
isUserAuthenticated && canSendMsg
? 'Message'
: isUserAuthenticated
? 'This room is read only'
: 'Sign in to chat'
? 'This room is read only'
: 'Sign in to chat'
}
className={styles.textInput}
onChange={onTextChange}
Expand Down
6 changes: 3 additions & 3 deletions packages/react/src/components/Markup/elements/Mention.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import PropTypes from 'prop-types';
import { useMemberStore, useUserStore } from '../../../store';

const Mention = ({ contents }) => {
const members = useMemberStore((state) => state.members);
const username = useUserStore((state) => state.username);

const mentionStyles = css`
background-color: ${contents.value === 'all' || contents.value === 'here' ? '#f38c39' :
(contents.value === username ? '#ec0d2a' : '#e4e7ea')
Expand All @@ -21,9 +24,6 @@ const Mention = ({ contents }) => {
}
`;

const members = useMemberStore((state) => state.members);
const username = useUserStore((state) => state.username);

const hasMember = (user) => {
if (user === 'all' || user === 'here') return true;
let found = false;
Expand Down
10 changes: 0 additions & 10 deletions packages/react/src/store/mentionmemberStore.js

This file was deleted.

0 comments on commit 2e9f9a7

Please sign in to comment.