Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] Making EffectPanelHeader actions configurable #2432

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
91 changes: 66 additions & 25 deletions src/components/src/effects/effect-panel-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export type EffectPanelHeaderProps = {
effectId: string;
isEnabled: boolean;
isConfigActive: boolean;
isJsonEditorActive: boolean;
showSortHandle?: boolean;
isDragNDropEnabled: boolean;
onToggleEnabled: () => void;
Expand All @@ -52,6 +53,13 @@ export type EffectPanelHeaderProps = {
hidden: React.ComponentType<Partial<BaseProps>>;
enableConfig: React.ComponentType<Partial<BaseProps>>;
};
actionItems?: {
key: string;
isHidden?: boolean;
tooltip: string;
classNames?: Record<string, boolean>;
icon: React.ElementType;
}[];
};

export const defaultProps = {
Expand Down Expand Up @@ -173,38 +181,71 @@ export function EffectPanelHeaderActionSectionFactory(
onToggleEnabled,
onRemoveEffect,
onToggleEnableConfig,
actionItems,
actionIcons = defaultActionIcons
} = props;

const effectActionItems = useMemo(
() =>
actionItems ?? [
{
key: 'remove-effect',
isHidden: true,
tooltip: 'tooltip.removeEffect',
onClick: onRemoveEffect,
icon: actionIcons.remove
},
{
key: 'visibility-toggle',
tooltip: isEnabled ? 'tooltip.disableEffect' : 'tooltip.enabledEffect',
onClick: onToggleEnabled,
icon: isEnabled ? actionIcons.visible : actionIcons.hidden
},
{
key: 'enable-config',
classNames: {'is-open': isConfigActive},
tooltip: 'tooltip.effectSettings',
onClick: onToggleEnableConfig,
icon: actionIcons.enableConfig
}
],
[actionItems, isEnabled, onRemoveEffect, onToggleEnabled, actionIcons]
);

return (
<HeaderActionSection className="effect-panel__header__actions">
<StyledPanelHeaderHiddenActions isConfigActive={isConfigActive}>
<PanelHeaderAction
className="effect__remove-effect"
testId="remove-effect-action"
id={effectId}
tooltip={'tooltip.removeEffect'}
onClick={onRemoveEffect}
tooltipType="error"
IconComponent={actionIcons.remove}
/>
{effectActionItems
// @ts-expect-error typed in follow up
.filter(item => Boolean(item.isHidden))
.map((item, i) => (
<PanelHeaderAction
key={item.key}
className={`effect__${item.key}`}
testId={`${item.key}-action`}
id={effectId}
tooltip={item.tooltip}
onClick={item.onClick}
tooltipType="error"
IconComponent={item.icon}
/>
))}
</StyledPanelHeaderHiddenActions>
<PanelHeaderAction
className="effect__visibility-toggle"
id={effectId}
tooltip={isEnabled ? 'tooltip.disableEffect' : 'tooltip.enabledEffect'}
onClick={onToggleEnabled}
IconComponent={isEnabled ? actionIcons.visible : actionIcons.hidden}
/>
<PanelHeaderAction
className={classnames('effect__enable-config ', {
'is-open': isConfigActive
})}
id={effectId}
tooltip={'tooltip.effectSettings'}
onClick={onToggleEnableConfig}
IconComponent={actionIcons.enableConfig}
/>
{effectActionItems
// @ts-expect-error typed in follow up
.filter(item => !item.isHidden)
.map((item, i) => (
<PanelHeaderAction
key={item.key}
className={classnames(`effect__${item.key}`, item.classNames)}
testId={`${item.key}-action`}
id={effectId}
tooltip={item.tooltip}
onClick={item.onClick}
tooltipType="error"
IconComponent={item.icon}
/>
))}
</HeaderActionSection>
);
};
Expand Down
3 changes: 2 additions & 1 deletion src/components/src/effects/effect-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function EffectPanelFactory(

render() {
const {effect, isDraggable, listeners} = this.props;
const {id, type, isConfigActive, isEnabled} = effect;
const {id, type, isConfigActive, isJsonEditorActive, isEnabled} = effect;

const sortingAllowed = type !== LIGHT_AND_SHADOW_EFFECT.type;

Expand All @@ -96,6 +96,7 @@ function EffectPanelFactory(
effectId={id}
type={type}
isEnabled={isEnabled}
isJsonEditorActive={isJsonEditorActive}
onToggleEnabled={this._toggleEnabled}
onRemoveEffect={this._removeEffect}
onToggleEnableConfig={this._toggleConfigActive}
Expand Down
4 changes: 4 additions & 0 deletions src/effects/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class Effect implements EffectInterface {
type: string;
isEnabled: boolean;
isConfigActive: boolean;
isJsonEditorActive: boolean;
// effect specific parameters for a deck.gl effect (uniforms)
parameters: {[key: string]: any};
deckEffect: any;
Expand All @@ -24,6 +25,7 @@ export class Effect implements EffectInterface {
this.type = _props.type;
this.isEnabled = _props.isEnabled;
this.isConfigActive = _props.isConfigActive;
this.isJsonEditorActive = _props.isJsonEditorActive;
this.parameters = _props.parameters;

this._uiConfig = POSTPROCESSING_EFFECTS[this.type]?.parameters || [];
Expand All @@ -42,6 +44,7 @@ export class Effect implements EffectInterface {
type: props.type || DEFAULT_POST_PROCESSING_EFFECT_TYPE,
isEnabled: props.isEnabled ?? true,
isConfigActive: props.isConfigActive ?? true,
isJsonEditorActive: props.isJsonEditorActive ?? false,
parameters: {...props.parameters}
};
}
Expand All @@ -51,6 +54,7 @@ export class Effect implements EffectInterface {
this.type = props.type ?? this.type;
this.isEnabled = props.isEnabled ?? this.isEnabled;
this.isConfigActive = props.isConfigActive ?? this.isConfigActive;
this.isJsonEditorActive = props.isJsonEditorActive ?? this.isJsonEditorActive;
this.parameters = {...this.parameters, ...props.parameters};
}

Expand Down
16 changes: 15 additions & 1 deletion src/reducers/src/vis-state-updaters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1517,12 +1517,26 @@ export const updateEffectUpdater = (
return state;
}

let effectOrder = state.effectOrder;
if (props.id !== undefined && props.id !== id) {
const idx2 = state.effects.findIndex(l => l.id === props.id);
if (idx2 >= 0) {
Console.warn(`can not update effect with existing effect id ${id}`);
return state;
}

effectOrder = effectOrder.map(effectOrderId =>
effectOrderId === id ? (props.id as string) : effectOrderId
);
}

const newEffects = [...state.effects];
newEffects[idx].setProps(props);

return {
...state,
effects: newEffects
effects: newEffects,
effectOrder
};
};

Expand Down
2 changes: 2 additions & 0 deletions src/types/effects.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type EffectDescription = {
export type EffectUpdateProps = {
isEnabled: boolean;
isConfigActive: boolean;
isJsonEditorActive: boolean;
// effect specific parameters for a deck.gl effect (uniforms)
parameters: {[key: string]: any};
};
Expand All @@ -32,6 +33,7 @@ export interface Effect {
type: string;
isEnabled: boolean;
isConfigActive: boolean;
isJsonEditorActive: boolean;
// effect specific parameters for a deck.gl effect (uniforms)
parameters: {[key: string]: any};
deckEffect: any;
Expand Down
13 changes: 12 additions & 1 deletion test/node/reducers/vis-state-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5552,6 +5552,7 @@ test('#VisStateUpdater -> updateEffect', t => {

let nextState = reducer(initialState, VisStateActions.addEffect({id: 'e_1'}));
nextState = reducer(nextState, VisStateActions.addEffect({}));
nextState = reducer(nextState, VisStateActions.addEffect({id: 'e_3'}));

const expectedEffect = {
id: 'e_1',
Expand All @@ -5567,9 +5568,19 @@ test('#VisStateUpdater -> updateEffect', t => {

cmpEffects(t, expectedEffect, nextState.effects[0], {id: true});

// bad id, shouldn't fail
// non-existing id, shouldn't fail
nextState = reducer(nextState, VisStateActions.updateEffect('fake_id', {}));

// update effect id
nextState = reducer(nextState, VisStateActions.updateEffect('e_1', {id: 'e_2'}));
cmpEffects(t, {...expectedEffect, id: 'e_2'}, nextState.effects[0], {id: true});
t.equal(nextState.effectOrder[2], 'e_2', 'Effect id should be updated');

// update effect to an existing effect id
nextState = reducer(nextState, VisStateActions.updateEffect('e_2', {id: 'e_3'}));
cmpEffects(t, {...expectedEffect, id: 'e_2'}, nextState.effects[0], {id: true});
t.equal(nextState.effectOrder[2], 'e_2', "Effect id shouldn't be updated");

t.end();
});

Expand Down