2025-01-13 16:16:03 +01:00
|
|
|
/**
|
|
|
|
* Hooks: Provide the main API for components to interact with the store.
|
|
|
|
*
|
|
|
|
* These encapsulate store interactions, offering a consistent interface.
|
|
|
|
* Hooks simplify data access and manipulation for components.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*/
|
|
|
|
|
2025-01-17 19:25:10 +01:00
|
|
|
import { useDispatch } from '@wordpress/data';
|
2025-01-13 16:16:03 +01:00
|
|
|
|
2025-01-17 19:25:10 +01:00
|
|
|
import { createHooksForStore } from '../utils';
|
2025-01-13 16:16:03 +01:00
|
|
|
import { STORE_NAME } from './constants';
|
|
|
|
|
|
|
|
const useHooks = () => {
|
2025-01-17 19:25:10 +01:00
|
|
|
const { useTransient, usePersistent } = createHooksForStore( STORE_NAME );
|
|
|
|
const { persist } = useDispatch( STORE_NAME );
|
2025-01-13 16:16:03 +01:00
|
|
|
|
|
|
|
// Read-only flags and derived state.
|
|
|
|
// Nothing here yet.
|
|
|
|
|
|
|
|
// Transient accessors.
|
2025-01-17 19:25:10 +01:00
|
|
|
const [ isReady ] = useTransient( 'isReady' );
|
2025-01-13 16:16:03 +01:00
|
|
|
|
|
|
|
// Persistent accessors.
|
|
|
|
// TODO: Replace with real property.
|
2025-01-17 19:25:10 +01:00
|
|
|
const [ sampleValue, setSampleValue ] = usePersistent( 'sampleValue' );
|
2025-01-13 16:16:03 +01:00
|
|
|
|
|
|
|
return {
|
2025-01-13 17:08:59 +01:00
|
|
|
persist,
|
2025-01-13 16:16:03 +01:00
|
|
|
isReady,
|
|
|
|
sampleValue,
|
2025-01-13 17:08:59 +01:00
|
|
|
setSampleValue,
|
2025-01-13 16:16:03 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2025-01-30 16:39:26 +01:00
|
|
|
export const useStore = () => {
|
2025-01-13 17:08:59 +01:00
|
|
|
const { persist, isReady } = useHooks();
|
|
|
|
return { persist, isReady };
|
2025-01-13 16:16:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: Replace with real hook.
|
|
|
|
export const useSampleValue = () => {
|
|
|
|
const { sampleValue, setSampleValue } = useHooks();
|
|
|
|
|
|
|
|
return {
|
|
|
|
sampleValue,
|
|
|
|
setSampleValue,
|
|
|
|
};
|
|
|
|
};
|