Home.tsx62 lines, 1.8 KB
| 1 | import { |
| 2 | useInfiniteQuery, |
| 3 | useQuery, |
| 4 | useQueryClient, |
| 5 | } from '@tanstack/react-query' |
| 6 | import { Notice } from '../../components/ui/Notice' |
| 7 | import { freshOptions } from '../live/useLive' |
| 8 | import { Suggestions } from '../profile/Suggestions' |
| 9 | import { Composer } from './Composer' |
| 10 | import { PostList } from './PostList' |
| 11 | import { postsOptions } from './posts' |
| 12 | |
| 13 | const feedOptions = postsOptions('/feed', 'home') |
| 14 | |
| 15 | export function Home() { |
| 16 | const client = useQueryClient() |
| 17 | const feed = useInfiniteQuery(feedOptions) |
| 18 | const ids = useQuery(freshOptions).data ?? [] |
| 19 | const posts = feed.data?.pages.flatMap((p) => p.posts) |
| 20 | // Counted against the feed on screen: a post it shows isn't news, |
| 21 | // even when the stream announced it before the feed was read. |
| 22 | const fresh = posts |
| 23 | ? ids.filter((id) => !posts.some((p) => p.id === id)).length |
| 24 | : 0 |
| 25 | |
| 26 | // New posts wait behind a pill in the bar, so the page never moves |
| 27 | // under you and nothing is covered. |
| 28 | // Reset drops the pages read so far and fetches the first one again. |
| 29 | function showFresh() { |
| 30 | client.setQueryData(freshOptions.queryKey, []) |
| 31 | client.resetQueries({ queryKey: feedOptions.queryKey }) |
| 32 | window.scrollTo({ top: 0 }) |
| 33 | } |
| 34 | |
| 35 | return ( |
| 36 | <> |
| 37 | <header className="bar flex items-center justify-between"> |
| 38 | <h1>Home</h1> |
| 39 | {fresh > 0 && ( |
| 40 | <button |
| 41 | onClick={showFresh} |
| 42 | className="btn py-0.5 motion-safe:animate-drop" |
| 43 | > |
| 44 | {fresh} new {fresh === 1 ? 'post' : 'posts'} |
| 45 | </button> |
| 46 | )} |
| 47 | </header> |
| 48 | <Composer /> |
| 49 | <PostList |
| 50 | query={feed} |
| 51 | empty={ |
| 52 | <Notice title="Your feed is quiet"> |
| 53 | Follow a few people and their posts land here. |
| 54 | <div className="mt-6 text-left"> |
| 55 | <Suggestions /> |
| 56 | </div> |
| 57 | </Notice> |
| 58 | } |
| 59 | /> |
| 60 | </> |
| 61 | ) |
| 62 | } |