'use client'; /** @TODO !!! Revisit for SSR (CJP) */ import type { MediaStore } from '@vjs-10/media-store'; import type { Context, ReactNode } from 'react'; import { createMediaStore } from '@vjs-10/media-store'; import { createContext, useContext, useMemo } from 'react'; import { useSyncExternalStoreWithSelector } from './useSyncExternalStoreWithSelector.js'; const identity = (x?: any) => x; /** * @description The {@link https://react.dev/learn/passing-data-deeply-with-context#context-an-alternative-to-passing-props|React Context} * used "under the hood" for media ui state updates, state change requests, and the hooks and providers that integrate with this context. * It is unlikely that you will/should be using `MediaContext` directly. * * @see {@link MediaProvider} * @see {@link useMediaDispatch} * @see {@link useMediaSelector} */ export const MediaContext: Context = createContext(null); export function MediaProvider({ children }: { children: ReactNode }): JSX.Element { const value = useMemo(() => createMediaStore(), []); // useEffect(() => { // value?.dispatch({ // type: 'documentelementchangerequest', // detail: globalThis.document, // }); // return () => { // value?.dispatch({ // type: 'documentelementchangerequest', // detail: undefined, // }); // }; // }, []); return {children}; } export function useMediaStore(): MediaStore { return useContext(MediaContext); } export function useMediaDispatch(): (value: any) => unknown { const store = useContext(MediaContext); const dispatch = store?.dispatch ?? identity; return (value: any) => { return dispatch(value); }; } export function useMediaRef() { const dispatch = useMediaDispatch(); return (element: any): void => { // NOTE: This should get invoked with `null` when using as a `ref` callback whenever // the corresponding react media element instance (e.g. a `