index.tsx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. 'use client'
  2. import React, { useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { debounce } from 'lodash-es'
  5. import copy from 'copy-to-clipboard'
  6. import Tooltip from '../tooltip'
  7. import copyStyle from './style.module.css'
  8. type Props = {
  9. content: string
  10. selectorId: string
  11. className?: string
  12. }
  13. const prefixEmbedded = 'appOverview.overview.appInfo.embedded'
  14. const CopyFeedback = ({ content, selectorId, className }: Props) => {
  15. const { t } = useTranslation()
  16. const [isCopied, setIsCopied] = useState<boolean>(false)
  17. const onClickCopy = debounce(() => {
  18. copy(content)
  19. setIsCopied(true)
  20. }, 100)
  21. const onMouseLeave = debounce(() => {
  22. setIsCopied(false)
  23. }, 100)
  24. return (
  25. <Tooltip
  26. selector={`common-copy-feedback-${selectorId}`}
  27. content={
  28. (isCopied
  29. ? t(`${prefixEmbedded}.copied`)
  30. : t(`${prefixEmbedded}.copy`)) || ''
  31. }
  32. >
  33. <div
  34. className={`w-8 h-8 cursor-pointer hover:bg-gray-100 rounded-lg ${
  35. className ?? ''
  36. }`}
  37. onMouseLeave={onMouseLeave}
  38. >
  39. <div
  40. onClick={onClickCopy}
  41. className={`w-full h-full ${copyStyle.copyIcon} ${
  42. isCopied ? copyStyle.copied : ''
  43. }`}
  44. ></div>
  45. </div>
  46. </Tooltip>
  47. )
  48. }
  49. export default CopyFeedback