import { LoaderFunctionArgs, useLoaderData } from "react-router-dom"; import { createPopularPostsPageURL } from "#lib/urls"; import { parseOffset } from "#lib/pagination"; import { fetchPopularPosts } from "#api/posts"; import { PageSkeleton } from "#components/pages"; import { Paginator } from "#components/pagination"; import { FooterAd, HeaderAd, SliderAd } from "#components/ads"; import { CardList, PostFavoriteCard } from "#components/cards"; import { IPopularPostsPeriod, IPostWithFavorites, validatePeriod, } from "#entities/posts"; interface IProps { minDate: string; maxDate: string; rangeDescription: string; earliestDateForPopular: string; navigationDates: Record; scale?: IPopularPostsPeriod; today: string; count: number; posts: IPostWithFavorites[]; offset?: number; date?: string; } export function PopularPostsPage() { const { minDate, maxDate, rangeDescription, navigationDates, earliestDateForPopular, scale, today, count, offset, date, posts, } = useLoaderData() as IProps; const title = `Popular posts for ${rangeDescription}`; return ( Popular Posts for{" "} {rangeDescription} } >
{navigationDates.day[0] < earliestDateForPopular ? ( next » ) : ( « prev )} {" "} {scale === "day" ? ( Day ) : ( Day )} {" "} {navigationDates.day[1] > today ? ( next » ) : ( next » )}
{navigationDates.week[0] < earliestDateForPopular ? ( next » ) : ( « prev )} {" "} {scale === "week" ? ( Week ) : ( Week )} {" "} {navigationDates.week[1] > today ? ( next » ) : ( next » )}
{navigationDates.month[0] < earliestDateForPopular ? ( next » ) : ( « prev )} {" "} {scale === "month" ? ( Month ) : ( Month )} {" "} {navigationDates.month[1] > today ? ( next » ) : ( next » )}
String(createPopularPostsPageURL(date, scale, offset)) } /> {count === 0 ? (

Nobody here but us chickens!

There are no posts for your query.

) : ( posts.map((post) => ( )) )}
String(createPopularPostsPageURL(date, scale, offset)) } />
); } export async function loader({ request }: LoaderFunctionArgs): Promise { const searchParams = new URL(request.url).searchParams; const date = searchParams.get("date")?.trim(); const scale = searchParams.get("period")?.trim() ?? "recent"; validatePeriod(scale); let offset: number | undefined; { const inputOffset = searchParams.get("o")?.trim(); if (inputOffset) { offset = parseOffset(inputOffset); } } const { info, props, results } = await fetchPopularPosts(date, scale, offset); const { count, earliest_date_for_popular, today } = props; const { range_desc, min_date, max_date, navigation_dates } = info; return { date, count, scale, offset, posts: results, today, earliestDateForPopular: earliest_date_for_popular, rangeDescription: range_desc, minDate: min_date, maxDate: max_date, navigationDates: navigation_dates, }; }