'use client' import { cn } from '@langgenius/dify-ui/cn' import { IconButton } from '@langgenius/dify-ui/icon-button' import { Tooltip, TooltipContent, TooltipTrigger } from '@langgenius/dify-ui/tooltip' import copy from 'copy-to-clipboard' import { useCallback, useEffect, useId, useState } from 'react' import { useTranslation } from 'react-i18next' type Props = Readonly<{ label: string labelWidthClassName?: string value: string maskedValue?: string valueMaxWidthClassName?: string }> function KeyValueItem({ label, labelWidthClassName = 'w-10', value, maskedValue, valueMaxWidthClassName = 'max-w-[162px]', }: Props) { const { t } = useTranslation() const [isCopied, setIsCopied] = useState(false) const labelId = useId() const handleCopy = useCallback(() => { copy(value) setIsCopied(true) }, [value]) useEffect(() => { if (isCopied) { const timer = setTimeout(() => { setIsCopied(false) }, 2000) return () => { clearTimeout(timer) } } }, [isCopied]) const copiedLabel = t(($) => $['operation.copied'], { ns: 'common' }) const copyLabel = t(($) => $['operation.copy'], { ns: 'common' }) const copyButtonLabel = `${copyLabel}: ${label}` const copyStatus = `${copiedLabel}: ${label}` const tooltipLabel = isCopied ? copiedLabel : copyLabel return (
{label}
{maskedValue || value} {isCopied ? ( ) : ( )} } /> {tooltipLabel}
{isCopied ? copyStatus : ''}
) } export default KeyValueItem