import { useSnapshot } from '../use-snapshot.js' import { withDerived } from '../../vanilla/enhancers/with-derived.js' import type { VanillaStore } from '../../vanilla/index.js' import type { SnapshotOptions } from '../use-snapshot.js' import type { WithDerivedContributes } from '../../vanilla/enhancers/with-derived.js' export interface WithUseDerivedContributes extends WithDerivedContributes { /** * Get the derived state. */ useDerived: (options?: SnapshotOptions) => Derived } /** * Enhances a store with `useDerived` & `derived` method that returns the derived state. * * @param store - The store to enhance. * @param mapFn - The function to map the state to the derived state. * @returns The enhanced store. * * @since 0.2.0 * * @example * * ```tsx * const store = withDerived( * create({ * count: 123, * info: { name: 'Viki' }, * }), * (s) => ({ * isViki: s.info.name === 'Viki', * isNegative: s.count < 0, * }), * ) * // in normal JS/TS * console.log(store.derived().isViki) * // in React component * export function App() { * const { isNegative } = store.useDerived() * } * ``` */ export function withUseDerived< Store extends VanillaStore, Derived extends object = Store['mutate'], >( store: Store, mapFn: (state: Store['mutate']) => Derived = (s) => s as unknown as Derived ): Store & WithUseDerivedContributes { const storeWithDerived = withDerived(store, mapFn) return { ...storeWithDerived, useDerived: (options: SnapshotOptions = {}) => { return useSnapshot(store.mutate, mapFn, options) }, } }