index.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 'use client'
  2. import React, { useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { debounce } from 'lodash-es'
  5. import QRCode from 'qrcode.react'
  6. import Tooltip from '../tooltip'
  7. import QrcodeStyle from './style.module.css'
  8. type Props = {
  9. content: string
  10. selectorId: string
  11. className?: string
  12. }
  13. const prefixEmbedded = 'appOverview.overview.appInfo.qrcode.title'
  14. const ShareQRCode = ({ content, selectorId, className }: Props) => {
  15. const { t } = useTranslation()
  16. const [isShow, setisShow] = useState<boolean>(false)
  17. const onClickShow = debounce(() => {
  18. setisShow(true)
  19. }, 100)
  20. const downloadQR = () => {
  21. const canvas = document.getElementsByTagName('canvas')[0]
  22. const link = document.createElement('a')
  23. link.download = 'qrcode.png'
  24. link.href = canvas.toDataURL()
  25. link.click()
  26. }
  27. const onMouseLeave = debounce(() => {
  28. setisShow(false)
  29. }, 500)
  30. return (
  31. <Tooltip
  32. selector={`common-qrcode-show-${selectorId}`}
  33. content={t(`${prefixEmbedded}`) || ''}
  34. >
  35. <div
  36. className={`w-8 h-8 cursor-pointer rounded-lg ${className ?? ''}`}
  37. onMouseLeave={onMouseLeave}
  38. onClick={onClickShow}
  39. >
  40. <div className={`w-full h-full ${QrcodeStyle.QrcodeIcon} ${isShow ? QrcodeStyle.show : ''}`} />
  41. {isShow && <div className={QrcodeStyle.qrcodeform}>
  42. <QRCode size={160} value={content} className={QrcodeStyle.qrcodeimage}/>
  43. <div className={QrcodeStyle.text}>
  44. <div className={`text-gray-500 ${QrcodeStyle.scan}`}>{t('appOverview.overview.appInfo.qrcode.scan')}</div>
  45. <div className={`text-gray-500 ${QrcodeStyle.scan}`}>·</div>
  46. <div className={QrcodeStyle.download} onClick={downloadQR}>{t('appOverview.overview.appInfo.qrcode.download')}</div>
  47. </div>
  48. </div>
  49. }
  50. </div>
  51. </Tooltip>
  52. )
  53. }
  54. export default ShareQRCode