mirror of
https://github.com/lumehq/lume.git
synced 2025-09-21 18:40:28 +02:00
refactor newsfeed to use react-virtuoso
This commit is contained in:
@@ -80,7 +80,7 @@ export const NoteBase = memo(function NoteBase({ event }: { event: any }) {
|
|||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
onClick={(e) => openThread(e)}
|
onClick={(e) => openThread(e)}
|
||||||
className="relative z-10 flex h-min min-h-min w-full select-text flex-col border-b border-zinc-800 py-5 px-3 hover:bg-black/20"
|
className="relative z-10 m-0 flex h-min min-h-min w-full select-text flex-col border-b border-zinc-800 py-5 px-3 hover:bg-black/20"
|
||||||
>
|
>
|
||||||
<>{getParent}</>
|
<>{getParent}</>
|
||||||
<div className="relative z-10 flex flex-col">
|
<div className="relative z-10 flex flex-col">
|
||||||
|
@@ -5,48 +5,109 @@ import FormBase from '@components/form/base';
|
|||||||
import { NoteBase } from '@components/note/base';
|
import { NoteBase } from '@components/note/base';
|
||||||
import { Placeholder } from '@components/note/placeholder';
|
import { Placeholder } from '@components/note/placeholder';
|
||||||
|
|
||||||
import { notesAtom } from '@stores/note';
|
import { hasNewerNoteAtom } from '@stores/note';
|
||||||
|
|
||||||
import { useVirtualizer } from '@tanstack/react-virtual';
|
import { dateToUnix } from '@utils/getDate';
|
||||||
|
import { getLatestNotes, getNotes } from '@utils/storage';
|
||||||
|
|
||||||
|
import { ArrowUpIcon } from '@radix-ui/react-icons';
|
||||||
import { useAtom } from 'jotai';
|
import { useAtom } from 'jotai';
|
||||||
import { JSXElementConstructor, ReactElement, ReactFragment, ReactPortal, Suspense, useRef } from 'react';
|
import {
|
||||||
|
JSXElementConstructor,
|
||||||
|
ReactElement,
|
||||||
|
ReactFragment,
|
||||||
|
ReactPortal,
|
||||||
|
useCallback,
|
||||||
|
useEffect,
|
||||||
|
useRef,
|
||||||
|
useState,
|
||||||
|
} from 'react';
|
||||||
|
import { Virtuoso } from 'react-virtuoso';
|
||||||
|
|
||||||
export default function Page() {
|
export default function Page() {
|
||||||
const [data]: any = useAtom(notesAtom);
|
const [data, setData] = useState([]);
|
||||||
const parentRef = useRef(null);
|
const [hasNewerNote, setHasNewerNote] = useAtom(hasNewerNoteAtom);
|
||||||
|
|
||||||
const virtualizer = useVirtualizer({
|
const virtuosoRef = useRef(null);
|
||||||
count: data.length,
|
const now = useRef(new Date());
|
||||||
estimateSize: () => 500,
|
const limit = useRef(20);
|
||||||
getScrollElement: () => parentRef.current,
|
const offset = useRef(0);
|
||||||
getItemKey: (index) => data[index].id,
|
|
||||||
});
|
const itemContent: any = useCallback(
|
||||||
const items = virtualizer.getVirtualItems();
|
(index: string | number) => {
|
||||||
|
return <NoteBase event={data[index]} />;
|
||||||
|
},
|
||||||
|
[data]
|
||||||
|
);
|
||||||
|
|
||||||
|
const computeItemKey = useCallback(
|
||||||
|
(index: string | number) => {
|
||||||
|
return data[index].id;
|
||||||
|
},
|
||||||
|
[data]
|
||||||
|
);
|
||||||
|
|
||||||
|
const initialData = useCallback(async () => {
|
||||||
|
const result: any = await getNotes(dateToUnix(now.current), limit.current, offset.current);
|
||||||
|
setData((data) => [...data, ...result]);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const loadMore = useCallback(async () => {
|
||||||
|
offset.current += limit.current;
|
||||||
|
// next query
|
||||||
|
const result: any = await getNotes(dateToUnix(now.current), limit.current, offset.current);
|
||||||
|
setData((data) => [...data, ...result]);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const loadLatest = useCallback(async () => {
|
||||||
|
offset.current += limit.current;
|
||||||
|
// next query
|
||||||
|
const result: any = await getLatestNotes(dateToUnix(now.current));
|
||||||
|
// update data
|
||||||
|
setData((data) => [...result, ...data]);
|
||||||
|
// hide newer trigger
|
||||||
|
setHasNewerNote(false);
|
||||||
|
// scroll to top
|
||||||
|
virtuosoRef.current.scrollToIndex({ index: 0 });
|
||||||
|
}, [setHasNewerNote]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
initialData().catch(console.error);
|
||||||
|
}, [initialData]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div ref={parentRef} className="scrollbar-hide h-full w-full overflow-y-auto" style={{ contain: 'strict' }}>
|
<div className="relative h-full w-full">
|
||||||
<div className="relative">
|
{hasNewerNote && (
|
||||||
<FormBase />
|
<div className="absolute top-2 left-1/2 z-50 -translate-x-1/2 transform">
|
||||||
</div>
|
<button
|
||||||
<Suspense fallback={<Placeholder />}>
|
onClick={() => loadLatest()}
|
||||||
<div>
|
className="inline-flex h-8 transform items-center justify-center gap-1 rounded-full bg-fuchsia-500 pl-3 pr-3.5 text-sm shadow-md shadow-fuchsia-800/20 active:translate-y-1"
|
||||||
{items.length > 0 && (
|
>
|
||||||
<div className="relative w-full" style={{ height: virtualizer.getTotalSize() }}>
|
<ArrowUpIcon className="h-3.5 w-3.5" />
|
||||||
<div className="absolute top-0 left-0 w-full" style={{ transform: `translateY(${items[0].start}px)` }}>
|
Load latest
|
||||||
{items.map((virtualRow) => (
|
</button>
|
||||||
<div key={virtualRow.key} data-index={virtualRow.index} ref={virtualizer.measureElement}>
|
|
||||||
<NoteBase event={data[virtualRow.index]} />
|
|
||||||
</div>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
</Suspense>
|
)}
|
||||||
|
<Virtuoso
|
||||||
|
ref={virtuosoRef}
|
||||||
|
data={data}
|
||||||
|
itemContent={itemContent}
|
||||||
|
computeItemKey={computeItemKey}
|
||||||
|
components={COMPONENTS}
|
||||||
|
overscan={200}
|
||||||
|
endReached={loadMore}
|
||||||
|
className="scrollbar-hide h-full w-full overflow-y-auto"
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const COMPONENTS = {
|
||||||
|
Header: () => <FormBase />,
|
||||||
|
EmptyPlaceholder: () => <Placeholder />,
|
||||||
|
ScrollSeekPlaceholder: () => <Placeholder />,
|
||||||
|
};
|
||||||
|
|
||||||
Page.getLayout = function getLayout(
|
Page.getLayout = function getLayout(
|
||||||
page:
|
page:
|
||||||
| string
|
| string
|
||||||
|
@@ -1,24 +1,7 @@
|
|||||||
import { isSSR } from '@utils/ssr';
|
|
||||||
import { getAllNotes } from '@utils/storage';
|
|
||||||
|
|
||||||
import { atom } from 'jotai';
|
import { atom } from 'jotai';
|
||||||
import { atomsWithQuery } from 'jotai-tanstack-query';
|
|
||||||
import { atomWithReset } from 'jotai/utils';
|
import { atomWithReset } from 'jotai/utils';
|
||||||
|
|
||||||
// note content
|
// note content
|
||||||
export const noteContentAtom = atomWithReset('');
|
export const noteContentAtom = atomWithReset('');
|
||||||
// notify user that connector has receive newer note
|
// notify user that connector has receive newer note
|
||||||
export const hasNewerNoteAtom = atom(false);
|
export const hasNewerNoteAtom = atom(false);
|
||||||
// query notes from database
|
|
||||||
export const [notesAtom] = atomsWithQuery(() => ({
|
|
||||||
queryKey: ['notes'],
|
|
||||||
queryFn: async ({ queryKey: [] }) => {
|
|
||||||
const res = isSSR ? [] : await getAllNotes();
|
|
||||||
return res;
|
|
||||||
},
|
|
||||||
refetchInterval: 1000000,
|
|
||||||
refetchOnReconnect: true,
|
|
||||||
refetchOnWindowFocus: true,
|
|
||||||
refetchOnMount: true,
|
|
||||||
keepPreviousData: false,
|
|
||||||
}));
|
|
||||||
|
@@ -89,9 +89,19 @@ export async function getCacheProfile(id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get all notes
|
// get all notes
|
||||||
export async function getAllNotes() {
|
export async function getNotes(time, limit, offset) {
|
||||||
const db = await connect();
|
const db = await connect();
|
||||||
return await db.select(`SELECT * FROM cache_notes GROUP BY parent_id ORDER BY created_at DESC LIMIT 500`);
|
return await db.select(
|
||||||
|
`SELECT * FROM cache_notes WHERE created_at <= "${time}" GROUP BY parent_id ORDER BY created_at DESC LIMIT "${limit}" OFFSET "${offset}"`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// get all latest notes
|
||||||
|
export async function getLatestNotes(time) {
|
||||||
|
const db = await connect();
|
||||||
|
return await db.select(
|
||||||
|
`SELECT * FROM cache_notes WHERE created_at > "${time}" GROUP BY parent_id ORDER BY created_at DESC`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// get note by id
|
// get note by id
|
||||||
|
Reference in New Issue
Block a user