-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[chore] Export effects types/utils and incapsulate dnd logic into new…
… hooks (#2384) * [chore] Export effects types/utils and incapsulate dnd logic into new hooks Signed-off-by: Ihor Dykhta <dikhta.igor@gmail.com> * try to fix tests Signed-off-by: Ihor Dykhta <dikhta.igor@gmail.com> * adjust mocks Signed-off-by: Ihor Dykhta <dikhta.igor@gmail.com> --------- Signed-off-by: Ihor Dykhta <dikhta.igor@gmail.com> Co-authored-by: Giuseppe Macrì <macri.giuseppe@gmail.com>
- Loading branch information
1 parent
2500a27
commit 08492a8
Showing
11 changed files
with
648 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) 2023 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
module.exports = require('./dist/effects'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import {useCallback, useState} from 'react'; | ||
import {useDispatch} from 'react-redux'; | ||
import {DragEndEvent, DragStartEvent} from '@dnd-kit/core'; | ||
|
||
import {reorderEffect, updateEffect} from '@kepler.gl/actions'; | ||
import {SORTABLE_EFFECT_PANEL_TYPE, SORTABLE_EFFECT_TYPE} from '@kepler.gl/constants'; | ||
import {reorderEffectOrder} from '@kepler.gl/utils'; | ||
import {Effect} from '@kepler.gl/types'; | ||
|
||
type DndEffectsHook = { | ||
activeEffect: Effect | undefined; | ||
onDragStart: (event: DragStartEvent) => void; | ||
onDragEnd: (event: DragEndEvent) => void; | ||
}; | ||
|
||
const useDndEffects: (effects: Effect[], effectOrder: string[]) => DndEffectsHook = ( | ||
effects, | ||
effectOrder | ||
) => { | ||
const dispatch = useDispatch(); | ||
const [activeEffect, setActiveEffect]: [activeEffect: Effect | undefined, setActiveEffect: (effect: Effect | undefined) => void] = useState(); | ||
const onEffectDragStart = useCallback( | ||
event => { | ||
const {active} = event; | ||
const newActiveEffect = effects.find(effect => effect.id === active.id); | ||
if (newActiveEffect) { | ||
setActiveEffect(newActiveEffect); | ||
if (newActiveEffect.config.isConfigActive) { | ||
dispatch(updateEffect(newActiveEffect.id, {isConfigActive: false})); | ||
} | ||
} | ||
}, | ||
[dispatch, effects] | ||
); | ||
|
||
const onEffectDragEnd = useCallback( | ||
event => { | ||
const {active, over} = event; | ||
|
||
const {id: activeEffectId} = active; | ||
const overType = over?.data?.current?.type; | ||
|
||
if (!overType) { | ||
setActiveEffect(undefined); | ||
return; | ||
} | ||
|
||
switch (overType) { | ||
// swaping effects | ||
case SORTABLE_EFFECT_TYPE: | ||
dispatch(reorderEffect(reorderEffectOrder(effectOrder, activeEffectId, over.id))); | ||
break; | ||
// moving effects within side panel | ||
case SORTABLE_EFFECT_PANEL_TYPE: | ||
// move effect to the end of the list | ||
dispatch( | ||
reorderEffect( | ||
reorderEffectOrder(effectOrder, activeEffectId, effectOrder[effectOrder.length - 1]) | ||
) | ||
); | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
setActiveEffect(undefined); | ||
}, | ||
[dispatch, effectOrder] | ||
); | ||
|
||
return {activeEffect, onDragStart: onEffectDragStart, onDragEnd: onEffectDragEnd}; | ||
}; | ||
|
||
export default useDndEffects; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import {useCallback, useState} from 'react'; | ||
import {useDispatch} from 'react-redux'; | ||
import {DragEndEvent, DragStartEvent} from '@dnd-kit/core'; | ||
import {layerConfigChange, reorderLayer, toggleLayerForMap} from '@kepler.gl/actions'; | ||
import { | ||
DROPPABLE_MAP_CONTAINER_TYPE, | ||
SORTABLE_LAYER_TYPE, | ||
SORTABLE_SIDE_PANEL_TYPE | ||
} from '@kepler.gl/constants'; | ||
import {reorderLayerOrder} from '@kepler.gl/reducers'; | ||
import {Layer} from '@kepler.gl/layers'; | ||
|
||
type DndEffectsHook = { | ||
activeLayer: Layer | undefined; | ||
onDragStart: (event: DragStartEvent) => void; | ||
onDragEnd: (event: DragEndEvent) => void; | ||
}; | ||
|
||
const useDndLayers: (layers: Layer[], layerOrder: string[]) => DndEffectsHook = (layers, layerOrder) => { | ||
const dispatch = useDispatch(); | ||
|
||
const [activeLayer, setActiveLayer]: [activeEffect: Layer | undefined, setActiveEffect: (effect: Layer | undefined) => void] = useState(); | ||
|
||
const onDragStart = useCallback( | ||
event => { | ||
const {active} = event; | ||
const newActiveLayer = layers.find(layer => layer.id === active.id); | ||
if (newActiveLayer) { | ||
setActiveLayer(newActiveLayer); | ||
if (newActiveLayer?.config.isConfigActive) { | ||
dispatch(layerConfigChange(newActiveLayer, {isConfigActive: false})); | ||
} | ||
} | ||
}, | ||
[dispatch, layers] | ||
); | ||
|
||
const onDragEnd = useCallback( | ||
event => { | ||
const {active, over} = event; | ||
|
||
const {id: activeLayerId} = active; | ||
const overType = over?.data?.current?.type; | ||
|
||
if (!overType) { | ||
setActiveLayer(undefined); | ||
return; | ||
} | ||
|
||
switch (overType) { | ||
// moving layers into maps | ||
case DROPPABLE_MAP_CONTAINER_TYPE: | ||
const mapIndex = over.data.current?.index ?? 0; | ||
dispatch(toggleLayerForMap(mapIndex, activeLayerId)); | ||
break; | ||
// swaping layers | ||
case SORTABLE_LAYER_TYPE: | ||
const newLayerOrder = reorderLayerOrder(layerOrder, activeLayerId, over.id); | ||
dispatch(reorderLayer(newLayerOrder)); | ||
break; | ||
// moving layers within side panel | ||
case SORTABLE_SIDE_PANEL_TYPE: | ||
// move layer to the end of the list | ||
dispatch( | ||
reorderLayer( | ||
reorderLayerOrder(layerOrder, activeLayerId, layerOrder[layerOrder.length - 1]) | ||
) | ||
); | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
setActiveLayer(undefined); | ||
}, | ||
[dispatch, layerOrder] | ||
); | ||
|
||
return {activeLayer, onDragStart, onDragEnd}; | ||
}; | ||
|
||
export default useDndLayers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {useContext} from 'react'; | ||
import {FeatureFlagsContext} from '../context'; | ||
|
||
type FeatureFlags = { | ||
// Define your feature flags here | ||
[flagName: string]: boolean; | ||
}; | ||
|
||
// @ts-expect-error FeatureFlagsContext is typed as object | ||
const useFeatureFlags = () => useContext<FeatureFlags>(FeatureFlagsContext); | ||
|
||
export default useFeatureFlags; |
Oops, something went wrong.