diff --git a/web/src/app/auth/layout.tsx b/web/src/app/auth/layout.tsx index a7025e07c..7e5df271b 100644 --- a/web/src/app/auth/layout.tsx +++ b/web/src/app/auth/layout.tsx @@ -14,7 +14,7 @@ const LoginLayout: React.FC> = async ({ children }) => {
-
{children}
+
{children}
diff --git a/web/src/components/ui/list/createChatRecord.ts b/web/src/components/ui/list/createChatRecord.ts new file mode 100644 index 000000000..b2bff715d --- /dev/null +++ b/web/src/components/ui/list/createChatRecord.ts @@ -0,0 +1,70 @@ +import { getNow, isDateAfter, isDateBefore, isDateSame } from '@/lib/date'; + +type ListItem = { + id: string; + last_edited: string; +}; + +export const createChatRecord = ( + data: T[] +): { + TODAY: T[]; + YESTERDAY: T[]; + LAST_WEEK: T[]; + ALL_OTHERS: T[]; +} => { + const today = getNow(); + const yesterday = today.subtract(2, 'day'); + const weekStartDate = today.subtract(8, 'day').startOf('day'); + const twoDaysAgo = today.subtract(2, 'day').startOf('day'); + + const TODAY: T[] = []; + const YESTERDAY: T[] = []; + const LAST_WEEK: T[] = []; + const ALL_OTHERS: T[] = []; + + // Loop through the data array only once + data.forEach((item) => { + if ( + isDateSame({ + date: item.last_edited, + compareDate: today, + interval: 'day' + }) + ) { + TODAY.push(item); + } else if ( + isDateSame({ + date: item.last_edited, + compareDate: yesterday, + interval: 'day' + }) + ) { + YESTERDAY.push(item); + } else if ( + isDateAfter({ + date: item.last_edited, + compareDate: weekStartDate, + interval: 'day' + }) && + isDateBefore({ + date: item.last_edited, + compareDate: twoDaysAgo, + interval: 'day' + }) + ) { + LAST_WEEK.push(item); + } else { + ALL_OTHERS.push(item); + } + }); + + const result = { + TODAY, + YESTERDAY, + LAST_WEEK, + ALL_OTHERS + }; + + return result; +}; diff --git a/web/src/components/ui/list/useCreateListByDate.ts b/web/src/components/ui/list/useCreateListByDate.ts index 7cb766ee7..ba04b0734 100644 --- a/web/src/components/ui/list/useCreateListByDate.ts +++ b/web/src/components/ui/list/useCreateListByDate.ts @@ -1,61 +1,11 @@ -import { getNow, isDateAfter, isDateBefore, isDateSame } from '@/lib/date'; import { useMemo } from 'react'; +import { createChatRecord } from './createChatRecord'; type ListItem = { id: string; last_edited: string; }; -const createChatRecord = ( - data: T[] -): { - TODAY: T[]; - YESTERDAY: T[]; - LAST_WEEK: T[]; - ALL_OTHERS: T[]; -} => { - const today = getNow(); - const TODAY = data.filter((d) => - isDateSame({ - date: d.last_edited, - compareDate: today, - interval: 'day' - }) - ); - const YESTERDAY = data.filter((d) => - isDateSame({ - date: d.last_edited, - compareDate: today.subtract(1, 'day'), - interval: 'day' - }) - ); - const LAST_WEEK = data.filter( - (d) => - isDateBefore({ - date: d.last_edited, - compareDate: today.subtract(2, 'day').startOf('day'), - interval: 'day' - }) && - isDateAfter({ - date: d.last_edited, - compareDate: today.subtract(8, 'day').startOf('day'), - interval: 'day' - }) - ); - const ALL_OTHERS = data.filter( - (d) => !TODAY.includes(d) && !YESTERDAY.includes(d) && !LAST_WEEK.includes(d) - ); - - return { - TODAY, - YESTERDAY, - LAST_WEEK, - ALL_OTHERS - }; -}; - export const useCreateListByDate = ({ data }: { data: T[] }) => { - const listRecord = useMemo(() => createChatRecord(data), [data]); - - return listRecord; + return useMemo(() => createChatRecord(data), [data]); };