added filtered duplicate parent id

This commit is contained in:
Ren Amamiya 2023-04-06 17:14:11 +07:00
parent 62ca74994d
commit 7d5f9d90c4
2 changed files with 12 additions and 12 deletions

View File

@ -8,6 +8,7 @@ import { Placeholder } from '@components/note/placeholder';
import { hasNewerNoteAtom } from '@stores/note';
import { dateToUnix } from '@utils/getDate';
import { filterDuplicateParentID } from '@utils/transform';
import { ArrowUpIcon } from '@radix-ui/react-icons';
import { useAtom } from 'jotai';
@ -73,7 +74,11 @@ export default function Page() {
// next query
const result: any = await getLatestNotes({ date: dateToUnix(now.current) });
// update data
setData((data) => [...data, ...result]);
if (result.length > 0) {
setData((data) => [...data, ...result]);
} else {
setData((data) => [...data, result]);
}
// hide newer trigger
setHasNewerNote(false);
// scroll to top
@ -99,7 +104,7 @@ export default function Page() {
)}
<Virtuoso
ref={virtuosoRef}
data={data}
data={filterDuplicateParentID(data)}
itemContent={itemContent}
computeItemKey={computeItemKey}
components={COMPONENTS}

View File

@ -46,15 +46,10 @@ export const getParentID = (arr, fallback) => {
return parentID;
};
export const filteredData = (obj) => {
const filteredArr = obj.reduce((item, current) => {
const x = item.find((item) => item.parent_id === current.parent_id);
if (!x) {
return item.concat([current]);
} else {
return item;
}
}, []);
export const filterDuplicateParentID = (arr) => {
const filteredArray = arr.filter(
(item, index) => index === arr.findIndex((other) => item.parent_id === other.parent_id)
);
return filteredArr;
return filteredArray;
};