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

ui-speedspacechart: fix display issues when the GEV is integrated in OSRD #254

Merged
merged 1 commit into from
Jul 24, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions ui-speedspacechart/src/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getAdaptiveHeight,
positionOnGraphScale,
getLinearLayerMarginTop,
getLinearLayersDisplayedHeight,
slopesValues,
findPreviousAndNextPosition,
} from '../components/utils';
Expand Down Expand Up @@ -267,6 +268,48 @@ describe('getLinearLayerMarginTop', () => {
});
});

describe('getLinearLayersDisplayedHeight', () => {
let layersDisplay = {
steps: false,
declivities: false,
speedLimits: false,
temporarySpeedLimits: false,
electricalProfiles: false,
powerRestrictions: false,
speedLimitTags: false,
};

it('should return the sum of the heights of the linear layers that are displayed', () => {
layersDisplay = {
...layersDisplay,
electricalProfiles: true,
powerRestrictions: true,
speedLimitTags: true,
};
expect(getLinearLayersDisplayedHeight(layersDisplay)).toBe(136);
});

it('should return one linear layer height', () => {
layersDisplay = {
...layersDisplay,
electricalProfiles: true,
powerRestrictions: false,
speedLimitTags: false,
};

expect(getLinearLayersDisplayedHeight(layersDisplay)).toBe(56);
});

it('should return 0 if no linear layers are displayed', () => {
layersDisplay = {
...layersDisplay,
electricalProfiles: false,
};

expect(getLinearLayersDisplayedHeight(layersDisplay)).toBe(0);
});
});

describe('findPreviousAndNextPosition', () => {
const xPositionReference = (ref: number) => ref;

Expand Down
7 changes: 7 additions & 0 deletions ui-speedspacechart/src/components/SpeedSpaceChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type SpeedSpaceChartProps = {
width: number;
height: number;
backgroundColor: string;
setHeight: React.Dispatch<React.SetStateAction<number>>;
data: Data;
translations?: {
detailsBoxDisplay: {
Expand Down Expand Up @@ -56,6 +57,7 @@ const SpeedSpaceChart = ({
height,
backgroundColor,
data,
setHeight,
translations,
}: SpeedSpaceChartProps) => {
const [store, setStore] = useState<Store>({
Expand Down Expand Up @@ -145,6 +147,10 @@ const SpeedSpaceChart = ({
}
}, [data]);

useEffect(() => {
setHeight(dynamicHeight);
}, [setHeight, dynamicHeight]);

return (
<div
style={{
Expand All @@ -166,6 +172,7 @@ const SpeedSpaceChart = ({
style={{ width: adjustedWidthRightAxis }}
>
<SettingsPanel
globalHeight={dynamicHeight}
color={backgroundColor}
store={store}
setStore={setStore}
Expand Down
4 changes: 2 additions & 2 deletions ui-speedspacechart/src/components/common/DetailsBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ const DetailsBox = ({
{tractionStatus && effortText && <span id="effort-text">{effortText}</span>}
{electricalModeText && (
<div id="electrical-mode-text">
<p>{electricalModeText}</p>
{electricalProfiles && <p className="ml-2">{electricalProfileText}</p>}
<span>{electricalModeText}</span>
{electricalProfiles && <span className="ml-2">{electricalProfileText}</span>}
</div>
)}
{powerRestrictions && <span id="power-restriction">{powerRestrictionText}</span>}
Expand Down
22 changes: 19 additions & 3 deletions ui-speedspacechart/src/components/common/SettingsPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import type { Store } from '../../types/chartTypes';
import type { SpeedSpaceChartProps } from '../SpeedSpaceChart';
import { DETAILS_BOX_SELECTION, LAYERS_SELECTION } from '../const';
import { X } from '@osrd-project/ui-icons';
import { Checkbox } from '@osrd-project/ui-core';
import { checkLayerData } from '../utils';
import { checkLayerData, getAdaptiveHeight } from '../utils';

const SETTINGS_PANEL_BASE_HEIGHT = 442;
const SPEEDSPACECHART_BASE_HEIGHT = 521.5;

type SettingsPanelProps = {
globalHeight: number;
color: string;
store: Store;
setStore: React.Dispatch<React.SetStateAction<Store>>;
Expand All @@ -15,12 +19,14 @@ type SettingsPanelProps = {
};

const SettingsPanel = ({
globalHeight,
color,
store,
setStore,
setIsMouseHoveringSettingsPanel,
translations,
}: SettingsPanelProps) => {
const [height, setHeight] = useState(`${SETTINGS_PANEL_BASE_HEIGHT}px`);
const closeSettingsPanel = () => {
setIsMouseHoveringSettingsPanel(false);
setStore((prev) => ({
Expand All @@ -29,10 +35,20 @@ const SettingsPanel = ({
}));
};

useEffect(() => {
const linearLayersHeight = getAdaptiveHeight(0, store.layersDisplay);

if (globalHeight < SPEEDSPACECHART_BASE_HEIGHT + linearLayersHeight) {
setHeight(
`${globalHeight - linearLayersHeight - (SPEEDSPACECHART_BASE_HEIGHT - SETTINGS_PANEL_BASE_HEIGHT)}px`
);
}
}, [globalHeight, store.layersDisplay]);

return (
<div
id="settings-panel"
style={{ background: `rgba(${color.substring(4, color.length - 1)}, 0.4)` }}
style={{ background: `rgba(${color.substring(4, color.length - 1)}, 0.4)`, height }}
className="font-sans"
onMouseEnter={() => setIsMouseHoveringSettingsPanel(true)}
onMouseLeave={() => setIsMouseHoveringSettingsPanel(false)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const FrontInteractivityLayer = ({
return (
<canvas
id="front-interactivity-layer"
className="absolute ml-10 mt-2"
className="absolute"
ref={canvas}
width={width}
height={height}
Expand Down
28 changes: 17 additions & 11 deletions ui-speedspacechart/src/components/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
LINEAR_LAYER_SEPARATOR_HEIGHT,
LINEAR_LAYERS_HEIGHTS_BY_NAME,
LAYERS_SELECTION,
LINEAR_LAYERS_HEIGHTS,
} from './const';

type SpeedRangeValues = {
Expand Down Expand Up @@ -75,17 +76,13 @@ export const getAdaptiveHeight = (
layersDisplay: Store['layersDisplay'],
isIncludingLinearLayers: boolean = true
): number => {
let adjustment = 0;
const adjustment = [
layersDisplay.electricalProfiles ? LINEAR_LAYERS_HEIGHTS.ELECTRICAL_PROFILES_HEIGHT : 0,
layersDisplay.powerRestrictions ? LINEAR_LAYERS_HEIGHTS.POWER_RESTRICTIONS_HEIGHT : 0,
layersDisplay.speedLimitTags ? LINEAR_LAYERS_HEIGHTS.SPEED_LIMIT_TAGS_HEIGHT : 0,
].reduce((acc, curr) => acc + curr, 0);

Object.keys(LINEAR_LAYERS_HEIGHTS_BY_NAME).forEach((key) => {
const layer = key as keyof typeof LINEAR_LAYERS_HEIGHTS_BY_NAME;
if (layersDisplay[layer]) {
adjustment += isIncludingLinearLayers
? LINEAR_LAYERS_HEIGHTS_BY_NAME[layer]
: -LINEAR_LAYERS_HEIGHTS_BY_NAME[layer];
}
});
return height + adjustment;
return (height += isIncludingLinearLayers ? adjustment : -adjustment);
};

/**
Expand All @@ -108,6 +105,13 @@ export const getLinearLayerMarginTop = (
return height + adjustment - MARGINS.MARGIN_BOTTOM;
};

export const getLinearLayersDisplayedHeight = (layersDisplay: Store['layersDisplay']) =>
[
layersDisplay.electricalProfiles ? LINEAR_LAYERS_HEIGHTS.ELECTRICAL_PROFILES_HEIGHT : 0,
layersDisplay.powerRestrictions ? LINEAR_LAYERS_HEIGHTS.POWER_RESTRICTIONS_HEIGHT : 0,
layersDisplay.speedLimitTags ? LINEAR_LAYERS_HEIGHTS.SPEED_LIMIT_TAGS_HEIGHT : 0,
].reduce((acc, curr) => acc + curr, 0);

SharglutDev marked this conversation as resolved.
Show resolved Hide resolved
/**
* Calculates the position on the graph scale based on the given parameters.
* @param position - The current position.
Expand Down Expand Up @@ -191,7 +195,9 @@ export const drawLinearLayerBackground = (
*/
export const checkLayerData = (store: Store, selection: (typeof LAYERS_SELECTION)[number]) => {
return (
(selection === 'electricalProfiles' ||
(selection === 'speedLimits' ||
selection === 'temporarySpeedLimits' ||
selection === 'electricalProfiles' ||
selection === 'powerRestrictions' ||
selection === 'speedLimitTags') &&
!store[selection]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { useEffect, useState } from 'react';
import type { Meta, StoryObj } from '@storybook/react';
import '@osrd-project/ui-core/dist/theme.css';
import '@osrd-project/ui-speedspacechart/dist/theme.css';
import SpeedSpaceChart from '../components/SpeedSpaceChart';
import SpeedSpaceChart, { type SpeedSpaceChartProps } from '../components/SpeedSpaceChart';
import { pathPropertiesPmpLm } from './assets/path_properties_PMP_LM';
import { simulationPmpLm } from './assets/simulation_PMP_LM';
import { formatData } from './utils';
Expand All @@ -16,19 +17,58 @@ const data = formatData(
speedLimitTags
);

const SpeedSpaceChartStory = ({
height,
width,
backgroundColor,
data,
translations,
}: SpeedSpaceChartProps) => {
const [containerHeight, setContainerHeight] = useState(521.5);

useEffect(() => {
setContainerHeight(height);
}, [height]);

return (
<div style={{ height: containerHeight }}>
SharglutDev marked this conversation as resolved.
Show resolved Hide resolved
<SpeedSpaceChart
width={width}
height={height}
backgroundColor={backgroundColor}
data={data}
setHeight={setContainerHeight}
translations={translations}
/>
</div>
);
};

const meta: Meta<typeof SpeedSpaceChart> = {
title: 'SpeedSpaceChart/Rendering',
component: SpeedSpaceChart,
decorators: [(Story) => <Story />],
parameters: {
layout: 'centered',
backgrounds: {
default: 'dark',
},
},
args: {
width: 1440,
height: 521.5,
backgroundColor: 'rgb(247, 246, 238)',
data: data,
setHeight: () => {},
translations: translations,
},

render: (args) => <SpeedSpaceChartStory {...args} />,
tags: ['autodocs'],
};

export default meta;

type Story = StoryObj<typeof SpeedSpaceChart>;

export const SpeedSpaceChartDefault: Story = {
Expand All @@ -37,6 +77,7 @@ export const SpeedSpaceChartDefault: Story = {
height: 521.5,
backgroundColor: 'rgb(247, 246, 238)',
data,
setHeight: () => {},
translations,
},
};
1 change: 0 additions & 1 deletion ui-speedspacechart/src/styles/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@
#settings-panel {
position: absolute;
display: flex;
height: 27.625rem;
width: 32.875rem;
margin-right: 20px;
color: rgb(49, 46, 43);
Expand Down
5 changes: 4 additions & 1 deletion ui-speedspacechart/src/types/chartTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export type ElectricalPofilelValues = {
electricalProfile: string;
color?: string;
heightLevel?: number;
handled?: boolean;
};

export type PowerRestrictionValues = {
Expand All @@ -15,7 +16,7 @@ export type SpeedLimitTagValues = {
};

export type ElectrificationValues = {
type: 'electrification' | 'neutral_section';
type: 'electrification' | 'neutral_section' | 'non_electrified';
voltage?: '1500V' | '25000V';
lowerPantograph?: boolean;
};
Expand All @@ -37,6 +38,8 @@ export type Data = {
electricalProfiles?: LayerData<ElectricalPofilelValues>[];
powerRestrictions?: LayerData<PowerRestrictionValues>[];
speedLimitTags?: LayerData<SpeedLimitTagValues>[];
speedLimits?: LayerData<number>[];
temporarySpeedLimits?: LayerData<number>[];
};

export type Store = Data & {
Expand Down