'use client'
import type { PropsWithChildren, ReactElement, ReactNode } from 'react'
import { cn } from '@langgenius/dify-ui/cn'
import {
Tabs,
TabsList,
TabsPanel,
TabsTab,
} from '@langgenius/dify-ui/tabs'
import {
Children,
createContext,
useContext,
useEffect,
useRef,
useState,
} from 'react'
import { writeTextToClipboard } from '@/utils/clipboard'
import { Tag } from './tag'
type IChildrenProps = {
children: React.ReactNode
[key: string]: any
}
function ClipboardIcon(props: any) {
return (
)
}
function CopyButton({ code }: { code: string }) {
const [copyCount, setCopyCount] = useState(0)
const copied = copyCount > 0
useEffect(() => {
if (copyCount > 0) {
const timeout = setTimeout(() => setCopyCount(0), 1000)
return () => {
clearTimeout(timeout)
}
}
}, [copyCount])
return (
)
}
function CodePanelHeader({ tag, label }: { tag?: string, label?: string }) {
if (!tag && !label)
return null
return (
{tag && (
{tag}
)}
{tag && label && (
)}
{label && (
{label}
)}
)
}
type CodeExample = {
title?: string
tag?: string
code: string
}
type CodeTab = {
title: string
value: string
}
type ICodePanelProps = {
children?: React.ReactNode
tag?: string
label?: string
code?: string
title?: string
targetCode?: CodeExample
}
function CodePanel({ tag, label, children, targetCode }: ICodePanelProps) {
const child = Children.toArray(children)[0] as ReactElement
return (
{/*
{children} */}
{/*
*/}
{/*
*/}
{targetCode?.code
? (
{targetCode?.code}
)
: (
child
)}
)
}
type CodeGroupHeaderProps = {
title?: string
tabs?: CodeTab[]
}
function CodeGroupHeader({ title, tabs }: CodeGroupHeaderProps) {
const hasTabs = (tabs?.length ?? 0) > 1
return (
{title && (
{title}
)}
{hasTabs && (
{tabs!.map(tab => (
{tab.title}
))}
)}
)
}
type ICodeGroupPanelsProps = PropsWithChildren<{
targetCode?: CodeExample[]
tabs?: CodeTab[]
[key: string]: any
}>
function CodeGroupPanels({ children, targetCode, tabs, ...props }: ICodeGroupPanelsProps) {
if ((targetCode?.length ?? 0) > 1 && tabs) {
return (
<>
{targetCode!.map((code, index) => {
const tab = tabs[index]
return (
)
})}
>
)
}
return {children}
}
function usePreventLayoutShift() {
const positionRef = useRef(null)
const rafRef = useRef(null)
useEffect(() => {
return () => {
if (rafRef.current)
window.cancelAnimationFrame(rafRef.current)
}
}, [])
return {
positionRef,
preventLayoutShift(callback: () => void) {
if (!positionRef.current) {
callback()
return
}
const initialTop = positionRef.current.getBoundingClientRect().top
callback()
rafRef.current = window.requestAnimationFrame(() => {
if (!positionRef.current)
return
const newTop = positionRef.current.getBoundingClientRect().top
window.scrollBy(0, newTop - initialTop)
})
},
}
}
function useTabGroupProps(tabValues: string[]) {
const [selectedValue, setSelectedValue] = useState(tabValues[0] ?? '')
const { positionRef, preventLayoutShift } = usePreventLayoutShift()
const value = tabValues.includes(selectedValue)
? selectedValue
: tabValues[0] ?? ''
return {
ref: positionRef,
value,
onValueChange: (newValue: string | number | null) => {
if (newValue == null)
return
const nextValue = String(newValue)
if (!tabValues.includes(nextValue))
return
preventLayoutShift(() => {
setSelectedValue(nextValue)
})
},
}
}
const CodeGroupContext = createContext(false)
type CodeGroupProps = PropsWithChildren<{
/** Code example(s) to display */
targetCode?: string | CodeExample[]
/** Example block title */
title?: string
/** HTTP method tag, e.g. GET, POST */
tag?: string
/** API path */
label?: string
}>
export function CodeGroup({ children, title, targetCode, ...props }: CodeGroupProps) {
const examples = typeof targetCode === 'string' ? [{ code: targetCode }] as CodeExample[] : targetCode
const tabs = examples?.map(({ title }, index) => ({
title: title || 'Code',
value: String(index),
})) || []
const tabGroupProps = useTabGroupProps(tabs.map(tab => tab.value))
const hasTabs = tabs.length > 1
const content = (
<>
{children}
>
)
return (
{hasTabs
? (
{content}
)
: (
{content}
)}
)
}
type IChildProps = {
children: ReactNode
[key: string]: any
}
export function Code({ children, ...props }: IChildProps) {
return {children}
}
export function Pre({ children, ...props }: IChildrenProps) {
const isGrouped = useContext(CodeGroupContext)
if (isGrouped)
return children
return {children}
}
export function Embed({ value, ...props }: IChildrenProps) {
return {value}
}