operation.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useEffect, useRef, useState } from 'react'
  4. import {
  5. RiDeleteBinLine,
  6. RiEditLine,
  7. RiMoreFill,
  8. RiPushpinLine,
  9. RiUnpinLine,
  10. } from '@remixicon/react'
  11. import { useTranslation } from 'react-i18next'
  12. import { useBoolean } from 'ahooks'
  13. import { PortalToFollowElem, PortalToFollowElemContent, PortalToFollowElemTrigger } from '@/app/components/base/portal-to-follow-elem'
  14. import ActionButton, { ActionButtonState } from '@/app/components/base/action-button'
  15. import cn from '@/utils/classnames'
  16. type Props = {
  17. isActive?: boolean
  18. isItemHovering?: boolean
  19. isPinned: boolean
  20. isShowRenameConversation?: boolean
  21. onRenameConversation?: () => void
  22. isShowDelete: boolean
  23. togglePin: () => void
  24. onDelete: () => void
  25. }
  26. const Operation: FC<Props> = ({
  27. isActive,
  28. isItemHovering,
  29. isPinned,
  30. togglePin,
  31. isShowRenameConversation,
  32. onRenameConversation,
  33. isShowDelete,
  34. onDelete,
  35. }) => {
  36. const { t } = useTranslation()
  37. const [open, setOpen] = useState(false)
  38. const ref = useRef(null)
  39. const [isHovering, { setTrue: setIsHovering, setFalse: setNotHovering }] = useBoolean(false)
  40. useEffect(() => {
  41. if (!isItemHovering && !isHovering)
  42. setOpen(false)
  43. }, [isItemHovering, isHovering])
  44. return (
  45. <PortalToFollowElem
  46. open={open}
  47. onOpenChange={setOpen}
  48. placement='bottom-end'
  49. offset={4}
  50. >
  51. <PortalToFollowElemTrigger
  52. onClick={() => setOpen(v => !v)}
  53. >
  54. <ActionButton
  55. className={cn((isItemHovering || open) ? 'opacity-100' : 'opacity-0')}
  56. state={
  57. isActive
  58. ? ActionButtonState.Active
  59. : open
  60. ? ActionButtonState.Hover
  61. : ActionButtonState.Default
  62. }
  63. >
  64. <RiMoreFill className='w-4 h-4' />
  65. </ActionButton>
  66. </PortalToFollowElemTrigger>
  67. <PortalToFollowElemContent className="z-50">
  68. <div
  69. ref={ref}
  70. className={'min-w-[120px] p-1 bg-components-panel-bg-blur backdrop-blur-sm rounded-xl border-[0.5px] border-components-panel-border shadow-lg'}
  71. onMouseEnter={setIsHovering}
  72. onMouseLeave={setNotHovering}
  73. onClick={(e) => {
  74. e.stopPropagation()
  75. }}
  76. >
  77. <div className={cn('flex items-center space-x-1 px-2 py-1.5 rounded-lg text-text-secondary system-md-regular cursor-pointer hover:bg-state-base-hover')} onClick={togglePin}>
  78. {isPinned && <RiUnpinLine className='shrink-0 w-4 h-4 text-text-tertiary' />}
  79. {!isPinned && <RiPushpinLine className='shrink-0 w-4 h-4 text-text-tertiary' />}
  80. <span className='grow'>{isPinned ? t('explore.sidebar.action.unpin') : t('explore.sidebar.action.pin')}</span>
  81. </div>
  82. {isShowRenameConversation && (
  83. <div className={cn('flex items-center space-x-1 px-2 py-1.5 rounded-lg text-text-secondary system-md-regular cursor-pointer hover:bg-state-base-hover')} onClick={onRenameConversation}>
  84. <RiEditLine className='shrink-0 w-4 h-4 text-text-tertiary' />
  85. <span className='grow'>{t('explore.sidebar.action.rename')}</span>
  86. </div>
  87. )}
  88. {isShowDelete && (
  89. <div className={cn('group flex items-center space-x-1 px-2 py-1.5 rounded-lg text-text-secondary system-md-regular cursor-pointer hover:bg-state-destructive-hover hover:text-text-destructive')} onClick={onDelete} >
  90. <RiDeleteBinLine className={cn('shrink-0 w-4 h-4 text-text-tertiary group-hover:text-text-destructive')} />
  91. <span className='grow'>{t('explore.sidebar.action.delete')}</span>
  92. </div>
  93. )}
  94. </div>
  95. </PortalToFollowElemContent>
  96. </PortalToFollowElem>
  97. )
  98. }
  99. export default React.memo(Operation)