import { memo, useCallback, useMemo, useState, } from 'react' import { useTranslation } from 'react-i18next' import { RiClipboardLine, RiEqualizer2Line, RiInformation2Fill, } from '@remixicon/react' import Button from '@/app/components/base/button' import type { ButtonProps } from '@/app/components/base/button' import OAuthClientSettings from './oauth-client-settings' import cn from '@/utils/classnames' import type { PluginPayload } from '../types' import { openOAuthPopup } from '@/hooks/use-oauth' import Badge from '@/app/components/base/badge' import { useGetPluginOAuthClientSchemaHook, useGetPluginOAuthUrlHook, } from '../hooks/use-credential' import type { FormSchema } from '@/app/components/base/form/types' import { FormTypeEnum } from '@/app/components/base/form/types' import ActionButton from '@/app/components/base/action-button' import { useRenderI18nObject } from '@/hooks/use-i18n' export type AddOAuthButtonProps = { pluginPayload: PluginPayload buttonVariant?: ButtonProps['variant'] buttonText?: string className?: string buttonLeftClassName?: string buttonRightClassName?: string dividerClassName?: string disabled?: boolean onUpdate?: () => void oAuthData?: { schema?: FormSchema[] is_oauth_custom_client_enabled?: boolean is_system_oauth_params_exists?: boolean client_params?: Record redirect_uri?: string } } const AddOAuthButton = ({ pluginPayload, buttonVariant = 'primary', buttonText = 'use oauth', className, buttonLeftClassName, buttonRightClassName, dividerClassName, disabled, onUpdate, oAuthData, }: AddOAuthButtonProps) => { const { t } = useTranslation() const renderI18nObject = useRenderI18nObject() const [isOAuthSettingsOpen, setIsOAuthSettingsOpen] = useState(false) const { mutateAsync: getPluginOAuthUrl } = useGetPluginOAuthUrlHook(pluginPayload) const { data, isLoading } = useGetPluginOAuthClientSchemaHook(pluginPayload) const mergedOAuthData = useMemo(() => { if (oAuthData) return oAuthData return data }, [oAuthData, data]) const { schema = [], is_oauth_custom_client_enabled, is_system_oauth_params_exists, client_params, redirect_uri, } = mergedOAuthData as any const isConfigured = is_system_oauth_params_exists || is_oauth_custom_client_enabled const handleOAuth = useCallback(async () => { const { authorization_url } = await getPluginOAuthUrl() if (authorization_url) { openOAuthPopup( authorization_url, () => onUpdate?.(), ) } }, [getPluginOAuthUrl, onUpdate]) const renderCustomLabel = useCallback((item: FormSchema) => { return (
{t('plugin.auth.clientInfo')}
{ redirect_uri && (
{redirect_uri}
{ navigator.clipboard.writeText(redirect_uri || '') }} >
) }
{renderI18nObject(item.label as Record)} { item.required && ( * ) }
) }, [t, redirect_uri, renderI18nObject]) const memorizedSchemas = useMemo(() => { const result: FormSchema[] = (schema as FormSchema[]).map((item, index) => { return { ...item, label: index === 0 ? renderCustomLabel(item) : item.label, labelClassName: index === 0 ? 'h-auto' : undefined, } }) if (is_system_oauth_params_exists) { result.unshift({ name: '__oauth_client__', label: t('plugin.auth.oauthClient'), type: FormTypeEnum.radio, options: [ { label: t('plugin.auth.default'), value: 'default', }, { label: t('plugin.auth.custom'), value: 'custom', }, ], required: false, default: is_oauth_custom_client_enabled ? 'custom' : 'default', } as FormSchema) result.forEach((item, index) => { if (index > 0) { item.show_on = [ { variable: '__oauth_client__', value: 'custom', }, ] if (client_params) item.default = client_params[item.name] || item.default } }) } return result }, [schema, renderCustomLabel, t, is_system_oauth_params_exists, is_oauth_custom_client_enabled, client_params]) const __auth_client__ = useMemo(() => { if (isConfigured) { if (is_oauth_custom_client_enabled) return 'custom' return 'default' } else { if (is_system_oauth_params_exists) return 'default' return 'custom' } }, [isConfigured, is_oauth_custom_client_enabled, is_system_oauth_params_exists]) return ( <> { isConfigured && ( ) } { !isConfigured && ( ) } { isOAuthSettingsOpen && ( setIsOAuthSettingsOpen(false)} disabled={disabled || isLoading} schemas={memorizedSchemas} onAuth={handleOAuth} editValues={{ ...client_params, __oauth_client__: __auth_client__, }} hasOriginalClientParams={Object.keys(client_params || {}).length > 0} onUpdate={onUpdate} /> ) } ) } export default memo(AddOAuthButton)