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.

7.3 KB
Raw Blame
'use client'
import type { DatasetUpdatePayload } from '@dify/contracts/api/console/datasets/types.gen'
import type { IconPickerValue } from '@/app/components/base/icon-picker'
import { Button } from '@langgenius/dify-ui/button'
import {
  Dialog,
  DialogBody,
  DialogContent,
  DialogFooter,
  DialogHeader,
  DialogHeaderCloseButton,
  DialogTitle,
} from '@langgenius/dify-ui/dialog'
import { Field, FieldLabel } from '@langgenius/dify-ui/field'
import { Input } from '@langgenius/dify-ui/input'
import { Textarea } from '@langgenius/dify-ui/textarea'
import { useCallback, useState } from 'react'
import { useTranslation } from 'react-i18next'
import {
  IconPicker,
  IconPickerContent,
  IconPickerIcon,
  IconPickerTrigger,
} from '@/app/components/base/icon-picker'
import { toast } from '@/app/notifications'
import { updateDatasetSetting } from '@/service/datasets'
import { getErrorMessage } from '@/utils/error-message'

type RenameDatasetModalProps = {
  open: boolean
  dataset: {
    description?: string | null
    external_knowledge_info?: {
      external_knowledge_api_id?: string | null
      external_knowledge_id?: string | null
    } | null
    icon_info?: {
      icon?: string | null
      icon_background?: string | null
      icon_type?: string | null
      icon_url?: string | null
    } | null
    id: string
    name: string
  }
  onSuccess?: () => void
  onOpenChange: (open: boolean) => void
}
const RenameDatasetModal = ({
  open,
  onOpenChange,
  dataset,
  onSuccess,
}: RenameDatasetModalProps) => {
  const [loading, setLoading] = useState(false)
  return (
    <Dialog
      open={open}
      onOpenChange={(nextOpen, details) => {
        if (!nextOpen && loading) {
          details.cancel()
          return
        }
        onOpenChange(nextOpen)
      }}
    >
      <DialogContent className="w-130">
        <RenameDatasetForm
          dataset={dataset}
          loading={loading}
          onLoadingChange={setLoading}
          onSuccess={onSuccess}
          onClose={() => onOpenChange(false)}
        />
      </DialogContent>
    </Dialog>
  )
}

function RenameDatasetForm({
  dataset,
  onSuccess,
  onClose,
  loading,
  onLoadingChange,
}: Pick<RenameDatasetModalProps, 'dataset' | 'onSuccess'> & {
  onClose: () => void
  loading: boolean
  onLoadingChange: (loading: boolean) => void
}) {
  const { t } = useTranslation(['common', 'datasetSettings'])
  const [name, setName] = useState<string>(dataset.name)
  const [description, setDescription] = useState(dataset.description ?? '')
  const externalKnowledgeId = dataset.external_knowledge_info?.external_knowledge_id
  const externalKnowledgeApiId = dataset.external_knowledge_info?.external_knowledge_api_id
  const [appIcon, setAppIcon] = useState<IconPickerValue>(
    dataset.icon_info?.icon_type === 'image'
      ? {
          type: 'image' as const,
          url: dataset.icon_info?.icon_url || '',
          fileId: dataset.icon_info?.icon || '',
        }
      : {
          type: 'emoji' as const,
          icon: dataset.icon_info?.icon || '',
          background: dataset.icon_info?.icon_background || '',
        },
  )
  const handleSelectAppIcon = useCallback((icon: IconPickerValue) => {
    setAppIcon(icon)
  }, [])
  const onConfirm = useCallback(async () => {
    if (loading) return
    if (!name.trim()) {
      toast.error(t(($) => $['form.nameError'], { ns: 'datasetSettings' }))
      return
    }
    try {
      onLoadingChange(true)
      const body: DatasetUpdatePayload = {
        name,
        description,
        icon_info: {
          icon: appIcon.type === 'image' ? appIcon.fileId : appIcon.icon,
          icon_type: appIcon.type,
          icon_background: appIcon.type === 'image' ? undefined : appIcon.background,
          icon_url: appIcon.type === 'image' ? appIcon.url : undefined,
        },
      }
      if (externalKnowledgeId && externalKnowledgeApiId) {
        body.external_knowledge_id = externalKnowledgeId
        body.external_knowledge_api_id = externalKnowledgeApiId
      }
      await updateDatasetSetting(
        {
          datasetId: dataset.id,
          body,
        },
        { silent: true },
      )
      if (onSuccess) onSuccess()
      onClose()
    } catch (error) {
      toast.error(
        getErrorMessage(error) ?? t(($) => $['actionMsg.modifiedUnsuccessfully'], { ns: 'common' }),
      )
    } finally {
      onLoadingChange(false)
    }
  }, [
    appIcon,
    description,
    dataset.id,
    externalKnowledgeApiId,
    externalKnowledgeId,
    name,
    loading,
    onLoadingChange,
    onClose,
    onSuccess,
    t,
  ])
  return (
    <form
      className="contents"
      onSubmit={(event) => {
        event.preventDefault()
        onConfirm()
      }}
    >
      <DialogHeader>
        <DialogTitle>{t(($) => $.title, { ns: 'datasetSettings' })}</DialogTitle>
        <DialogHeaderCloseButton
          disabled={loading}
          aria-label={t(($) => $['operation.close'], { ns: 'common' })}
        />
      </DialogHeader>
      <DialogBody>
        <IconPicker value={appIcon} onValueChange={handleSelectAppIcon}>
          <Field name="name" className="gap-0 py-4">
            <FieldLabel className="w-full shrink-0 py-2 text-sm leading-5 font-medium text-text-primary">
              {t(($) => $['form.name'], { ns: 'datasetSettings' })}
            </FieldLabel>
            <div className="flex items-center gap-x-2">
              <IconPickerTrigger
                aria-label={`${t(($) => $['operation.edit'], { ns: 'common' })} ${t(($) => $['form.nameAndIcon'], { ns: 'datasetSettings' })}`}
                className="group/edit-icon relative shrink-0 cursor-pointer rounded-[10px] border-0 bg-transparent p-0"
              >
                <IconPickerIcon size="medium" />
                <span
                  aria-hidden="true"
                  className="pointer-events-none absolute inset-0 hidden items-center justify-center rounded-[inherit] bg-background-overlay-alt group-hover/edit-icon:flex group-focus-visible/edit-icon:flex"
                >
                  <span className="i-ri-edit-line size-5.5 text-text-primary-on-surface" />
                </span>
              </IconPickerTrigger>
              <Input
                value={name}
                onChange={(e) => setName(e.target.value)}
                className="h-9 grow"
                placeholder={t(($) => $['form.namePlaceholder'], { ns: 'datasetSettings' })}
              />
            </div>
          </Field>
          <IconPickerContent />
        </IconPicker>
        <Field name="description" className="gap-0 py-4">
          <FieldLabel className="w-full shrink-0 py-2 text-sm leading-5 font-medium text-text-primary">
            {t(($) => $['form.desc'], { ns: 'datasetSettings' })}
          </FieldLabel>
          <Textarea
            value={description}
            onValueChange={(value) => setDescription(value)}
            className="resize-none"
            placeholder={t(($) => $['form.descPlaceholder'], { ns: 'datasetSettings' })}
          />
        </Field>
      </DialogBody>
      <DialogFooter>
        <Button type="button" disabled={loading} onClick={onClose}>
          {t(($) => $['operation.cancel'], { ns: 'common' })}
        </Button>
        <Button type="submit" loading={loading} variant="primary">
          {t(($) => $['operation.save'], { ns: 'common' })}
        </Button>
      </DialogFooter>
    </form>
  )
}
export default RenameDatasetModal