feat(media): extract workspace package

This commit is contained in:
Maofeng
2026-09-20 16:44:15 +08:00
parent 2a6284d1b0
commit c519d4e74b
29 changed files with 112 additions and 16 deletions
+35
View File
@@ -0,0 +1,35 @@
import * as React from "react"
import type { MediaAdapter, MediaNotice } from "./types"
interface MediaContextValue {
adapter: MediaAdapter
notify?: (notice: MediaNotice) => void
}
const MediaContext = React.createContext<MediaContextValue | null>(null)
export interface MediaProviderProps {
adapter: MediaAdapter
children: React.ReactNode
notify?: (notice: MediaNotice) => void
}
/** Supplies all persistence and transport dependencies for the media blocks. */
export function MediaProvider({
adapter,
children,
notify,
}: MediaProviderProps) {
const value = React.useMemo(() => ({ adapter, notify }), [adapter, notify])
return <MediaContext.Provider value={value}>{children}</MediaContext.Provider>
}
export function useMedia(): MediaContextValue {
const value = React.useContext(MediaContext)
if (!value) {
throw new Error("Media blocks must be rendered inside a MediaProvider.")
}
return value
}