2024-10-12 18:02:24 +08:00
|
|
|
'use client'
|
|
|
|
|
|
|
|
import type { ReactNode } from 'react'
|
|
|
|
import {
|
2024-10-29 10:51:41 +08:00
|
|
|
useCallback,
|
2024-10-12 18:02:24 +08:00
|
|
|
useState,
|
|
|
|
} from 'react'
|
|
|
|
import {
|
|
|
|
createContext,
|
|
|
|
useContextSelector,
|
|
|
|
} from 'use-context-selector'
|
2024-10-29 10:51:41 +08:00
|
|
|
import { useDebounceFn } from 'ahooks'
|
|
|
|
import { PLUGIN_TYPE_SEARCH_MAP } from './plugin-type-switch'
|
|
|
|
import type { Plugin } from '../types'
|
|
|
|
import type { PluginsSearchParams } from './types'
|
2024-10-12 18:02:24 +08:00
|
|
|
|
|
|
|
export type MarketplaceContextValue = {
|
|
|
|
intersected: boolean
|
|
|
|
setIntersected: (intersected: boolean) => void
|
2024-10-29 10:51:41 +08:00
|
|
|
searchPluginText: string
|
|
|
|
handleSearchPluginTextChange: (text: string) => void
|
|
|
|
filterPluginTags: string[]
|
|
|
|
handleFilterPluginTagsChange: (tags: string[]) => void
|
|
|
|
activePluginType: string
|
|
|
|
handleActivePluginTypeChange: (type: string) => void
|
|
|
|
plugins?: Plugin[]
|
|
|
|
setPlugins?: (plugins: Plugin[]) => void
|
2024-10-12 18:02:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export const MarketplaceContext = createContext<MarketplaceContextValue>({
|
|
|
|
intersected: true,
|
|
|
|
setIntersected: () => {},
|
2024-10-29 10:51:41 +08:00
|
|
|
searchPluginText: '',
|
|
|
|
handleSearchPluginTextChange: () => {},
|
|
|
|
filterPluginTags: [],
|
|
|
|
handleFilterPluginTagsChange: () => {},
|
|
|
|
activePluginType: PLUGIN_TYPE_SEARCH_MAP.all,
|
|
|
|
handleActivePluginTypeChange: () => {},
|
|
|
|
plugins: undefined,
|
|
|
|
setPlugins: () => {},
|
2024-10-12 18:02:24 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
type MarketplaceContextProviderProps = {
|
|
|
|
children: ReactNode
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useMarketplaceContext(selector: (value: MarketplaceContextValue) => any) {
|
|
|
|
return useContextSelector(MarketplaceContext, selector)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const MarketplaceContextProvider = ({
|
|
|
|
children,
|
|
|
|
}: MarketplaceContextProviderProps) => {
|
|
|
|
const [intersected, setIntersected] = useState(true)
|
2024-10-29 10:51:41 +08:00
|
|
|
const [searchPluginText, setSearchPluginText] = useState('')
|
|
|
|
const [filterPluginTags, setFilterPluginTags] = useState<string[]>([])
|
|
|
|
const [activePluginType, setActivePluginType] = useState(PLUGIN_TYPE_SEARCH_MAP.all)
|
|
|
|
const [plugins, setPlugins] = useState<Plugin[]>()
|
|
|
|
|
|
|
|
const handleUpdatePlugins = useCallback((query: PluginsSearchParams) => {
|
|
|
|
const fetchPlugins = async () => {
|
|
|
|
const response = await fetch(
|
|
|
|
'https://marketplace.dify.dev/api/v1/plugins/search/basic',
|
|
|
|
{
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
query: query.query,
|
|
|
|
page: 1,
|
|
|
|
page_size: 10,
|
|
|
|
sort_by: query.sortBy,
|
|
|
|
sort_order: query.sortOrder,
|
|
|
|
category: query.category,
|
|
|
|
tag: query.tag,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
const data = await response.json()
|
|
|
|
setPlugins(data.data.plugins)
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchPlugins()
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const { run: handleUpdatePluginsWithDebounced } = useDebounceFn(handleUpdatePlugins, {
|
|
|
|
wait: 500,
|
|
|
|
})
|
|
|
|
|
|
|
|
const handleSearchPluginTextChange = useCallback((text: string) => {
|
|
|
|
setSearchPluginText(text)
|
|
|
|
|
|
|
|
handleUpdatePluginsWithDebounced({ query: text })
|
|
|
|
}, [handleUpdatePluginsWithDebounced])
|
|
|
|
|
|
|
|
const handleFilterPluginTagsChange = useCallback((tags: string[]) => {
|
|
|
|
setFilterPluginTags(tags)
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const handleActivePluginTypeChange = useCallback((type: string) => {
|
|
|
|
setActivePluginType(type)
|
|
|
|
}, [])
|
2024-10-12 18:02:24 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<MarketplaceContext.Provider
|
|
|
|
value={{
|
|
|
|
intersected,
|
|
|
|
setIntersected,
|
2024-10-29 10:51:41 +08:00
|
|
|
searchPluginText,
|
|
|
|
handleSearchPluginTextChange,
|
|
|
|
filterPluginTags,
|
|
|
|
handleFilterPluginTagsChange,
|
|
|
|
activePluginType,
|
|
|
|
handleActivePluginTypeChange,
|
|
|
|
plugins,
|
|
|
|
setPlugins,
|
2024-10-12 18:02:24 +08:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</MarketplaceContext.Provider>
|
|
|
|
)
|
|
|
|
}
|