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.9 KB
Raw Blame
import type { StatusDotStatus } from '@langgenius/dify-ui/status-dot'
import type { ComponentPropsWithRef } from 'react'
import type { Credential, PluginPayload } from './types'
import { Button } from '@langgenius/dify-ui/button'
import { cn } from '@langgenius/dify-ui/cn'
import { StatusDot } from '@langgenius/dify-ui/status-dot'
import { RiArrowDownSLine } from '@remixicon/react'
import { memo, useCallback, useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { Authorized, usePluginAuth } from '.'

type AuthorizedInNodeProps = {
  pluginPayload: PluginPayload
  onAuthorizationItemClick: (id: string) => void
  credentialId?: string
  onDefaultCredentialChange?: (id?: string) => void
}
const AuthorizedInNode = ({
  pluginPayload,
  onAuthorizationItemClick,
  credentialId,
  onDefaultCredentialChange,
}: AuthorizedInNodeProps) => {
  const { t } = useTranslation(['common', 'plugin'])
  const [isOpen, setIsOpen] = useState(false)
  const {
    canApiKey,
    canOAuth,
    credentials,
    isLoading,
    invalidPluginCredentialInfo,
    notAllowCustomCredential,
  } = usePluginAuth(pluginPayload, true, credentialId ? [credentialId] : undefined)
  const defaultCredentialId = credentials.find((c) => c.is_default)?.id

  useEffect(() => {
    onDefaultCredentialChange?.(defaultCredentialId)
  }, [defaultCredentialId, onDefaultCredentialChange])

  const renderTrigger = useCallback(
    (open: boolean, triggerProps: ComponentPropsWithRef<'button'>) => {
      let label = ''
      let removed = false
      let unavailable = false
      let color: StatusDotStatus = 'success'
      let defaultUnavailable = false
      if (!credentialId) {
        label = t(($) => $['auth.workspaceDefault'], { ns: 'plugin' })

        const defaultCredential = credentials.find((c) => c.is_default)

        if (isLoading) {
          color = 'disabled'
        } else if (!defaultCredential) {
          color = 'error'
          defaultUnavailable = true
        } else if (defaultCredential.not_allowed_to_use) {
          color = 'disabled'
          defaultUnavailable = true
        }
      } else {
        const credential = credentials.find((c) => c.id === credentialId)
        if (credential) label = credential.name
        else if (isLoading) {
          label = t(($) => $.loading, { ns: 'common' })
          color = 'disabled'
        } else {
          label = t(($) => $['auth.authRemoved'], { ns: 'plugin' })
          removed = true
        }
        unavailable = !!credential?.not_allowed_to_use && !credential?.from_enterprise

        if (removed) color = 'error'
        else if (unavailable) color = 'disabled'
      }
      return (
        <Button
          {...triggerProps}
          size="small"
          className={cn(
            triggerProps.className,
            open && !removed && 'bg-components-button-ghost-bg-hover',
            removed && 'bg-transparent text-text-destructive',
          )}
          variant={defaultUnavailable || unavailable ? 'ghost' : 'secondary'}
        >
          <StatusDot status={color} />
          {label}
          {(unavailable || defaultUnavailable) && (
            <>
              &nbsp;
              {t(($) => $['auth.unavailable'], { ns: 'plugin' })}
            </>
          )}
          <RiArrowDownSLine
            className={cn(
              'size-3.5 text-components-button-ghost-text',
              removed && 'text-text-destructive',
            )}
          />
        </Button>
      )
    },
    [credentialId, credentials, isLoading, t],
  )
  const defaultUnavailable = credentials.find((c) => c.is_default)?.not_allowed_to_use
  const extraAuthorizationItems: Credential[] = [
    {
      id: '__workspace_default__',
      name: t(($) => $['auth.workspaceDefault'], { ns: 'plugin' }),
      provider: '',
      is_default: !credentialId,
      isWorkspaceDefault: true,
      not_allowed_to_use: defaultUnavailable,
    },
  ]
  const handleAuthorizationItemClick = useCallback(
    (id: string) => {
      onAuthorizationItemClick(
        id === '__workspace_default__' && onDefaultCredentialChange
          ? defaultCredentialId || id
          : id,
      )
      setIsOpen(false)
    },
    [defaultCredentialId, onDefaultCredentialChange, onAuthorizationItemClick, setIsOpen],
  )

  return (
    <Authorized
      pluginPayload={pluginPayload}
      credentials={credentials}
      canOAuth={canOAuth}
      canApiKey={canApiKey}
      renderTrigger={renderTrigger}
      isOpen={isOpen}
      onOpenChange={setIsOpen}
      sideOffset={4}
      align="end"
      triggerPopupSameWidth={false}
      popupClassName="w-[360px]"
      disableSetDefault
      onItemClick={handleAuthorizationItemClick}
      extraAuthorizationItems={extraAuthorizationItems}
      showItemSelectedIcon
      selectedCredentialId={credentialId || '__workspace_default__'}
      onUpdate={invalidPluginCredentialInfo}
      notAllowCustomCredential={notAllowCustomCredential}
    />
  )
}

export default memo(AuthorizedInNode)