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