invitation-link.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. 'use client'
  2. import React, { useCallback, useEffect, useRef, useState } from 'react'
  3. import { t } from 'i18next'
  4. import copy from 'copy-to-clipboard'
  5. import s from './index.module.css'
  6. import Tooltip from '@/app/components/base/tooltip'
  7. import { randomString } from '@/app/components/app-sidebar/basic'
  8. type IInvitationLinkProps = {
  9. value?: string
  10. }
  11. const InvitationLink = ({
  12. value = '',
  13. }: IInvitationLinkProps) => {
  14. const [isCopied, setIsCopied] = useState(false)
  15. const selector = useRef(`invite-link-${randomString(4)}`)
  16. const copyHandle = useCallback(() => {
  17. copy(value)
  18. setIsCopied(true)
  19. }, [value])
  20. useEffect(() => {
  21. if (isCopied) {
  22. const timeout = setTimeout(() => {
  23. setIsCopied(false)
  24. }, 1000)
  25. return () => {
  26. clearTimeout(timeout)
  27. }
  28. }
  29. }, [isCopied])
  30. return (
  31. <div className='flex rounded-lg bg-gray-100 hover:bg-gray-100 border border-gray-200 py-2 items-center'>
  32. <div className="flex items-center flex-grow h-5">
  33. <div className='flex-grow bg-gray-100 text-[13px] relative h-full'>
  34. <Tooltip
  35. selector={selector.current}
  36. content={isCopied ? `${t('appApi.copied')}` : `${t('appApi.copy')}`}
  37. className='z-10'
  38. >
  39. <div className='absolute top-0 left-0 w-full pl-2 pr-2 truncate cursor-pointer r-0' onClick={copyHandle}>{value}</div>
  40. </Tooltip>
  41. </div>
  42. <div className="flex-shrink-0 h-4 bg-gray-200 border" />
  43. <Tooltip
  44. selector={selector.current}
  45. content={isCopied ? `${t('appApi.copied')}` : `${t('appApi.copy')}`}
  46. className='z-10'
  47. >
  48. <div className="px-0.5 flex-shrink-0">
  49. <div className={`box-border w-[30px] h-[30px] flex items-center justify-center rounded-lg hover:bg-gray-100 cursor-pointer ${s.copyIcon} ${isCopied ? s.copied : ''}`} onClick={copyHandle}>
  50. </div>
  51. </div>
  52. </Tooltip>
  53. </div>
  54. </div>
  55. )
  56. }
  57. export default InvitationLink