feat: categories carousel
This commit is contained in:
@@ -3,12 +3,14 @@ import type { inferRouterInputs, inferRouterOutputs } from "@trpc/server";
|
||||
import { createTRPCRouter } from "#api/trpc/init";
|
||||
import { articlesRouter } from "#api/trpc/routers/articles";
|
||||
import { authRouter } from "#api/trpc/routers/auth";
|
||||
import { categoriesRouter } from "#api/trpc/routers/categories";
|
||||
import { reportsRouter } from "#api/trpc/routers/reports";
|
||||
import { sourcesRouter } from "#api/trpc/routers/sources";
|
||||
|
||||
export const appRouter = createTRPCRouter({
|
||||
articles: articlesRouter,
|
||||
auth: authRouter,
|
||||
categories: categoriesRouter,
|
||||
reports: reportsRouter,
|
||||
sources: sourcesRouter,
|
||||
});
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
import { getCategories } from "@basango/db/queries";
|
||||
|
||||
import { createTRPCRouter, protectedProcedure } from "#api/trpc/init";
|
||||
|
||||
export const categoriesRouter = createTRPCRouter({
|
||||
list: protectedProcedure.query(async ({ ctx }) => getCategories(ctx.db)),
|
||||
});
|
||||
@@ -9,6 +9,7 @@ export const metadata: Metadata = {
|
||||
};
|
||||
|
||||
export default function Page() {
|
||||
prefetch(trpc.categories.list.queryOptions());
|
||||
prefetch(trpc.articles.list.infiniteQueryOptions({ limit: 12 }));
|
||||
|
||||
return (
|
||||
|
||||
@@ -20,6 +20,7 @@ export default async function Page({ params }: { params: Promise<{ id: string }>
|
||||
trpc.sources.getById.queryOptions({ id }),
|
||||
trpc.sources.getCategoryShares.queryOptions({ id, limit: 10 }),
|
||||
trpc.sources.getPublications.queryOptions({ id }),
|
||||
trpc.categories.list.queryOptions(),
|
||||
trpc.articles.list.infiniteQueryOptions({ limit: 12, sourceId: id }),
|
||||
]);
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import * as React from "react";
|
||||
import { useTRPC } from "#dashboard/trpc/client";
|
||||
|
||||
import { ArticleCard, ArticleCardSkeleton } from "./article-card";
|
||||
import { CategoriesCarousel } from "./categories-carousel";
|
||||
|
||||
type ArticlesTableProps = {
|
||||
sourceId?: string;
|
||||
@@ -18,10 +19,16 @@ const PLACEHOLDER_COUNT = 8;
|
||||
|
||||
export function ArticlesFeed({ sourceId }: ArticlesTableProps) {
|
||||
const trpc = useTRPC();
|
||||
const [selectedCategory, setSelectedCategory] = React.useState<string | null>(null);
|
||||
|
||||
const handleCategorySelect = React.useCallback((categoryId: string | null) => {
|
||||
setSelectedCategory((current) => (current === categoryId ? null : categoryId));
|
||||
}, []);
|
||||
|
||||
const query = useInfiniteQuery(
|
||||
trpc.articles.list.infiniteQueryOptions(
|
||||
{
|
||||
category: selectedCategory ?? undefined,
|
||||
limit: 12,
|
||||
sourceId,
|
||||
},
|
||||
@@ -41,6 +48,8 @@ export function ArticlesFeed({ sourceId }: ArticlesTableProps) {
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<CategoriesCarousel onSelect={handleCategorySelect} selectedCategory={selectedCategory} />
|
||||
|
||||
{query.isError && (
|
||||
<Alert variant="destructive">
|
||||
<AlertTitle>Unable to load articles</AlertTitle>
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
"use client";
|
||||
|
||||
import {
|
||||
Carousel,
|
||||
CarouselContent,
|
||||
CarouselItem,
|
||||
CarouselNext,
|
||||
CarouselPrevious,
|
||||
} from "@basango/ui/components/carousel";
|
||||
import { Skeleton } from "@basango/ui/components/skeleton";
|
||||
import { cn } from "@basango/ui/lib/utils";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import * as React from "react";
|
||||
|
||||
import { useTRPC } from "#dashboard/trpc/client";
|
||||
|
||||
type Props = {
|
||||
onSelect: (categoryId: string | null) => void;
|
||||
selectedCategory: string | null;
|
||||
};
|
||||
|
||||
const PLACEHOLDER_COUNT = 10;
|
||||
|
||||
export function CategoriesCarousel({ onSelect, selectedCategory }: Props) {
|
||||
const trpc = useTRPC();
|
||||
const { data, isLoading } = useQuery(trpc.categories.list.queryOptions());
|
||||
const categories = data ?? [];
|
||||
const showSkeletons = isLoading && categories.length === 0;
|
||||
|
||||
return (
|
||||
<div className="relative">
|
||||
<Carousel
|
||||
className="w-full"
|
||||
opts={{
|
||||
align: "start",
|
||||
containScroll: "trimSnaps",
|
||||
dragFree: true,
|
||||
}}
|
||||
>
|
||||
<CarouselContent className="-ml-2">
|
||||
<CarouselItem className="basis-auto pl-2">
|
||||
<CategoryPill active={!selectedCategory} onClick={() => onSelect(null)}>
|
||||
All
|
||||
</CategoryPill>
|
||||
</CarouselItem>
|
||||
{showSkeletons
|
||||
? Array.from({ length: PLACEHOLDER_COUNT }).map((_, index) => (
|
||||
<CarouselItem className="basis-auto pl-2" key={`category-skeleton-${index}`}>
|
||||
<Skeleton className="h-8 w-20 rounded-full bg-muted/70" />
|
||||
</CarouselItem>
|
||||
))
|
||||
: categories.map((category) => (
|
||||
<CarouselItem className="basis-auto pl-2" key={category.id}>
|
||||
<CategoryPill
|
||||
active={selectedCategory === category.id}
|
||||
onClick={() => onSelect(category.id)}
|
||||
>
|
||||
{category.name}
|
||||
</CategoryPill>
|
||||
</CarouselItem>
|
||||
))}
|
||||
</CarouselContent>
|
||||
<CarouselPrevious className="hidden md:flex" size="icon" />
|
||||
<CarouselNext className="hidden md:flex" size="icon" />
|
||||
</Carousel>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
type CategoryPillProps = {
|
||||
active?: boolean;
|
||||
children: React.ReactNode;
|
||||
onClick: () => void;
|
||||
};
|
||||
|
||||
function CategoryPill({ active, children, onClick }: CategoryPillProps) {
|
||||
return (
|
||||
<button
|
||||
aria-pressed={active}
|
||||
className={cn(
|
||||
"shrink-0 rounded-full border px-3 py-1.5 text-sm font-medium transition",
|
||||
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
|
||||
active
|
||||
? "border-foreground bg-foreground text-background shadow-sm"
|
||||
: "border-border bg-muted/60 text-foreground hover:border-foreground/60",
|
||||
)}
|
||||
onClick={onClick}
|
||||
type="button"
|
||||
>
|
||||
{children}
|
||||
</button>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user