Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Sidebar starred messages fetched by api to include thread messages. #669

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion packages/react/src/hooks/useFetchChatData.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
useChannelStore,
useMemberStore,
useMessageStore,
useStarredMessageStore,
} from '../store';

const useFetchChatData = (showRoles) => {
Expand All @@ -13,6 +14,9 @@ const useFetchChatData = (showRoles) => {
const isChannelPrivate = useChannelStore((state) => state.isChannelPrivate);
const setMessages = useMessageStore((state) => state.setMessages);
const setAdmins = useMemberStore((state) => state.setAdmins);
const setStarredMessages = useStarredMessageStore(
(state) => state.setStarredMessages
);
const isUserAuthenticated = useUserStore(
(state) => state.isUserAuthenticated
);
Expand Down Expand Up @@ -80,7 +84,24 @@ const useFetchChatData = (showRoles) => {
]
);

return getMessagesAndRoles;
const getStarredMessages = useCallback(
async (anonymousMode) => {
if (isUserAuthenticated) {
try {
if (!isUserAuthenticated && !anonymousMode) {
return;
}
const { messages } = await RCInstance.getStarredMessages();
setStarredMessages(messages);
} catch (e) {
console.error(e);
}
}
},
[isUserAuthenticated, RCInstance, setStarredMessages]
);

return { getMessagesAndRoles, getStarredMessages };
};

export default useFetchChatData;
2 changes: 2 additions & 0 deletions packages/react/src/store/starredMessageStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { create } from 'zustand';
const useStarredMessageStore = create((set) => ({
showStarred: false,
setShowStarred: (showStarred) => set(() => ({ showStarred })),
starredMessages: [],
setStarredMessages: (messages) => set(() => ({ starredMessages: messages })),
}));

export default useStarredMessageStore;
2 changes: 1 addition & 1 deletion packages/react/src/views/ChatBody/ChatBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const ChatBody = ({

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

const getMessagesAndRoles = useFetchChatData(showRoles);
const { getMessagesAndRoles } = useFetchChatData(showRoles);

const getThreadMessages = useCallback(async () => {
if (isUserAuthenticated && threadMainMessage?._id) {
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/views/ChatHeader/ChatHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const ChatHeader = ({
);

const dispatchToastMessage = useToastBarDispatch();
const getMessagesAndRoles = useFetchChatData(showRoles);
const { getMessagesAndRoles } = useFetchChatData(showRoles);
const setMessageLimit = useSettingsStore((state) => state.setMessageLimit);

const avatarUrl = useUserStore((state) => state.avatarUrl);
Expand Down
30 changes: 27 additions & 3 deletions packages/react/src/views/ChatLayout/ChatLayout.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef } from 'react';
import React, { useEffect, useRef, useCallback, useState } from 'react';
import { Box, useComponentOverrides } from '@embeddedchat/ui-elements';
import styles from './ChatLayout.styles';
import {
Expand Down Expand Up @@ -36,9 +36,15 @@ import useUiKitStore from '../../store/uiKitStore';
const ChatLayout = () => {
const messageListRef = useRef(null);
const { classNames, styleOverrides } = useComponentOverrides('ChatBody');
const { ECOptions } = useRCContext();
const { RCInstance, ECOptions } = useRCContext();
const anonymousMode = ECOptions?.anonymousMode;
const showRoles = ECOptions?.anonymousMode;
const setStarredMessages = useStarredMessageStore(
(state) => state.setStarredMessages
);
const starredMessages = useStarredMessageStore(
(state) => state.starredMessages
);
const showSidebar = useSidebarStore((state) => state.showSidebar);
const showMentions = useMentionsStore((state) => state.showMentions);
const showAllFiles = useFileStore((state) => state.showAllFiles);
Expand All @@ -57,6 +63,9 @@ const ChatLayout = () => {
const attachmentWindowOpen = useAttachmentWindowStore(
(state) => state.attachmentWindowOpen
);
const isUserAuthenticated = useUserStore(
(state) => state.isUserAuthenticated
);
const { data, handleDrag, handleDragDrop } = useDropBox();
const { uiKitContextualBarOpen, uiKitContextualBarData } = useUiKitStore(
(state) => ({
Expand All @@ -72,7 +81,22 @@ const ChatLayout = () => {
});
}
};

const getStarredMessages = useCallback(async () => {
if (isUserAuthenticated) {
try {
if (!isUserAuthenticated && !anonymousMode) {
return;
}
const { messages } = await RCInstance.getStarredMessages();
setStarredMessages(messages);
} catch (e) {
console.error(e);
}
}
}, [isUserAuthenticated, anonymousMode, RCInstance]);
useEffect(() => {
getStarredMessages();
}, [showSidebar]);
return (
<Box
css={styles.layout}
Expand Down
4 changes: 3 additions & 1 deletion packages/react/src/views/Message/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { LinkPreview } from '../LinkPreview';
import { getMessageStyles } from './Message.styles';
import useBubbleStyles from './BubbleVariant/useBubbleStyles';
import UiKitMessageBlock from './uiKit/UiKitMessageBlock';
import useFetchChatData from '../../hooks/useFetchChatData';

const Message = ({
message,
Expand Down Expand Up @@ -56,7 +57,7 @@ const Message = ({
);
const setQuoteMessage = useMessageStore((state) => state.setQuoteMessage);
const openThread = useMessageStore((state) => state.openThread);

const { getStarredMessages } = useFetchChatData();
const dispatchToastMessage = useToastBarDispatch();
const { editMessage, setEditMessage } = useMessageStore((state) => ({
editMessage: state.editMessage,
Expand Down Expand Up @@ -87,6 +88,7 @@ const Message = ({
message: 'Message unstarred',
});
}
getStarredMessages();
};

const handlePinMessage = async (msg) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import React, { useCallback } from 'react';
import React, { useCallback, useEffect } from 'react';
import { useComponentOverrides } from '@embeddedchat/ui-elements';
import { useUserStore } from '../../store';
import { useStarredMessageStore, useUserStore } from '../../store';
import { MessageAggregator } from './common/MessageAggregator';

const StarredMessages = () => {
const authenticatedUserId = useUserStore((state) => state.userId);
const { variantOverrides } = useComponentOverrides('StarredMessages');
const viewType = variantOverrides.viewType || 'Sidebar';
const starredMessages = useStarredMessageStore(
(state) => state.starredMessages
);
const shouldRender = useCallback(
(msg) =>
msg.starred &&
Expand All @@ -18,6 +21,7 @@ const StarredMessages = () => {
title="Starred Messages"
iconName="star"
noMessageInfo="No Starred Messages"
fetchedMessageList={starredMessages}
shouldRender={shouldRender}
viewType={viewType}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const MessageAggregator = ({
iconName,
noMessageInfo,
shouldRender,
fetchedMessageList,
searchProps,
searchFiltered,
fetching,
Expand All @@ -33,13 +34,13 @@ export const MessageAggregator = ({
);
const [messageRendered, setMessageRendered] = useState(false);
const { loading, messageList } = useSetMessageList(
searchFiltered || allMessages,
fetchedMessageList || searchFiltered || allMessages,
shouldRender
);

const isMessageNewDay = (current, previous) =>
!previous ||
!shouldRender(previous) ||
shouldRender(previous) ||
!isSameDay(new Date(current.ts), new Date(previous.ts));

const noMessages = messageList?.length === 0 || !messageRendered;
Expand Down
Loading