Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(chats): now in order and with a little bonus ;) #1200

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/models/databases/supabase/chats.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def get_user_chats(self, user_id: str):
self.db.from_("chats")
.select("chat_id,user_id,creation_time,chat_name")
.filter("user_id", "eq", user_id)
.order("creation_time", desc=False)
.execute()
)
return response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const ChatsListItem = ({ chat }: ChatsListItemProps): JSX.Element => {
return (
<div
className={cn(
"w-full border-b border-black/10 dark:border-white/25 last:border-none relative group flex overflow-x-hidden hover:bg-gray-100 dark:hover:bg-gray-800",
"w-full relative group flex overflow-x-hidden hover:bg-gray-100 dark:hover:bg-gray-800",
selected
? "bg-gray-100 dark:bg-gray-800 text-primary dark:text-white"
: ""
Expand Down
58 changes: 56 additions & 2 deletions frontend/app/chat/components/ChatsList/index.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,58 @@
/* eslint-disable */
StanGirard marked this conversation as resolved.
Show resolved Hide resolved
"use client";
import { motion, MotionConfig } from "framer-motion";
import { MdChevronRight } from "react-icons/md";

import { useChatsContext } from "@/lib/context/ChatsProvider/hooks/useChatsContext";
import { cn } from "@/lib/utils";

import { useSelectedChatPage } from "../../[chatId]/hooks/useSelectedChatPage";
import { ChatsListItem } from "./components/ChatsListItem";
import { MiniFooter } from "./components/ChatsListItem/components/MiniFooter";
import { NewChatButton } from "./components/NewChatButton";
import { useChatsList } from "./hooks/useChatsList";
import { useSelectedChatPage } from "../../[chatId]/hooks/useSelectedChatPage";

export const ChatsList = (): JSX.Element => {
const { allChats } = useChatsContext();
const { open, setOpen } = useChatsList();
useSelectedChatPage();

// Utility functions for chat categorization
StanGirard marked this conversation as resolved.
Show resolved Hide resolved
const isToday = (date: Date) => {
const today = new Date();

return date.toDateString() === today.toDateString();
};

const isYesterday = (date: Date) => {
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);

return date.toDateString() === yesterday.toDateString();
};

const isWithinLast7Days = (date: Date) => {
const today = new Date();
const weekAgo = new Date();
weekAgo.setDate(weekAgo.getDate() - 7);

return date > weekAgo && !isToday(date) && !isYesterday(date);
};

const isWithinLast30Days = (date: Date) => {
const today = new Date();
const monthAgo = new Date();
monthAgo.setDate(monthAgo.getDate() - 30);

return date > monthAgo && !isToday(date) && !isYesterday(date) && !isWithinLast7Days(date);
};

// Filtering chats into different groups
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)));

return (
<MotionConfig transition={{ mass: 1, damping: 10 }}>
<motion.div
Expand Down Expand Up @@ -48,9 +85,26 @@ export const ChatsList = (): JSX.Element => {
data-testid="chats-list-items"
className="flex-1 overflow-auto scrollbar h-full"
>
{allChats.map((chat) => (
{todayChats.length > 0 && <div className="bg-gray-100 text-black rounded-md px-3 py-1 mt-2">Today</div>}
{todayChats.map((chat) => (
<ChatsListItem key={chat.chat_id} chat={chat} />
))}

{yesterdayChats.length > 0 && <div className="bg-gray-100 text-black rounded-md px-3 py-1 mt-2">Yesterday</div>}
{yesterdayChats.map((chat) => (
<ChatsListItem key={chat.chat_id} chat={chat} />
))}

{last7DaysChats.length > 0 && <div className="bg-gray-100 text-black rounded-md px-3 py-1 mt-2">Previous 7 Days</div>}
{last7DaysChats.map((chat) => (
<ChatsListItem key={chat.chat_id} chat={chat} />
))}

{last30DaysChats.length > 0 && <div className="bg-gray-100 text-black rounded-md px-3 py-1 mt-2">Previous 30 Days</div>}
{last30DaysChats.map((chat) => (
<ChatsListItem key={chat.chat_id} chat={chat} />
))}

</div>
<MiniFooter />
</div>
Expand Down