|
1
|
"use client" |
|
2
|
|
|
3
|
import clsx from "clsx" |
|
4
|
import React from "react" |
|
5
|
import { Link } from "@/components/Link" |
|
6
|
import { Tooltip } from "@/components/Tooltip" |
|
7
|
import { ExclamationCircle, PlaySolid } from "@medusajs/icons" |
|
8
|
import { GITHUB_ISSUES_LINK } from "@/constants" |
|
9
|
import { CodeBlockCopyAction } from "./Copy" |
|
10
|
import { CodeBlockAskAiAction } from "./AskAi" |
|
11
|
import { useSiteConfig } from "../../../providers/SiteConfig" |
|
12
|
import { CodeBlockBloomAction } from "./Bloom" |
|
13
|
import { CodeBlockStyle } from ".." |
|
14
|
|
|
15
|
export type CodeBlockActionsProps = { |
|
16
|
source: string |
|
17
|
isSingleLine?: boolean |
|
18
|
inHeader: boolean |
|
19
|
showGradientBg?: boolean |
|
20
|
inInnerCode?: boolean |
|
21
|
isCollapsed: boolean |
|
22
|
canShowApiTesting?: boolean |
|
23
|
onApiTesting?: React.Dispatch<React.SetStateAction<boolean>> |
|
24
|
noReport?: boolean |
|
25
|
noCopy?: boolean |
|
26
|
noAskAi?: boolean |
|
27
|
codeBlockStyle?: CodeBlockStyle |
|
28
|
} |
|
29
|
|
|
30
|
export const CodeBlockActions = ({ |
|
31
|
source, |
|
32
|
inHeader, |
|
33
|
showGradientBg = true, |
|
34
|
inInnerCode = false, |
|
35
|
isCollapsed, |
|
36
|
isSingleLine = false, |
|
37
|
canShowApiTesting = false, |
|
38
|
onApiTesting, |
|
39
|
noReport = false, |
|
40
|
noCopy = false, |
|
41
|
noAskAi = false, |
|
42
|
codeBlockStyle = "loud", |
|
43
|
}: CodeBlockActionsProps) => { |
|
44
|
const { productView } = useSiteConfig() |
|
45
|
|
|
46
|
const iconClassName = [ |
|
47
|
"text-medusa-contrast-fg-secondary", |
|
48
|
"group-hover:text-medusa-contrast-fg-primary", |
|
49
|
"group-focus:text-medusa-contrast-fg-primary", |
|
50
|
] |
|
51
|
|
|
52
|
return ( |
|
53
|
<div |
|
54
|
className={clsx( |
|
55
|
"hidden md:block", |
|
56
|
!inHeader && |
|
57
|
"xs:rounded xs:absolute xs:right-0 xs:top-0 xs:w-[calc(17%+10px)] xs:h-full" |
|
58
|
)} |
|
59
|
> |
|
60
|
{!inHeader && ( |
|
61
|
<div |
|
62
|
className={clsx( |
|
63
|
!inHeader && |
|
64
|
showGradientBg && [ |
|
65
|
inInnerCode && |
|
66
|
"xs:bg-subtle-code-fade-right-to-left dark:xs:bg-subtle-code-fade-right-to-left-dark", |
|
67
|
!inInnerCode && |
|
68
|
"xs:bg-base-code-fade-right-to-left dark:xs:bg-base-code-fade-right-to-left-dark", |
|
69
|
], |
|
70
|
(inHeader || !showGradientBg) && "xs:bg-transparent", |
|
71
|
"z-[9] w-full h-full absolute top-0 left-0" |
|
72
|
)} |
|
73
|
/> |
|
74
|
)} |
|
75
|
<div |
|
76
|
className={clsx( |
|
77
|
"md:flex md:justify-end md:gap-docs_0.25 relative", |
|
78
|
!inHeader && [ |
|
79
|
"md:pr-docs_0.5 z-[11]", |
|
80
|
isCollapsed && "md:pt-docs_2.5", |
|
81
|
!isCollapsed && [ |
|
82
|
isSingleLine && "md:pt-docs_0.25", |
|
83
|
!isSingleLine && "md:pt-docs_0.5", |
|
84
|
], |
|
85
|
] |
|
86
|
)} |
|
87
|
> |
|
88
|
{!noAskAi && ( |
|
89
|
<CodeBlockAskAiAction source={source} inHeader={inHeader} /> |
|
90
|
)} |
|
91
|
{canShowApiTesting && ( |
|
92
|
<Tooltip |
|
93
|
text="Test API" |
|
94
|
tooltipClassName="font-base" |
|
95
|
className={clsx("group")} |
|
96
|
innerClassName={clsx( |
|
97
|
inHeader && "flex", |
|
98
|
"h-fit rounded-docs_sm", |
|
99
|
"group-hover:bg-medusa-contrast-bg-base-hover group-focus:bg-medusa-contrast-bg-base-hover" |
|
100
|
)} |
|
101
|
> |
|
102
|
<span |
|
103
|
className={clsx( |
|
104
|
!inHeader && "p-[6px]", |
|
105
|
inHeader && "p-[4.5px]", |
|
106
|
"cursor-pointer" |
|
107
|
)} |
|
108
|
onClick={() => onApiTesting?.(true)} |
|
109
|
> |
|
110
|
<PlaySolid className={clsx(iconClassName)} /> |
|
111
|
</span> |
|
112
|
</Tooltip> |
|
113
|
)} |
|
114
|
{!noReport && ( |
|
115
|
<Tooltip |
|
116
|
text="Report Issue" |
|
117
|
tooltipClassName="font-base" |
|
118
|
className={clsx("group")} |
|
119
|
innerClassName={clsx( |
|
120
|
inHeader && "flex", |
|
121
|
"h-fit rounded-docs_sm", |
|
122
|
"group-hover:bg-medusa-contrast-bg-base-hover group-focus:bg-medusa-contrast-bg-base-hover" |
|
123
|
)} |
|
124
|
> |
|
125
|
<Link |
|
126
|
href={GITHUB_ISSUES_LINK} |
|
127
|
target="_blank" |
|
128
|
className={clsx( |
|
129
|
"bg-transparent border-none cursor-pointer rounded", |
|
130
|
"[&:not(:first-child)]:ml-docs_0.5", |
|
131
|
"inline-flex justify-center items-center invisible xs:visible", |
|
132
|
!inHeader && "p-[6px]", |
|
133
|
inHeader && "p-[4.5px]" |
|
134
|
)} |
|
135
|
rel="noreferrer" |
|
136
|
> |
|
137
|
<ExclamationCircle className={clsx(iconClassName)} /> |
|
138
|
</Link> |
|
139
|
</Tooltip> |
|
140
|
)} |
|
141
|
{!noCopy && productView !== "bloom" && ( |
|
142
|
<CodeBlockCopyAction source={source} inHeader={inHeader} codeBlockStyle={codeBlockStyle} /> |
|
143
|
)} |
|
144
|
{productView === "bloom" && ( |
|
145
|
<CodeBlockBloomAction source={source} inHeader={inHeader} /> |
|
146
|
)} |
|
147
|
</div> |
|
148
|
</div> |
|
149
|
) |
|
150
|
} |