2024-02-06 14:39:02 +00:00
|
|
|
|
import {
|
|
|
|
|
Box,
|
|
|
|
|
FormControl,
|
|
|
|
|
IconButton,
|
|
|
|
|
InputAdornment,
|
|
|
|
|
InputBase,
|
2024-03-06 11:13:28 +00:00
|
|
|
|
InputLabel,
|
2024-02-06 14:39:02 +00:00
|
|
|
|
SxProps,
|
|
|
|
|
Theme,
|
|
|
|
|
Typography,
|
|
|
|
|
useMediaQuery,
|
|
|
|
|
useTheme,
|
|
|
|
|
} from "@mui/material";
|
2024-03-06 11:13:28 +00:00
|
|
|
|
import { makeRequest } from "@frontend/kitui";
|
2024-02-06 14:39:02 +00:00
|
|
|
|
import {
|
2024-03-06 11:13:28 +00:00
|
|
|
|
useTicketStore,
|
2024-02-06 14:39:02 +00:00
|
|
|
|
incrementUnauthMessageApiPage,
|
|
|
|
|
setUnauthIsPreventAutoscroll,
|
2024-03-06 11:13:28 +00:00
|
|
|
|
} from "@root/ticket";
|
|
|
|
|
import { useEffect, useMemo, useRef, useState } from "react";
|
2024-02-06 14:39:02 +00:00
|
|
|
|
import ChatMessage from "./ChatMessage";
|
|
|
|
|
import SendIcon from "@icons/SendIcon";
|
|
|
|
|
import UserCircleIcon from "./UserCircleIcon";
|
2024-03-06 11:13:28 +00:00
|
|
|
|
import { throttle, getAuthToken } from "@frontend/kitui";
|
|
|
|
|
import { useEventListener } from "@frontend/kitui";
|
2024-02-10 14:58:36 +00:00
|
|
|
|
import ArrowLeft from "@icons/questionsPage/arrowLeft";
|
2024-03-06 11:13:28 +00:00
|
|
|
|
import { useUserStore } from "@root/user";
|
2024-03-09 21:01:57 +00:00
|
|
|
|
import AttachFileIcon from "@mui/icons-material/AttachFile";
|
2024-03-06 11:13:28 +00:00
|
|
|
|
import ChatImage from "./ChatImage";
|
2024-03-09 22:05:54 +00:00
|
|
|
|
import ChatDocument from "@ui_kit/FloatingSupportChat/ChatDocument";
|
|
|
|
|
import * as React from "react";
|
2024-02-06 14:39:02 +00:00
|
|
|
|
|
|
|
|
|
interface Props {
|
2024-02-14 16:47:30 +00:00
|
|
|
|
open: boolean;
|
2024-02-06 14:39:02 +00:00
|
|
|
|
sx?: SxProps<Theme>;
|
2024-02-10 14:58:36 +00:00
|
|
|
|
onclickArrow?: () => void;
|
2024-03-09 21:01:57 +00:00
|
|
|
|
sendMessage: (a: string) => Promise<boolean>;
|
2024-03-06 11:13:28 +00:00
|
|
|
|
sendFile: (a: File | undefined) => Promise<true>;
|
2024-02-06 14:39:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-09 22:05:54 +00:00
|
|
|
|
const fileTypes = {
|
|
|
|
|
picture: [".jpg", ".png"],
|
|
|
|
|
video: [".mp4"],
|
|
|
|
|
document: [".doc", ".docx", ".pdf", ".txt", ".xlsx", ".csv"],
|
|
|
|
|
};
|
|
|
|
|
|
2024-03-09 21:01:57 +00:00
|
|
|
|
export default function Chat({
|
|
|
|
|
open = false,
|
|
|
|
|
sx,
|
|
|
|
|
onclickArrow,
|
|
|
|
|
sendMessage,
|
|
|
|
|
sendFile,
|
|
|
|
|
}: Props) {
|
2024-02-06 14:39:02 +00:00
|
|
|
|
const theme = useTheme();
|
|
|
|
|
const upMd = useMediaQuery(theme.breakpoints.up("md"));
|
2024-02-10 14:58:36 +00:00
|
|
|
|
const isMobile = useMediaQuery(theme.breakpoints.down(800));
|
2024-02-06 14:39:02 +00:00
|
|
|
|
const [messageField, setMessageField] = useState<string>("");
|
2024-03-06 11:13:28 +00:00
|
|
|
|
|
|
|
|
|
const user = useUserStore((state) => state.user?._id);
|
2024-03-09 21:01:57 +00:00
|
|
|
|
const ticket = useTicketStore(
|
|
|
|
|
(state) => state[user ? "authData" : "unauthData"],
|
|
|
|
|
);
|
2024-03-06 11:13:28 +00:00
|
|
|
|
|
2024-03-09 21:01:57 +00:00
|
|
|
|
const messages = ticket.messages;
|
|
|
|
|
const isMessageSending = ticket.isMessageSending;
|
|
|
|
|
const isPreventAutoscroll = ticket.isPreventAutoscroll;
|
|
|
|
|
const lastMessageId = ticket.lastMessageId;
|
|
|
|
|
const fetchState = ticket.unauthTicketMessageFetchState;
|
2024-03-06 11:13:28 +00:00
|
|
|
|
|
2024-02-06 14:39:02 +00:00
|
|
|
|
const chatBoxRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
|
2024-03-06 11:13:28 +00:00
|
|
|
|
const sendMessageHC = async () => {
|
2024-03-09 21:01:57 +00:00
|
|
|
|
const successful = await sendMessage(messageField);
|
|
|
|
|
console.log("successful ", successful);
|
2024-03-06 11:13:28 +00:00
|
|
|
|
if (successful) {
|
|
|
|
|
setMessageField("");
|
|
|
|
|
}
|
2024-03-09 21:01:57 +00:00
|
|
|
|
};
|
2024-02-06 14:39:02 +00:00
|
|
|
|
|
2024-03-09 21:01:57 +00:00
|
|
|
|
const fileInputRef = useRef<HTMLInputElement>(null);
|
2024-02-06 14:39:02 +00:00
|
|
|
|
|
|
|
|
|
const throttledScrollHandler = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
throttle(() => {
|
|
|
|
|
const chatBox = chatBoxRef.current;
|
|
|
|
|
if (!chatBox) return;
|
|
|
|
|
|
|
|
|
|
const scrollBottom =
|
|
|
|
|
chatBox.scrollHeight - chatBox.scrollTop - chatBox.clientHeight;
|
|
|
|
|
const isPreventAutoscroll = scrollBottom > chatBox.clientHeight;
|
|
|
|
|
setUnauthIsPreventAutoscroll(isPreventAutoscroll);
|
|
|
|
|
|
|
|
|
|
if (fetchState !== "idle") return;
|
|
|
|
|
|
|
|
|
|
if (chatBox.scrollTop < chatBox.clientHeight) {
|
|
|
|
|
incrementUnauthMessageApiPage();
|
|
|
|
|
}
|
|
|
|
|
}, 200),
|
|
|
|
|
[fetchState],
|
|
|
|
|
);
|
|
|
|
|
useEventListener("scroll", throttledScrollHandler, chatBoxRef);
|
|
|
|
|
|
2024-03-06 11:13:28 +00:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (chatBoxRef.current && chatBoxRef.current.scrollTop < 1)
|
|
|
|
|
chatBoxRef.current.scrollTop = 1;
|
2024-03-09 21:01:57 +00:00
|
|
|
|
}, [messages]);
|
2024-02-06 14:39:02 +00:00
|
|
|
|
useEffect(
|
|
|
|
|
function scrollOnNewMessage() {
|
|
|
|
|
if (!chatBoxRef.current) return;
|
|
|
|
|
|
|
|
|
|
if (!isPreventAutoscroll) {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
scrollToBottom();
|
|
|
|
|
}, 50);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[lastMessageId],
|
|
|
|
|
);
|
|
|
|
|
function scrollToBottom(behavior?: ScrollBehavior) {
|
|
|
|
|
if (!chatBoxRef.current) return;
|
|
|
|
|
|
|
|
|
|
const chatBox = chatBoxRef.current;
|
|
|
|
|
chatBox.scroll({
|
|
|
|
|
left: 0,
|
|
|
|
|
top: chatBox.scrollHeight,
|
|
|
|
|
behavior,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
const handleTextfieldKeyPress: React.KeyboardEventHandler<
|
|
|
|
|
HTMLInputElement | HTMLTextAreaElement
|
|
|
|
|
> = (e) => {
|
|
|
|
|
if (e.key === "Enter" && !e.shiftKey) {
|
|
|
|
|
e.preventDefault();
|
2024-03-06 11:13:28 +00:00
|
|
|
|
sendMessageHC();
|
2024-02-06 14:39:02 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2024-02-14 16:47:30 +00:00
|
|
|
|
<>
|
|
|
|
|
{open && (
|
2024-02-06 14:39:02 +00:00
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
display: "flex",
|
|
|
|
|
flexDirection: "column",
|
2024-02-14 16:47:30 +00:00
|
|
|
|
height: isMobile
|
|
|
|
|
? "100%"
|
|
|
|
|
: "clamp(250px, calc(100vh - 90px), 600px)",
|
|
|
|
|
backgroundColor: "#944FEE",
|
|
|
|
|
borderRadius: "8px",
|
|
|
|
|
...sx,
|
2024-02-06 14:39:02 +00:00
|
|
|
|
}}
|
|
|
|
|
>
|
2024-02-14 16:47:30 +00:00
|
|
|
|
<Box
|
2024-02-06 14:39:02 +00:00
|
|
|
|
sx={{
|
2024-02-14 16:47:30 +00:00
|
|
|
|
display: "flex",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
gap: "9px",
|
|
|
|
|
pl: "22px",
|
|
|
|
|
pt: "12px",
|
|
|
|
|
pb: "20px",
|
|
|
|
|
filter: "drop-shadow(0px 3px 12px rgba(37, 39, 52, 0.3))",
|
2024-02-06 14:39:02 +00:00
|
|
|
|
}}
|
|
|
|
|
>
|
2024-02-14 16:47:30 +00:00
|
|
|
|
{isMobile && (
|
|
|
|
|
<IconButton onClick={onclickArrow}>
|
|
|
|
|
<ArrowLeft color="white" />
|
|
|
|
|
</IconButton>
|
|
|
|
|
)}
|
|
|
|
|
<UserCircleIcon />
|
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
mt: "5px",
|
|
|
|
|
display: "flex",
|
|
|
|
|
flexDirection: "column",
|
|
|
|
|
gap: "3px",
|
|
|
|
|
color: theme.palette.common.white,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Typography>Мария</Typography>
|
|
|
|
|
<Typography
|
|
|
|
|
sx={{
|
|
|
|
|
fontSize: "16px",
|
|
|
|
|
lineHeight: "19px",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
онлайн-консультант
|
|
|
|
|
</Typography>
|
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
<Box
|
2024-02-06 14:39:02 +00:00
|
|
|
|
sx={{
|
2024-02-14 16:47:30 +00:00
|
|
|
|
flexGrow: 1,
|
|
|
|
|
backgroundColor: "white",
|
|
|
|
|
borderRadius: "8px",
|
|
|
|
|
display: "flex",
|
|
|
|
|
flexDirection: "column",
|
2024-02-06 14:39:02 +00:00
|
|
|
|
}}
|
2024-02-14 16:47:30 +00:00
|
|
|
|
>
|
|
|
|
|
<Box
|
|
|
|
|
ref={chatBoxRef}
|
|
|
|
|
sx={{
|
|
|
|
|
display: "flex",
|
|
|
|
|
width: "100%",
|
|
|
|
|
flexBasis: 0,
|
|
|
|
|
flexDirection: "column",
|
|
|
|
|
gap: upMd ? "20px" : "16px",
|
|
|
|
|
px: upMd ? "20px" : "5px",
|
|
|
|
|
py: upMd ? "20px" : "13px",
|
|
|
|
|
overflowY: "auto",
|
|
|
|
|
flexGrow: 1,
|
|
|
|
|
}}
|
|
|
|
|
>
|
2024-03-06 11:13:28 +00:00
|
|
|
|
{ticket.sessionData?.ticketId &&
|
|
|
|
|
messages.map((message) => {
|
2024-03-09 22:05:54 +00:00
|
|
|
|
const isFileImage = () => {
|
|
|
|
|
if (message.files.name) {
|
|
|
|
|
fileTypes.picture.some((fileType) =>
|
|
|
|
|
message.files.name.toLowerCase().endsWith(fileType),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
const isFileDocument = () => {
|
|
|
|
|
if (message.files.name) {
|
|
|
|
|
fileTypes.document.some((fileType) =>
|
|
|
|
|
message.files.name.toLowerCase().endsWith(fileType),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
console.log(message)
|
|
|
|
|
if (message.files.length > 0 && isFileImage) {
|
|
|
|
|
return <ChatImage
|
|
|
|
|
unAuthenticated
|
|
|
|
|
key={message.id}
|
|
|
|
|
file={message.files[0]}
|
|
|
|
|
createdAt={message.created_at}
|
|
|
|
|
isSelf={(ticket.sessionData?.sessionId || user) === message.user_id}
|
|
|
|
|
/>
|
|
|
|
|
}
|
|
|
|
|
if (message.files.length > 0 && isFileDocument) {
|
|
|
|
|
return <ChatDocument
|
|
|
|
|
unAuthenticated
|
|
|
|
|
key={message.id}
|
|
|
|
|
file={message.files[0]}
|
|
|
|
|
createdAt={message.created_at}
|
|
|
|
|
isSelf={(ticket.sessionData?.sessionId || user) === message.user_id}
|
|
|
|
|
/>
|
|
|
|
|
} else {
|
|
|
|
|
return <ChatMessage
|
|
|
|
|
unAuthenticated
|
|
|
|
|
key={message.id}
|
|
|
|
|
text={message.message}
|
|
|
|
|
createdAt={message.created_at}
|
|
|
|
|
isSelf={(ticket.sessionData?.sessionId || user) === message.user_id}
|
2024-03-06 11:13:28 +00:00
|
|
|
|
/>
|
2024-03-09 22:05:54 +00:00
|
|
|
|
}
|
2024-03-09 21:01:57 +00:00
|
|
|
|
})}
|
2024-02-14 16:47:30 +00:00
|
|
|
|
</Box>
|
|
|
|
|
<FormControl fullWidth sx={{ borderTop: "1px solid black" }}>
|
|
|
|
|
<InputBase
|
|
|
|
|
value={messageField}
|
|
|
|
|
fullWidth
|
|
|
|
|
placeholder="Введите сообщение..."
|
|
|
|
|
id="message"
|
|
|
|
|
multiline
|
|
|
|
|
onKeyDown={handleTextfieldKeyPress}
|
|
|
|
|
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">
|
2024-03-06 11:13:28 +00:00
|
|
|
|
<InputLabel htmlFor="fileinput">
|
2024-03-09 21:01:57 +00:00
|
|
|
|
<IconButton onClick={() => fileInputRef.current?.click()}>
|
2024-03-06 11:13:28 +00:00
|
|
|
|
<AttachFileIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</InputLabel>
|
|
|
|
|
<input
|
|
|
|
|
ref={fileInputRef}
|
|
|
|
|
id="fileinput"
|
|
|
|
|
onChange={(e) => {
|
2024-03-09 21:01:57 +00:00
|
|
|
|
sendFile(e.target.files?.[0]);
|
2024-03-06 11:13:28 +00:00
|
|
|
|
}}
|
|
|
|
|
style={{ display: "none" }}
|
|
|
|
|
type="file"
|
|
|
|
|
/>
|
2024-02-14 16:47:30 +00:00
|
|
|
|
<IconButton
|
|
|
|
|
disabled={isMessageSending}
|
2024-03-06 11:13:28 +00:00
|
|
|
|
onClick={sendMessageHC}
|
2024-02-14 16:47:30 +00:00
|
|
|
|
sx={{
|
|
|
|
|
height: "53px",
|
|
|
|
|
width: "53px",
|
|
|
|
|
mr: "13px",
|
|
|
|
|
p: 0,
|
|
|
|
|
opacity: isMessageSending ? 0.3 : 1,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<SendIcon
|
|
|
|
|
style={{
|
|
|
|
|
width: "100%",
|
|
|
|
|
height: "100%",
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</IconButton>
|
|
|
|
|
</InputAdornment>
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
</FormControl>
|
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
2024-02-06 14:39:02 +00:00
|
|
|
|
);
|
|
|
|
|
}
|