dify/web/app/components/plugins/plugin-page/context.tsx

43 lines
883 B
TypeScript
Raw Normal View History

2024-10-12 11:33:12 +08:00
'use client'
import type { ReactNode } from 'react'
2024-10-12 16:34:02 +08:00
import {
useRef,
} from 'react'
import {
createContext,
useContextSelector,
} from 'use-context-selector'
2024-10-12 11:33:12 +08:00
export type PluginPageContextValue = {
containerRef: React.RefObject<HTMLDivElement>
}
export const PluginPageContext = createContext<PluginPageContextValue>({
containerRef: { current: null },
})
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) => {
const containerRef = useRef<HTMLDivElement>(null)
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-10-12 11:33:12 +08:00
{children}
</PluginPageContext.Provider>
)
}