Files

89 lines
2.1 KiB
TypeScript

import { BookOpenIcon } from 'lucide-react'
import type { MouseEvent } from 'react'
import { Button } from 'ui'
import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
import { getDocsPath } from '@/lib/telemetry/docs-path'
import { useTrack } from '@/lib/telemetry/track'
interface DocsButtonProps {
href: string
abbrev?: boolean
className?: string
topic?: string
/** Custom button text, e.g. to distinguish multiple docs buttons side by side. */
label?: string
/** Shows only the book icon, with the label in a tooltip */
iconOnly?: boolean
}
export const DocsButton = ({
href,
abbrev = true,
className,
topic,
label,
iconOnly = false,
}: DocsButtonProps) => {
const track = useTrack()
const trackDocsClick = () => {
const docsPath = getDocsPath(href)
if (docsPath) track('studio_docs_link_clicked', { docsPath })
}
const handleClick = (e: MouseEvent<HTMLElement>) => {
e.stopPropagation()
trackDocsClick()
}
const handleAuxClick = (e: MouseEvent<HTMLElement>) => {
const isMiddleClick = e.button === 1
if (isMiddleClick) trackDocsClick()
}
const text = label ?? (abbrev ? 'Docs' : 'Documentation')
const topicLabel = topic ? `${topic} documentation (opens in new tab)` : undefined
if (iconOnly) {
return (
<ButtonTooltip
asChild
variant="default"
className={className}
icon={<BookOpenIcon />}
onClick={handleClick}
tooltip={{ content: { side: 'bottom', text } }}
>
<a
target="_blank"
rel="noopener noreferrer"
href={href}
aria-label={topicLabel ?? `${text} (opens in new tab)`}
onAuxClick={handleAuxClick}
/>
</ButtonTooltip>
)
}
return (
<Button
asChild
variant="default"
className={className}
icon={<BookOpenIcon />}
onClick={handleClick}
>
<a
target="_blank"
rel="noopener noreferrer"
href={href}
aria-label={topicLabel}
onAuxClick={handleAuxClick}
>
{text}
</a>
</Button>
)
}