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.

6.6 KB
Raw Blame
import type { FC } from 'react'
import {
  AlertDialog,
  AlertDialogCancelButton,
  AlertDialogConfirmButton,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
} from '@langgenius/dify-ui/alert-dialog'
import { Button } from '@langgenius/dify-ui/button'
import { cn } from '@langgenius/dify-ui/cn'
import { Separator } from '@langgenius/dify-ui/separator'
import {
  RiArchive2Line,
  RiCheckboxCircleLine,
  RiCloseCircleLine,
  RiDeleteBinLine,
  RiDownload2Line,
  RiDraftLine,
  RiRefreshLine,
} from '@remixicon/react'
import { useSuspenseQuery } from '@tanstack/react-query'
import { useBoolean } from 'ahooks'
import * as React from 'react'
import { useTranslation } from 'react-i18next'
import { systemFeaturesQueryOptions } from '@/features/system-features/client'

const i18nPrefix = 'batchAction'
type IBatchActionProps = {
  className?: string
  selectedIds: string[]
  disabled?: boolean
  onBatchEnable?: () => void
  onBatchDisable?: () => void
  onBatchDownload?: () => void
  onBatchDelete?: () => Promise<void>
  onBatchSummary?: () => void
  onArchive?: () => void
  onEditMetadata?: () => void
  onBatchReIndex?: () => void
  onCancel: () => void
}

const BatchAction: FC<IBatchActionProps> = ({
  className,
  selectedIds,
  disabled = false,
  onBatchEnable,
  onBatchDisable,
  onBatchSummary,
  onBatchDownload,
  onArchive,
  onBatchDelete,
  onEditMetadata,
  onBatchReIndex,
  onCancel,
}) => {
  const { t } = useTranslation(['common', 'dataset', 'datasetDocuments'])
  const { data: deploymentEdition } = useSuspenseQuery({
    ...systemFeaturesQueryOptions(),
    select: ({ deployment_edition }) => deployment_edition,
  })
  const isNonCloudEdition = deploymentEdition === 'COMMUNITY' || deploymentEdition === 'ENTERPRISE'
  const [isShowDeleteConfirm, { setTrue: showDeleteConfirm, setFalse: hideDeleteConfirm }] =
    useBoolean(false)
  const [isDeleting, { setTrue: setIsDeleting }] = useBoolean(false)

  const handleBatchDelete = async () => {
    if (!onBatchDelete) return

    setIsDeleting()
    await onBatchDelete()
    hideDeleteConfirm()
  }
  return (
    <div className={cn('pointer-events-none flex w-full justify-center gap-x-2', className)}>
      <div className="pointer-events-auto flex items-center gap-x-1 rounded-[10px] border border-components-actionbar-border-accent bg-components-actionbar-bg-accent p-1 shadow-xl shadow-shadow-shadow-5">
        <div className="inline-flex items-center gap-x-2 py-1 ps-2 pe-3">
          <span className="flex size-5 items-center justify-center rounded-md bg-text-accent system-xs-medium text-text-primary-on-surface">
            {selectedIds.length}
          </span>
          <span className="system-sm-semibold text-text-accent">
            {t(($) => $[`${i18nPrefix}.selected`], { ns: 'dataset' })}
          </span>
        </div>
        <Separator decorative orientation="vertical" className="mx-0.5 h-3.5" />
        {onBatchEnable && (
          <Button variant="ghost" disabled={disabled} onClick={onBatchEnable}>
            <RiCheckboxCircleLine className="size-4" />
            <span>{t(($) => $[`${i18nPrefix}.enable`], { ns: 'dataset' })}</span>
          </Button>
        )}
        {onBatchDisable && (
          <Button variant="ghost" disabled={disabled} onClick={onBatchDisable}>
            <RiCloseCircleLine className="size-4" />
            <span>{t(($) => $[`${i18nPrefix}.disable`], { ns: 'dataset' })}</span>
          </Button>
        )}
        {onEditMetadata && (
          <Button variant="ghost" disabled={disabled} onClick={onEditMetadata}>
            <RiDraftLine className="size-4" />
            <span>{t(($) => $['metadata.metadata'], { ns: 'dataset' })}</span>
          </Button>
        )}
        {onBatchSummary && isNonCloudEdition && (
          <Button variant="ghost" disabled={disabled} onClick={onBatchSummary}>
            <span aria-hidden className="i-custom-vender-knowledge-search-lines-sparkle size-4" />
            <span>{t(($) => $['list.action.summary'], { ns: 'datasetDocuments' })}</span>
          </Button>
        )}
        {onArchive && (
          <Button variant="ghost" disabled={disabled} onClick={onArchive}>
            <RiArchive2Line className="size-4" />
            <span>{t(($) => $[`${i18nPrefix}.archive`], { ns: 'dataset' })}</span>
          </Button>
        )}
        {onBatchReIndex && (
          <Button variant="ghost" disabled={disabled} onClick={onBatchReIndex}>
            <RiRefreshLine className="size-4" />
            <span>{t(($) => $[`${i18nPrefix}.reIndex`], { ns: 'dataset' })}</span>
          </Button>
        )}
        {onBatchDownload && (
          <Button variant="ghost" disabled={disabled} onClick={onBatchDownload}>
            <RiDownload2Line className="size-4" />
            <span>{t(($) => $[`${i18nPrefix}.download`], { ns: 'dataset' })}</span>
          </Button>
        )}
        {onBatchDelete && (
          <Button
            variant="ghost"
            tone="destructive"
            disabled={disabled}
            onClick={showDeleteConfirm}
          >
            <RiDeleteBinLine className="size-4" />
            <span>{t(($) => $[`${i18nPrefix}.delete`], { ns: 'dataset' })}</span>
          </Button>
        )}

        <Separator decorative orientation="vertical" className="mx-0.5 h-3.5" />
        <Button variant="ghost" onClick={onCancel}>
          <span>{t(($) => $[`${i18nPrefix}.cancel`], { ns: 'dataset' })}</span>
        </Button>
      </div>
      {onBatchDelete && (
        <AlertDialog
          open={isShowDeleteConfirm}
          onOpenChange={(open) => !open && hideDeleteConfirm()}
        >
          <AlertDialogContent>
            <AlertDialogHeader>
              <AlertDialogTitle>
                {t(($) => $['list.delete.title'], { ns: 'datasetDocuments' })}
              </AlertDialogTitle>
              <AlertDialogDescription>
                {t(($) => $['list.delete.content'], { ns: 'datasetDocuments' })}
              </AlertDialogDescription>
            </AlertDialogHeader>
            <AlertDialogFooter>
              <AlertDialogCancelButton>
                {t(($) => $['operation.cancel'], { ns: 'common' })}
              </AlertDialogCancelButton>
              <AlertDialogConfirmButton
                disabled={disabled}
                loading={isDeleting}
                onClick={handleBatchDelete}
              >
                {t(($) => $['operation.sure'], { ns: 'common' })}
              </AlertDialogConfirmButton>
            </AlertDialogFooter>
          </AlertDialogContent>
        </AlertDialog>
      )}
    </div>
  )
}

export default React.memo(BatchAction)