|
|
|
|
@ -1,4 +1,5 @@
|
|
|
|
|
import { ArrowUpIcon, LoaderIcon } from "lucide-react";
|
|
|
|
|
import { useQueryClient } from "@tanstack/react-query";
|
|
|
|
|
import { ArrowUpIcon } from "lucide-react";
|
|
|
|
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
|
|
|
import { matchPath } from "react-router-dom";
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
@ -6,6 +7,7 @@ import { userServiceClient } from "@/connect";
|
|
|
|
|
import { useView } from "@/contexts/ViewContext";
|
|
|
|
|
import { DEFAULT_LIST_MEMOS_PAGE_SIZE } from "@/helpers/consts";
|
|
|
|
|
import { useInfiniteMemos } from "@/hooks/useMemoQueries";
|
|
|
|
|
import { userKeys } from "@/hooks/useUserQueries";
|
|
|
|
|
import { Routes } from "@/router";
|
|
|
|
|
import { State } from "@/types/proto/api/v1/common_pb";
|
|
|
|
|
import type { Memo } from "@/types/proto/api/v1/memo_service_pb";
|
|
|
|
|
@ -81,6 +83,7 @@ function useAutoFetchWhenNotScrollable({
|
|
|
|
|
const PagedMemoList = (props: Props) => {
|
|
|
|
|
const t = useTranslate();
|
|
|
|
|
const { layout } = useView();
|
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
|
|
|
|
// Show memo editor only on the root route
|
|
|
|
|
const showMemoEditor = Boolean(matchPath(Routes.ROOT, window.location.pathname));
|
|
|
|
|
@ -99,7 +102,7 @@ const PagedMemoList = (props: Props) => {
|
|
|
|
|
// Apply custom sorting if provided, otherwise use memos directly
|
|
|
|
|
const sortedMemoList = useMemo(() => (props.listSort ? props.listSort(memos) : memos), [memos, props.listSort]);
|
|
|
|
|
|
|
|
|
|
// Batch-fetch creators when new data arrives to improve performance
|
|
|
|
|
// Prefetch creators when new data arrives to improve performance
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!data?.pages || !props.showCreator) return;
|
|
|
|
|
|
|
|
|
|
@ -107,14 +110,17 @@ const PagedMemoList = (props: Props) => {
|
|
|
|
|
if (!lastPage?.memos) return;
|
|
|
|
|
|
|
|
|
|
const uniqueCreators = Array.from(new Set(lastPage.memos.map((memo) => memo.creator)));
|
|
|
|
|
void Promise.allSettled(
|
|
|
|
|
uniqueCreators.map((creator) =>
|
|
|
|
|
userServiceClient.getUser({ name: creator }).catch(() => {
|
|
|
|
|
/* silently ignore errors */
|
|
|
|
|
}),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}, [data?.pages, props.showCreator]);
|
|
|
|
|
for (const creator of uniqueCreators) {
|
|
|
|
|
void queryClient.prefetchQuery({
|
|
|
|
|
queryKey: userKeys.detail(creator),
|
|
|
|
|
queryFn: async () => {
|
|
|
|
|
const user = await userServiceClient.getUser({ name: creator });
|
|
|
|
|
return user;
|
|
|
|
|
},
|
|
|
|
|
staleTime: 1000 * 60 * 5,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, [data?.pages, props.showCreator, queryClient]);
|
|
|
|
|
|
|
|
|
|
// Auto-fetch hook: fetches more content when page isn't scrollable
|
|
|
|
|
useAutoFetchWhenNotScrollable({
|
|
|
|
|
|