@@ -10,10 +10,8 @@ import {
1010 useMemo ,
1111 useState ,
1212 ReactNode ,
13- RefObject ,
1413 useCallback ,
1514} from "react" ;
16- import { IUnit } from "@macrostrat/column-views" ;
1715
1816type UnitSelectDispatch = (
1917 unit : BaseUnit | null ,
@@ -24,13 +22,8 @@ type UnitSelectDispatch = (
2422const UnitSelectionContext = createContext < BaseUnit | null > ( null ) ;
2523const DispatchContext = createContext < UnitSelectDispatch | null > ( null ) ;
2624
27- export function useUnitSelector ( u : BaseUnit | null ) {
28- const dispatch = useContext ( DispatchContext ) ;
29- return ( target : HTMLElement , evt : Event ) => {
30- console . log ( "Dispatch" , u , target , evt ) ;
31- dispatch ?.( u , target , evt ) ;
32- evt . stopPropagation ( ) ;
33- } ;
25+ export function useUnitSelectionDispatch ( ) {
26+ return useContext ( DispatchContext ) ;
3427}
3528
3629export function useSelectedUnit ( ) {
@@ -78,9 +71,15 @@ function BaseUnitSelectionProvider<T extends BaseUnit>({
7871 const value = useMemo ( ( ) => unit , [ unit ?. unit_id ] ) ;
7972
8073 const _onUnitSelected = useCallback (
81- ( unit : T , target : HTMLElement , event : Event ) => {
82- setUnit ( unit ) ;
83- onUnitSelected ?.( unit , target , event ) ;
74+ ( u : T , target : HTMLElement , event : Event ) => {
75+ let newUnit = u ;
76+ if ( u == unit ) {
77+ // If the same unit is selected, deselect it
78+ newUnit = null ;
79+ }
80+
81+ setUnit ( newUnit ) ;
82+ onUnitSelected ?.( newUnit , target , event ) ;
8483 } ,
8584 [ setUnit , onUnitSelected ]
8685 ) ;
0 commit comments