2023-04-13 16:48:17 +00:00
|
|
|
|
import { Box, FormControl, IconButton, InputAdornment, InputBase, SxProps, Theme, Typography, useMediaQuery, useTheme } from "@mui/material";
|
2023-06-06 13:13:58 +00:00
|
|
|
|
import { TicketMessage } from "@frontend/kitui";
|
|
|
|
|
import { addOrUpdateUnauthMessages, useUnauthTicketStore, incrementUnauthMessageApiPage, setUnauthIsPreventAutoscroll, setUnauthSessionData, setIsMessageSending } from "@root/stores/unauthTicket";
|
2023-04-13 16:48:17 +00:00
|
|
|
|
import { enqueueSnackbar } from "notistack";
|
2023-06-06 13:13:58 +00:00
|
|
|
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
2023-04-13 16:48:17 +00:00
|
|
|
|
import ChatMessage from "../ChatMessage";
|
|
|
|
|
import SendIcon from "../icons/SendIcon";
|
|
|
|
|
import UserCircleIcon from "./UserCircleIcon";
|
2023-06-06 13:13:58 +00:00
|
|
|
|
import { throttle } from "@frontend/kitui";
|
2023-06-11 10:18:37 +00:00
|
|
|
|
import { makeRequest } from "@frontend/kitui";
|
2023-06-06 13:13:58 +00:00
|
|
|
|
import { useTicketMessages, getMessageFromFetchError, useSSESubscription, useEventListener, createTicket } from "@frontend/kitui";
|
2023-04-13 16:48:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
|
sx?: SxProps<Theme>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function Chat({ sx }: Props) {
|
|
|
|
|
const theme = useTheme();
|
|
|
|
|
const upMd = useMediaQuery(theme.breakpoints.up("md"));
|
|
|
|
|
const [messageField, setMessageField] = useState<string>("");
|
2023-05-03 16:24:15 +00:00
|
|
|
|
const sessionData = useUnauthTicketStore(state => state.sessionData);
|
2023-04-13 16:48:17 +00:00
|
|
|
|
const messages = useUnauthTicketStore(state => state.messages);
|
|
|
|
|
const messageApiPage = useUnauthTicketStore(state => state.apiPage);
|
|
|
|
|
const messagesPerPage = useUnauthTicketStore(state => state.messagesPerPage);
|
2023-05-23 12:49:51 +00:00
|
|
|
|
const isMessageSending = useUnauthTicketStore(state => state.isMessageSending);
|
2023-05-03 16:24:15 +00:00
|
|
|
|
const isPreventAutoscroll = useUnauthTicketStore(state => state.isPreventAutoscroll);
|
|
|
|
|
const lastMessageId = useUnauthTicketStore(state => state.lastMessageId);
|
2023-06-06 13:13:58 +00:00
|
|
|
|
const chatBoxRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
|
|
|
|
|
const fetchState = useTicketMessages({
|
|
|
|
|
url: "https://admin.pena.digital/heruvym/getMessages",
|
|
|
|
|
isUnauth: true,
|
|
|
|
|
ticketId: sessionData?.ticketId,
|
|
|
|
|
messagesPerPage,
|
|
|
|
|
messageApiPage,
|
|
|
|
|
onNewMessages: useCallback(messages => {
|
|
|
|
|
if (chatBoxRef.current && chatBoxRef.current.scrollTop < 1) chatBoxRef.current.scrollTop = 1;
|
|
|
|
|
addOrUpdateUnauthMessages(messages);
|
|
|
|
|
}, []),
|
|
|
|
|
onError: useCallback((error: Error) => {
|
|
|
|
|
const message = getMessageFromFetchError(error);
|
|
|
|
|
if (message) enqueueSnackbar(message);
|
|
|
|
|
}, []),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
useSSESubscription<TicketMessage>({
|
|
|
|
|
enabled: Boolean(sessionData),
|
|
|
|
|
url: `https://hub.pena.digital/heruvym/ticket?ticket=${sessionData?.ticketId}&s=${sessionData?.sessionId}`,
|
|
|
|
|
onNewData: addOrUpdateUnauthMessages,
|
|
|
|
|
onDisconnect: useCallback(() => {
|
2023-05-03 16:24:15 +00:00
|
|
|
|
setUnauthIsPreventAutoscroll(false);
|
2023-06-06 13:13:58 +00:00
|
|
|
|
}, []),
|
|
|
|
|
marker: "ticket"
|
|
|
|
|
});
|
2023-05-03 16:24:15 +00:00
|
|
|
|
|
2023-06-06 13:13:58 +00:00
|
|
|
|
const throttledScrollHandler = useMemo(() => throttle(() => {
|
2023-05-03 16:24:15 +00:00
|
|
|
|
const chatBox = chatBoxRef.current;
|
2023-06-06 13:13:58 +00:00
|
|
|
|
if (!chatBox) return;
|
2023-05-03 16:24:15 +00:00
|
|
|
|
|
2023-06-06 13:13:58 +00:00
|
|
|
|
const scrollBottom = chatBox.scrollHeight - chatBox.scrollTop - chatBox.clientHeight;
|
|
|
|
|
const isPreventAutoscroll = scrollBottom > chatBox.clientHeight;
|
|
|
|
|
setUnauthIsPreventAutoscroll(isPreventAutoscroll);
|
2023-05-03 16:24:15 +00:00
|
|
|
|
|
2023-06-06 13:13:58 +00:00
|
|
|
|
if (fetchState !== "idle") return;
|
2023-05-03 16:24:15 +00:00
|
|
|
|
|
2023-06-06 13:13:58 +00:00
|
|
|
|
if (chatBox.scrollTop < chatBox.clientHeight) {
|
|
|
|
|
incrementUnauthMessageApiPage();
|
|
|
|
|
}
|
|
|
|
|
}, 200), [fetchState]);
|
2023-05-03 16:24:15 +00:00
|
|
|
|
|
2023-06-06 13:13:58 +00:00
|
|
|
|
useEventListener("scroll", throttledScrollHandler, chatBoxRef);
|
2023-05-03 16:24:15 +00:00
|
|
|
|
|
|
|
|
|
useEffect(function scrollOnNewMessage() {
|
|
|
|
|
if (!chatBoxRef.current) return;
|
|
|
|
|
|
|
|
|
|
if (!isPreventAutoscroll) {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
scrollToBottom();
|
|
|
|
|
}, 50);
|
|
|
|
|
}
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [lastMessageId]);
|
|
|
|
|
|
2023-04-13 16:48:17 +00:00
|
|
|
|
async function handleSendMessage() {
|
2023-05-23 12:49:51 +00:00
|
|
|
|
if (!messageField || isMessageSending) return;
|
2023-04-13 16:48:17 +00:00
|
|
|
|
|
2023-05-03 16:24:15 +00:00
|
|
|
|
if (!sessionData) {
|
2023-05-23 12:49:51 +00:00
|
|
|
|
setIsMessageSending(true);
|
|
|
|
|
createTicket({
|
2023-06-06 13:13:58 +00:00
|
|
|
|
url: "https://hub.pena.digital/heruvym/create",
|
|
|
|
|
body: {
|
|
|
|
|
Title: "Unauth title",
|
|
|
|
|
Message: messageField,
|
|
|
|
|
},
|
|
|
|
|
useToken: false,
|
|
|
|
|
}).then(response => {
|
2023-05-23 12:49:51 +00:00
|
|
|
|
setUnauthSessionData({
|
|
|
|
|
ticketId: response.Ticket,
|
|
|
|
|
sessionId: response.sess,
|
|
|
|
|
});
|
|
|
|
|
}).catch(error => {
|
|
|
|
|
const errorMessage = getMessageFromFetchError(error);
|
|
|
|
|
if (errorMessage) enqueueSnackbar(errorMessage);
|
|
|
|
|
}).finally(() => {
|
|
|
|
|
setMessageField("");
|
|
|
|
|
setIsMessageSending(false);
|
2023-05-03 16:24:15 +00:00
|
|
|
|
});
|
2023-04-13 16:48:17 +00:00
|
|
|
|
} else {
|
2023-05-23 12:49:51 +00:00
|
|
|
|
setIsMessageSending(true);
|
2023-06-06 13:13:58 +00:00
|
|
|
|
makeRequest({
|
|
|
|
|
url: "https://hub.pena.digital/heruvym/send",
|
|
|
|
|
method: "POST",
|
|
|
|
|
useToken: false,
|
|
|
|
|
body: {
|
|
|
|
|
ticket: sessionData.ticketId,
|
|
|
|
|
message: messageField,
|
|
|
|
|
lang: "ru",
|
|
|
|
|
files: [],
|
|
|
|
|
},
|
|
|
|
|
withCredentials: true,
|
|
|
|
|
}).catch(error => {
|
2023-05-23 12:49:51 +00:00
|
|
|
|
const errorMessage = getMessageFromFetchError(error);
|
|
|
|
|
if (errorMessage) enqueueSnackbar(errorMessage);
|
|
|
|
|
}).finally(() => {
|
|
|
|
|
setMessageField("");
|
|
|
|
|
setIsMessageSending(false);
|
2023-05-03 16:24:15 +00:00
|
|
|
|
});
|
2023-04-13 16:48:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-03 16:24:15 +00:00
|
|
|
|
function scrollToBottom(behavior?: ScrollBehavior) {
|
|
|
|
|
if (!chatBoxRef.current) return;
|
|
|
|
|
|
|
|
|
|
const chatBox = chatBoxRef.current;
|
|
|
|
|
chatBox.scroll({
|
|
|
|
|
left: 0,
|
|
|
|
|
top: chatBox.scrollHeight,
|
|
|
|
|
behavior,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-03 09:54:04 +00:00
|
|
|
|
const handleTextfieldKeyPress: React.KeyboardEventHandler<HTMLInputElement | HTMLTextAreaElement> = (e) => {
|
|
|
|
|
if (e.key === "Enter" && !e.shiftKey) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
handleSendMessage();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-13 16:48:17 +00:00
|
|
|
|
return (
|
|
|
|
|
<Box sx={{
|
|
|
|
|
display: "flex",
|
|
|
|
|
flexDirection: "column",
|
2023-07-28 15:56:19 +00:00
|
|
|
|
height: "clamp(250px, calc(100vh - 90px), 600px)",
|
2023-04-13 16:48:17 +00:00
|
|
|
|
backgroundColor: "#944FEE",
|
|
|
|
|
borderRadius: "8px",
|
|
|
|
|
...sx,
|
|
|
|
|
}}>
|
|
|
|
|
<Box sx={{
|
|
|
|
|
display: "flex",
|
|
|
|
|
gap: "9px",
|
|
|
|
|
pl: "22px",
|
|
|
|
|
pt: "12px",
|
2023-05-03 10:35:20 +00:00
|
|
|
|
pb: "20px",
|
2023-04-13 16:48:17 +00:00
|
|
|
|
filter: "drop-shadow(0px 3px 12px rgba(37, 39, 52, 0.3))",
|
|
|
|
|
}}>
|
|
|
|
|
<UserCircleIcon />
|
|
|
|
|
<Box sx={{
|
|
|
|
|
mt: "5px",
|
|
|
|
|
display: "flex",
|
|
|
|
|
flexDirection: "column",
|
|
|
|
|
gap: "3px",
|
|
|
|
|
}}>
|
|
|
|
|
<Typography>Мария</Typography>
|
|
|
|
|
<Typography sx={{
|
|
|
|
|
fontSize: "16px",
|
|
|
|
|
lineHeight: "19px",
|
|
|
|
|
}}>онлайн-консультант</Typography>
|
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
<Box sx={{
|
2023-05-03 10:35:20 +00:00
|
|
|
|
flexGrow: 1,
|
2023-04-13 16:48:17 +00:00
|
|
|
|
backgroundColor: "white",
|
|
|
|
|
borderRadius: "8px",
|
|
|
|
|
display: "flex",
|
|
|
|
|
flexDirection: "column",
|
|
|
|
|
}}>
|
|
|
|
|
<Box
|
|
|
|
|
ref={chatBoxRef}
|
|
|
|
|
sx={{
|
|
|
|
|
display: "flex",
|
|
|
|
|
width: "100%",
|
2023-05-03 16:24:15 +00:00
|
|
|
|
flexBasis: 0,
|
2023-04-13 16:48:17 +00:00
|
|
|
|
flexDirection: "column",
|
|
|
|
|
gap: upMd ? "20px" : "16px",
|
|
|
|
|
px: upMd ? "20px" : "5px",
|
|
|
|
|
py: upMd ? "20px" : "13px",
|
|
|
|
|
overflowY: "auto",
|
|
|
|
|
flexGrow: 1,
|
|
|
|
|
}}
|
|
|
|
|
>
|
2023-05-03 16:24:15 +00:00
|
|
|
|
{sessionData && messages.map((message) => (
|
2023-04-13 16:48:17 +00:00
|
|
|
|
<ChatMessage
|
|
|
|
|
unAuthenticated
|
|
|
|
|
key={message.id}
|
|
|
|
|
text={message.message}
|
2023-05-09 16:28:52 +00:00
|
|
|
|
createdAt={message.created_at}
|
2023-05-03 16:24:15 +00:00
|
|
|
|
isSelf={sessionData.sessionId === message.user_id}
|
2023-04-13 16:48:17 +00:00
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</Box>
|
|
|
|
|
<FormControl fullWidth sx={{ borderTop: "1px solid black" }}>
|
|
|
|
|
<InputBase
|
|
|
|
|
value={messageField}
|
|
|
|
|
fullWidth
|
|
|
|
|
placeholder="Введите сообщение..."
|
|
|
|
|
id="message"
|
|
|
|
|
multiline
|
2023-05-03 09:54:04 +00:00
|
|
|
|
onKeyDown={handleTextfieldKeyPress}
|
2023-04-13 16:48:17 +00:00
|
|
|
|
sx={{
|
|
|
|
|
width: "100%",
|
|
|
|
|
p: 0,
|
|
|
|
|
}}
|
|
|
|
|
inputProps={{
|
|
|
|
|
sx: {
|
|
|
|
|
fontWeight: 400,
|
|
|
|
|
fontSize: "16px",
|
|
|
|
|
lineHeight: "19px",
|
|
|
|
|
pt: upMd ? "30px" : "28px",
|
|
|
|
|
pb: upMd ? "30px" : "24px",
|
|
|
|
|
px: "19px",
|
|
|
|
|
maxHeight: "calc(19px * 5)",
|
|
|
|
|
color: "black",
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
onChange={(e) => setMessageField(e.target.value)}
|
|
|
|
|
endAdornment={
|
|
|
|
|
<InputAdornment position="end">
|
|
|
|
|
<IconButton
|
2023-05-23 12:49:51 +00:00
|
|
|
|
disabled={isMessageSending}
|
2023-04-13 16:48:17 +00:00
|
|
|
|
onClick={handleSendMessage}
|
|
|
|
|
sx={{
|
|
|
|
|
height: "53px",
|
|
|
|
|
width: "53px",
|
|
|
|
|
mr: "13px",
|
|
|
|
|
p: 0,
|
2023-05-23 12:49:51 +00:00
|
|
|
|
opacity: isMessageSending ? 0.3 : 1,
|
2023-04-13 16:48:17 +00:00
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<SendIcon style={{
|
|
|
|
|
width: "100%",
|
|
|
|
|
height: "100%",
|
|
|
|
|
}} />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</InputAdornment>
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
</FormControl>
|
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
);
|
|
|
|
|
}
|