From 56885a419368dc478db9b18868661282e1f0bacd Mon Sep 17 00:00:00 2001 From: Ren Amamiya <123083837+reyamir@users.noreply.github.com> Date: Thu, 4 May 2023 22:04:43 +0700 Subject: [PATCH] rename following to daily page --- src/app/channel/components/members.tsx | 4 +- src/app/daily/_default.page.tsx | 1 + src/app/daily/layout.tsx | 29 ++++++++ src/app/daily/pages/index.page.tsx | 92 ++++++++++++++++++++++++ src/app/inital-data/pages/index.page.tsx | 2 +- src/utils/storage.tsx | 7 ++ 6 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 src/app/daily/_default.page.tsx create mode 100644 src/app/daily/layout.tsx create mode 100644 src/app/daily/pages/index.page.tsx diff --git a/src/app/channel/components/members.tsx b/src/app/channel/components/members.tsx index dd24e8c0..de536ed8 100644 --- a/src/app/channel/components/members.tsx +++ b/src/app/channel/components/members.tsx @@ -22,8 +22,8 @@ export default function ChannelMembers() { {miniMembersList.map((member, index) => ( ))} - {totalMembers > 0 ? ( -
+ {totalMembers ? ( +
{totalMembers}
) : ( diff --git a/src/app/daily/_default.page.tsx b/src/app/daily/_default.page.tsx new file mode 100644 index 00000000..7a0967d9 --- /dev/null +++ b/src/app/daily/_default.page.tsx @@ -0,0 +1 @@ +export { LayoutNewsfeed as Layout } from './layout'; diff --git a/src/app/daily/layout.tsx b/src/app/daily/layout.tsx new file mode 100644 index 00000000..0b260f69 --- /dev/null +++ b/src/app/daily/layout.tsx @@ -0,0 +1,29 @@ +import AppHeader from '@lume/shared/appHeader'; +import MultiAccounts from '@lume/shared/multiAccounts'; +import Navigation from '@lume/shared/navigation'; + +export function LayoutNewsfeed({ children }: { children: React.ReactNode }) { + return ( +
+
+
+ +
+
+
+ +
+
+
+ +
+
{children}
+
+
+
+
+ ); +} diff --git a/src/app/daily/pages/index.page.tsx b/src/app/daily/pages/index.page.tsx new file mode 100644 index 00000000..3446d291 --- /dev/null +++ b/src/app/daily/pages/index.page.tsx @@ -0,0 +1,92 @@ +import NoteBase from '@lume/app/note/components/base'; +import { Placeholder } from '@lume/app/note/components/placeholder'; +import { NoteQuoteRepost } from '@lume/app/note/components/quoteRepost'; +import { getNotes } from '@lume/utils/storage'; + +import { useInfiniteQuery } from '@tanstack/react-query'; +import { useVirtualizer } from '@tanstack/react-virtual'; +import { useEffect, useRef } from 'react'; + +const ITEM_PER_PAGE = 10; +const TIME = Math.floor(Date.now() / 1000); + +export function Page() { + const { status, error, data, fetchNextPage, hasNextPage, isFetching, isFetchingNextPage }: any = useInfiniteQuery({ + queryKey: ['following'], + queryFn: async ({ pageParam = 0 }) => { + return await getNotes(TIME, ITEM_PER_PAGE, pageParam); + }, + getNextPageParam: (lastPage) => lastPage.nextCursor, + }); + + const allRows = data ? data.pages.flatMap((d: { data: any }) => d.data) : []; + const parentRef = useRef(); + + const rowVirtualizer = useVirtualizer({ + count: hasNextPage ? allRows.length + 1 : allRows.length, + getScrollElement: () => parentRef.current, + estimateSize: () => 400, + overscan: 2, + }); + + const itemsVirtualizer = rowVirtualizer.getVirtualItems(); + + useEffect(() => { + const [lastItem] = [...rowVirtualizer.getVirtualItems()].reverse(); + + if (!lastItem) { + return; + } + + if (lastItem.index >= allRows.length - 1 && hasNextPage && !isFetchingNextPage) { + fetchNextPage(); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [fetchNextPage, allRows.length, rowVirtualizer.getVirtualItems()]); + + return ( +
+ {status === 'loading' ? ( + + ) : status === 'error' ? ( +
{error.message}
+ ) : ( +
+
+
+ {rowVirtualizer.getVirtualItems().map((virtualRow) => { + const note = allRows[virtualRow.index]; + if (note) { + if (note.kind === 1) { + return ( +
+ +
+ ); + } else { + return ( +
+ +
+ ); + } + } + })} +
+
+
+ )} +
{isFetching && !isFetchingNextPage ? : null}
+
+ ); +} diff --git a/src/app/inital-data/pages/index.page.tsx b/src/app/inital-data/pages/index.page.tsx index 980d92b8..d401bd22 100644 --- a/src/app/inital-data/pages/index.page.tsx +++ b/src/app/inital-data/pages/index.page.tsx @@ -126,7 +126,7 @@ export function Page() { () => { updateLastLogin(dateToUnix(now.current)); timeout = setTimeout(() => { - navigate('/app/newsfeed/following', { overwriteLastHistoryEntry: true }); + navigate('/app/daily', { overwriteLastHistoryEntry: true }); }, 5000); } ); diff --git a/src/utils/storage.tsx b/src/utils/storage.tsx index b243c0b4..e61ba274 100644 --- a/src/utils/storage.tsx +++ b/src/utils/storage.tsx @@ -95,6 +95,13 @@ export async function getNotes(time: number, limit: number, offset: number) { return notes; } +// get all note authors +export async function getNoteAuthors() { + const db = await connect(); + const result = await db.select(`SELECT DISTINCT pubkey FROM notes ORDER BY created_at DESC`); + return result; +} + // get note by id export async function getNoteByID(event_id: string) { const db = await connect();