import { getCombinedDomain } from '@h5web/lib' ; import type { ArrayShape , Dataset , ScalarShape , Value } from '@h5web/shared' ; import { isDefined , ScaleType , getBounds , getValidDomainForScale , assertDatasetValue , } from '@h5web/shared' ; import type { NdArray } from 'ndarray' ; import { useContext , useMemo } from 'react' ; import { createMemo } from 'react-use' ; import type { DimensionMapping } from '../../dimension-mapper/models' ; import { isAxis } from '../../dimension-mapper/utils' ; import { ProviderContext } from '../../providers/context' ; import { applyMapping , getBaseArray } from './utils' ; export function usePrefetchValues ( datasets , selection ? ) { const { valuesStore } = useContext ( ProviderContext ) ; datasets . filter ( isDefined ) . forEach ( ( dataset ) => { valuesStore . prefetch ( { dataset , selection } ) ; } ) ; } export function useDatasetValue < D extends Dataset < ArrayShape | ScalarShape > > ( dataset , selection ? ) ; export function useDatasetValue < D extends Dataset < ArrayShape | ScalarShape > > ( dataset , selection ? ) ; export function useDatasetValue < D extends Dataset < ArrayShape | ScalarShape > > ( dataset , selection ? ) { const { valuesStore } = useContext ( ProviderContext ) ; if ( ! dataset ) { return undefined ; } const value = valuesStore . get ( { dataset , selection } ) ; assertDatasetValue ( value , dataset ) ; return value ; } export function useDatasetValues < D extends Dataset < ArrayShape | ScalarShape > > ( datasets , selection ? ) { const { valuesStore } = useContext ( ProviderContext ) ; return Object . fromEntries ( datasets . map ( ( dataset ) => { const value = valuesStore . get ( { dataset , selection } ) ; assertDatasetValue ( value , dataset ) ; return [ dataset . name , value ] ; } ) ) ; } const useBounds = createMemo ( getBounds ) ; const useValidDomainForScale = createMemo ( getValidDomainForScale ) ; export function useDomain ( valuesArray , scaleType = ScaleType . Linear , errorArray ? ) { const bounds = useBounds ( valuesArray , errorArray ) ; return useValidDomainForScale ( bounds , scaleType ) ; } export function useDomains ( valuesArrays , scaleType = ScaleType . Linear ) { const allBounds = useMemo ( ( ) => { return valuesArrays . map ( ( arr ) => getBounds ( arr ) ) ; } , [ valuesArrays ] ) ; return useMemo ( ( ) => allBounds . map ( ( bounds ) => getValidDomainForScale ( bounds , scaleType ) ) , [ allBounds , scaleType ] ) ; } export const useCombinedDomain = createMemo ( getCombinedDomain ) ; const useBaseArray = createMemo ( getBaseArray ) ; const useApplyMapping = createMemo ( applyMapping ) ; export function useMappedArray < T , U extends T [ ] | undefined > ( value , dims , mapping , autoScale ? ) ; export function useMappedArray < T > ( value , dims , mapping , autoScale ? ) { const baseArray = useBaseArray ( value , dims ) ; const mappedArray = useApplyMapping ( baseArray , mapping ) ; return [ mappedArray , autoScale ? mappedArray : baseArray ] ; } export function useMappedArrays ( values , dims , mapping , autoScale ? ) { const baseArrays = useMemo ( ( ) => values . map ( ( v ) => getBaseArray ( v , dims ) ) , [ dims , values ] ) ; const mappedArrays = useMemo ( ( ) => baseArrays . map ( ( v ) => applyMapping ( v , mapping ) ) , [ baseArrays , mapping ] ) ; return [ mappedArrays , autoScale ? mappedArrays : baseArrays ] ; } export function useSlicedDimsAndMapping ( dims , dimMapping ) { return useMemo ( ( ) => [ dims . filter ( ( _ , i ) => isAxis ( dimMapping [ i ] ) ) , dimMapping . filter ( isAxis ) , ] , [ dimMapping , dims ] ) ; }