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,
|
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-10-15 19:20:59 +08:00
|
|
|
import type { Permissions } from '../types'
|
|
|
|
import { PermissionType } from '../types'
|
2024-10-12 11:33:12 +08:00
|
|
|
|
|
|
|
export type PluginPageContextValue = {
|
|
|
|
containerRef: React.RefObject<HTMLDivElement>
|
2024-10-15 19:20:59 +08:00
|
|
|
permissions: Permissions
|
|
|
|
setPermissions: (permissions: PluginPageContextValue['permissions']) => void
|
|
|
|
|
2024-10-12 11:33:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export const PluginPageContext = createContext<PluginPageContextValue>({
|
|
|
|
containerRef: { current: null },
|
2024-10-15 19:20:59 +08:00
|
|
|
permissions: {
|
|
|
|
canInstall: PermissionType.noOne,
|
|
|
|
canDebugger: PermissionType.noOne,
|
|
|
|
},
|
|
|
|
setPermissions: () => { },
|
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) => {
|
|
|
|
const containerRef = useRef<HTMLDivElement>(null)
|
2024-10-15 19:20:59 +08:00
|
|
|
const [permissions, setPermissions] = useState<PluginPageContextValue['permissions']>({
|
|
|
|
canInstall: PermissionType.noOne,
|
|
|
|
canDebugger: PermissionType.noOne,
|
|
|
|
})
|
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-15 19:20:59 +08:00
|
|
|
permissions,
|
|
|
|
setPermissions,
|
2024-10-12 16:34:02 +08:00
|
|
|
}}
|
|
|
|
>
|
2024-10-12 11:33:12 +08:00
|
|
|
{children}
|
|
|
|
</PluginPageContext.Provider>
|
|
|
|
)
|
|
|
|
}
|