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

View File

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