mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-05-24 23:28:32 +08:00

Signed-off-by: -LAN- <laipz8200@outlook.com> Co-authored-by: Hash Brown <hi@xzd.me> Co-authored-by: crazywoola <427733928@qq.com> Co-authored-by: GareArc <chen4851@purdue.edu> Co-authored-by: Byron.wang <byron@dify.ai> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: Garfield Dai <dai.hai@foxmail.com> Co-authored-by: KVOJJJin <jzongcode@gmail.com> Co-authored-by: Alexi.F <654973939@qq.com> Co-authored-by: Xiyuan Chen <52963600+GareArc@users.noreply.github.com> Co-authored-by: kautsar_masuara <61046989+izon-masuara@users.noreply.github.com> Co-authored-by: achmad-kautsar <achmad.kautsar@insignia.co.id> Co-authored-by: Xin Zhang <sjhpzx@gmail.com> Co-authored-by: kelvintsim <83445753+kelvintsim@users.noreply.github.com> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: Zixuan Cheng <61724187+Theysua@users.noreply.github.com>
90 lines
2.3 KiB
TypeScript
90 lines
2.3 KiB
TypeScript
'use client'
|
|
|
|
import type { ReactNode } from 'react'
|
|
import {
|
|
useMemo,
|
|
useRef,
|
|
useState,
|
|
} from 'react'
|
|
import {
|
|
createContext,
|
|
useContextSelector,
|
|
} from 'use-context-selector'
|
|
import type { FilterState } from './filter-management'
|
|
import { useTabSearchParams } from '@/hooks/use-tab-searchparams'
|
|
import { noop } from 'lodash-es'
|
|
import { PLUGIN_PAGE_TABS_MAP, usePluginPageTabs } from '../hooks'
|
|
import { useGlobalPublicStore } from '@/context/global-public-context'
|
|
|
|
export type PluginPageContextValue = {
|
|
containerRef: React.RefObject<HTMLDivElement>
|
|
currentPluginID: string | undefined
|
|
setCurrentPluginID: (pluginID?: string) => void
|
|
filters: FilterState
|
|
setFilters: (filter: FilterState) => void
|
|
activeTab: string
|
|
setActiveTab: (tab: string) => void
|
|
options: Array<{ value: string, text: string }>
|
|
}
|
|
|
|
export const PluginPageContext = createContext<PluginPageContextValue>({
|
|
containerRef: { current: null },
|
|
currentPluginID: undefined,
|
|
setCurrentPluginID: noop,
|
|
filters: {
|
|
categories: [],
|
|
tags: [],
|
|
searchQuery: '',
|
|
},
|
|
setFilters: noop,
|
|
activeTab: '',
|
|
setActiveTab: noop,
|
|
options: [],
|
|
})
|
|
|
|
type PluginPageContextProviderProps = {
|
|
children: ReactNode
|
|
}
|
|
|
|
export function usePluginPageContext(selector: (value: PluginPageContextValue) => any) {
|
|
return useContextSelector(PluginPageContext, selector)
|
|
}
|
|
|
|
export const PluginPageContextProvider = ({
|
|
children,
|
|
}: PluginPageContextProviderProps) => {
|
|
const containerRef = useRef<HTMLDivElement>(null)
|
|
const [filters, setFilters] = useState<FilterState>({
|
|
categories: [],
|
|
tags: [],
|
|
searchQuery: '',
|
|
})
|
|
const [currentPluginID, setCurrentPluginID] = useState<string | undefined>()
|
|
|
|
const { enable_marketplace } = useGlobalPublicStore(s => s.systemFeatures)
|
|
const tabs = usePluginPageTabs()
|
|
const options = useMemo(() => {
|
|
return enable_marketplace ? tabs : tabs.filter(tab => tab.value !== PLUGIN_PAGE_TABS_MAP.marketplace)
|
|
}, [tabs, enable_marketplace])
|
|
const [activeTab, setActiveTab] = useTabSearchParams({
|
|
defaultTab: options[0].value,
|
|
})
|
|
|
|
return (
|
|
<PluginPageContext.Provider
|
|
value={{
|
|
containerRef,
|
|
currentPluginID,
|
|
setCurrentPluginID,
|
|
filters,
|
|
setFilters,
|
|
activeTab,
|
|
setActiveTab,
|
|
options,
|
|
}}
|
|
>
|
|
{children}
|
|
</PluginPageContext.Provider>
|
|
)
|
|
}
|