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.7 KB
Raw Blame
'use client'
import type { Dependency, Plugin, PluginCategoryEnum, PluginManifestInMarket } from '../../types'
import { cn } from '@langgenius/dify-ui/cn'
import { Dialog, DialogClose, DialogContent, DialogTitle } from '@langgenius/dify-ui/dialog'
import { IconButton } from '@langgenius/dify-ui/icon-button'
import * as React from 'react'
import { useCallback, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { InstallStep } from '../../types'
import Installed from '../base/installed'
import useHideLogic from '../hooks/use-hide-logic'
import useRefreshPluginList from '../hooks/use-refresh-plugin-list'
import ReadyToInstallBundle from '../install-bundle/ready-to-install'
import Install from './steps/install'

const i18nPrefix = 'installModal'

type InstallFromMarketplaceProps = {
  uniqueIdentifier: string
  manifest: PluginManifestInMarket | Plugin
  isBundle?: boolean
  dependencies?: Dependency[]
  installContextCategory?: PluginCategoryEnum
  onSuccess: () => void
  onClose: () => void
}

const InstallFromMarketplace: React.FC<InstallFromMarketplaceProps> = ({
  uniqueIdentifier,
  manifest,
  isBundle,
  dependencies,
  installContextCategory,
  onSuccess,
  onClose,
}) => {
  const { t } = useTranslation(['common', 'plugin'])
  // readyToInstall -> check installed -> installed/failed
  const [step, setStep] = useState<InstallStep>(InstallStep.readyToInstall)
  const [errorMsg, setErrorMsg] = useState<string | null>(null)
  const { refreshPluginList } = useRefreshPluginList()

  const {
    modalClassName,
    foldAnimInto,
    foldIntoTaskTrigger,
    setIsInstalling,
    handleStartToInstall,
  } = useHideLogic(onClose)

  const getTitle = useCallback(() => {
    if (isBundle && step === InstallStep.installed)
      return t(($) => $[`${i18nPrefix}.installedSuccessfully`], { ns: 'plugin' })
    if (step === InstallStep.installed)
      return t(($) => $[`${i18nPrefix}.installedSuccessfully`], { ns: 'plugin' })
    if (step === InstallStep.installFailed)
      return t(($) => $[`${i18nPrefix}.installFailed`], { ns: 'plugin' })
    return t(($) => $[`${i18nPrefix}.installPlugin`], { ns: 'plugin' })
  }, [isBundle, step, t])

  const handleInstalled = useCallback(
    (notRefresh?: boolean) => {
      setStep(InstallStep.installed)
      if (!notRefresh) refreshPluginList(manifest)
      setIsInstalling(false)
    },
    [manifest, refreshPluginList, setIsInstalling],
  )

  const handleFailed = useCallback(
    (errorMsg?: string) => {
      setStep(InstallStep.installFailed)
      setIsInstalling(false)
      if (errorMsg) setErrorMsg(errorMsg)
    },
    [setIsInstalling],
  )

  const completedSteps: InstallStep[] = [InstallStep.installed, InstallStep.installFailed]

  return (
    <Dialog
      open
      onOpenChange={(open) => {
        if (!open) foldAnimInto()
      }}
    >
      <DialogContent
        backdropProps={{ forceRender: true }}
        className={cn('max-h-[calc(100dvh-3rem)] w-140', modalClassName)}
      >
        <div className="flex shrink-0 items-start gap-2 self-stretch ps-6 pe-14 pt-6 pb-3">
          <DialogTitle>{getTitle()}</DialogTitle>
        </div>
        {isBundle ? (
          <ReadyToInstallBundle
            step={step}
            onStepChange={setStep}
            onStartToInstall={handleStartToInstall}
            setIsInstalling={setIsInstalling}
            onClose={onClose}
            allPlugins={dependencies!}
            isFromMarketPlace
          />
        ) : (
          <>
            {step === InstallStep.readyToInstall && (
              <Install
                uniqueIdentifier={uniqueIdentifier}
                payload={manifest!}
                onCancel={onClose}
                onInstalled={handleInstalled}
                onFailed={handleFailed}
                onStartToInstall={handleStartToInstall}
                onTaskStarted={foldIntoTaskTrigger}
              />
            )}
            {completedSteps.includes(step) && (
              <Installed
                payload={manifest!}
                isMarketPayload
                isFailed={step === InstallStep.installFailed}
                errMsg={errorMsg}
                installContextCategory={installContextCategory}
                onCancel={onSuccess}
              />
            )}
          </>
        )}
        <DialogClose
          render={
            <IconButton
              aria-label={t(($) => $['operation.close'], { ns: 'common' })}
              size="lg"
              className="absolute inset-e-6 top-6"
            >
              <span aria-hidden className="i-ri-close-line size-4" />
            </IconButton>
          }
        />
      </DialogContent>
    </Dialog>
  )
}

export default InstallFromMarketplace