Files
dify/web/app/components/plugins/plugin-page/context.tsx
yyh 806ece9a67 refactor!: replace Zustand global store with TanStack Query for systemFeatures
Follow-up to SSR prefetch migration (2833965). Eliminates the Zustand
middleman that was syncing TanStack Query data into a separate store.

- Remove useGlobalPublicStore Zustand store entirely
- Create hooks/use-global-public.ts with useSystemFeatures,
  useSystemFeaturesQuery, useIsSystemFeaturesPending, useSetupStatusQuery
- Migrate all 93 consumers to import from @/hooks/use-global-public
- Simplify global-public-context.tsx to a thin provider component
- Update 18 test files to mock the new hook interface
- Fix SetupStatusResponse.setup_at type from Date to string (JSON)
- Fix setup-status.spec.ts mock target to match consoleClient

BREAKING CHANGE: useGlobalPublicStore is removed. Use useSystemFeatures()
from @/hooks/use-global-public instead.
2026-02-01 19:06:08 +08:00

92 lines
2.4 KiB
TypeScript

'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 { useSystemFeatures } from '@/hooks/use-global-public'
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: (value: PluginPageContextValue) => 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 } = useSystemFeatures()
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('tab', {
defaultValue: options[0].value,
})
return (
<PluginPageContext.Provider
value={{
containerRef,
currentPluginID,
setCurrentPluginID,
filters,
setFilters,
activeTab,
setActiveTab,
options,
}}
>
{children}
</PluginPageContext.Provider>
)
}