mirror of https://github.com/buster-so/buster.git
create chat records update
This commit is contained in:
parent
1f6e8b7f42
commit
a60700dec2
|
@ -14,7 +14,7 @@ const LoginLayout: React.FC<PropsWithChildren<{}>> = async ({ children }) => {
|
|||
<div className="hidden w-1/2 bg-gray-50 md:flex dark:bg-gray-900">
|
||||
<WelcomeToBuster hasUser={true} />
|
||||
</div>
|
||||
<div className="w-1/2 bg-white dark:bg-black">{children}</div>
|
||||
<div className="w-full bg-white md:w-1/2 dark:bg-black">{children}</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
|
|
@ -0,0 +1,70 @@
|
|||
import { getNow, isDateAfter, isDateBefore, isDateSame } from '@/lib/date';
|
||||
|
||||
type ListItem = {
|
||||
id: string;
|
||||
last_edited: string;
|
||||
};
|
||||
|
||||
export const createChatRecord = <T extends ListItem>(
|
||||
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;
|
||||
};
|
|
@ -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 = <T extends ListItem>(
|
||||
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 = <T extends ListItem>({ data }: { data: T[] }) => {
|
||||
const listRecord = useMemo(() => createChatRecord(data), [data]);
|
||||
|
||||
return listRecord;
|
||||
return useMemo(() => createChatRecord(data), [data]);
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue