import { commands } from "@/commands.gen"; import { toLumeEvents } from "@/commons"; import { Quote, RepostNote, Spinner, TextNote } from "@/components"; import type { LumeEvent } from "@/system"; import { Kind } from "@/types"; import { ArrowDown } from "@phosphor-icons/react"; import * as ScrollArea from "@radix-ui/react-scroll-area"; import { useInfiniteQuery } from "@tanstack/react-query"; import { createLazyFileRoute } from "@tanstack/react-router"; import { listen } from "@tauri-apps/api/event"; import { useCallback, useEffect, useRef } from "react"; import { Virtualizer } from "virtua"; export const Route = createLazyFileRoute("/columns/_layout/interests/$id")({ component: Screen, }); export function Screen() { const hashtags = Route.useLoaderData(); const params = Route.useParams(); const { queryClient } = Route.useRouteContext(); const { data, isLoading, isFetching, isFetchingNextPage, hasNextPage, fetchNextPage, } = useInfiniteQuery({ queryKey: ["hashtags", params.id], initialPageParam: 0, queryFn: async ({ pageParam }: { pageParam: number }) => { const tags = hashtags.map((tag) => tag.toLowerCase().replace("#", "")); const until = pageParam > 0 ? pageParam.toString() : undefined; const res = await commands.getAllEventsByHashtags(tags, until); if (res.status === "error") { throw new Error(res.error); } return toLumeEvents(res.data); }, getNextPageParam: (lastPage) => lastPage?.at(-1)?.created_at - 1, select: (data) => data?.pages.flat(), refetchOnWindowFocus: false, }); const ref = useRef(null); const renderItem = useCallback( (event: LumeEvent) => { if (!event) return; switch (event.kind) { case Kind.Repost: return ( ); default: { if (event.isQuote) { return ( ); } else { return ( ); } } } }, [data], ); useEffect(() => { const unlisten = listen("synchronized", async () => { await queryClient.invalidateQueries({ queryKey: ["hashtags", params.id], }); }); return () => { unlisten.then((f) => f()); }; }, []); return ( {isFetching && !isLoading && !isFetchingNextPage ? (
Getting new notes...
) : null} {isLoading ? (
Loading...
) : !data.length ? (
🎉 Yo. You're catching up on all latest notes.
) : ( data.map((item) => renderItem(item)) )} {hasNextPage ? (
) : null}
); }