dify/web/app/components/datasets/formatted-text/flavours/edit-slice.tsx

94 lines
2.9 KiB
TypeScript

import { useState } from 'react'
import type { FC, ReactNode } from 'react'
import { FloatingFocusManager, autoUpdate, flip, shift, useDismiss, useFloating, useHover, useInteractions, useRole } from '@floating-ui/react'
import { RiDeleteBinLine } from '@remixicon/react'
import type { SliceProps } from './type'
import { SliceContainer, SliceContent, SliceDivider, SliceLabel } from './shared'
import classNames from '@/utils/classnames'
import ActionButton, { ActionButtonState } from '@/app/components/base/action-button'
type EditSliceProps = SliceProps<{
label: ReactNode
onDelete: () => void
labelClassName?: string
contentClassName?: string
}>
export const EditSlice: FC<EditSliceProps> = (props) => {
const { label, className, text, onDelete, labelClassName, contentClassName, ...rest } = props
const [delBtnShow, setDelBtnShow] = useState(false)
const [isDelBtnHover, setDelBtnHover] = useState(false)
const { refs, floatingStyles, context } = useFloating({
open: delBtnShow,
onOpenChange: setDelBtnShow,
placement: 'right',
whileElementsMounted: autoUpdate,
middleware: [
flip(),
shift(),
],
})
const hover = useHover(context, {})
const dismiss = useDismiss(context)
const role = useRole(context)
const { getReferenceProps, getFloatingProps } = useInteractions([hover, dismiss, role])
const isDestructive = delBtnShow && isDelBtnHover
return (
<div>
<SliceContainer {...rest}
className={className}
ref={refs.setReference}
{...getReferenceProps()}
>
<SliceLabel
className={classNames(
isDestructive && '!bg-state-destructive-solid !text-text-primary-on-surface',
labelClassName,
)}
>
{label}
</SliceLabel>
<SliceContent
className={classNames(
isDestructive && '!bg-state-destructive-hover-alt',
contentClassName,
)}
>
{text}
</SliceContent>
<SliceDivider
className={classNames(
isDestructive && '!bg-state-destructive-hover-alt',
)}
/>
{delBtnShow && <FloatingFocusManager
context={context}
>
<div
ref={refs.setFloating}
style={floatingStyles}
{...getFloatingProps()}
className='p-1 rounded-lg bg-components-actionbar-bg shadow flex items-center justify-center'
onMouseEnter={() => setDelBtnHover(true)}
onMouseLeave={() => setDelBtnHover(false)}
>
<ActionButton
onClick={(e) => {
e.stopPropagation()
onDelete()
setDelBtnShow(false)
}}
state={ActionButtonState.Destructive}
>
<RiDeleteBinLine className='w-4 h-4' />
</ActionButton>
</div>
</FloatingFocusManager>}
</SliceContainer>
</div>
)
}