mirror of
https://github.com/langgenius/dify.git
synced 2026-05-04 09:28:04 +08:00
refactor(web): simplify MCP tool availability context and hook
- Add useMemo to prevent unnecessary re-renders of context value - Extract ProviderProps type for better readability - Convert arrow functions to standard function declarations - Remove unused versionSupported/sandboxEnabled from hook return type
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
'use client'
|
||||
import type { ReactNode } from 'react'
|
||||
import { createContext, useContext } from 'react'
|
||||
import { createContext, useContext, useMemo } from 'react'
|
||||
|
||||
type MCPToolAvailabilityContextValue = {
|
||||
versionSupported?: boolean
|
||||
@ -11,29 +11,27 @@ const MCPToolAvailabilityContext = createContext<MCPToolAvailabilityContextValue
|
||||
|
||||
export type MCPToolAvailability = {
|
||||
allowed: boolean
|
||||
versionSupported?: boolean
|
||||
sandboxEnabled?: boolean
|
||||
blockedBy?: 'version' | 'sandbox'
|
||||
}
|
||||
|
||||
export const MCPToolAvailabilityProvider = ({
|
||||
versionSupported,
|
||||
sandboxEnabled,
|
||||
children,
|
||||
}: {
|
||||
type ProviderProps = {
|
||||
versionSupported?: boolean
|
||||
sandboxEnabled?: boolean
|
||||
children: ReactNode
|
||||
}) => {
|
||||
const parentContext = useContext(MCPToolAvailabilityContext)
|
||||
const value = {
|
||||
versionSupported: versionSupported !== undefined
|
||||
? versionSupported
|
||||
: parentContext?.versionSupported,
|
||||
sandboxEnabled: sandboxEnabled !== undefined
|
||||
? sandboxEnabled
|
||||
: parentContext?.sandboxEnabled,
|
||||
}
|
||||
}
|
||||
|
||||
export function MCPToolAvailabilityProvider({
|
||||
versionSupported,
|
||||
sandboxEnabled,
|
||||
children,
|
||||
}: ProviderProps): ReactNode {
|
||||
const parent = useContext(MCPToolAvailabilityContext)
|
||||
|
||||
const value = useMemo<MCPToolAvailabilityContextValue>(() => ({
|
||||
versionSupported: versionSupported ?? parent?.versionSupported,
|
||||
sandboxEnabled: sandboxEnabled ?? parent?.sandboxEnabled,
|
||||
}), [versionSupported, sandboxEnabled, parent])
|
||||
|
||||
return (
|
||||
<MCPToolAvailabilityContext.Provider value={value}>
|
||||
{children}
|
||||
@ -41,24 +39,21 @@ export const MCPToolAvailabilityProvider = ({
|
||||
)
|
||||
}
|
||||
|
||||
export const useMCPToolAvailability = (): MCPToolAvailability => {
|
||||
export function useMCPToolAvailability(): MCPToolAvailability {
|
||||
const context = useContext(MCPToolAvailabilityContext)
|
||||
if (context === undefined)
|
||||
|
||||
if (!context)
|
||||
return { allowed: true }
|
||||
|
||||
const { versionSupported, sandboxEnabled } = context
|
||||
const versionAllowed = versionSupported ?? true
|
||||
const sandboxAllowed = sandboxEnabled ?? true
|
||||
const versionAllowed = context.versionSupported ?? true
|
||||
const sandboxAllowed = context.sandboxEnabled ?? true
|
||||
const allowed = versionAllowed && sandboxAllowed
|
||||
|
||||
let blockedBy: MCPToolAvailability['blockedBy']
|
||||
if (!versionAllowed)
|
||||
blockedBy = 'version'
|
||||
else if (!sandboxAllowed)
|
||||
blockedBy = 'sandbox'
|
||||
return {
|
||||
allowed,
|
||||
versionSupported,
|
||||
sandboxEnabled,
|
||||
blockedBy,
|
||||
}
|
||||
|
||||
return { allowed, blockedBy }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user