Files
supabase/apps/studio/components/ui/InlineLink.tsx

78 lines
2.0 KiB
TypeScript

import { type MouseEvent, type PropsWithChildren } from 'react'
import { cn } from 'ui'
import { DynamicLink } from '@/components/ui/DynamicLink'
import { getDocsPath } from '@/lib/telemetry/docs-path'
import { useTrack } from '@/lib/telemetry/track'
interface InlineLinkProps {
href: string
className?: string
target?: string
rel?: string
title?: string
onClick?: (e: MouseEvent<HTMLAnchorElement>) => void
onAuxClick?: (e: MouseEvent<HTMLAnchorElement>) => void
}
export const InlineLinkClassName =
'underline transition underline-offset-2 decoration-inherit hover:decoration-foreground text-inherit hover:text-foreground'
export const UntrackedInlineLink = ({
href,
className: _className,
children,
title,
...props
}: PropsWithChildren<InlineLinkProps>) => {
const className = cn(InlineLinkClassName, _className)
if (href.startsWith('http')) {
return (
<a
title={title}
className={className}
href={href}
target="_blank"
rel="noreferrer noopener"
{...props}
>
{children}
</a>
)
}
return (
<DynamicLink className={className} href={href} title={title} {...props}>
{children}
</DynamicLink>
)
}
const DocsInlineLink = ({
docsPath,
onClick,
onAuxClick,
...props
}: PropsWithChildren<InlineLinkProps & { docsPath: string }>) => {
const track = useTrack()
const handleClick = (e: MouseEvent<HTMLAnchorElement>) => {
track('studio_docs_link_clicked', { docsPath })
onClick?.(e)
}
const handleAuxClick = (e: MouseEvent<HTMLAnchorElement>) => {
const isMiddleClick = e.button === 1
if (isMiddleClick) track('studio_docs_link_clicked', { docsPath })
onAuxClick?.(e)
}
return <UntrackedInlineLink {...props} onClick={handleClick} onAuxClick={handleAuxClick} />
}
export const InlineLink = (props: PropsWithChildren<InlineLinkProps>) => {
const docsPath = getDocsPath(props.href)
if (!docsPath) return <UntrackedInlineLink {...props} />
return <DocsInlineLink {...props} docsPath={docsPath} />
}