"use client"
import Link from "next/link"
import type { ComponentType, MouseEvent, ReactNode } from "react"
import { Button, buttonVariants } from "@/shared/components/ui/button"
import { cn } from "@/shared/lib/utils"
type IconComponent = ComponentType<{ className?: string }>
export type RelationLinkProps = {
/** Destination href. When falsy the chip renders as a non-interactive button-styled span. */
href?: string | null
/** Primary label rendered inside the button. */
label?: ReactNode
/** Optional icon shown before the label. */
icon?: IconComponent
/** Rendered when label is empty (with or without href). Defaults to "—". */
fallback?: ReactNode
/** Optional secondary text rendered after the label (e.g. phone, plate). */
meta?: ReactNode
className?: string
}
/**
* Unified cell for related-data columns across every CRUD table.
* Renders an outlined button-style chip either way:
* - With href: clickable next/link wrapped by a Button (asChild).
* - Without href: same outline styling on a plain span so visual columns
* stay consistent across resources that don't have detail routes.
*/
export function RelationLink({
href,
label,
icon: Icon,
fallback = "—",
meta,
className,
}: RelationLinkProps) {
const hasLabel = label != null && label !== ""
if (!hasLabel && !href) {
return {fallback}
}
const body = (
<>
{Icon ? : null}
{hasLabel ? label : fallback}
{meta ? · {meta} : null}
>
)
if (!href) {
return (
{body}
)
}
return (
)
}