mirror of
https://github.com/langgenius/dify.git
synced 2026-06-23 12:31:13 +08:00
Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: fatelei <fatelei@gmail.com> Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: 盐粒 Yanli <yanli@dify.ai> Co-authored-by: Charles Yao <chongbinyao33@gmail.com> Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com> Co-authored-by: yunlu.wen <yunlu.wen@dify.ai> Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com> Co-authored-by: Jingyi <jingyi.qi@dify.ai> Co-authored-by: yyh <yuanyouhuilyz@gmail.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: hjlarry <hjlarry@163.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Xiyuan Chen <52963600+GareArc@users.noreply.github.com> Co-authored-by: gigglewang <gigglewang@dify.ai> Co-authored-by: chariri <w@chariri.moe> Co-authored-by: Evan <2869018789@qq.com> Co-authored-by: zyssyz123 <916125788@qq.com>
158 lines
4.9 KiB
TypeScript
158 lines
4.9 KiB
TypeScript
import type { PluginPayload } from '../types'
|
|
import type { AddApiKeyButtonProps } from './add-api-key-button'
|
|
import type { AddOAuthButtonProps } from './add-oauth-button'
|
|
import { cn } from '@langgenius/dify-ui/cn'
|
|
import { Tooltip, TooltipContent, TooltipTrigger } from '@langgenius/dify-ui/tooltip'
|
|
import {
|
|
memo,
|
|
useMemo,
|
|
} from 'react'
|
|
import { useTranslation } from 'react-i18next'
|
|
import { useSelector as useAppContextWithSelector } from '@/context/app-context'
|
|
import { hasPermission } from '@/utils/permission'
|
|
import AddApiKeyButton from './add-api-key-button'
|
|
import AddOAuthButton from './add-oauth-button'
|
|
|
|
type AuthorizeProps = {
|
|
pluginPayload: PluginPayload
|
|
theme?: 'primary' | 'secondary'
|
|
showDivider?: boolean
|
|
canOAuth?: boolean
|
|
canApiKey?: boolean
|
|
onUpdate?: () => void
|
|
notAllowCustomCredential?: boolean
|
|
/**
|
|
* If provided, the API-key button delegates modal-opening to the parent
|
|
* instead of rendering it inline. Used when this Authorize is mounted
|
|
* inside a Popover whose outside-click handler would otherwise unmount
|
|
* the modal.
|
|
*/
|
|
onApiKeyClick?: () => void
|
|
}
|
|
const Authorize = ({
|
|
pluginPayload,
|
|
theme = 'primary',
|
|
showDivider = true,
|
|
canOAuth,
|
|
canApiKey,
|
|
onUpdate,
|
|
notAllowCustomCredential,
|
|
onApiKeyClick,
|
|
}: AuthorizeProps) => {
|
|
const { t } = useTranslation()
|
|
const workspacePermissionKeys = useAppContextWithSelector(s => s.workspacePermissionKeys)
|
|
const canManageCredential = hasPermission(workspacePermissionKeys, 'credential.manage')
|
|
|
|
const oAuthButtonProps: AddOAuthButtonProps = useMemo(() => {
|
|
if (theme === 'secondary') {
|
|
return {
|
|
buttonText: !canApiKey ? t('auth.useOAuthAuth', { ns: 'plugin' }) : t('auth.addOAuth', { ns: 'plugin' }),
|
|
buttonVariant: 'secondary',
|
|
className: 'hover:bg-components-button-secondary-bg',
|
|
buttonLeftClassName: 'hover:bg-components-button-secondary-bg-hover',
|
|
buttonRightClassName: 'hover:bg-components-button-secondary-bg-hover',
|
|
dividerClassName: 'bg-divider-regular opacity-100',
|
|
pluginPayload,
|
|
}
|
|
}
|
|
|
|
return {
|
|
buttonText: !canApiKey ? t('auth.useOAuthAuth', { ns: 'plugin' }) : t('auth.addOAuth', { ns: 'plugin' }),
|
|
pluginPayload,
|
|
}
|
|
}, [canApiKey, theme, pluginPayload, t])
|
|
|
|
const apiKeyButtonProps: AddApiKeyButtonProps = useMemo(() => {
|
|
if (theme === 'secondary') {
|
|
return {
|
|
pluginPayload,
|
|
buttonVariant: 'secondary',
|
|
buttonText: !canOAuth ? t('auth.useApiAuth', { ns: 'plugin' }) : t('auth.addApi', { ns: 'plugin' }),
|
|
onClick: onApiKeyClick,
|
|
}
|
|
}
|
|
return {
|
|
pluginPayload,
|
|
buttonText: !canOAuth ? t('auth.useApiAuth', { ns: 'plugin' }) : t('auth.addApi', { ns: 'plugin' }),
|
|
buttonVariant: !canOAuth ? 'primary' : 'secondary-accent',
|
|
onClick: onApiKeyClick,
|
|
}
|
|
}, [canOAuth, theme, pluginPayload, t, onApiKeyClick])
|
|
|
|
const OAuthButton = useMemo(() => {
|
|
const Item = (
|
|
<div className={cn('min-w-0 flex-1', notAllowCustomCredential && 'opacity-50')}>
|
|
<AddOAuthButton
|
|
{...oAuthButtonProps}
|
|
disabled={!canManageCredential || notAllowCustomCredential}
|
|
onUpdate={onUpdate}
|
|
/>
|
|
</div>
|
|
)
|
|
|
|
if (notAllowCustomCredential) {
|
|
return (
|
|
<Tooltip>
|
|
<TooltipTrigger render={Item} />
|
|
<TooltipContent>
|
|
{t('auth.credentialUnavailable', { ns: 'plugin' })}
|
|
</TooltipContent>
|
|
</Tooltip>
|
|
)
|
|
}
|
|
return Item
|
|
}, [notAllowCustomCredential, oAuthButtonProps, canManageCredential, onUpdate, t])
|
|
|
|
const ApiKeyButton = useMemo(() => {
|
|
const Item = (
|
|
<div className={cn('min-w-0 flex-1', notAllowCustomCredential && 'opacity-50')}>
|
|
<AddApiKeyButton
|
|
{...apiKeyButtonProps}
|
|
disabled={!canManageCredential || notAllowCustomCredential}
|
|
onUpdate={onUpdate}
|
|
/>
|
|
</div>
|
|
)
|
|
|
|
if (notAllowCustomCredential) {
|
|
return (
|
|
<Tooltip>
|
|
<TooltipTrigger render={Item} />
|
|
<TooltipContent>
|
|
{t('auth.credentialUnavailable', { ns: 'plugin' })}
|
|
</TooltipContent>
|
|
</Tooltip>
|
|
)
|
|
}
|
|
return Item
|
|
}, [notAllowCustomCredential, apiKeyButtonProps, canManageCredential, onUpdate, t])
|
|
|
|
return (
|
|
<>
|
|
<div className="flex items-center space-x-1.5">
|
|
{
|
|
canOAuth && (
|
|
OAuthButton
|
|
)
|
|
}
|
|
{
|
|
showDivider && canOAuth && canApiKey && (
|
|
<div className="flex shrink-0 flex-col items-center justify-between system-2xs-medium-uppercase text-text-tertiary">
|
|
<div className="h-2 w-px bg-divider-subtle"></div>
|
|
or
|
|
<div className="h-2 w-px bg-divider-subtle"></div>
|
|
</div>
|
|
)
|
|
}
|
|
{
|
|
canApiKey && (
|
|
ApiKeyButton
|
|
)
|
|
}
|
|
</div>
|
|
</>
|
|
)
|
|
}
|
|
|
|
export default memo(Authorize)
|