forked from agentic-review-benchmarks/dify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.tsx
More file actions
91 lines (82 loc) · 2.39 KB
/
context.tsx
File metadata and controls
91 lines (82 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
'use client'
import type { ReactNode, RefObject } from 'react'
import type { FilterState } from './filter-management'
import { noop } from 'es-toolkit/function'
import { useQueryState } from 'nuqs'
import {
useMemo,
useRef,
useState,
} from 'react'
import {
createContext,
useContextSelector,
} from 'use-context-selector'
import { useGlobalPublicStore } from '@/context/global-public-context'
import { PLUGIN_PAGE_TABS_MAP, usePluginPageTabs } from '../hooks'
export type PluginPageContextValue = {
containerRef: RefObject<HTMLDivElement | null>
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 }>
}
const emptyContainerRef: RefObject<HTMLDivElement | null> = { current: null }
export const PluginPageContext = createContext<PluginPageContextValue>({
containerRef: emptyContainerRef,
currentPluginID: undefined,
setCurrentPluginID: noop,
filters: {
categories: [],
tags: [],
searchQuery: '',
},
setFilters: noop,
activeTab: '',
setActiveTab: noop,
options: [],
})
type PluginPageContextProviderProps = {
children: ReactNode
}
export function usePluginPageContext(selector: any) {
return useContextSelector(PluginPageContext, selector)
}
export const PluginPageContextProvider = ({
children,
}: PluginPageContextProviderProps) => {
const containerRef = useRef<HTMLDivElement | null>(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] = useQueryState('category', {
defaultValue: options[0].value,
})
return (
<PluginPageContext.Provider
value={{
containerRef,
currentPluginID,
setCurrentPluginID,
filters,
setFilters,
activeTab,
setActiveTab,
options,
}}
>
{children}
</PluginPageContext.Provider>
)
}