fix: style

feat/tool-oauth
zxhlyh 10 months ago
parent 3f65117007
commit 686b4b8e0e

@ -12,12 +12,14 @@ export type AddApiKeyButtonProps = {
buttonVariant?: ButtonProps['variant'] buttonVariant?: ButtonProps['variant']
buttonText?: string buttonText?: string
disabled?: boolean disabled?: boolean
onUpdate?: () => void
} }
const AddApiKeyButton = ({ const AddApiKeyButton = ({
pluginPayload, pluginPayload,
buttonVariant = 'secondary-accent', buttonVariant = 'secondary-accent',
buttonText = 'use api key', buttonText = 'use api key',
disabled, disabled,
onUpdate,
}: AddApiKeyButtonProps) => { }: AddApiKeyButtonProps) => {
const [isApiKeyModalOpen, setIsApiKeyModalOpen] = useState(false) const [isApiKeyModalOpen, setIsApiKeyModalOpen] = useState(false)
@ -36,6 +38,7 @@ const AddApiKeyButton = ({
<ApiKeyModal <ApiKeyModal
pluginPayload={pluginPayload} pluginPayload={pluginPayload}
onClose={() => setIsApiKeyModalOpen(false)} onClose={() => setIsApiKeyModalOpen(false)}
onUpdate={onUpdate}
/> />
) )
} }

@ -21,7 +21,6 @@ import Badge from '@/app/components/base/badge'
import { import {
useGetPluginOAuthClientSchemaHook, useGetPluginOAuthClientSchemaHook,
useGetPluginOAuthUrlHook, useGetPluginOAuthUrlHook,
useInvalidPluginCredentialInfoHook,
} from '../hooks/use-credential' } from '../hooks/use-credential'
import type { FormSchema } from '@/app/components/base/form/types' import type { FormSchema } from '@/app/components/base/form/types'
import { FormTypeEnum } from '@/app/components/base/form/types' import { FormTypeEnum } from '@/app/components/base/form/types'
@ -37,6 +36,7 @@ export type AddOAuthButtonProps = {
buttonRightClassName?: string buttonRightClassName?: string
dividerClassName?: string dividerClassName?: string
disabled?: boolean disabled?: boolean
onUpdate?: () => void
} }
const AddOAuthButton = ({ const AddOAuthButton = ({
pluginPayload, pluginPayload,
@ -47,6 +47,7 @@ const AddOAuthButton = ({
buttonRightClassName, buttonRightClassName,
dividerClassName, dividerClassName,
disabled, disabled,
onUpdate,
}: AddOAuthButtonProps) => { }: AddOAuthButtonProps) => {
const { t } = useTranslation() const { t } = useTranslation()
const renderI18nObject = useRenderI18nObject() const renderI18nObject = useRenderI18nObject()
@ -61,17 +62,16 @@ const AddOAuthButton = ({
redirect_uri, redirect_uri,
} = data || {} } = data || {}
const isConfigured = is_system_oauth_params_exists || Object.keys(client_params || {}).length > 0 const isConfigured = is_system_oauth_params_exists || Object.keys(client_params || {}).length > 0
const invalidatePluginCredentialInfo = useInvalidPluginCredentialInfoHook(pluginPayload)
const handleOAuth = useCallback(async () => { const handleOAuth = useCallback(async () => {
const { authorization_url } = await getPluginOAuthUrl() const { authorization_url } = await getPluginOAuthUrl()
if (authorization_url) { if (authorization_url) {
openOAuthPopup( openOAuthPopup(
authorization_url, authorization_url,
invalidatePluginCredentialInfo, () => onUpdate?.(),
) )
} }
}, [getPluginOAuthUrl, invalidatePluginCredentialInfo]) }, [getPluginOAuthUrl, onUpdate])
const renderCustomLabel = useCallback((item: FormSchema) => { const renderCustomLabel = useCallback((item: FormSchema) => {
return ( return (
@ -177,14 +177,14 @@ const AddOAuthButton = ({
<Button <Button
variant={buttonVariant} variant={buttonVariant}
className={cn( className={cn(
'w-full py-0 pl-0.5 pr-0 hover:bg-components-button-primary-bg', 'w-full px-0 py-0 hover:bg-components-button-primary-bg',
className, className,
)} )}
disabled={disabled} disabled={disabled}
onClick={handleOAuth} onClick={handleOAuth}
> >
<div className={cn( <div className={cn(
'flex h-full w-0 grow items-center justify-center rounded-l-lg hover:bg-components-button-primary-bg-hover', 'flex h-full w-0 grow items-center justify-center rounded-l-lg pl-0.5 hover:bg-components-button-primary-bg-hover',
buttonLeftClassName, buttonLeftClassName,
)}> )}>
<div <div
@ -251,6 +251,7 @@ const AddOAuthButton = ({
__oauth_client__: __auth_client__, __oauth_client__: __auth_client__,
}} }}
hasOriginalClientParams={Object.keys(client_params || {}).length > 0} hasOriginalClientParams={Object.keys(client_params || {}).length > 0}
onUpdate={onUpdate}
/> />
) )
} }

@ -19,7 +19,6 @@ import type { PluginPayload } from '../types'
import { import {
useAddPluginCredentialHook, useAddPluginCredentialHook,
useGetPluginCredentialSchemaHook, useGetPluginCredentialSchemaHook,
useInvalidPluginCredentialInfoHook,
useUpdatePluginCredentialHook, useUpdatePluginCredentialHook,
} from '../hooks/use-credential' } from '../hooks/use-credential'
import { useRenderI18nObject } from '@/hooks/use-i18n' import { useRenderI18nObject } from '@/hooks/use-i18n'
@ -30,6 +29,7 @@ export type ApiKeyModalProps = {
editValues?: Record<string, any> editValues?: Record<string, any>
onRemove?: () => void onRemove?: () => void
disabled?: boolean disabled?: boolean
onUpdate?: () => void
} }
const ApiKeyModal = ({ const ApiKeyModal = ({
pluginPayload, pluginPayload,
@ -37,6 +37,7 @@ const ApiKeyModal = ({
editValues, editValues,
onRemove, onRemove,
disabled, disabled,
onUpdate,
}: ApiKeyModalProps) => { }: ApiKeyModalProps) => {
const { t } = useTranslation() const { t } = useTranslation()
const { notify } = useToastContext() const { notify } = useToastContext()
@ -67,7 +68,6 @@ const ApiKeyModal = ({
const renderI18nObject = useRenderI18nObject() const renderI18nObject = useRenderI18nObject()
const { mutateAsync: addPluginCredential } = useAddPluginCredentialHook(pluginPayload) const { mutateAsync: addPluginCredential } = useAddPluginCredentialHook(pluginPayload)
const { mutateAsync: updatePluginCredential } = useUpdatePluginCredentialHook(pluginPayload) const { mutateAsync: updatePluginCredential } = useUpdatePluginCredentialHook(pluginPayload)
const invalidatePluginCredentialInfo = useInvalidPluginCredentialInfoHook(pluginPayload)
const formRef = useRef<FormRefObject>(null) const formRef = useRef<FormRefObject>(null)
const handleConfirm = useCallback(async () => { const handleConfirm = useCallback(async () => {
if (doingActionRef.current) if (doingActionRef.current)
@ -110,12 +110,12 @@ const ApiKeyModal = ({
}) })
onClose?.() onClose?.()
invalidatePluginCredentialInfo() onUpdate?.()
} }
finally { finally {
handleSetDoingAction(false) handleSetDoingAction(false)
} }
}, [addPluginCredential, onClose, invalidatePluginCredentialInfo, updatePluginCredential, notify, t, editValues, handleSetDoingAction]) }, [addPluginCredential, onClose, onUpdate, updatePluginCredential, notify, t, editValues, handleSetDoingAction])
return ( return (
<Modal <Modal

@ -16,6 +16,7 @@ type AuthorizeProps = {
canOAuth?: boolean canOAuth?: boolean
canApiKey?: boolean canApiKey?: boolean
disabled?: boolean disabled?: boolean
onUpdate?: () => void
} }
const Authorize = ({ const Authorize = ({
pluginPayload, pluginPayload,
@ -24,6 +25,7 @@ const Authorize = ({
canOAuth, canOAuth,
canApiKey, canApiKey,
disabled, disabled,
onUpdate,
}: AuthorizeProps) => { }: AuthorizeProps) => {
const { t } = useTranslation() const { t } = useTranslation()
const oAuthButtonProps: AddOAuthButtonProps = useMemo(() => { const oAuthButtonProps: AddOAuthButtonProps = useMemo(() => {
@ -69,6 +71,7 @@ const Authorize = ({
<AddOAuthButton <AddOAuthButton
{...oAuthButtonProps} {...oAuthButtonProps}
disabled={disabled} disabled={disabled}
onUpdate={onUpdate}
/> />
</div> </div>
) )
@ -88,6 +91,7 @@ const Authorize = ({
<AddApiKeyButton <AddApiKeyButton
{...apiKeyButtonProps} {...apiKeyButtonProps}
disabled={disabled} disabled={disabled}
onUpdate={onUpdate}
/> />
</div> </div>
) )

@ -13,7 +13,6 @@ import { useTranslation } from 'react-i18next'
import Modal from '@/app/components/base/modal/modal' import Modal from '@/app/components/base/modal/modal'
import { import {
useDeletePluginOAuthCustomClientHook, useDeletePluginOAuthCustomClientHook,
useInvalidPluginCredentialInfoHook,
useInvalidPluginOAuthClientSchemaHook, useInvalidPluginOAuthClientSchemaHook,
useSetPluginOAuthCustomClientHook, useSetPluginOAuthCustomClientHook,
} from '../hooks/use-credential' } from '../hooks/use-credential'
@ -35,6 +34,7 @@ type OAuthClientSettingsProps = {
schemas: FormSchema[] schemas: FormSchema[]
onAuth?: () => Promise<void> onAuth?: () => Promise<void>
hasOriginalClientParams?: boolean hasOriginalClientParams?: boolean
onUpdate?: () => void
} }
const OAuthClientSettings = ({ const OAuthClientSettings = ({
pluginPayload, pluginPayload,
@ -44,6 +44,7 @@ const OAuthClientSettings = ({
schemas, schemas,
onAuth, onAuth,
hasOriginalClientParams, hasOriginalClientParams,
onUpdate,
}: OAuthClientSettingsProps) => { }: OAuthClientSettingsProps) => {
const { t } = useTranslation() const { t } = useTranslation()
const { notify } = useToastContext() const { notify } = useToastContext()
@ -59,7 +60,6 @@ const OAuthClientSettings = ({
return acc return acc
}, {} as Record<string, any>) }, {} as Record<string, any>)
const { mutateAsync: setPluginOAuthCustomClient } = useSetPluginOAuthCustomClientHook(pluginPayload) const { mutateAsync: setPluginOAuthCustomClient } = useSetPluginOAuthCustomClientHook(pluginPayload)
const invalidPluginCredentialInfo = useInvalidPluginCredentialInfoHook(pluginPayload)
const invalidPluginOAuthClientSchema = useInvalidPluginOAuthClientSchemaHook(pluginPayload) const invalidPluginOAuthClientSchema = useInvalidPluginOAuthClientSchemaHook(pluginPayload)
const formRef = useRef<FormRefObject>(null) const formRef = useRef<FormRefObject>(null)
const handleConfirm = useCallback(async () => { const handleConfirm = useCallback(async () => {
@ -92,13 +92,13 @@ const OAuthClientSettings = ({
}) })
onClose?.() onClose?.()
invalidPluginCredentialInfo() onUpdate?.()
invalidPluginOAuthClientSchema() invalidPluginOAuthClientSchema()
} }
finally { finally {
handleSetDoingAction(false) handleSetDoingAction(false)
} }
}, [onClose, invalidPluginCredentialInfo, invalidPluginOAuthClientSchema, setPluginOAuthCustomClient, notify, t, handleSetDoingAction]) }, [onClose, onUpdate, invalidPluginOAuthClientSchema, setPluginOAuthCustomClient, notify, t, handleSetDoingAction])
const handleConfirmAndAuthorize = useCallback(async () => { const handleConfirmAndAuthorize = useCallback(async () => {
await handleConfirm() await handleConfirm()
@ -118,13 +118,13 @@ const OAuthClientSettings = ({
message: t('common.api.actionSuccess'), message: t('common.api.actionSuccess'),
}) })
onClose?.() onClose?.()
invalidPluginCredentialInfo() onUpdate?.()
invalidPluginOAuthClientSchema() invalidPluginOAuthClientSchema()
} }
finally { finally {
handleSetDoingAction(false) handleSetDoingAction(false)
} }
}, [invalidPluginCredentialInfo, invalidPluginOAuthClientSchema, deletePluginOAuthCustomClient, notify, t, handleSetDoingAction, onClose]) }, [onUpdate, invalidPluginOAuthClientSchema, deletePluginOAuthCustomClient, notify, t, handleSetDoingAction, onClose])
const form = useForm({ const form = useForm({
defaultValues: editValues || defaultValues, defaultValues: editValues || defaultValues,
}) })

@ -34,6 +34,7 @@ const AuthorizedInNode = ({
canOAuth, canOAuth,
credentials, credentials,
disabled, disabled,
invalidPluginCredentialInfo,
} = usePluginAuth(pluginPayload, isOpen || !!credentialId) } = usePluginAuth(pluginPayload, isOpen || !!credentialId)
const renderTrigger = useCallback((open?: boolean) => { const renderTrigger = useCallback((open?: boolean) => {
let label = '' let label = ''
@ -104,6 +105,7 @@ const AuthorizedInNode = ({
extraAuthorizationItems={extraAuthorizationItems} extraAuthorizationItems={extraAuthorizationItems}
showItemSelectedIcon showItemSelectedIcon
selectedCredentialId={credentialId || '__workspace_default__'} selectedCredentialId={credentialId || '__workspace_default__'}
onUpdate={invalidPluginCredentialInfo}
/> />
) )
} }

@ -29,7 +29,6 @@ import { useToastContext } from '@/app/components/base/toast'
import type { PluginPayload } from '../types' import type { PluginPayload } from '../types'
import { import {
useDeletePluginCredentialHook, useDeletePluginCredentialHook,
useInvalidPluginCredentialInfoHook,
useSetPluginDefaultCredentialHook, useSetPluginDefaultCredentialHook,
useUpdatePluginCredentialHook, useUpdatePluginCredentialHook,
} from '../hooks/use-credential' } from '../hooks/use-credential'
@ -52,6 +51,7 @@ type AuthorizedProps = {
extraAuthorizationItems?: Credential[] extraAuthorizationItems?: Credential[]
showItemSelectedIcon?: boolean showItemSelectedIcon?: boolean
selectedCredentialId?: string selectedCredentialId?: string
onUpdate?: () => void
} }
const Authorized = ({ const Authorized = ({
pluginPayload, pluginPayload,
@ -71,6 +71,7 @@ const Authorized = ({
extraAuthorizationItems, extraAuthorizationItems,
showItemSelectedIcon, showItemSelectedIcon,
selectedCredentialId, selectedCredentialId,
onUpdate,
}: AuthorizedProps) => { }: AuthorizedProps) => {
const { t } = useTranslation() const { t } = useTranslation()
const { notify } = useToastContext() const { notify } = useToastContext()
@ -87,7 +88,6 @@ const Authorized = ({
const pendingOperationCredentialId = useRef<string | null>(null) const pendingOperationCredentialId = useRef<string | null>(null)
const [deleteCredentialId, setDeleteCredentialId] = useState<string | null>(null) const [deleteCredentialId, setDeleteCredentialId] = useState<string | null>(null)
const { mutateAsync: deletePluginCredential } = useDeletePluginCredentialHook(pluginPayload) const { mutateAsync: deletePluginCredential } = useDeletePluginCredentialHook(pluginPayload)
const invalidatePluginCredentialInfo = useInvalidPluginCredentialInfoHook(pluginPayload)
const openConfirm = useCallback((credentialId?: string) => { const openConfirm = useCallback((credentialId?: string) => {
if (credentialId) if (credentialId)
pendingOperationCredentialId.current = credentialId pendingOperationCredentialId.current = credentialId
@ -118,14 +118,14 @@ const Authorized = ({
type: 'success', type: 'success',
message: t('common.api.actionSuccess'), message: t('common.api.actionSuccess'),
}) })
invalidatePluginCredentialInfo() onUpdate?.()
setDeleteCredentialId(null) setDeleteCredentialId(null)
pendingOperationCredentialId.current = null pendingOperationCredentialId.current = null
} }
finally { finally {
handleSetDoingAction(false) handleSetDoingAction(false)
} }
}, [deletePluginCredential, invalidatePluginCredentialInfo, notify, t, handleSetDoingAction]) }, [deletePluginCredential, onUpdate, notify, t, handleSetDoingAction])
const [editValues, setEditValues] = useState<Record<string, any> | null>(null) const [editValues, setEditValues] = useState<Record<string, any> | null>(null)
const handleEdit = useCallback((id: string, values: Record<string, any>) => { const handleEdit = useCallback((id: string, values: Record<string, any>) => {
pendingOperationCredentialId.current = id pendingOperationCredentialId.current = id
@ -145,12 +145,12 @@ const Authorized = ({
type: 'success', type: 'success',
message: t('common.api.actionSuccess'), message: t('common.api.actionSuccess'),
}) })
invalidatePluginCredentialInfo() onUpdate?.()
} }
finally { finally {
handleSetDoingAction(false) handleSetDoingAction(false)
} }
}, [setPluginDefaultCredential, invalidatePluginCredentialInfo, notify, t, handleSetDoingAction]) }, [setPluginDefaultCredential, onUpdate, notify, t, handleSetDoingAction])
const { mutateAsync: updatePluginCredential } = useUpdatePluginCredentialHook(pluginPayload) const { mutateAsync: updatePluginCredential } = useUpdatePluginCredentialHook(pluginPayload)
const handleRename = useCallback(async (payload: { const handleRename = useCallback(async (payload: {
credential_id: string credential_id: string
@ -165,12 +165,12 @@ const Authorized = ({
type: 'success', type: 'success',
message: t('common.api.actionSuccess'), message: t('common.api.actionSuccess'),
}) })
invalidatePluginCredentialInfo() onUpdate?.()
} }
finally { finally {
handleSetDoingAction(false) handleSetDoingAction(false)
} }
}, [updatePluginCredential, notify, t, handleSetDoingAction, invalidatePluginCredentialInfo]) }, [updatePluginCredential, notify, t, handleSetDoingAction, onUpdate])
return ( return (
<> <>

@ -1,5 +1,8 @@
import { useAppContext } from '@/context/app-context' import { useAppContext } from '@/context/app-context'
import { useGetPluginCredentialInfoHook } from './use-credential' import {
useGetPluginCredentialInfoHook,
useInvalidPluginCredentialInfoHook,
} from './use-credential'
import { CredentialTypeEnum } from '../types' import { CredentialTypeEnum } from '../types'
import type { PluginPayload } from '../types' import type { PluginPayload } from '../types'
@ -9,6 +12,7 @@ export const usePluginAuth = (pluginPayload: PluginPayload, enable?: boolean) =>
const isAuthorized = !!data?.credentials.length const isAuthorized = !!data?.credentials.length
const canOAuth = data?.supported_credential_types.includes(CredentialTypeEnum.OAUTH2) const canOAuth = data?.supported_credential_types.includes(CredentialTypeEnum.OAUTH2)
const canApiKey = data?.supported_credential_types.includes(CredentialTypeEnum.API_KEY) const canApiKey = data?.supported_credential_types.includes(CredentialTypeEnum.API_KEY)
const invalidPluginCredentialInfo = useInvalidPluginCredentialInfoHook(pluginPayload)
return { return {
isAuthorized, isAuthorized,
@ -16,5 +20,6 @@ export const usePluginAuth = (pluginPayload: PluginPayload, enable?: boolean) =>
canApiKey, canApiKey,
credentials: data?.credentials || [], credentials: data?.credentials || [],
disabled: !isCurrentWorkspaceManager, disabled: !isCurrentWorkspaceManager,
invalidPluginCredentialInfo,
} }
} }

@ -34,6 +34,7 @@ const PluginAuthInAgent = ({
canApiKey, canApiKey,
credentials, credentials,
disabled, disabled,
invalidPluginCredentialInfo,
} = usePluginAuth(pluginPayload, true) } = usePluginAuth(pluginPayload, true)
const extraAuthorizationItems: Credential[] = [ const extraAuthorizationItems: Credential[] = [
@ -91,6 +92,7 @@ const PluginAuthInAgent = ({
canOAuth={canOAuth} canOAuth={canOAuth}
canApiKey={canApiKey} canApiKey={canApiKey}
disabled={disabled} disabled={disabled}
onUpdate={invalidPluginCredentialInfo}
/> />
) )
} }
@ -110,6 +112,7 @@ const PluginAuthInAgent = ({
isOpen={isOpen} isOpen={isOpen}
onOpenChange={setIsOpen} onOpenChange={setIsOpen}
selectedCredentialId={credentialId || '__workspace_default__'} selectedCredentialId={credentialId || '__workspace_default__'}
onUpdate={invalidPluginCredentialInfo}
/> />
) )
} }

@ -21,6 +21,7 @@ const PluginAuth = ({
canApiKey, canApiKey,
credentials, credentials,
disabled, disabled,
invalidPluginCredentialInfo,
} = usePluginAuth(pluginPayload, !!pluginPayload.provider) } = usePluginAuth(pluginPayload, !!pluginPayload.provider)
return ( return (
@ -32,6 +33,7 @@ const PluginAuth = ({
canOAuth={canOAuth} canOAuth={canOAuth}
canApiKey={canApiKey} canApiKey={canApiKey}
disabled={disabled} disabled={disabled}
onUpdate={invalidPluginCredentialInfo}
/> />
) )
} }
@ -43,6 +45,7 @@ const PluginAuth = ({
canOAuth={canOAuth} canOAuth={canOAuth}
canApiKey={canApiKey} canApiKey={canApiKey}
disabled={disabled} disabled={disabled}
onUpdate={invalidPluginCredentialInfo}
/> />
) )
} }

Loading…
Cancel
Save