index.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 copyStyle from './style.module.css'
  7. import Tooltip from '@/app/components/base/tooltip'
  8. type Props = {
  9. content: string
  10. className?: string
  11. }
  12. const prefixEmbedded = 'appOverview.overview.appInfo.embedded'
  13. const CopyFeedback = ({ content, className }: Props) => {
  14. const { t } = useTranslation()
  15. const [isCopied, setIsCopied] = useState<boolean>(false)
  16. const onClickCopy = debounce(() => {
  17. copy(content)
  18. setIsCopied(true)
  19. }, 100)
  20. const onMouseLeave = debounce(() => {
  21. setIsCopied(false)
  22. }, 100)
  23. return (
  24. <Tooltip
  25. popupContent={
  26. (isCopied
  27. ? t(`${prefixEmbedded}.copied`)
  28. : t(`${prefixEmbedded}.copy`)) || ''
  29. }
  30. >
  31. <div
  32. className={`w-8 h-8 cursor-pointer hover:bg-gray-100 rounded-lg ${
  33. className ?? ''
  34. }`}
  35. >
  36. <div
  37. onClick={onClickCopy}
  38. onMouseLeave={onMouseLeave}
  39. className={`w-full h-full ${copyStyle.copyIcon} ${
  40. isCopied ? copyStyle.copied : ''
  41. }`}
  42. ></div>
  43. </div>
  44. </Tooltip>
  45. )
  46. }
  47. export default CopyFeedback
  48. export const CopyFeedbackNew = ({ content, className }: Pick<Props, 'className' | 'content'>) => {
  49. const { t } = useTranslation()
  50. const [isCopied, setIsCopied] = useState<boolean>(false)
  51. const onClickCopy = debounce(() => {
  52. copy(content)
  53. setIsCopied(true)
  54. }, 100)
  55. const onMouseLeave = debounce(() => {
  56. setIsCopied(false)
  57. }, 100)
  58. return (
  59. <Tooltip
  60. popupContent={
  61. (isCopied
  62. ? t(`${prefixEmbedded}.copied`)
  63. : t(`${prefixEmbedded}.copy`)) || ''
  64. }
  65. >
  66. <div
  67. className={`w-8 h-8 cursor-pointer hover:bg-gray-100 rounded-lg ${
  68. className ?? ''
  69. }`}
  70. >
  71. <div
  72. onClick={onClickCopy}
  73. onMouseLeave={onMouseLeave}
  74. className={`w-full h-full ${copyStyle.copyIcon} ${
  75. isCopied ? copyStyle.copied : ''
  76. }`}
  77. ></div>
  78. </div>
  79. </Tooltip>
  80. )
  81. }