dify/web/app/components/tools/marketplace/hooks.ts

31 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-10-29 10:51:41 +08:00
import {
useCallback,
useEffect,
useState,
} from 'react'
import type { Plugin } from '@/app/components/plugins/types'
import type { MarketplaceCollection } from '@/app/components/plugins/marketplace/types'
2024-10-29 14:44:30 +08:00
import { getMarketplaceCollectionsAndPlugins } from '@/app/components/plugins/marketplace/utils'
2024-10-29 10:51:41 +08:00
export const useMarketplace = () => {
const [marketplaceCollections, setMarketplaceCollections] = useState<MarketplaceCollection[]>([])
const [marketplaceCollectionPluginsMap, setMarketplaceCollectionPluginsMap] = useState<Record<string, Plugin[]>>({})
2024-10-29 14:44:30 +08:00
const [isLoading, setIsLoading] = useState(true)
2024-10-29 10:51:41 +08:00
const getMarketplaceCollections = useCallback(async () => {
2024-10-29 14:44:30 +08:00
setIsLoading(true)
const { marketplaceCollections, marketplaceCollectionPluginsMap } = await getMarketplaceCollectionsAndPlugins()
setIsLoading(false)
2024-10-29 10:51:41 +08:00
setMarketplaceCollections(marketplaceCollections)
setMarketplaceCollectionPluginsMap(marketplaceCollectionPluginsMap)
}, [])
useEffect(() => {
getMarketplaceCollections()
}, [getMarketplaceCollections])
return {
2024-10-29 14:44:30 +08:00
isLoading,
2024-10-29 10:51:41 +08:00
marketplaceCollections,
marketplaceCollectionPluginsMap,
}
}