Initial
This commit is contained in:
@@ -267,7 +267,17 @@ export const registerMessagingHandlers = ({ socket, client, currentUser, bodysho
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { conversationId, type, job_conversations, messageIds, ...fields } = data;
|
const {
|
||||||
|
conversationId,
|
||||||
|
type,
|
||||||
|
job_conversations,
|
||||||
|
messageIds, // used by "conversation-marked-read"
|
||||||
|
messageIdsMarkedRead, // used by "conversation-marked-unread"
|
||||||
|
lastUnreadMessageId, // used by "conversation-marked-unread"
|
||||||
|
unreadCount, // used by "conversation-marked-unread"
|
||||||
|
...fields
|
||||||
|
} = data;
|
||||||
|
|
||||||
logLocal("handleConversationChanged - Start", data);
|
logLocal("handleConversationChanged - Start", data);
|
||||||
|
|
||||||
const updatedAt = new Date().toISOString();
|
const updatedAt = new Date().toISOString();
|
||||||
@@ -313,15 +323,65 @@ export const registerMessagingHandlers = ({ socket, client, currentUser, bodysho
|
|||||||
return message;
|
return message;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
// Keep unread badge in sync (badge uses messages_aggregate.aggregate.count)
|
||||||
messages_aggregate: () => ({
|
messages_aggregate: () => ({
|
||||||
__typename: "messages_aggregate",
|
__typename: "messages_aggregate",
|
||||||
aggregate: { __typename: "messages_aggregate_fields", count: 0 }
|
aggregate: { __typename: "messages_aggregate_fields", count: 0 }
|
||||||
})
|
}),
|
||||||
|
unreadcnt: () => 0
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case "conversation-marked-unread": {
|
||||||
|
if (!conversationId) break;
|
||||||
|
|
||||||
|
const safeUnreadCount = typeof unreadCount === "number" ? unreadCount : 1;
|
||||||
|
const idsMarkedRead = Array.isArray(messageIdsMarkedRead) ? messageIdsMarkedRead : [];
|
||||||
|
|
||||||
|
client.cache.modify({
|
||||||
|
id: client.cache.identify({ __typename: "conversations", id: conversationId }),
|
||||||
|
fields: {
|
||||||
|
// Bubble the conversation up in the list (since UI sorts by updated_at)
|
||||||
|
updated_at: () => updatedAt,
|
||||||
|
|
||||||
|
// If details are already cached, flip the read flags appropriately
|
||||||
|
messages(existingMessages = [], { readField }) {
|
||||||
|
if (!Array.isArray(existingMessages) || existingMessages.length === 0) return existingMessages;
|
||||||
|
|
||||||
|
return existingMessages.map((msg) => {
|
||||||
|
const id = readField("id", msg);
|
||||||
|
|
||||||
|
if (lastUnreadMessageId && id === lastUnreadMessageId) {
|
||||||
|
return { ...msg, read: false };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (idsMarkedRead.includes(id)) {
|
||||||
|
return { ...msg, read: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
return msg;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// Update unread badge
|
||||||
|
messages_aggregate: () => ({
|
||||||
|
__typename: "messages_aggregate",
|
||||||
|
aggregate: {
|
||||||
|
__typename: "messages_aggregate_fields",
|
||||||
|
count: safeUnreadCount
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
|
||||||
|
// Optional: keep legacy/parallel unread field consistent if present
|
||||||
|
unreadcnt: () => safeUnreadCount
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
case "conversation-created":
|
case "conversation-created":
|
||||||
updateConversationList({ ...fields, job_conversations, updated_at: updatedAt });
|
updateConversationList({ ...fields, job_conversations, updated_at: updatedAt });
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import _ from "lodash";
|
|||||||
import { ExclamationCircleOutlined } from "@ant-design/icons";
|
import { ExclamationCircleOutlined } from "@ant-design/icons";
|
||||||
import "./chat-conversation-list.styles.scss";
|
import "./chat-conversation-list.styles.scss";
|
||||||
import { useQuery } from "@apollo/client";
|
import { useQuery } from "@apollo/client";
|
||||||
import { GET_PHONE_NUMBER_OPT_OUTS } from "../../graphql/phone-number-opt-out.queries.js";
|
import { GET_PHONE_NUMBER_OPT_OUTS_BY_NUMBERS } from "../../graphql/phone-number-opt-out.queries.js";
|
||||||
import { phone } from "phone";
|
import { phone } from "phone";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { selectBodyshop } from "../../redux/user/user.selectors";
|
import { selectBodyshop } from "../../redux/user/user.selectors";
|
||||||
@@ -29,13 +29,26 @@ const mapDispatchToProps = (dispatch) => ({
|
|||||||
function ChatConversationListComponent({ conversationList, selectedConversation, setSelectedConversation, bodyshop }) {
|
function ChatConversationListComponent({ conversationList, selectedConversation, setSelectedConversation, bodyshop }) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const [, forceUpdate] = useState(false);
|
const [, forceUpdate] = useState(false);
|
||||||
const phoneNumbers = conversationList.map((item) => phone(item.phone_num, "CA").phoneNumber.replace(/^\+1/, ""));
|
|
||||||
const { data: optOutData } = useQuery(GET_PHONE_NUMBER_OPT_OUTS, {
|
const phoneNumbers = useMemo(() => {
|
||||||
|
return (conversationList || [])
|
||||||
|
.map((item) => {
|
||||||
|
try {
|
||||||
|
const p = phone(item.phone_num, "CA")?.phoneNumber;
|
||||||
|
return p ? p.replace(/^\+1/, "") : null;
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.filter(Boolean);
|
||||||
|
}, [conversationList]);
|
||||||
|
|
||||||
|
const { data: optOutData } = useQuery(GET_PHONE_NUMBER_OPT_OUTS_BY_NUMBERS, {
|
||||||
variables: {
|
variables: {
|
||||||
bodyshopid: bodyshop.id,
|
bodyshopid: bodyshop?.id,
|
||||||
phone_numbers: phoneNumbers
|
phone_numbers: phoneNumbers
|
||||||
},
|
},
|
||||||
skip: !conversationList.length,
|
skip: !bodyshop?.id || phoneNumbers.length === 0,
|
||||||
fetchPolicy: "cache-and-network"
|
fetchPolicy: "cache-and-network"
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -58,15 +71,25 @@ function ChatConversationListComponent({ conversationList, selectedConversation,
|
|||||||
return _.orderBy(conversationList, ["updated_at"], ["desc"]);
|
return _.orderBy(conversationList, ["updated_at"], ["desc"]);
|
||||||
}, [conversationList]);
|
}, [conversationList]);
|
||||||
|
|
||||||
const renderConversation = (index, t) => {
|
const renderConversation = (index) => {
|
||||||
const item = sortedConversationList[index];
|
const item = sortedConversationList[index];
|
||||||
const normalizedPhone = phone(item.phone_num, "CA").phoneNumber.replace(/^\+1/, "");
|
|
||||||
const hasOptOutEntry = optOutMap.has(normalizedPhone);
|
const normalizedPhone = (() => {
|
||||||
|
try {
|
||||||
|
return phone(item.phone_num, "CA")?.phoneNumber?.replace(/^\+1/, "") || "";
|
||||||
|
} catch {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
|
const hasOptOutEntry = normalizedPhone ? optOutMap.has(normalizedPhone) : false;
|
||||||
|
|
||||||
const cardContentRight = <TimeAgoFormatter>{item.updated_at}</TimeAgoFormatter>;
|
const cardContentRight = <TimeAgoFormatter>{item.updated_at}</TimeAgoFormatter>;
|
||||||
const cardContentLeft =
|
const cardContentLeft =
|
||||||
item.job_conversations.length > 0
|
item.job_conversations.length > 0
|
||||||
? item.job_conversations.map((j, idx) => <Tag key={idx}>{j.job.ro_number}</Tag>)
|
? item.job_conversations.map((j, idx) => <Tag key={idx}>{j.job.ro_number}</Tag>)
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
const names = <>{_.uniq(item.job_conversations.map((j) => OwnerNameDisplayFunction(j.job)))}</>;
|
const names = <>{_.uniq(item.job_conversations.map((j) => OwnerNameDisplayFunction(j.job)))}</>;
|
||||||
const cardTitle = (
|
const cardTitle = (
|
||||||
<>
|
<>
|
||||||
@@ -80,9 +103,10 @@ function ChatConversationListComponent({ conversationList, selectedConversation,
|
|||||||
)}
|
)}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|
||||||
const cardExtra = (
|
const cardExtra = (
|
||||||
<>
|
<>
|
||||||
<Badge count={item.messages_aggregate.aggregate.count} />
|
<Badge count={item.messages_aggregate?.aggregate?.count || 0} />
|
||||||
{hasOptOutEntry && (
|
{hasOptOutEntry && (
|
||||||
<Tooltip title={t("consent.text_body")}>
|
<Tooltip title={t("consent.text_body")}>
|
||||||
<Tag color="red" icon={<ExclamationCircleOutlined />}>
|
<Tag color="red" icon={<ExclamationCircleOutlined />}>
|
||||||
@@ -92,6 +116,7 @@ function ChatConversationListComponent({ conversationList, selectedConversation,
|
|||||||
)}
|
)}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|
||||||
const getCardStyle = () =>
|
const getCardStyle = () =>
|
||||||
item.id === selectedConversation
|
item.id === selectedConversation
|
||||||
? { backgroundColor: "var(--card-selected-bg)" }
|
? { backgroundColor: "var(--card-selected-bg)" }
|
||||||
@@ -104,24 +129,8 @@ function ChatConversationListComponent({ conversationList, selectedConversation,
|
|||||||
className={`chat-list-item ${item.id === selectedConversation ? "chat-list-selected-conversation" : ""}`}
|
className={`chat-list-item ${item.id === selectedConversation ? "chat-list-selected-conversation" : ""}`}
|
||||||
>
|
>
|
||||||
<Card style={getCardStyle()} variant={true} size="small" extra={cardExtra} title={cardTitle}>
|
<Card style={getCardStyle()} variant={true} size="small" extra={cardExtra} title={cardTitle}>
|
||||||
<div
|
<div style={{ display: "inline-block", width: "70%", textAlign: "left" }}>{cardContentLeft}</div>
|
||||||
style={{
|
<div style={{ display: "inline-block", width: "30%", textAlign: "right" }}>{cardContentRight}</div>
|
||||||
display: "inline-block",
|
|
||||||
width: "70%",
|
|
||||||
textAlign: "left"
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{cardContentLeft}
|
|
||||||
</div>
|
|
||||||
<div
|
|
||||||
style={{
|
|
||||||
display: "inline-block",
|
|
||||||
width: "30%",
|
|
||||||
textAlign: "right"
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{cardContentRight}
|
|
||||||
</div>
|
|
||||||
</Card>
|
</Card>
|
||||||
</List.Item>
|
</List.Item>
|
||||||
);
|
);
|
||||||
@@ -131,7 +140,7 @@ function ChatConversationListComponent({ conversationList, selectedConversation,
|
|||||||
<div className="chat-list-container">
|
<div className="chat-list-container">
|
||||||
<Virtuoso
|
<Virtuoso
|
||||||
data={sortedConversationList}
|
data={sortedConversationList}
|
||||||
itemContent={(index) => renderConversation(index, t)}
|
itemContent={(index) => renderConversation(index)}
|
||||||
style={{ height: "100%", width: "100%" }}
|
style={{ height: "100%", width: "100%" }}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -5,24 +5,24 @@ import ChatConversationTitleTags from "../chat-conversation-title-tags/chat-conv
|
|||||||
import ChatLabelComponent from "../chat-label/chat-label.component";
|
import ChatLabelComponent from "../chat-label/chat-label.component";
|
||||||
import ChatPrintButton from "../chat-print-button/chat-print-button.component";
|
import ChatPrintButton from "../chat-print-button/chat-print-button.component";
|
||||||
import ChatTagRoContainer from "../chat-tag-ro/chat-tag-ro.container";
|
import ChatTagRoContainer from "../chat-tag-ro/chat-tag-ro.container";
|
||||||
import { createStructuredSelector } from "reselect";
|
import ChatMarkUnreadButton from "../chat-mark-unread-button/chat-mark-unread-button.component";
|
||||||
import { connect } from "react-redux";
|
|
||||||
|
|
||||||
const mapStateToProps = createStructuredSelector({});
|
export function ChatConversationTitle({ conversation, onMarkUnread, markUnreadDisabled, markUnreadLoading }) {
|
||||||
|
|
||||||
const mapDispatchToProps = () => ({});
|
|
||||||
|
|
||||||
export function ChatConversationTitle({ conversation }) {
|
|
||||||
return (
|
return (
|
||||||
<Space className="chat-title" wrap>
|
<Space className="chat-title" wrap>
|
||||||
<PhoneNumberFormatter>{conversation?.phone_num}</PhoneNumberFormatter>
|
<PhoneNumberFormatter>{conversation?.phone_num}</PhoneNumberFormatter>
|
||||||
|
|
||||||
<ChatLabelComponent conversation={conversation} />
|
<ChatLabelComponent conversation={conversation} />
|
||||||
<ChatPrintButton conversation={conversation} />
|
<ChatPrintButton conversation={conversation} />
|
||||||
|
|
||||||
<ChatConversationTitleTags jobConversations={conversation?.job_conversations || []} />
|
<ChatConversationTitleTags jobConversations={conversation?.job_conversations || []} />
|
||||||
<ChatTagRoContainer conversation={conversation || []} />
|
<ChatTagRoContainer conversation={conversation || []} />
|
||||||
|
|
||||||
|
<ChatMarkUnreadButton disabled={markUnreadDisabled} loading={markUnreadLoading} onMarkUnread={onMarkUnread} />
|
||||||
|
|
||||||
<ChatArchiveButton conversation={conversation} />
|
<ChatArchiveButton conversation={conversation} />
|
||||||
</Space>
|
</Space>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default connect(mapStateToProps, mapDispatchToProps)(ChatConversationTitle);
|
export default ChatConversationTitle;
|
||||||
|
|||||||
@@ -19,7 +19,9 @@ export function ChatConversationComponent({
|
|||||||
conversation,
|
conversation,
|
||||||
messages,
|
messages,
|
||||||
handleMarkConversationAsRead,
|
handleMarkConversationAsRead,
|
||||||
bodyshop
|
handleMarkLastMessageAsUnread,
|
||||||
|
markingAsUnreadInProgress,
|
||||||
|
canMarkUnread
|
||||||
}) {
|
}) {
|
||||||
const [loading, error] = subState;
|
const [loading, error] = subState;
|
||||||
|
|
||||||
@@ -33,7 +35,12 @@ export function ChatConversationComponent({
|
|||||||
onMouseDown={handleMarkConversationAsRead}
|
onMouseDown={handleMarkConversationAsRead}
|
||||||
onKeyDown={handleMarkConversationAsRead}
|
onKeyDown={handleMarkConversationAsRead}
|
||||||
>
|
>
|
||||||
<ChatConversationTitle conversation={conversation} bodyshop={bodyshop} />
|
<ChatConversationTitle
|
||||||
|
conversation={conversation}
|
||||||
|
onMarkUnread={handleMarkLastMessageAsUnread}
|
||||||
|
markUnreadDisabled={!canMarkUnread}
|
||||||
|
markUnreadLoading={markingAsUnreadInProgress}
|
||||||
|
/>
|
||||||
<ChatMessageListComponent messages={messages} />
|
<ChatMessageListComponent messages={messages} />
|
||||||
<ChatSendMessage conversation={conversation} />
|
<ChatSendMessage conversation={conversation} />
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { gql, useApolloClient, useQuery, useSubscription } from "@apollo/client";
|
import { gql, useApolloClient, useQuery, useSubscription } from "@apollo/client";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { useCallback, useEffect, useState } from "react";
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||||
import { connect } from "react-redux";
|
import { connect } from "react-redux";
|
||||||
import { createStructuredSelector } from "reselect";
|
import { createStructuredSelector } from "reselect";
|
||||||
import { CONVERSATION_SUBSCRIPTION_BY_PK, GET_CONVERSATION_DETAILS } from "../../graphql/conversations.queries";
|
import { CONVERSATION_SUBSCRIPTION_BY_PK, GET_CONVERSATION_DETAILS } from "../../graphql/conversations.queries";
|
||||||
@@ -18,8 +18,8 @@ function ChatConversationContainer({ bodyshop, selectedConversation }) {
|
|||||||
const client = useApolloClient();
|
const client = useApolloClient();
|
||||||
const { socket } = useSocket();
|
const { socket } = useSocket();
|
||||||
const [markingAsReadInProgress, setMarkingAsReadInProgress] = useState(false);
|
const [markingAsReadInProgress, setMarkingAsReadInProgress] = useState(false);
|
||||||
|
const [markingAsUnreadInProgress, setMarkingAsUnreadInProgress] = useState(false);
|
||||||
|
|
||||||
// Fetch conversation details
|
|
||||||
const {
|
const {
|
||||||
loading: convoLoading,
|
loading: convoLoading,
|
||||||
error: convoError,
|
error: convoError,
|
||||||
@@ -27,24 +27,23 @@ function ChatConversationContainer({ bodyshop, selectedConversation }) {
|
|||||||
} = useQuery(GET_CONVERSATION_DETAILS, {
|
} = useQuery(GET_CONVERSATION_DETAILS, {
|
||||||
variables: { conversationId: selectedConversation },
|
variables: { conversationId: selectedConversation },
|
||||||
fetchPolicy: "network-only",
|
fetchPolicy: "network-only",
|
||||||
nextFetchPolicy: "network-only"
|
nextFetchPolicy: "network-only",
|
||||||
|
skip: !selectedConversation
|
||||||
});
|
});
|
||||||
|
|
||||||
// Subscription for conversation updates
|
const conversation = convoData?.conversations_by_pk;
|
||||||
|
|
||||||
|
// Subscription for conversation updates (used when socket is NOT connected)
|
||||||
useSubscription(CONVERSATION_SUBSCRIPTION_BY_PK, {
|
useSubscription(CONVERSATION_SUBSCRIPTION_BY_PK, {
|
||||||
skip: socket?.connected,
|
skip: socket?.connected || !selectedConversation,
|
||||||
variables: { conversationId: selectedConversation },
|
variables: { conversationId: selectedConversation },
|
||||||
onData: ({ data: subscriptionResult, client }) => {
|
onData: ({ data: subscriptionResult, client }) => {
|
||||||
// Extract the messages array from the result
|
|
||||||
const messages = subscriptionResult?.data?.messages;
|
const messages = subscriptionResult?.data?.messages;
|
||||||
if (!messages || messages.length === 0) {
|
if (!messages || messages.length === 0) return;
|
||||||
console.warn("No messages found in subscription result.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
messages.forEach((message) => {
|
messages.forEach((message) => {
|
||||||
const messageRef = client.cache.identify(message);
|
const messageRef = client.cache.identify(message);
|
||||||
// Write the new message to the cache
|
|
||||||
client.cache.writeFragment({
|
client.cache.writeFragment({
|
||||||
id: messageRef,
|
id: messageRef,
|
||||||
fragment: gql`
|
fragment: gql`
|
||||||
@@ -64,7 +63,6 @@ function ChatConversationContainer({ bodyshop, selectedConversation }) {
|
|||||||
data: message
|
data: message
|
||||||
});
|
});
|
||||||
|
|
||||||
// Update the conversation cache to include the new message
|
|
||||||
client.cache.modify({
|
client.cache.modify({
|
||||||
id: client.cache.identify({ __typename: "conversations", id: selectedConversation }),
|
id: client.cache.identify({ __typename: "conversations", id: selectedConversation }),
|
||||||
fields: {
|
fields: {
|
||||||
@@ -82,6 +80,28 @@ function ChatConversationContainer({ bodyshop, selectedConversation }) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Best-effort badge update:
|
||||||
|
* This assumes your list query uses messages_aggregate.aggregate.count as UNREAD inbound count.
|
||||||
|
* If it’s total messages, rename/create a dedicated unread aggregate in the list query and update that field instead.
|
||||||
|
*/
|
||||||
|
const setConversationUnreadCountBestEffort = useCallback(
|
||||||
|
(conversationId, unreadCount) => {
|
||||||
|
if (!conversationId) return;
|
||||||
|
|
||||||
|
client.cache.modify({
|
||||||
|
id: client.cache.identify({ __typename: "conversations", id: conversationId }),
|
||||||
|
fields: {
|
||||||
|
messages_aggregate(existing) {
|
||||||
|
if (!existing?.aggregate) return existing;
|
||||||
|
return { ...existing, aggregate: { ...existing.aggregate, count: unreadCount } };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
[client.cache]
|
||||||
|
);
|
||||||
|
|
||||||
const updateCacheWithReadMessages = useCallback(
|
const updateCacheWithReadMessages = useCallback(
|
||||||
(conversationId, messageIds) => {
|
(conversationId, messageIds) => {
|
||||||
if (!conversationId || !messageIds?.length) return;
|
if (!conversationId || !messageIds?.length) return;
|
||||||
@@ -89,13 +109,34 @@ function ChatConversationContainer({ bodyshop, selectedConversation }) {
|
|||||||
messageIds.forEach((messageId) => {
|
messageIds.forEach((messageId) => {
|
||||||
client.cache.modify({
|
client.cache.modify({
|
||||||
id: client.cache.identify({ __typename: "messages", id: messageId }),
|
id: client.cache.identify({ __typename: "messages", id: messageId }),
|
||||||
fields: {
|
fields: { read: () => true }
|
||||||
read: () => true
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
setConversationUnreadCountBestEffort(conversationId, 0);
|
||||||
},
|
},
|
||||||
[client.cache]
|
[client.cache, setConversationUnreadCountBestEffort]
|
||||||
|
);
|
||||||
|
|
||||||
|
const applyUnreadStateWithMaxOneUnread = useCallback(
|
||||||
|
({ conversationId, lastUnreadMessageId, messageIdsMarkedRead = [], unreadCount = 1 }) => {
|
||||||
|
if (!conversationId || !lastUnreadMessageId) return;
|
||||||
|
|
||||||
|
messageIdsMarkedRead.forEach((id) => {
|
||||||
|
client.cache.modify({
|
||||||
|
id: client.cache.identify({ __typename: "messages", id }),
|
||||||
|
fields: { read: () => true }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
client.cache.modify({
|
||||||
|
id: client.cache.identify({ __typename: "messages", id: lastUnreadMessageId }),
|
||||||
|
fields: { read: () => false }
|
||||||
|
});
|
||||||
|
|
||||||
|
setConversationUnreadCountBestEffort(conversationId, unreadCount);
|
||||||
|
},
|
||||||
|
[client.cache, setConversationUnreadCountBestEffort]
|
||||||
);
|
);
|
||||||
|
|
||||||
// WebSocket event handlers
|
// WebSocket event handlers
|
||||||
@@ -103,20 +144,25 @@ function ChatConversationContainer({ bodyshop, selectedConversation }) {
|
|||||||
if (!socket?.connected) return;
|
if (!socket?.connected) return;
|
||||||
|
|
||||||
const handleConversationChange = (data) => {
|
const handleConversationChange = (data) => {
|
||||||
if (data.type === "conversation-marked-read") {
|
if (data?.type === "conversation-marked-read") {
|
||||||
const { conversationId, messageIds } = data;
|
updateCacheWithReadMessages(data.conversationId, data.messageIds);
|
||||||
updateCacheWithReadMessages(conversationId, messageIds);
|
}
|
||||||
|
|
||||||
|
if (data?.type === "conversation-marked-unread") {
|
||||||
|
applyUnreadStateWithMaxOneUnread({
|
||||||
|
conversationId: data.conversationId,
|
||||||
|
lastUnreadMessageId: data.lastUnreadMessageId,
|
||||||
|
messageIdsMarkedRead: data.messageIdsMarkedRead,
|
||||||
|
unreadCount: data.unreadCount
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
socket.on("conversation-changed", handleConversationChange);
|
socket.on("conversation-changed", handleConversationChange);
|
||||||
|
return () => socket.off("conversation-changed", handleConversationChange);
|
||||||
|
}, [socket, updateCacheWithReadMessages, applyUnreadStateWithMaxOneUnread]);
|
||||||
|
|
||||||
return () => {
|
// Join/leave conversation via WebSocket
|
||||||
socket.off("conversation-changed", handleConversationChange);
|
|
||||||
};
|
|
||||||
}, [socket, updateCacheWithReadMessages]);
|
|
||||||
|
|
||||||
// Join and leave conversation via WebSocket
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!socket?.connected || !selectedConversation || !bodyshop?.id) return;
|
if (!socket?.connected || !selectedConversation || !bodyshop?.id) return;
|
||||||
|
|
||||||
@@ -133,15 +179,21 @@ function ChatConversationContainer({ bodyshop, selectedConversation }) {
|
|||||||
};
|
};
|
||||||
}, [socket, bodyshop, selectedConversation]);
|
}, [socket, bodyshop, selectedConversation]);
|
||||||
|
|
||||||
// Mark conversation as read
|
const inboundNonSystemMessages = useMemo(() => {
|
||||||
const handleMarkConversationAsRead = async () => {
|
const msgs = conversation?.messages || [];
|
||||||
if (!convoData || markingAsReadInProgress) return;
|
return msgs
|
||||||
|
.filter((m) => m && !m.isoutbound && !m.is_system)
|
||||||
|
.slice()
|
||||||
|
.sort((a, b) => new Date(a.created_at).getTime() - new Date(b.created_at).getTime());
|
||||||
|
}, [conversation?.messages]);
|
||||||
|
|
||||||
const conversation = convoData.conversations_by_pk;
|
const canMarkUnread = inboundNonSystemMessages.length > 0;
|
||||||
if (!conversation) return;
|
|
||||||
|
const handleMarkConversationAsRead = async () => {
|
||||||
|
if (!conversation || markingAsReadInProgress) return;
|
||||||
|
|
||||||
const unreadMessageIds = conversation.messages
|
const unreadMessageIds = conversation.messages
|
||||||
?.filter((message) => !message.read && !message.isoutbound)
|
?.filter((message) => !message.read && !message.isoutbound && !message.is_system)
|
||||||
.map((message) => message.id);
|
.map((message) => message.id);
|
||||||
|
|
||||||
if (unreadMessageIds?.length > 0) {
|
if (unreadMessageIds?.length > 0) {
|
||||||
@@ -162,12 +214,48 @@ function ChatConversationContainer({ bodyshop, selectedConversation }) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleMarkLastMessageAsUnread = async () => {
|
||||||
|
if (!conversation || markingAsUnreadInProgress) return;
|
||||||
|
if (!bodyshop?.id || !bodyshop?.imexshopid) return;
|
||||||
|
|
||||||
|
const lastInbound = inboundNonSystemMessages[inboundNonSystemMessages.length - 1];
|
||||||
|
if (!lastInbound?.id) return;
|
||||||
|
|
||||||
|
setMarkingAsUnreadInProgress(true);
|
||||||
|
try {
|
||||||
|
const res = await axios.post("/sms/markLastMessageUnread", {
|
||||||
|
conversationId: conversation.id,
|
||||||
|
imexshopid: bodyshop.imexshopid,
|
||||||
|
bodyshopid: bodyshop.id
|
||||||
|
});
|
||||||
|
|
||||||
|
const payload = res?.data || {};
|
||||||
|
if (payload.lastUnreadMessageId) {
|
||||||
|
applyUnreadStateWithMaxOneUnread({
|
||||||
|
conversationId: conversation.id,
|
||||||
|
lastUnreadMessageId: payload.lastUnreadMessageId,
|
||||||
|
messageIdsMarkedRead: payload.messageIdsMarkedRead || [],
|
||||||
|
unreadCount: typeof payload.unreadCount === "number" ? payload.unreadCount : 1
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
setConversationUnreadCountBestEffort(conversation.id, 0);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error marking last message unread:", error.message);
|
||||||
|
} finally {
|
||||||
|
setMarkingAsUnreadInProgress(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ChatConversationComponent
|
<ChatConversationComponent
|
||||||
subState={[convoLoading, convoError]}
|
subState={[convoLoading, convoError]}
|
||||||
conversation={convoData?.conversations_by_pk || {}}
|
conversation={conversation || {}}
|
||||||
messages={convoData?.conversations_by_pk?.messages || []}
|
messages={conversation?.messages || []}
|
||||||
handleMarkConversationAsRead={handleMarkConversationAsRead}
|
handleMarkConversationAsRead={handleMarkConversationAsRead}
|
||||||
|
handleMarkLastMessageAsUnread={handleMarkLastMessageAsUnread}
|
||||||
|
markingAsUnreadInProgress={markingAsUnreadInProgress}
|
||||||
|
canMarkUnread={canMarkUnread}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import { MailOutlined } from "@ant-design/icons";
|
||||||
|
import { Button, Tooltip } from "antd";
|
||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
|
export default function ChatMarkUnreadButton({ disabled, loading, onMarkUnread }) {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Tooltip title={t("messaging.labels.mark_unread")}>
|
||||||
|
<Button
|
||||||
|
size="small"
|
||||||
|
icon={<MailOutlined />}
|
||||||
|
loading={loading}
|
||||||
|
disabled={disabled}
|
||||||
|
onMouseDown={(e) => e.stopPropagation()} // prevent parent mark-read handler
|
||||||
|
onClick={(e) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
onMarkUnread?.();
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</Tooltip>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -2,7 +2,27 @@ import { gql } from "@apollo/client";
|
|||||||
|
|
||||||
export const UNREAD_CONVERSATION_COUNT = gql`
|
export const UNREAD_CONVERSATION_COUNT = gql`
|
||||||
query UNREAD_CONVERSATION_COUNT {
|
query UNREAD_CONVERSATION_COUNT {
|
||||||
messages_aggregate(where: { read: { _eq: false }, isoutbound: { _eq: false } }) {
|
# How many conversations have at least one unread inbound, non-system message
|
||||||
|
conversations_aggregate(
|
||||||
|
where: {
|
||||||
|
archived: { _eq: false }
|
||||||
|
messages: { read: { _eq: false }, isoutbound: { _eq: false }, is_system: { _eq: false } }
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
aggregate {
|
||||||
|
count
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# How many unread inbound, non-system messages exist (excluding archived conversations)
|
||||||
|
messages_aggregate(
|
||||||
|
where: {
|
||||||
|
read: { _eq: false }
|
||||||
|
isoutbound: { _eq: false }
|
||||||
|
is_system: { _eq: false }
|
||||||
|
conversation: { archived: { _eq: false } }
|
||||||
|
}
|
||||||
|
) {
|
||||||
aggregate {
|
aggregate {
|
||||||
count
|
count
|
||||||
}
|
}
|
||||||
@@ -19,7 +39,7 @@ export const CONVERSATION_LIST_QUERY = gql`
|
|||||||
unreadcnt
|
unreadcnt
|
||||||
archived
|
archived
|
||||||
label
|
label
|
||||||
messages_aggregate(where: { read: { _eq: false }, isoutbound: { _eq: false } }) {
|
messages_aggregate(where: { read: { _eq: false }, isoutbound: { _eq: false }, is_system: { _eq: false } }) {
|
||||||
aggregate {
|
aggregate {
|
||||||
count
|
count
|
||||||
}
|
}
|
||||||
@@ -41,6 +61,7 @@ export const CONVERSATION_SUBSCRIPTION_BY_PK = gql`
|
|||||||
subscription CONVERSATION_SUBSCRIPTION_BY_PK($conversationId: uuid!) {
|
subscription CONVERSATION_SUBSCRIPTION_BY_PK($conversationId: uuid!) {
|
||||||
messages(order_by: { created_at: asc_nulls_first }, where: { conversationid: { _eq: $conversationId } }) {
|
messages(order_by: { created_at: asc_nulls_first }, where: { conversationid: { _eq: $conversationId } }) {
|
||||||
id
|
id
|
||||||
|
conversationid
|
||||||
status
|
status
|
||||||
text
|
text
|
||||||
is_system
|
is_system
|
||||||
@@ -76,6 +97,7 @@ export const GET_CONVERSATION_DETAILS = gql`
|
|||||||
}
|
}
|
||||||
messages(order_by: { created_at: asc_nulls_first }) {
|
messages(order_by: { created_at: asc_nulls_first }) {
|
||||||
id
|
id
|
||||||
|
conversationid
|
||||||
status
|
status
|
||||||
text
|
text
|
||||||
is_system
|
is_system
|
||||||
@@ -110,7 +132,7 @@ export const CONVERSATION_ID_BY_PHONE = gql`
|
|||||||
ro_number
|
ro_number
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
messages_aggregate(where: { read: { _eq: false }, isoutbound: { _eq: false } }) {
|
messages_aggregate(where: { read: { _eq: false }, isoutbound: { _eq: false }, is_system: { _eq: false } }) {
|
||||||
aggregate {
|
aggregate {
|
||||||
count
|
count
|
||||||
}
|
}
|
||||||
@@ -139,7 +161,7 @@ export const CREATE_CONVERSATION = gql`
|
|||||||
ro_number
|
ro_number
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
messages_aggregate(where: { read: { _eq: false }, isoutbound: { _eq: false } }) {
|
messages_aggregate(where: { read: { _eq: false }, isoutbound: { _eq: false }, is_system: { _eq: false } }) {
|
||||||
aggregate {
|
aggregate {
|
||||||
count
|
count
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2430,7 +2430,8 @@
|
|||||||
"selectmedia": "Select Media",
|
"selectmedia": "Select Media",
|
||||||
"sentby": "Sent by {{by}} at {{time}}",
|
"sentby": "Sent by {{by}} at {{time}}",
|
||||||
"typeamessage": "Send a message...",
|
"typeamessage": "Send a message...",
|
||||||
"unarchive": "Unarchive"
|
"unarchive": "Unarchive",
|
||||||
|
"mark_unread": "Mark as Unread"
|
||||||
},
|
},
|
||||||
"render": {
|
"render": {
|
||||||
"conversation_list": "Conversation List"
|
"conversation_list": "Conversation List"
|
||||||
|
|||||||
@@ -2430,7 +2430,8 @@
|
|||||||
"selectmedia": "",
|
"selectmedia": "",
|
||||||
"sentby": "",
|
"sentby": "",
|
||||||
"typeamessage": "Enviar un mensaje...",
|
"typeamessage": "Enviar un mensaje...",
|
||||||
"unarchive": ""
|
"unarchive": "",
|
||||||
|
"mark_unread": ""
|
||||||
},
|
},
|
||||||
"render": {
|
"render": {
|
||||||
"conversation_list": ""
|
"conversation_list": ""
|
||||||
|
|||||||
@@ -2430,7 +2430,8 @@
|
|||||||
"selectmedia": "",
|
"selectmedia": "",
|
||||||
"sentby": "",
|
"sentby": "",
|
||||||
"typeamessage": "Envoyer un message...",
|
"typeamessage": "Envoyer un message...",
|
||||||
"unarchive": ""
|
"unarchive": "",
|
||||||
|
"mark_unread": ""
|
||||||
},
|
},
|
||||||
"render": {
|
"render": {
|
||||||
"conversation_list": ""
|
"conversation_list": ""
|
||||||
|
|||||||
Reference in New Issue
Block a user