feat(admin, admin-ui, medusa-js, medusa-react, medusa): Support Admin Extensions (#4761)
Co-authored-by: Rares Stefan <948623+StephixOne@users.noreply.github.com> Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
co-authored by
Rares Stefan
Oli Juhl
parent
26c78bbc03
commit
f1a05f4725
@@ -0,0 +1,38 @@
|
||||
import React, { PropsWithChildren, useCallback, useMemo } from "react"
|
||||
import WidgetRegistry from "../registries/widget-registry"
|
||||
import { InjectionZone, Widget } from "../types/extensions"
|
||||
|
||||
type WidgetContextType = {
|
||||
getWidgets: (injectionZone: InjectionZone) => Widget[]
|
||||
}
|
||||
|
||||
const WidgetContext = React.createContext<WidgetContextType | null>(null)
|
||||
|
||||
export const useWidgets = () => {
|
||||
const context = React.useContext(WidgetContext)
|
||||
|
||||
if (!context) {
|
||||
throw new Error("useWidgets must be used within a WidgetContext")
|
||||
}
|
||||
|
||||
return context
|
||||
}
|
||||
|
||||
type WidgetProviderProps = PropsWithChildren<{
|
||||
registry: WidgetRegistry
|
||||
}>
|
||||
|
||||
export const WidgetProvider = ({ registry, children }: WidgetProviderProps) => {
|
||||
const getWidgets = useCallback(
|
||||
(injectionZone: InjectionZone) => {
|
||||
return registry.getWidgets(injectionZone)
|
||||
},
|
||||
[registry]
|
||||
)
|
||||
|
||||
const values = useMemo(() => ({ getWidgets }), [getWidgets])
|
||||
|
||||
return (
|
||||
<WidgetContext.Provider value={values}>{children}</WidgetContext.Provider>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user