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.

5.7 KB
Raw Blame
'use client'
import type {
  TriggerOAuthConfig,
  TriggerSubscriptionBuilder,
} from '@/app/components/workflow/block-selector/types'
import { Button } from '@langgenius/dify-ui/button'
import {
  Dialog,
  DialogBody,
  DialogContent,
  DialogFooter,
  DialogHeader,
  DialogHeaderCloseButton,
  DialogTitle,
} from '@langgenius/dify-ui/dialog'
import { useCallback } from 'react'
import { useTranslation } from 'react-i18next'
import { BaseForm } from '@/app/components/base/form/components/base'
import OptionCard from '@/app/components/workflow/nodes/_base/components/option-card'
import { toast } from '@/app/notifications'
import { usePluginStore } from '../../store'
import {
  ClientTypeEnum,
  useOAuthClientState as useOAuthClientSettings,
} from './hooks/use-oauth-client-state'

type Props = Readonly<{
  open: boolean
  oauthConfig?: TriggerOAuthConfig
  onOpenChange: (open: boolean) => void
  showOAuthCreateModal: (builder: TriggerSubscriptionBuilder) => void
}>

const CLIENT_TYPE_OPTIONS = [ClientTypeEnum.Default, ClientTypeEnum.Custom] as const

export const OAuthClientSettingsModal = ({
  open,
  oauthConfig,
  onOpenChange,
  showOAuthCreateModal,
}: Props) => {
  const { t } = useTranslation(['common', 'plugin', 'pluginTrigger'])
  const detail = usePluginStore((state) => state.detail)
  const providerName = detail?.provider || ''
  const closeModal = useCallback(() => onOpenChange(false), [onOpenChange])

  const oauthClientSettings = useOAuthClientSettings({
    oauthConfig,
    providerName,
    onOpenChange,
    showOAuthCreateModal,
  })
  const {
    clientType,
    setClientType,
    clientFormRef,
    oauthClientSchema,
    confirmButtonText,
    handleRemove,
    handleSave,
  } = oauthClientSettings

  const isCustomClient = clientType === ClientTypeEnum.Custom
  const showRemoveButton = oauthConfig?.custom_enabled && oauthConfig?.params && isCustomClient
  const showRedirectInfo = isCustomClient && oauthConfig?.redirect_uri
  const showClientForm = isCustomClient && oauthClientSchema.length > 0

  const handleCopyRedirectUri = () => {
    navigator.clipboard.writeText(oauthConfig?.redirect_uri || '')
    toast.success(t(($) => $['actionMsg.copySuccessfully'], { ns: 'common' }))
  }

  const title = t(($) => $['modal.oauth.title'], { ns: 'pluginTrigger' })

  return (
    <Dialog open={open} disablePointerDismissal onOpenChange={onOpenChange}>
      <DialogContent backdropProps={{ forceRender: true }}>
        <DialogHeader>
          <DialogTitle>{title}</DialogTitle>
          <DialogHeaderCloseButton aria-label={t(($) => $['operation.close'], { ns: 'common' })} />
        </DialogHeader>
        <DialogBody>
          <div className="mb-2 system-sm-medium text-text-secondary">
            {t(($) => $['subscription.addType.options.oauth.clientTitle'], {
              ns: 'pluginTrigger',
            })}
          </div>

          {oauthConfig?.system_configured && (
            <div className="mb-4 flex w-full items-start justify-between gap-2">
              {CLIENT_TYPE_OPTIONS.map((option) => (
                <OptionCard
                  key={option}
                  title={t(($) => $[`subscription.addType.options.oauth.${option}`], {
                    ns: 'pluginTrigger',
                  })}
                  onSelect={() => setClientType(option)}
                  selected={clientType === option}
                  className="flex-1"
                />
              ))}
            </div>
          )}

          {showRedirectInfo && (
            <div className="mb-4 flex items-start gap-3 rounded-xl bg-background-section-burn p-4">
              <div className="rounded-lg border-[0.5px] border-components-card-border bg-components-card-bg p-2 shadow-xs shadow-shadow-shadow-3">
                <span
                  aria-hidden="true"
                  className="i-ri-information-2-fill size-5 shrink-0 text-text-accent"
                />
              </div>
              <div className="min-w-0 flex-1 text-text-secondary">
                <div className="system-sm-regular leading-4 whitespace-pre-wrap">
                  {t(($) => $['modal.oauthRedirectInfo'], { ns: 'pluginTrigger' })}
                </div>
                <div className="my-1.5 system-sm-medium leading-4 break-all">
                  {oauthConfig?.redirect_uri}
                </div>
                <Button variant="secondary" size="small" onClick={handleCopyRedirectUri}>
                  <span aria-hidden="true" className="i-ri-clipboard-line h-3.5 w-3.5" />
                  {t(($) => $['operation.copy'], { ns: 'common' })}
                </Button>
              </div>
            </div>
          )}

          {showClientForm && (
            <BaseForm
              formSchemas={oauthClientSchema}
              ref={clientFormRef}
              labelClassName="system-sm-medium mb-2 block text-text-secondary"
              formClassName="space-y-4"
            />
          )}
        </DialogBody>
        <DialogFooter>
          {showRemoveButton && (
            <Button
              variant="secondary"
              className="me-auto text-components-button-destructive-secondary-text"
              onClick={handleRemove}
            >
              {t(($) => $['operation.remove'], { ns: 'common' })}
            </Button>
          )}
          <Button variant="secondary" onClick={closeModal}>
            {t(($) => $['operation.cancel'], { ns: 'common' })}
          </Button>
          <div className="mx-1 h-4 w-px bg-divider-regular"></div>
          <Button onClick={() => handleSave(false)}>
            {t(($) => $['auth.saveOnly'], { ns: 'plugin' })}
          </Button>
          <Button variant="primary" onClick={() => handleSave(true)}>
            {confirmButtonText}
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  )
}