|
1 | 1 | import { resolveContentRef, resolveContentRefFallback } from '@/lib/references'; |
2 | | -import * as api from '@gitbook/api'; |
| 2 | +import type * as api from '@gitbook/api'; |
3 | 3 | import type { IconName } from '@gitbook/icons'; |
4 | | -import { Button } from '../primitives'; |
| 4 | +import { Button, type ButtonProps } from '../primitives'; |
5 | 5 | import type { InlineProps } from './Inline'; |
| 6 | +import { InlineActionButton } from './InlineActionButton'; |
6 | 7 | import { NotFoundRefHoverCard } from './NotFoundRefHoverCard'; |
7 | 8 |
|
8 | | -export async function InlineButton(props: InlineProps<api.DocumentInlineButton>) { |
9 | | - const { inline, context } = props; |
| 9 | +export function InlineButton(props: InlineProps<api.DocumentInlineButton>) { |
| 10 | + const { inline } = props; |
10 | 11 |
|
11 | | - const resolved = context.contentContext |
12 | | - ? await resolveContentRef(inline.data.ref, context.contentContext) |
13 | | - : null; |
| 12 | + const buttonProps: ButtonProps = { |
| 13 | + label: inline.data.label, |
| 14 | + variant: inline.data.kind, |
| 15 | + icon: inline.data.icon as IconName | undefined, |
| 16 | + }; |
14 | 17 |
|
15 | | - const href = resolved?.href ?? resolveContentRefFallback(inline.data.ref)?.href; |
| 18 | + const ButtonImplementation = () => { |
| 19 | + if ('action' in inline.data && 'query' in inline.data.action) { |
| 20 | + return ( |
| 21 | + <InlineActionButton |
| 22 | + action={inline.data.action.action} |
| 23 | + query={inline.data.action.query ?? ''} |
| 24 | + buttonProps={buttonProps} |
| 25 | + /> |
| 26 | + ); |
| 27 | + } |
| 28 | + |
| 29 | + return <InlineLinkButton {...props} buttonProps={buttonProps} />; |
| 30 | + }; |
16 | 31 |
|
17 | 32 | const inlineElement = ( |
18 | 33 | // Set the leading to have some vertical space between adjacent buttons |
19 | 34 | <span className="inline-button leading-12 [&:has(+.inline-button)]:mr-2"> |
20 | | - <Button |
21 | | - href={href} |
22 | | - label={inline.data.label} |
23 | | - // TODO: use a variant specifically for user-defined buttons. |
24 | | - variant={inline.data.kind} |
25 | | - className="leading-normal" |
26 | | - disabled={href === undefined} |
27 | | - icon={inline.data.icon as IconName | undefined} |
28 | | - insights={{ |
29 | | - type: 'link_click', |
30 | | - link: { |
31 | | - target: inline.data.ref, |
32 | | - position: api.SiteInsightsLinkPosition.Content, |
33 | | - }, |
34 | | - }} |
35 | | - /> |
| 35 | + <ButtonImplementation /> |
36 | 36 | </span> |
37 | 37 | ); |
38 | 38 |
|
39 | | - if (!resolved) { |
40 | | - return <NotFoundRefHoverCard context={context}>{inlineElement}</NotFoundRefHoverCard>; |
| 39 | + return inlineElement; |
| 40 | +} |
| 41 | + |
| 42 | +export async function InlineLinkButton( |
| 43 | + props: InlineProps<api.DocumentInlineButton> & { buttonProps: ButtonProps } |
| 44 | +) { |
| 45 | + const { inline, context, buttonProps } = props; |
| 46 | + |
| 47 | + if (!('ref' in inline.data)) return; |
| 48 | + |
| 49 | + const resolved = |
| 50 | + context.contentContext && inline.data.ref |
| 51 | + ? await resolveContentRef(inline.data.ref, context.contentContext) |
| 52 | + : null; |
| 53 | + |
| 54 | + const href = |
| 55 | + resolved?.href ?? |
| 56 | + (inline.data.ref ? resolveContentRefFallback(inline.data.ref)?.href : undefined); |
| 57 | + |
| 58 | + const button = <Button {...buttonProps} href={href} disabled={href === undefined} />; |
| 59 | + |
| 60 | + if (inline.data.ref && !resolved) { |
| 61 | + return <NotFoundRefHoverCard context={context}>{button}</NotFoundRefHoverCard>; |
41 | 62 | } |
42 | 63 |
|
43 | | - return inlineElement; |
| 64 | + return button; |
44 | 65 | } |
0 commit comments