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.

3.9 KB
Raw Blame
import {
  AlertDialog,
  AlertDialogBody,
  AlertDialogCancelButton,
  AlertDialogConfirmButton,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
} from '@langgenius/dify-ui/alert-dialog'
import { Field, FieldLabel } from '@langgenius/dify-ui/field'
import { Input } from '@langgenius/dify-ui/input'
import { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { toast } from '@/app/notifications'
import { useDeleteTriggerSubscription } from '@/service/use-triggers'
import { useSubscriptionList } from './use-subscription-list'

type Props = Readonly<{
  onClose: (deleted: boolean) => void
  isShow: boolean
  currentId: string
  currentName: string
  workflowsInUse: number
}>

const tPrefix = 'subscription.list.item.actions.deleteConfirm'

export const DeleteConfirm = (props: Props) => {
  const { onClose, isShow, currentId, currentName, workflowsInUse } = props
  const { refetch } = useSubscriptionList()
  const { mutate: deleteSubscription, isPending: isDeleting } = useDeleteTriggerSubscription({
    silent: true,
  })
  const { t } = useTranslation(['common', 'pluginTrigger'])
  const [inputName, setInputName] = useState('')

  const handleOpenChange = (open: boolean) => {
    if (isDeleting) return

    if (!open) onClose(false)
  }

  const onConfirm = () => {
    if (isDeleting) return

    if (workflowsInUse > 0 && inputName !== currentName) {
      toast.error(t(($) => $[`${tPrefix}.confirmInputWarning`], { ns: 'pluginTrigger' }))
      return
    }
    deleteSubscription(currentId, {
      onSuccess: () => {
        refetch?.()
        onClose(true)
      },
      onError: (error: unknown) => {
        toast.error(
          error instanceof Error
            ? error.message
            : t(($) => $[`${tPrefix}.error`], { ns: 'pluginTrigger', name: currentName }),
        )
      },
    })
  }

  return (
    <AlertDialog open={isShow} onOpenChange={handleOpenChange}>
      <AlertDialogContent backdropProps={{ forceRender: true }}>
        <form
          className="contents"
          onSubmit={(event) => {
            event.preventDefault()
            onConfirm()
          }}
        >
          <AlertDialogHeader>
            <AlertDialogTitle>
              {t(($) => $[`${tPrefix}.title`], { ns: 'pluginTrigger', name: currentName })}
            </AlertDialogTitle>
            <AlertDialogDescription>
              {workflowsInUse > 0
                ? t(($) => $[`${tPrefix}.contentWithApps`], {
                    ns: 'pluginTrigger',
                    count: workflowsInUse,
                  })
                : t(($) => $[`${tPrefix}.content`], { ns: 'pluginTrigger' })}
            </AlertDialogDescription>
          </AlertDialogHeader>
          {workflowsInUse > 0 && (
            <AlertDialogBody>
              <Field name="confirmation">
                <FieldLabel>
                  {t(($) => $[`${tPrefix}.confirmInputTip`], {
                    ns: 'pluginTrigger',
                    name: currentName,
                  })}
                </FieldLabel>
                <Input
                  value={inputName}
                  onChange={(e) => setInputName(e.target.value)}
                  placeholder={t(($) => $[`${tPrefix}.confirmInputPlaceholder`], {
                    ns: 'pluginTrigger',
                    name: currentName,
                  })}
                />
              </Field>
            </AlertDialogBody>
          )}
          <AlertDialogFooter>
            <AlertDialogCancelButton disabled={isDeleting}>
              {t(($) => $['operation.cancel'], { ns: 'common' })}
            </AlertDialogCancelButton>
            <AlertDialogConfirmButton type="submit" loading={isDeleting}>
              {t(($) => $[`${tPrefix}.confirm`], { ns: 'pluginTrigger' })}
            </AlertDialogConfirmButton>
          </AlertDialogFooter>
        </form>
      </AlertDialogContent>
    </AlertDialog>
  )
}