From 8914c7c3573a2f2047df80718fca1e6e9721fefa Mon Sep 17 00:00:00 2001 From: Stan Girard Date: Mon, 18 Sep 2023 18:25:28 +0200 Subject: [PATCH] fix(chats): now in order and with a little bonus ;) (#1200) * fix(chats): now in order and with a little bonus ;) * style(eslint): fixed --- backend/models/databases/supabase/chats.py | 1 + .../ChatsListItem/ChatsListItem.tsx | 2 +- .../app/chat/components/ChatsList/index.tsx | 29 ++++++++++++++++-- frontend/lib/utils.ts | 30 +++++++++++++++++++ 4 files changed, 59 insertions(+), 3 deletions(-) diff --git a/backend/models/databases/supabase/chats.py b/backend/models/databases/supabase/chats.py index 7952eed86..9fa5bb54e 100644 --- a/backend/models/databases/supabase/chats.py +++ b/backend/models/databases/supabase/chats.py @@ -46,6 +46,7 @@ class Chats(Repository): 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 diff --git a/frontend/app/chat/components/ChatsList/components/ChatsListItem/ChatsListItem.tsx b/frontend/app/chat/components/ChatsList/components/ChatsListItem/ChatsListItem.tsx index ed0d81b19..637164f48 100644 --- a/frontend/app/chat/components/ChatsList/components/ChatsListItem/ChatsListItem.tsx +++ b/frontend/app/chat/components/ChatsList/components/ChatsListItem/ChatsListItem.tsx @@ -25,7 +25,7 @@ export const ChatsListItem = ({ chat }: ChatsListItemProps): JSX.Element => { return (
{ const { open, setOpen } = useChatsList(); useSelectedChatPage(); + + // 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 ( { data-testid="chats-list-items" className="flex-1 overflow-auto scrollbar h-full" > - {allChats.map((chat) => ( + {todayChats.length > 0 &&
Today
} + {todayChats.map((chat) => ( ))} + + {yesterdayChats.length > 0 &&
Yesterday
} + {yesterdayChats.map((chat) => ( + + ))} + + {last7DaysChats.length > 0 &&
Previous 7 Days
} + {last7DaysChats.map((chat) => ( + + ))} + + {last30DaysChats.length > 0 &&
Previous 30 Days
} + {last30DaysChats.map((chat) => ( + + ))} +
diff --git a/frontend/lib/utils.ts b/frontend/lib/utils.ts index 5ac647a0f..d95e132b7 100644 --- a/frontend/lib/utils.ts +++ b/frontend/lib/utils.ts @@ -4,3 +4,33 @@ import { twMerge } from "tailwind-merge"; export const cn = (...inputs: ClassValue[]): string => { return twMerge(clsx(inputs)); }; + + +const isToday = (date: Date): boolean => { + const today = new Date(); + + return date.toDateString() === today.toDateString(); +}; + +const isYesterday = (date: Date): boolean => { + const yesterday = new Date(); + yesterday.setDate(yesterday.getDate() - 1); + + return date.toDateString() === yesterday.toDateString(); +}; + +const isWithinLast7Days = (date: Date): boolean => { + const weekAgo = new Date(); + weekAgo.setDate(weekAgo.getDate() - 7); + + return date > weekAgo && !isToday(date) && !isYesterday(date); +}; + +const isWithinLast30Days = (date: Date): boolean => { + const monthAgo = new Date(); + monthAgo.setDate(monthAgo.getDate() - 30); + + return date > monthAgo && !isToday(date) && !isYesterday(date) && !isWithinLast7Days(date); +}; + +export { isToday, isWithinLast30Days, isWithinLast7Days, isYesterday };