front-hub/src/components/FloatingSupportChat/FloatingSupportChat.tsx

197 lines
5.1 KiB
TypeScript
Raw Normal View History

2025-05-12 15:18:16 +00:00
import { useState, useEffect, forwardRef, useMemo } from "react";
2024-03-12 10:28:13 +00:00
import {
Box,
Fab,
Typography,
Badge,
Dialog,
Slide,
useTheme,
useMediaQuery,
} from "@mui/material";
2024-02-14 09:43:19 +00:00
import CircleDoubleDown from "./CircleDoubleDownIcon";
import Chat from "./Chat";
2024-03-11 16:02:37 +00:00
import { useUserStore } from "@root/stores/user";
import { useTicketStore } from "@root/stores/tickets";
2023-04-13 16:48:17 +00:00
2024-03-12 10:28:13 +00:00
import type { ReactNode } from "react";
import type { TransitionProps } from "@mui/material/transitions";
const Transition = forwardRef(function Transition(
props: TransitionProps & {
children: ReactNode;
},
ref: React.Ref<unknown>
) {
return (
<Slide direction="up" ref={ref} {...props}>
{props.children ? (
<Box sx={{ height: "100%" }}>{props.children}</Box>
) : (
<Box />
)}
</Slide>
);
});
2023-04-13 16:48:17 +00:00
export default function FloatingSupportChat() {
2024-03-14 15:13:12 +00:00
const [monitorType, setMonitorType] = useState<"desktop" | "mobile" | "">("");
2024-02-14 09:43:19 +00:00
const [isChatOpened, setIsChatOpened] = useState<boolean>(false);
const theme = useTheme();
2024-03-12 10:28:13 +00:00
const isMobile = useMediaQuery(theme.breakpoints.down(800));
2024-03-11 16:02:37 +00:00
const user = useUserStore((state) => state.user?._id);
const { messages } = useTicketStore(
(state) => state[user ? "authData" : "unauthData"]
);
2024-02-14 09:43:19 +00:00
const animation = {
"@keyframes runningStripe": {
"0%": {
left: "10%",
backgroundColor: "transparent",
},
"10%": {
backgroundColor: "#ffffff",
},
"50%": {
backgroundColor: "#ffffff",
transform: "translate(400px, 0)",
},
"80%": {
backgroundColor: "#ffffff",
},
2024-02-14 09:43:19 +00:00
"100%": {
backgroundColor: "transparent",
boxShadow: "none",
left: "100%",
},
},
};
2024-03-11 16:02:37 +00:00
2025-05-12 15:18:16 +00:00
const unreadMessagesCount = useMemo(() => {
let count = 0;
// Идём с конца массива к началу
for (let i = messages.length - 1; i >= 0; i--) {
const message = messages[i];
// Пропускаем сообщение с id "111"
if (message.id === "111") continue;
// Если сообщение не прочитано (shown.me !== 1)
if (message.shown.me !== 1) {
count++;
} else {
// Встретили прочитанное сообщение - прекращаем подсчёт
break;
}
}
return count;
}, [messages]); // Зависимость от messages - пересчитывается при их изменении
2024-03-14 15:13:12 +00:00
useEffect(() => {
const onResize = () => {
if (document.fullscreenElement) {
setMonitorType(isMobile ? "mobile" : "desktop");
return;
}
setMonitorType("");
};
window.addEventListener("resize", onResize);
return () => {
window.removeEventListener("resize", onResize);
};
}, [isMobile]);
2024-02-14 09:43:19 +00:00
return (
<Box
sx={{
position: "fixed",
right: "20px",
bottom: "10px",
display: "flex",
flexDirection: "column",
gap: "8px",
width: "clamp(200px, 100% - 40px, 454px)",
zIndex: 10,
}}
>
<Chat
2024-03-14 15:13:12 +00:00
open={isChatOpened && (monitorType === "desktop" || !isMobile)}
2024-02-14 09:43:19 +00:00
sx={{ alignSelf: "start", width: "clamp(200px, 100%, 400px)" }}
/>
2024-03-12 10:28:13 +00:00
<Dialog
fullScreen
2024-03-14 15:13:12 +00:00
open={isChatOpened && (monitorType === "mobile" || isMobile)}
2024-03-12 10:28:13 +00:00
onClose={() => setIsChatOpened(false)}
TransitionComponent={Transition}
>
<Chat
2024-03-14 15:13:12 +00:00
open={isChatOpened && (monitorType === "mobile" || isMobile)}
2024-03-12 10:28:13 +00:00
onclickArrow={() => setIsChatOpened(false)}
/>
</Dialog>
2024-02-14 09:43:19 +00:00
<Fab
disableRipple
sx={{
position: "relative",
backgroundColor: "rgba(255, 255, 255, 0.7)",
pl: "11px",
pr: !isChatOpened ? "15px" : "11px",
gap: "11px",
height: "54px",
borderRadius: "27px",
alignSelf: "end",
overflow: "hidden",
"&:hover": {
background: "rgba(255, 255, 255, 0.7)",
},
}}
variant={"extended"}
onClick={() => setIsChatOpened((prev) => !prev)}
>
{!isChatOpened && (
<Box
sx={{
position: "absolute",
bgcolor: "#FFFFFF",
height: "100px",
width: "25px",
animation: "runningStripe linear 3s infinite",
transform:
" skew(-10deg) rotate(70deg) skewX(20deg) skewY(10deg)",
boxShadow: "0px 3px 12px rgba(126, 42, 234, 0.1)",
opacity: "0.4",
...animation,
}}
/>
)}
<Badge
2025-05-12 15:18:16 +00:00
badgeContent={unreadMessagesCount}
2024-02-14 09:43:19 +00:00
sx={{
"& .MuiBadge-badge": {
display: isChatOpened ? "none" : "flex",
color: "#FFFFFF",
background: theme.palette.purple.main,
},
}}
>
<CircleDoubleDown isUp={isChatOpened} />
</Badge>
2023-04-13 16:48:17 +00:00
2024-02-14 09:43:19 +00:00
{!isChatOpened && (
<Typography sx={{ zIndex: "10000" }}>Задайте нам вопрос</Typography>
)}
</Fab>
</Box>
);
}