2024-10-12 11:33:12 +08:00
|
|
|
'use client'
|
|
|
|
|
|
|
|
import type { ReactNode } from 'react'
|
2024-10-12 16:34:02 +08:00
|
|
|
import {
|
2024-11-05 11:05:14 +08:00
|
|
|
useMemo,
|
2024-10-12 16:34:02 +08:00
|
|
|
useRef,
|
2024-10-15 19:20:59 +08:00
|
|
|
useState,
|
2024-10-12 16:34:02 +08:00
|
|
|
} from 'react'
|
|
|
|
import {
|
|
|
|
createContext,
|
|
|
|
useContextSelector,
|
|
|
|
} from 'use-context-selector'
|
2024-11-05 11:05:14 +08:00
|
|
|
import { useSelector as useAppContextSelector } from '@/context/app-context'
|
2024-11-08 16:11:50 +08:00
|
|
|
import type { PluginDetail } from '../types'
|
2024-10-31 16:20:25 +08:00
|
|
|
import type { FilterState } from './filter-management'
|
2024-11-05 11:05:14 +08:00
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import { useTabSearchParams } from '@/hooks/use-tab-searchparams'
|
2024-10-12 11:33:12 +08:00
|
|
|
|
|
|
|
export type PluginPageContextValue = {
|
|
|
|
containerRef: React.RefObject<HTMLDivElement>
|
2024-11-05 11:05:14 +08:00
|
|
|
currentPluginDetail: PluginDetail | undefined
|
|
|
|
setCurrentPluginDetail: (plugin: PluginDetail) => void
|
2024-10-31 16:20:25 +08:00
|
|
|
filters: FilterState
|
|
|
|
setFilters: (filter: FilterState) => void
|
2024-11-05 11:05:14 +08:00
|
|
|
activeTab: string
|
|
|
|
setActiveTab: (tab: string) => void
|
|
|
|
options: Array<{ value: string, text: string }>
|
2024-10-12 11:33:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export const PluginPageContext = createContext<PluginPageContextValue>({
|
|
|
|
containerRef: { current: null },
|
2024-11-02 12:49:02 +08:00
|
|
|
currentPluginDetail: undefined,
|
2024-11-08 16:11:50 +08:00
|
|
|
setCurrentPluginDetail: () => { },
|
2024-10-31 16:20:25 +08:00
|
|
|
filters: {
|
|
|
|
categories: [],
|
|
|
|
tags: [],
|
|
|
|
searchQuery: '',
|
|
|
|
},
|
2024-11-08 16:11:50 +08:00
|
|
|
setFilters: () => { },
|
2024-11-05 11:05:14 +08:00
|
|
|
activeTab: '',
|
2024-11-08 16:11:50 +08:00
|
|
|
setActiveTab: () => { },
|
2024-11-05 11:05:14 +08:00
|
|
|
options: [],
|
2024-10-12 11:33:12 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
type PluginPageContextProviderProps = {
|
|
|
|
children: ReactNode
|
|
|
|
}
|
|
|
|
|
2024-10-12 16:34:02 +08:00
|
|
|
export function usePluginPageContext(selector: (value: PluginPageContextValue) => any) {
|
|
|
|
return useContextSelector(PluginPageContext, selector)
|
|
|
|
}
|
|
|
|
|
2024-10-12 11:33:12 +08:00
|
|
|
export const PluginPageContextProvider = ({
|
|
|
|
children,
|
|
|
|
}: PluginPageContextProviderProps) => {
|
2024-11-05 11:05:14 +08:00
|
|
|
const { t } = useTranslation()
|
2024-10-12 11:33:12 +08:00
|
|
|
const containerRef = useRef<HTMLDivElement>(null)
|
2024-10-31 16:20:25 +08:00
|
|
|
const [filters, setFilters] = useState<FilterState>({
|
|
|
|
categories: [],
|
|
|
|
tags: [],
|
|
|
|
searchQuery: '',
|
|
|
|
})
|
2024-11-05 11:05:14 +08:00
|
|
|
const [currentPluginDetail, setCurrentPluginDetail] = useState<PluginDetail | undefined>()
|
|
|
|
|
|
|
|
const { enable_marketplace } = useAppContextSelector(s => s.systemFeatures)
|
|
|
|
const options = useMemo(() => {
|
|
|
|
return [
|
|
|
|
{ value: 'plugins', text: t('common.menus.plugins') },
|
|
|
|
...(
|
|
|
|
enable_marketplace
|
|
|
|
? [{ value: 'discover', text: 'Explore Marketplace' }]
|
|
|
|
: []
|
|
|
|
),
|
|
|
|
]
|
|
|
|
}, [t, enable_marketplace])
|
|
|
|
const [activeTab, setActiveTab] = useTabSearchParams({
|
|
|
|
defaultTab: options[0].value,
|
|
|
|
})
|
2024-10-12 16:34:02 +08:00
|
|
|
|
2024-10-12 11:33:12 +08:00
|
|
|
return (
|
2024-10-12 16:34:02 +08:00
|
|
|
<PluginPageContext.Provider
|
|
|
|
value={{
|
|
|
|
containerRef,
|
2024-11-02 12:49:02 +08:00
|
|
|
currentPluginDetail,
|
|
|
|
setCurrentPluginDetail,
|
2024-10-31 16:20:25 +08:00
|
|
|
filters,
|
|
|
|
setFilters,
|
2024-11-05 11:05:14 +08:00
|
|
|
activeTab,
|
|
|
|
setActiveTab,
|
|
|
|
options,
|
2024-10-12 16:34:02 +08:00
|
|
|
}}
|
|
|
|
>
|
2024-10-12 11:33:12 +08:00
|
|
|
{children}
|
|
|
|
</PluginPageContext.Provider>
|
|
|
|
)
|
|
|
|
}
|