feat: add conditional links to podcast info card

This commit is contained in:
2024-04-20 12:31:58 +02:00
parent b25af2cad2
commit f40499e781
2 changed files with 39 additions and 3 deletions

View File

@@ -1,3 +1,6 @@
import { Link, useParams } from "react-router-dom";
import { Wrap } from "./wrap";
type Props = {
title: string;
author: string;
@@ -11,11 +14,23 @@ export function PodcastInfoCard({
description,
imageURL,
}: Props) {
const { episodeId, podcastId } = useParams<{
episodeId?: string;
podcastId?: string;
}>();
const href = `/podcast/${podcastId}`;
const shouldWrapWithLink = !!episodeId;
return (
<div>
<h1>{title}</h1>
<h2>{author}</h2>
<img src={imageURL} alt={title} />
<Wrap if={shouldWrapWithLink} with={Link} wrapperProps={{ to: href }}>
<img src={imageURL} alt={title} />
</Wrap>
<Wrap if={shouldWrapWithLink} with={Link} wrapperProps={{ to: href }}>
<h1>{title}</h1>
<h2>{author}</h2>
</Wrap>
<p>{description}</p>
</div>
);

21
src/components/wrap.tsx Normal file
View File

@@ -0,0 +1,21 @@
import { createElement, ReactNode } from "react";
type Props = {
if?: boolean;
with: (typeof createElement.arguments)[0];
wrapperProps: (typeof createElement.arguments)[1];
children: NonNullable<ReactNode>;
};
export function Wrap({
if: condition,
with: wrapper,
wrapperProps,
children,
}: Props) {
return condition ? (
createElement(wrapper, wrapperProps, [children])
) : (
<>{children}</>
);
}