Files
dify/web/context/external-knowledge-api-context.tsx
+11
Wu TianweiGitHubfateleiCopilotautofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>盐粒 YanliCharles YaoClaude Opus 4.8yunlu.wenyyhJingyiyyhJoelhjlarryAsuka Minatodependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>Xiyuan ChengigglewangchaririEvanzyssyz123
33edf97f81 feat: RBAC (#37107)
Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: fatelei <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: 盐粒 Yanli <[email protected]>
Co-authored-by: Charles Yao <[email protected]>
Co-authored-by: Claude Opus 4.8 <[email protected]>
Co-authored-by: yunlu.wen <[email protected]>
Co-authored-by: yyh <[email protected]>
Co-authored-by: Jingyi <[email protected]>
Co-authored-by: yyh <[email protected]>
Co-authored-by: Joel <[email protected]>
Co-authored-by: hjlarry <[email protected]>
Co-authored-by: Asuka Minato <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Xiyuan Chen <[email protected]>
Co-authored-by: gigglewang <[email protected]>
Co-authored-by: chariri <[email protected]>
Co-authored-by: Evan <[email protected]>
Co-authored-by: zyssyz123 <[email protected]>
2026-06-18 16:35:29 +00:00

51 lines
1.6 KiB
TypeScript

'use client'
import type { FC, ReactNode } from 'react'
import type { ExternalAPIItem, ExternalAPIListResponse } from '@/models/datasets'
import { createContext, use, useCallback, useMemo } from 'react'
import { useExternalKnowledgeApiList } from '@/service/knowledge/use-dataset'
type ExternalKnowledgeApiContextType = {
externalKnowledgeApiList: ExternalAPIItem[]
mutateExternalKnowledgeApis: () => Promise<ExternalAPIListResponse | undefined>
isLoading: boolean
}
const ExternalKnowledgeApiContext = createContext<ExternalKnowledgeApiContextType | undefined>(undefined)
type ExternalKnowledgeApiProviderProps = {
children: ReactNode
enabled?: boolean
}
export const ExternalKnowledgeApiProvider: FC<ExternalKnowledgeApiProviderProps> = ({ children, enabled = true }) => {
const { data, refetch, isLoading } = useExternalKnowledgeApiList({ enabled })
const mutateExternalKnowledgeApis = useCallback(() => {
if (!enabled)
return Promise.resolve(undefined)
return refetch().then(res => res.data)
}, [enabled, refetch])
const contextValue = useMemo<ExternalKnowledgeApiContextType>(() => ({
externalKnowledgeApiList: data?.data || [],
mutateExternalKnowledgeApis,
isLoading,
}), [data, mutateExternalKnowledgeApis, isLoading])
return (
<ExternalKnowledgeApiContext.Provider value={contextValue}>
{children}
</ExternalKnowledgeApiContext.Provider>
)
}
export const useExternalKnowledgeApi = () => {
const context = use(ExternalKnowledgeApiContext)
if (context === undefined)
throw new Error('useExternalKnowledgeApi must be used within a ExternalKnowledgeApiProvider')
return context
}