mirror of
https://github.com/langgenius/dify.git
synced 2026-09-01 13:47:18 +08:00
Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: jyong <718720800@qq.com> Co-authored-by: Yansong Zhang <916125788@qq.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: hj24 <mambahj24@gmail.com> Co-authored-by: hj24 <huangjian@dify.ai> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Co-authored-by: CodingOnStar <hanxujiang@dify.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: Ayush Baluni <73417844+aayushbaluni@users.noreply.github.com> Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com> Co-authored-by: jimcody1995 <jjimcody@gmail.com> Co-authored-by: James <63717587+jamesrayammons@users.noreply.github.com> Co-authored-by: Yunlu Wen <yunlu.wen@dify.ai> Co-authored-by: Stephen Zhou <hi@hyoban.cc> Co-authored-by: Coding On Star <447357187@qq.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: jerryzai <jerryzh8710@protonmail.com> Co-authored-by: NVIDIAN <speedy.hpc@hotmail.com> Co-authored-by: ai-hpc <ai-hpc@users.noreply.github.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: Junghwan <70629228+shaun0927@users.noreply.github.com> Co-authored-by: HeYinKazune <70251095+HeYin-OS@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: yyh <yuanyouhuilyz@gmail.com> Co-authored-by: Jingyi <jingyi.qi@dify.ai> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com> Co-authored-by: sxxtony <166789813+sxxtony@users.noreply.github.com>
107 lines
4.3 KiB
TypeScript
107 lines
4.3 KiB
TypeScript
import type { FC } from 'react'
|
|
import type { Dataset } from './index'
|
|
|
|
import { useState } from 'react'
|
|
import { useTranslation } from 'react-i18next'
|
|
import {
|
|
PortalToFollowElem,
|
|
PortalToFollowElemContent,
|
|
PortalToFollowElemTrigger,
|
|
} from '@/app/components/base/portal-to-follow-elem'
|
|
import { useEventEmitterContextContext } from '@/context/event-emitter'
|
|
import { UPDATE_DATASETS_EVENT_EMITTER } from '../../constants'
|
|
import { useSelectOrDelete, useTrigger } from '../../hooks'
|
|
import { DELETE_CONTEXT_BLOCK_COMMAND } from './index'
|
|
|
|
type ContextBlockComponentProps = {
|
|
nodeKey: string
|
|
datasets?: Dataset[]
|
|
onAddContext: () => void
|
|
canNotAddContext?: boolean
|
|
}
|
|
|
|
const ContextBlockComponent: FC<ContextBlockComponentProps> = ({
|
|
nodeKey,
|
|
datasets = [],
|
|
onAddContext,
|
|
canNotAddContext,
|
|
}) => {
|
|
const { t } = useTranslation()
|
|
const [ref, isSelected] = useSelectOrDelete(nodeKey, DELETE_CONTEXT_BLOCK_COMMAND)
|
|
const [triggerRef, open, setOpen] = useTrigger()
|
|
const { eventEmitter } = useEventEmitterContextContext()
|
|
const [localDatasets, setLocalDatasets] = useState<Dataset[]>(datasets)
|
|
|
|
eventEmitter?.useSubscription((v: any) => {
|
|
if (v?.type === UPDATE_DATASETS_EVENT_EMITTER)
|
|
setLocalDatasets(v.payload)
|
|
})
|
|
|
|
return (
|
|
<div
|
|
className={`
|
|
group inline-flex h-6 items-center rounded-[5px] border border-transparent bg-[#F4F3FF] pr-0.5 pl-1 text-[#6938EF] hover:bg-[#EBE9FE]
|
|
${open ? 'bg-[#EBE9FE]' : ''}
|
|
${isSelected && 'border-[#9B8AFB]!'}
|
|
`}
|
|
ref={ref}
|
|
>
|
|
<span className="mr-1 i-custom-vender-solid-files-file-05 h-[14px] w-[14px]" data-testid="file-icon" />
|
|
<div className="mr-1 text-xs font-medium">{t('promptEditor.context.item.title', { ns: 'common' })}</div>
|
|
{!canNotAddContext && (
|
|
<PortalToFollowElem
|
|
open={open}
|
|
onOpenChange={setOpen}
|
|
placement="bottom-end"
|
|
offset={{
|
|
mainAxis: 3,
|
|
alignmentAxis: -147,
|
|
}}
|
|
>
|
|
<PortalToFollowElemTrigger ref={triggerRef}>
|
|
<div className={`
|
|
flex h-[18px] w-[18px] cursor-pointer items-center justify-center rounded text-[11px] font-semibold
|
|
${open ? 'bg-[#6938EF] text-white' : 'bg-white/50 group-hover:bg-white group-hover:shadow-xs'}
|
|
`}>
|
|
{localDatasets.length}
|
|
</div>
|
|
</PortalToFollowElemTrigger>
|
|
<PortalToFollowElemContent style={{ zIndex: 100 }}>
|
|
<div className="w-[360px] rounded-xl bg-white shadow-lg">
|
|
<div className="p-4">
|
|
<div className="mb-2 text-xs font-medium text-gray-500">
|
|
{t('promptEditor.context.modal.title', { ns: 'common', num: localDatasets.length })}
|
|
</div>
|
|
<div className="max-h-[270px] overflow-y-auto">
|
|
{
|
|
localDatasets.map(dataset => (
|
|
<div key={dataset.id} className="flex h-8 items-center">
|
|
<div className="mr-2 flex h-6 w-6 shrink-0 items-center justify-center rounded-md border-[0.5px] border-[#EAECF5] bg-[#F5F8FF]">
|
|
<span className="i-custom-vender-solid-files-folder h-4 w-4 text-[#444CE7]" data-testid="folder-icon" />
|
|
</div>
|
|
<div className="truncate text-sm text-gray-800" title="">{dataset.name}</div>
|
|
</div>
|
|
))
|
|
}
|
|
</div>
|
|
<div className="flex h-8 cursor-pointer items-center text-[#155EEF]" onClick={onAddContext}>
|
|
<div className="mr-2 flex h-6 w-6 shrink-0 items-center justify-center rounded-md border-[0.5px] border-gray-100" data-testid="add-button">
|
|
<span className="i-ri-add-line h-[14px] w-[14px]" />
|
|
</div>
|
|
<div className="text-[13px] font-medium" title="">{t('promptEditor.context.modal.add', { ns: 'common' })}</div>
|
|
</div>
|
|
</div>
|
|
<div className="rounded-b-xl border-t-[0.5px] border-gray-50 bg-gray-50 px-4 py-3 text-xs text-gray-500">
|
|
{t('promptEditor.context.modal.footer', { ns: 'common' })}
|
|
</div>
|
|
</div>
|
|
</PortalToFollowElemContent>
|
|
</PortalToFollowElem>
|
|
)}
|
|
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default ContextBlockComponent
|