Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

4.0 KB
Raw Blame
import type { FC } from 'react'
import type { RoleName } from './index'
import type { EventEmitterValue } from '@/context/event-emitter'
import { Popover, PopoverContent, PopoverTrigger } from '@langgenius/dify-ui/popover'
import { RiMoreFill } from '@remixicon/react'
import { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { useEventEmitterContextContext } from '@/context/event-emitter'
import { UPDATE_HISTORY_EVENT_EMITTER } from '../../constants'
import { useSelectOrDelete, useTrigger } from '../../hooks'
import { DELETE_HISTORY_BLOCK_COMMAND } from './index'

type HistoryBlockComponentProps = {
  nodeKey: string
  roleName?: RoleName
  onEditRole: () => void
}

const HistoryBlockComponent: FC<HistoryBlockComponentProps> = ({
  nodeKey,
  roleName = { user: '', assistant: '' },
  onEditRole,
}) => {
  const { t } = useTranslation(['common'])
  const [ref, isSelected] = useSelectOrDelete(nodeKey, DELETE_HISTORY_BLOCK_COMMAND)
  const [triggerRef, open, setOpen] = useTrigger<HTMLButtonElement>()
  const { eventEmitter } = useEventEmitterContextContext()
  const [localRoleName, setLocalRoleName] = useState<RoleName>(roleName)

  eventEmitter?.useSubscription((event?: EventEmitterValue) => {
    if (typeof event === 'string') return

    if (
      event?.type === UPDATE_HISTORY_EVENT_EMITTER &&
      event.payload &&
      typeof event.payload === 'object'
    )
      setLocalRoleName(event.payload as RoleName)
  })

  return (
    <div
      className={`group inline-flex h-6 items-center rounded-[5px] border border-transparent pr-0.5 pl-1 text-[#DD2590] hover:bg-[#FCE7F6] ${open ? 'bg-[#FCE7F6]' : 'bg-[#FDF2FA]'} ${isSelected && 'border-[#F670C7]!'} `}
      ref={ref}
    >
      <span
        aria-hidden
        className="mr-1 i-custom-vender-solid-general-message-clock-circle h-3.5 w-3.5"
      />
      <div className="mr-1 text-xs font-medium">
        {t(($) => $['promptEditor.history.item.title'], { ns: 'common' })}
      </div>
      <Popover open={open} onOpenChange={setOpen}>
        <PopoverTrigger
          render={
            <button
              type="button"
              aria-label={t(($) => $['operation.more'], { ns: 'common' })}
              className={`flex h-4.5 w-4.5 cursor-pointer items-center justify-center rounded ${open ? 'bg-[#DD2590] text-white' : 'bg-white/50 group-hover:bg-white group-hover:shadow-xs'} `}
              ref={triggerRef}
              onClick={(e) => e.preventDefault()}
            >
              <RiMoreFill className="size-3" />
            </button>
          }
        />
        <PopoverContent
          side="top"
          align="end"
          sideOffset={4}
          alignOffset={-148}
          className="border-none bg-transparent shadow-none"
        >
          <div className="w-90 rounded-xl bg-white shadow-lg">
            <div className="p-4">
              <div className="mb-2 text-xs font-medium text-gray-500">
                {t(($) => $['promptEditor.history.modal.title'], { ns: 'common' })}
              </div>
              <div className="flex items-center text-sm text-gray-700">
                <div className="mr-1 w-20 text-xs font-semibold">{localRoleName?.user}</div>
                {t(($) => $['promptEditor.history.modal.user'], { ns: 'common' })}
              </div>
              <div className="flex items-center text-sm text-gray-700">
                <div className="mr-1 w-20 text-xs font-semibold">{localRoleName?.assistant}</div>
                {t(($) => $['promptEditor.history.modal.assistant'], { ns: 'common' })}
              </div>
            </div>
            <button
              type="button"
              className="w-full cursor-pointer rounded-b-xl border-t border-black/5 px-4 py-3 text-left text-xs text-primary-600"
              onClick={onEditRole}
            >
              {t(($) => $['promptEditor.history.modal.edit'], { ns: 'common' })}
            </button>
          </div>
        </PopoverContent>
      </Popover>
    </div>
  )
}

export default HistoryBlockComponent