2023-09-18 22:28:07 +03:00
|
|
|
/* eslint-disable max-lines */
|
2023-09-18 19:25:28 +03:00
|
|
|
/* eslint-disable complexity */
|
2023-06-11 00:59:16 +03:00
|
|
|
"use client";
|
2023-07-05 10:30:22 +03:00
|
|
|
import { motion, MotionConfig } from "framer-motion";
|
|
|
|
import { MdChevronRight } from "react-icons/md";
|
|
|
|
|
2023-07-03 19:38:12 +03:00
|
|
|
import { useChatsContext } from "@/lib/context/ChatsProvider/hooks/useChatsContext";
|
2023-09-18 22:28:07 +03:00
|
|
|
import { cn } from "@/lib/utils";
|
2023-06-15 12:52:46 +03:00
|
|
|
|
2023-07-03 18:39:59 +03:00
|
|
|
import { ChatsListItem } from "./components/ChatsListItem";
|
2023-06-27 12:28:09 +03:00
|
|
|
import { MiniFooter } from "./components/ChatsListItem/components/MiniFooter";
|
2023-07-03 18:39:59 +03:00
|
|
|
import { NewChatButton } from "./components/NewChatButton";
|
2023-06-27 12:28:09 +03:00
|
|
|
import { useChatsList } from "./hooks/useChatsList";
|
2023-09-18 22:28:07 +03:00
|
|
|
import {
|
|
|
|
isToday,
|
|
|
|
isWithinLast30Days,
|
|
|
|
isWithinLast7Days,
|
|
|
|
isYesterday,
|
|
|
|
} from "./utils";
|
2023-08-11 11:06:20 +03:00
|
|
|
import { useSelectedChatPage } from "../../[chatId]/hooks/useSelectedChatPage";
|
2023-06-15 12:52:46 +03:00
|
|
|
|
|
|
|
export const ChatsList = (): JSX.Element => {
|
2023-07-05 19:33:18 +03:00
|
|
|
const { allChats } = useChatsContext();
|
2023-09-18 22:28:07 +03:00
|
|
|
|
2023-06-27 12:28:09 +03:00
|
|
|
const { open, setOpen } = useChatsList();
|
2023-08-11 11:06:20 +03:00
|
|
|
useSelectedChatPage();
|
2023-07-05 10:30:22 +03:00
|
|
|
|
2023-09-18 22:28:07 +03:00
|
|
|
const todayChats = allChats.filter((chat) =>
|
|
|
|
isToday(new Date(chat.creation_time))
|
|
|
|
);
|
|
|
|
const yesterdayChats = allChats.filter((chat) =>
|
|
|
|
isYesterday(new Date(chat.creation_time))
|
|
|
|
);
|
|
|
|
const last7DaysChats = allChats.filter((chat) =>
|
|
|
|
isWithinLast7Days(new Date(chat.creation_time))
|
|
|
|
);
|
|
|
|
const last30DaysChats = allChats.filter((chat) =>
|
|
|
|
isWithinLast30Days(new Date(chat.creation_time))
|
|
|
|
);
|
2023-09-18 19:25:28 +03:00
|
|
|
|
2023-06-11 00:59:16 +03:00
|
|
|
return (
|
2023-06-14 23:58:37 +03:00
|
|
|
<MotionConfig transition={{ mass: 1, damping: 10 }}>
|
|
|
|
<motion.div
|
|
|
|
drag="x"
|
|
|
|
dragConstraints={{ right: 0, left: 0 }}
|
|
|
|
dragElastic={0.15}
|
|
|
|
onDragEnd={(event, info) => {
|
|
|
|
if (info.offset.x > 100 && !open) {
|
|
|
|
setOpen(true);
|
|
|
|
} else if (info.offset.x < -100 && open) {
|
|
|
|
setOpen(false);
|
|
|
|
}
|
|
|
|
}}
|
2023-07-02 15:30:11 +03:00
|
|
|
className="flex flex-col lg:sticky fixed top-16 left-0 bottom-0 lg:h-[90vh] overflow-visible z-30 border-r border-black/10 dark:border-white/25 bg-white dark:bg-black"
|
2023-06-14 23:58:37 +03:00
|
|
|
>
|
|
|
|
<motion.div
|
|
|
|
animate={{
|
|
|
|
width: open ? "fit-content" : "0px",
|
|
|
|
opacity: open ? 1 : 0.5,
|
|
|
|
boxShadow: open
|
|
|
|
? "10px 10px 16px rgba(0, 0, 0, 0)"
|
|
|
|
: "10px 10px 16px rgba(0, 0, 0, 0.5)",
|
|
|
|
}}
|
2023-07-02 15:30:11 +03:00
|
|
|
className={cn("overflow-hidden flex flex-col flex-1")}
|
2023-07-03 19:38:12 +03:00
|
|
|
data-testid="chats-list"
|
2023-06-14 23:58:37 +03:00
|
|
|
>
|
2023-08-30 16:53:10 +03:00
|
|
|
<div className="flex flex-col flex-1 h-full">
|
2023-06-27 12:28:09 +03:00
|
|
|
<NewChatButton />
|
2023-07-03 19:38:12 +03:00
|
|
|
<div
|
|
|
|
data-testid="chats-list-items"
|
|
|
|
className="flex-1 overflow-auto scrollbar h-full"
|
|
|
|
>
|
2023-09-18 22:28:07 +03:00
|
|
|
{todayChats.length > 0 && (
|
|
|
|
<div className="bg-gray-100 text-black rounded-md px-3 py-1 mt-2">
|
|
|
|
Today
|
|
|
|
</div>
|
|
|
|
)}
|
2023-09-18 19:25:28 +03:00
|
|
|
{todayChats.map((chat) => (
|
|
|
|
<ChatsListItem key={chat.chat_id} chat={chat} />
|
|
|
|
))}
|
|
|
|
|
2023-09-18 22:28:07 +03:00
|
|
|
{yesterdayChats.length > 0 && (
|
|
|
|
<div className="bg-gray-100 text-black rounded-md px-3 py-1 mt-2">
|
|
|
|
Yesterday
|
|
|
|
</div>
|
|
|
|
)}
|
2023-09-18 19:25:28 +03:00
|
|
|
{yesterdayChats.map((chat) => (
|
2023-07-05 19:33:18 +03:00
|
|
|
<ChatsListItem key={chat.chat_id} chat={chat} />
|
2023-06-27 12:28:09 +03:00
|
|
|
))}
|
2023-09-18 19:25:28 +03:00
|
|
|
|
2023-09-18 22:28:07 +03:00
|
|
|
{last7DaysChats.length > 0 && (
|
|
|
|
<div className="bg-gray-100 text-black rounded-md px-3 py-1 mt-2">
|
|
|
|
Previous 7 Days
|
|
|
|
</div>
|
|
|
|
)}
|
2023-09-18 19:25:28 +03:00
|
|
|
{last7DaysChats.map((chat) => (
|
|
|
|
<ChatsListItem key={chat.chat_id} chat={chat} />
|
|
|
|
))}
|
|
|
|
|
2023-09-18 22:28:07 +03:00
|
|
|
{last30DaysChats.length > 0 && (
|
|
|
|
<div className="bg-gray-100 text-black rounded-md px-3 py-1 mt-2">
|
|
|
|
Previous 30 Days
|
|
|
|
</div>
|
|
|
|
)}
|
2023-09-18 19:25:28 +03:00
|
|
|
{last30DaysChats.map((chat) => (
|
|
|
|
<ChatsListItem key={chat.chat_id} chat={chat} />
|
|
|
|
))}
|
2023-06-27 12:28:09 +03:00
|
|
|
</div>
|
|
|
|
<MiniFooter />
|
2023-06-14 23:58:37 +03:00
|
|
|
</div>
|
|
|
|
</motion.div>
|
|
|
|
<button
|
|
|
|
onClick={() => {
|
|
|
|
setOpen(!open);
|
|
|
|
}}
|
2023-06-27 12:28:09 +03:00
|
|
|
className="absolute left-full top-16 text-3xl bg-black dark:bg-white text-white dark:text-black rounded-r-full p-3 pl-1"
|
2023-07-03 19:38:12 +03:00
|
|
|
data-testid="chats-list-toggle"
|
2023-06-14 23:58:37 +03:00
|
|
|
>
|
|
|
|
<motion.div
|
|
|
|
whileTap={{ scale: 0.9 }}
|
|
|
|
animate={{ scaleX: open ? -1 : 1 }}
|
|
|
|
>
|
|
|
|
<MdChevronRight />
|
|
|
|
</motion.div>
|
|
|
|
</button>
|
|
|
|
</motion.div>
|
|
|
|
</MotionConfig>
|
2023-06-11 00:59:16 +03:00
|
|
|
);
|
2023-06-15 12:52:46 +03:00
|
|
|
};
|