91 lines
3.3 KiB
TypeScript
Raw Normal View History

2023-05-25 16:59:47 +08:00
'use client'
import type { FC } from 'react'
import React, { useEffect, useRef, useState } from 'react'
2024-06-20 11:05:08 +08:00
import {
RiDeleteBinLine,
RiEditLine,
} from '@remixicon/react'
2023-05-25 16:59:47 +08:00
import { useTranslation } from 'react-i18next'
import { useBoolean } from 'ahooks'
2024-06-20 11:05:08 +08:00
import { Pin02 } from '../../base/icons/src/vender/line/general'
2023-05-25 16:59:47 +08:00
import s from './style.module.css'
import cn from '@/utils/classnames'
import { PortalToFollowElem, PortalToFollowElemContent, PortalToFollowElemTrigger } from '@/app/components/base/portal-to-follow-elem'
2023-05-25 16:59:47 +08:00
export type IItemOperationProps = {
2023-05-25 16:59:47 +08:00
className?: string
2023-09-09 19:17:12 +08:00
isItemHovering?: boolean
2023-05-25 16:59:47 +08:00
isPinned: boolean
isShowRenameConversation?: boolean
onRenameConversation?: () => void
2023-05-25 16:59:47 +08:00
isShowDelete: boolean
togglePin: () => void
onDelete: () => void
}
const ItemOperation: FC<IItemOperationProps> = ({
className,
isItemHovering,
2023-05-25 16:59:47 +08:00
isPinned,
togglePin,
isShowRenameConversation,
onRenameConversation,
isShowDelete,
onDelete,
2023-05-25 16:59:47 +08:00
}) => {
const { t } = useTranslation()
const [open, setOpen] = useState(false)
const ref = useRef(null)
const [isHovering, { setTrue: setIsHovering, setFalse: setNotHovering }] = useBoolean(false)
useEffect(() => {
if (!isItemHovering && !isHovering)
setOpen(false)
}, [isItemHovering, isHovering])
2023-05-25 16:59:47 +08:00
return (
<PortalToFollowElem
open={open}
onOpenChange={setOpen}
placement='bottom-end'
offset={4}
>
<PortalToFollowElemTrigger
onClick={() => setOpen(v => !v)}
>
2025-03-03 16:06:28 +08:00
<div className={cn(className, s.btn, 'h-6 w-6 rounded-md border-none py-1', (isItemHovering || open) && `${s.open} !bg-components-actionbar-bg !shadow-none`)}></div>
</PortalToFollowElemTrigger>
<PortalToFollowElemContent
className="z-50"
>
<div
ref={ref}
className={'min-w-[120px] rounded-lg border border-components-panel-border bg-components-panel-bg-blur p-1 shadow-lg backdrop-blur-[5px]'}
onMouseEnter={setIsHovering}
onMouseLeave={setNotHovering}
onClick={(e) => {
e.stopPropagation()
}}
>
<div className={cn(s.actionItem, 'group hover:bg-state-base-hover')} onClick={togglePin}>
<Pin02 className='h-4 w-4 shrink-0 text-text-secondary' />
2023-05-25 16:59:47 +08:00
<span className={s.actionName}>{isPinned ? t('explore.sidebar.action.unpin') : t('explore.sidebar.action.pin')}</span>
</div>
{isShowRenameConversation && (
<div className={cn(s.actionItem, 'group hover:bg-state-base-hover')} onClick={onRenameConversation}>
<RiEditLine className='h-4 w-4 shrink-0 text-text-secondary' />
<span className={s.actionName}>{t('explore.sidebar.action.rename')}</span>
</div>
)}
2023-05-25 16:59:47 +08:00
{isShowDelete && (
<div className={cn(s.actionItem, s.deleteActionItem, 'group hover:bg-state-base-hover')} onClick={onDelete} >
<RiDeleteBinLine className={cn(s.deleteActionItemChild, 'h-4 w-4 shrink-0 stroke-current stroke-2 text-text-secondary')} />
<span className={cn(s.actionName, s.deleteActionItemChild)}>{t('explore.sidebar.action.delete')}</span>
</div>
2023-05-25 16:59:47 +08:00
)}
</div>
</PortalToFollowElemContent>
</PortalToFollowElem>
2023-05-25 16:59:47 +08:00
)
}
export default React.memo(ItemOperation)