| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- import {
- memo,
- useCallback,
- useState,
- } from 'react'
- import { useTranslation } from 'react-i18next'
- import { RiArrowDownSLine } from '@remixicon/react'
- import Button from '@/app/components/base/button'
- import Indicator from '@/app/components/header/indicator'
- import cn from '@/utils/classnames'
- import type {
- Credential,
- PluginPayload,
- } from './types'
- import {
- Authorized,
- usePluginAuth,
- } from '.'
-
- type AuthorizedInNodeProps = {
- pluginPayload: PluginPayload
- onAuthorizationItemClick: (id: string) => void
- credentialId?: string
- }
- const AuthorizedInNode = ({
- pluginPayload,
- onAuthorizationItemClick,
- credentialId,
- }: AuthorizedInNodeProps) => {
- const { t } = useTranslation()
- const [isOpen, setIsOpen] = useState(false)
- const {
- canApiKey,
- canOAuth,
- credentials,
- disabled,
- invalidPluginCredentialInfo,
- notAllowCustomCredential,
- } = usePluginAuth(pluginPayload, isOpen || !!credentialId)
- const renderTrigger = useCallback((open?: boolean) => {
- let label = ''
- let removed = false
- let unavailable = false
- let color = 'green'
- if (!credentialId) {
- label = t('plugin.auth.workspaceDefault')
- }
- else {
- const credential = credentials.find(c => c.id === credentialId)
- label = credential ? credential.name : t('plugin.auth.authRemoved')
- removed = !credential
- unavailable = !!credential?.not_allowed_to_use && !credential?.from_enterprise
-
- if (removed)
- color = 'red'
- else if (unavailable)
- color = 'gray'
- }
- return (
- <Button
- size='small'
- className={cn(
- open && !removed && 'bg-components-button-ghost-bg-hover',
- removed && 'bg-transparent text-text-destructive',
- )}
- >
- <Indicator
- className='mr-1.5'
- color={color as any}
- />
- {label}
- {
- unavailable && t('plugin.auth.unavailable')
- }
- <RiArrowDownSLine
- className={cn(
- 'h-3.5 w-3.5 text-components-button-ghost-text',
- removed && 'text-text-destructive',
- )}
- />
- </Button>
- )
- }, [credentialId, credentials, t])
- const extraAuthorizationItems: Credential[] = [
- {
- id: '__workspace_default__',
- name: t('plugin.auth.workspaceDefault'),
- provider: '',
- is_default: !credentialId,
- isWorkspaceDefault: true,
- },
- ]
- const handleAuthorizationItemClick = useCallback((id: string) => {
- onAuthorizationItemClick(id)
- setIsOpen(false)
- }, [
- onAuthorizationItemClick,
- setIsOpen,
- ])
-
- return (
- <Authorized
- pluginPayload={pluginPayload}
- credentials={credentials}
- canOAuth={canOAuth}
- canApiKey={canApiKey}
- renderTrigger={renderTrigger}
- isOpen={isOpen}
- onOpenChange={setIsOpen}
- offset={4}
- placement='bottom-end'
- triggerPopupSameWidth={false}
- popupClassName='w-[360px]'
- disabled={disabled}
- disableSetDefault
- onItemClick={handleAuthorizationItemClick}
- extraAuthorizationItems={extraAuthorizationItems}
- showItemSelectedIcon
- selectedCredentialId={credentialId || '__workspace_default__'}
- onUpdate={invalidPluginCredentialInfo}
- notAllowCustomCredential={notAllowCustomCredential}
- />
- )
- }
-
- export default memo(AuthorizedInNode)
|