11import { type CustomDomain } from '@h5web/lib' ;
2- import { isDefined } from '@h5web/shared/guards' ;
32import {
43 type ColorScaleType ,
54 type ComplexHeatmapVisType ,
65 ComplexVisType ,
76 type NoProps ,
87 ScaleType ,
98} from '@h5web/shared/vis-models' ;
10- import { useMap } from '@react-hookz/web' ;
119import {
1210 createContext ,
1311 type PropsWithChildren ,
@@ -17,6 +15,7 @@ import {
1715import { createStore , type StoreApi , useStore } from 'zustand' ;
1816import { persist } from 'zustand/middleware' ;
1917
18+ import { useSuggestion } from '../hooks' ;
2019import { type ColorMap } from './models' ;
2120
2221export interface HeatmapConfig {
@@ -27,7 +26,7 @@ export interface HeatmapConfig {
2726 setColorMap : ( colorMap : ColorMap ) => void ;
2827
2928 invertColorMap : boolean ;
30- toggleColorMapInversion : ( ) => void ;
29+ setInvertColorMap : ( invertColorMap : boolean ) => void ;
3130
3231 scaleType : ColorScaleType ;
3332 setScaleType : ( scaleType : ColorScaleType ) => void ;
@@ -36,16 +35,16 @@ export interface HeatmapConfig {
3635 setComplexVisType : ( complexVisType : ComplexHeatmapVisType ) => void ;
3736
3837 showGrid : boolean ;
39- toggleGrid : ( ) => void ;
38+ setShowGrid : ( showGrid : boolean ) => void ;
4039
4140 keepRatio : boolean ;
42- toggleKeepRatio : ( ) => void ;
41+ setKeepRatio : ( keepRatio : boolean ) => void ;
4342
4443 flipXAxis : boolean ;
45- toggleXAxisFlip : ( ) => void ;
44+ setFlipXAxis : ( flipXAxis : boolean ) => void ;
4645
4746 flipYAxis : boolean ;
48- toggleYAxisFlip : ( ) => void ;
47+ setFlipYAxis : ( flipYAxis : boolean ) => void ;
4948}
5049
5150function createHeatmapConfigStore ( ) {
@@ -59,9 +58,7 @@ function createHeatmapConfigStore() {
5958 setColorMap : ( colorMap ) => set ( { colorMap } ) ,
6059
6160 invertColorMap : false ,
62- toggleColorMapInversion : ( ) => {
63- set ( ( state ) => ( { invertColorMap : ! state . invertColorMap } ) ) ;
64- } ,
61+ setInvertColorMap : ( invertColorMap ) => set ( { invertColorMap } ) ,
6562
6663 scaleType : ScaleType . Linear ,
6764 setScaleType : ( scaleType ) => set ( ( ) => ( { scaleType } ) ) ,
@@ -70,23 +67,20 @@ function createHeatmapConfigStore() {
7067 setComplexVisType : ( complexVisType ) => set ( ( ) => ( { complexVisType } ) ) ,
7168
7269 showGrid : true ,
73- toggleGrid : ( ) => set ( ( state ) => ( { showGrid : ! state . showGrid } ) ) ,
70+ setShowGrid : ( showGrid ) => set ( { showGrid } ) ,
7471
7572 keepRatio : true ,
76- toggleKeepRatio : ( ) =>
77- set ( ( state ) => ( { keepRatio : ! state . keepRatio } ) ) ,
73+ setKeepRatio : ( keepRatio ) => set ( { keepRatio } ) ,
7874
7975 flipYAxis : false ,
80- toggleYAxisFlip : ( ) =>
81- set ( ( state ) => ( { flipYAxis : ! state . flipYAxis } ) ) ,
76+ setFlipYAxis : ( flipYAxis ) => set ( { flipYAxis } ) ,
8277
8378 flipXAxis : false ,
84- toggleXAxisFlip : ( ) =>
85- set ( ( state ) => ( { flipXAxis : ! state . flipXAxis } ) ) ,
79+ setFlipXAxis : ( flipXAxis ) => set ( { flipXAxis } ) ,
8680 } ) ,
8781 {
8882 name : 'h5web:heatmap' ,
89- version : 11 ,
83+ version : 12 ,
9084 } ,
9185 ) ,
9286 ) ;
@@ -105,30 +99,21 @@ export function HeatmapConfigProvider(props: PropsWithChildren<NoProps>) {
10599}
106100
107101export function useHeatmapConfig (
108- initialSuggestedOpts : Partial <
109- Pick < HeatmapConfig , 'scaleType' | 'keepRatio' >
110- > = { } ,
102+ suggestions : Partial < Pick < HeatmapConfig , 'scaleType' | 'keepRatio' > > = { } ,
111103) : HeatmapConfig {
112- const suggestedOpts = useMap (
113- Object . entries ( initialSuggestedOpts ) . filter ( ( [ , val ] ) => isDefined ( val ) ) ,
104+ const config = useStore ( useContext ( StoreContext ) ) ;
105+
106+ const [ scaleType , setScaleType ] = useSuggestion (
107+ suggestions . scaleType ,
108+ config . scaleType ,
109+ config . setScaleType ,
110+ ) ;
111+
112+ const [ keepRatio , setKeepRatio ] = useSuggestion (
113+ suggestions . keepRatio ,
114+ config . keepRatio ,
115+ config . setKeepRatio ,
114116 ) ;
115117
116- const persistedConfig = useStore ( useContext ( StoreContext ) ) ;
117- const {
118- setScaleType : setPersistedScaleType ,
119- toggleKeepRatio : togglePersistedKeepRatio ,
120- } = persistedConfig ;
121-
122- return {
123- ...persistedConfig ,
124- ...Object . fromEntries ( suggestedOpts . entries ( ) ) ,
125- setScaleType : ( scaleType : ColorScaleType ) => {
126- setPersistedScaleType ( scaleType ) ;
127- suggestedOpts . delete ( 'scaleType' ) ;
128- } ,
129- toggleKeepRatio : ( ) => {
130- togglePersistedKeepRatio ( ) ;
131- suggestedOpts . delete ( 'keepRatio' ) ;
132- } ,
133- } ;
118+ return { ...config , scaleType, keepRatio, setScaleType, setKeepRatio } ;
134119}
0 commit comments