invitation-link.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 { SuccessInvitationResult } from '.'
  7. import Tooltip from '@/app/components/base/tooltip'
  8. import { randomString } from '@/utils'
  9. type IInvitationLinkProps = {
  10. value: SuccessInvitationResult
  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.startsWith('http') ? window.location.origin : ''}${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 items-center rounded-lg border border-components-input-border-active bg-components-input-bg-normal py-2 hover:bg-state-base-hover'>
  33. <div className="flex h-5 grow items-center">
  34. <div className='relative h-full grow text-[13px]'>
  35. <Tooltip
  36. popupContent={isCopied ? `${t('appApi.copied')}` : `${t('appApi.copy')}`}
  37. >
  38. <div className='r-0 absolute left-0 top-0 w-full cursor-pointer truncate pl-2 pr-2' onClick={copyHandle}>{value.url}</div>
  39. </Tooltip>
  40. </div>
  41. <div className="h-4 shrink-0 border bg-divider-regular" />
  42. <Tooltip
  43. popupContent={isCopied ? `${t('appApi.copied')}` : `${t('appApi.copy')}`}
  44. >
  45. <div className="shrink-0 px-0.5">
  46. <div className={`box-border flex h-[30px] w-[30px] cursor-pointer items-center justify-center rounded-lg hover:bg-state-base-hover ${s.copyIcon} ${isCopied ? s.copied : ''}`} onClick={copyHandle}>
  47. </div>
  48. </div>
  49. </Tooltip>
  50. </div>
  51. </div>
  52. )
  53. }
  54. export default InvitationLink