Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
fix: typescript errors
Browse files Browse the repository at this point in the history
  • Loading branch information
reneaaron committed Jun 12, 2024
1 parent e89fe63 commit 00fe7a1
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions frontend/src/components/Breadcrumbs.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Fragment } from "react";
import { Link, useMatches } from "react-router-dom";
import {
Breadcrumb,
Expand All @@ -7,18 +8,32 @@ import {
BreadcrumbSeparator,
} from "src/components/ui/breadcrumb";

type MatchWithCrumb = {
pathname: string;
handle?: {
crumb?: () => React.ReactNode;
};
};

function Breadcrumbs() {
const matches = useMatches();
const matches = useMatches() as MatchWithCrumb[]; // Type-cast useMatches result to MatchWithCrumb array

const crumbs = matches
// Skip the root item
.slice(1)
// first get rid of any matches that don't have handle and crumb
.filter((match) => Boolean(match.handle?.crumb));
// First, get rid of any matches that don't have a handle or crumb
.filter(
(
match
): match is MatchWithCrumb & {
handle: { crumb: () => React.ReactNode };
} => Boolean(match.handle?.crumb)
);

// Compare pathnames of index routes to remove duplicates
const isIndexRoute =
crumbs.length >= 2 && crumbs[crumbs.length - 1].pathname
? crumbs[crumbs.length - 1].pathname.slice(0, -1) ==
? crumbs[crumbs.length - 1].pathname.slice(0, -1) ===
crumbs[crumbs.length - 2].pathname
: false;

Expand All @@ -27,16 +42,16 @@ function Breadcrumbs() {

// Don't render anything if there is only one item
if (filteredCrumbs.length < 2) {
return;
return null;
}

return (
<>
<Breadcrumb>
<BreadcrumbList>
{filteredCrumbs.map((crumb, index) => (
<>
<BreadcrumbItem key={index}>
<Fragment key={index}>
<BreadcrumbItem>
{index + 1 < filteredCrumbs.length ? (
<BreadcrumbLink asChild>
<Link to={crumb.pathname}>{crumb.handle.crumb()}</Link>
Expand All @@ -46,7 +61,7 @@ function Breadcrumbs() {
)}
</BreadcrumbItem>
{index + 1 < filteredCrumbs.length && <BreadcrumbSeparator />}
</>
</Fragment>
))}
</BreadcrumbList>
</Breadcrumb>
Expand Down

0 comments on commit 00fe7a1

Please sign in to comment.