import { useEffect, useRef, useState } from "react";
import { ActivityIndicator, Keyboard, Text, View } from "react-native";
import Animated, { LinearTransition } from "react-native-reanimated";
import { api } from "@/lib/trpc";
import { useScrollToTop } from "@react-navigation/native";
import type { ZGetBookmarksRequest } from "@hoarder/trpc/types/bookmarks";
import FullPageSpinner from "../ui/FullPageSpinner";
import BookmarkCard from "./BookmarkCard";
export default function BookmarkList({
query,
header,
}: {
query: ZGetBookmarksRequest;
header?: React.ReactElement;
}) {
const apiUtils = api.useUtils();
const [refreshing, setRefreshing] = useState(false);
const flatListRef = useRef(null);
useScrollToTop(flatListRef);
const {
data,
isPending,
isPlaceholderData,
error,
fetchNextPage,
isFetchingNextPage,
} = api.bookmarks.getBookmarks.useInfiniteQuery(query, {
initialCursor: null,
getNextPageParam: (lastPage) => lastPage.nextCursor,
});
useEffect(() => {
setRefreshing(isPending || isPlaceholderData);
}, [isPending, isPlaceholderData]);
if (error) {
return {JSON.stringify(error)};
}
if (isPending || !data) {
return ;
}
const onRefresh = () => {
apiUtils.bookmarks.getBookmarks.invalidate();
apiUtils.bookmarks.getBookmark.invalidate();
};
return (
}
ListEmptyComponent={
No Bookmarks
}
data={data.pages.flatMap((p) => p.bookmarks)}
refreshing={refreshing}
onRefresh={onRefresh}
onScrollBeginDrag={Keyboard.dismiss}
keyExtractor={(b) => b.id}
onEndReached={() => fetchNextPage()}
ListFooterComponent={
isFetchingNextPage ? (
) : (
)
}
/>
);
}