Skip to content

Commit

Permalink
add breadcrumbs
Browse files Browse the repository at this point in the history
  • Loading branch information
sadmann7 committed Jul 16, 2023
1 parent 7ee9740 commit c2f786a
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { DataTableLoading } from "@/components/data-table/data-table-loading"

export default function ProductsLoading() {
return <DataTableLoading isNewRowCreatable={true} isRowsDeletable={true} />
return (
<DataTableLoading
columnCount={6}
isNewRowCreatable={true}
isRowsDeletable={true}
/>
)
}
33 changes: 18 additions & 15 deletions src/app/(lobby)/product/[productId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { products, stores } from "@/db/schema"
import { env } from "@/env.mjs"
import { and, desc, eq, not } from "drizzle-orm"

import { cn, formatPrice } from "@/lib/utils"
import { formatPrice, toTitleCase } from "@/lib/utils"
import {
Accordion,
AccordionContent,
Expand All @@ -15,7 +15,7 @@ import {
} from "@/components/ui/accordion"
import { Separator } from "@/components/ui/separator"
import { AddToCartForm } from "@/components/forms/add-to-cart-form"
import { Icons } from "@/components/icons"
import { Breadcrumbs } from "@/components/pagers/breadcrumbs"
import { ProductCard } from "@/components/product-card"
import { ProductImageCarousel } from "@/components/product-image-carousel"
import { Shell } from "@/components/shells/shell"
Expand Down Expand Up @@ -67,19 +67,22 @@ export default async function ProductPage({ params }: ProductPageProps) {

return (
<Shell>
<div className="flex items-center space-x-1 text-sm capitalize text-muted-foreground">
<div className="truncate">Products</div>
<Icons.chevronRight className="h-4 w-4" aria-hidden="true" />
<div className={cn(!product.subcategory && "text-foreground")}>
{product.category}
</div>
{product.subcategory ? (
<>
<Icons.chevronRight className="h-4 w-4" aria-hidden="true" />
<div className="text-foreground">{product.subcategory}</div>
</>
) : null}
</div>
<Breadcrumbs
segments={[
{
title: "Products",
href: "/products",
},
{
title: toTitleCase(product.category),
href: `/products?category=${product.category}`,
},
{
title: product.name,
href: `/product/${product.id}`,
},
]}
/>
<div className="flex flex-col gap-8 md:flex-row md:gap-16">
<ProductImageCarousel
className="w-full md:w-1/2"
Expand Down
10 changes: 7 additions & 3 deletions src/components/data-table/data-table-loading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ import {
} from "@/components/ui/table"

interface DataTableLoadingProps {
columnCount: number
rowCount?: number
isNewRowCreatable?: boolean
isRowsDeletable?: boolean
}

export function DataTableLoading({
columnCount,
rowCount = 10,
isNewRowCreatable = false,
isRowsDeletable = false,
}: DataTableLoadingProps) {
Expand All @@ -38,7 +42,7 @@ export function DataTableLoading({
<TableHeader>
{Array.from({ length: 1 }).map((_, i) => (
<TableRow key={i} className="hover:bg-transparent">
{Array.from({ length: 6 }).map((_, i) => (
{Array.from({ length: columnCount }).map((_, i) => (
<TableHead key={i}>
<Skeleton className="h-6 w-full" />
</TableHead>
Expand All @@ -47,9 +51,9 @@ export function DataTableLoading({
))}
</TableHeader>
<TableBody>
{Array.from({ length: 10 }).map((_, i) => (
{Array.from({ length: rowCount }).map((_, i) => (
<TableRow key={i} className="hover:bg-transparent">
{Array.from({ length: 6 }).map((_, i) => (
{Array.from({ length: columnCount }).map((_, i) => (
<TableCell key={i}>
<Skeleton className="h-6 w-full" />
</TableCell>
Expand Down
48 changes: 48 additions & 0 deletions src/components/pagers/breadcrumbs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import * as React from "react"
import Link from "next/link"

import { cn } from "@/lib/utils"
import { Icons } from "@/components/icons"

interface BreadcrumbsProps {
segments: {
title: string
href: string
}[]
separator?: keyof typeof Icons
}

export function Breadcrumbs({ segments, separator }: BreadcrumbsProps) {
const SeparatorIcon = Icons[separator ?? "chevronRight"]

return (
<nav
aria-label="breadcrumbs"
className="flex items-center text-sm font-medium text-muted-foreground"
>
{segments.map((segment, index) => {
const isLastSegment = index === segments.length - 1

return (
<React.Fragment key={segment.href}>
<Link
aria-current={isLastSegment ? "page" : undefined}
href={segment.href}
className={cn(
"truncate transition-colors hover:text-muted-foreground",
isLastSegment
? "pointer-events-none text-muted-foreground"
: "text-foreground"
)}
>
{segment.title}
</Link>
{!isLastSegment && (
<SeparatorIcon className="mx-2 h-4 w-4" aria-hidden="true" />
)}
</React.Fragment>
)
})}
</nav>
)
}

0 comments on commit c2f786a

Please sign in to comment.