Skip to content

Commit

Permalink
front: fix typing
Browse files Browse the repository at this point in the history
Signed-off-by: Achraf Mohyeddine <[email protected]>
  • Loading branch information
achrafmohye committed Dec 11, 2024
1 parent f8d09aa commit a5c1dad
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { useEffect, useState } from 'react';

import { Table, TR, TH, TD } from '@ag-media/react-pdf-table';
import { Page, Text, Image, Document, View } from '@react-pdf/renderer';
import { useTranslation } from 'react-i18next';
Expand All @@ -12,13 +14,14 @@ import { secToMin } from 'utils/timeManipulation';

import styles from './SimulationReportStyleSheet';
import type { SimulationReportSheetProps } from '../../types';
import { getStopDurationTime } from '../../utils/formatSimulationReportSheet';
import { base64ToJpeg, getStopDurationTime } from '../../utils/formatSimulationReportSheet';

const SimulationReportSheet = ({
stdcmLinkedPaths,
stdcmData,
consist,
simulationReportSheetNumber,
mapCanvas,
operationalPointsList,
tolerances,
username,
Expand All @@ -33,6 +36,28 @@ const SimulationReportSheet = ({
const convoyLength = consist?.totalLength ?? rollingStock.length;
const convoyMaxSpeed = consist?.maxSpeed ?? msToKmh(rollingStock.max_speed);

const [mapImageUrl, setMapImageUrl] = useState<string | null>(null);

// Convert image to JPEG
useEffect(() => {
if (mapCanvas) {
base64ToJpeg(mapCanvas, 0.8).then((blob) => {
const objectUrl = URL.createObjectURL(blob);
setMapImageUrl(objectUrl);
});
}
}, [mapCanvas]);

// Cleanup the object URL when the component is unmounted or before a new one is created
useEffect(
() => () => {
if (mapImageUrl) {
URL.revokeObjectURL(mapImageUrl);
}
},
[mapImageUrl]
);

const getArrivalTime = (
step: PathStep,
{ isFirstStep, isLastStep }: { isFirstStep?: boolean; isLastStep?: boolean }
Expand Down Expand Up @@ -380,6 +405,11 @@ const SimulationReportSheet = ({
<View style={styles.simulation.horizontalBar} />
</View>
</View>
{mapCanvas && (
<View style={styles.map.map} id="simulationMap">
{mapImageUrl && <Image src={mapImageUrl} />}
</View>
)}
<View style={styles.footer.warrantyBox}>
<Text style={styles.footer.warrantyMessage}>{t('withoutWarranty')}</Text>
</View>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMemo } from 'react';
import { useMemo, useState } from 'react';

import { Button } from '@osrd-project/ui-core';
import { PDFDownloadLink } from '@react-pdf/renderer';
Expand Down Expand Up @@ -46,6 +46,7 @@ const StcdmResults = ({
pathTrackRanges,
}: StcdmResultsProps) => {
const { t } = useTranslation('stdcm', { keyPrefix: 'simulation.results' });
const [mapCanvas, setMapCanvas] = useState<string>();

const selectedSimulation = simulationsList[selectedSimulationIndex];
const { outputs } = selectedSimulation || {};
Expand Down Expand Up @@ -104,6 +105,7 @@ const StcdmResults = ({
tolerances={selectedSimulation.inputs.tolerances}
username={username}
simulationReportSheetNumber={simulationReportSheetNumber}
mapCanvas={mapCanvas}
operationalPointsList={operationalPointsList}
/>
}
Expand Down Expand Up @@ -170,6 +172,7 @@ const StcdmResults = ({
hideAttribution
showStdcmAssets
isFeasible={!hasConflictResults}
setMapCanvas={setMapCanvas}
pathGeometry={outputs?.pathProperties?.geometry}
simulationPathSteps={simulationPathSteps}
/>
Expand Down
16 changes: 11 additions & 5 deletions front/src/applications/stdcm/hooks/useStdcmResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,25 @@ const useStdcmResults = (
dispatch
);
const operationalPointsWithMetadata = operational_points.map((op) => {
let metadata;
const associatedTrackSection = trackSections[op.part.track];
if (associatedTrackSection) {
metadata = associatedTrackSection.properties?.extensions?.sncf;
}
const sncf = associatedTrackSection?.properties?.extensions?.sncf;

const metadata =
sncf && Object.values(sncf).every((value) => value !== undefined)
? {
lineCode: sncf.line_code!,
lineName: sncf.line_name!,
trackName: sncf.track_name!,
trackNumber: sncf.track_number!,
}
: undefined;

return {
...op,
metadata,
};
});

// Add unique IDs to operationalPointsWithMetadata
const operationalPointsWithUniqueIds = operationalPointsWithMetadata.map((op, index) => ({
...op,
id: `${op.id}-${op.position}-${index}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export function getOperationalPointsWithTimes(
duration: durationInSeconds,
departureTime,
stopEndTime,
trackName: op.metadata?.track_name,
trackName: op.metadata?.trackName,
};
});

Expand Down
6 changes: 5 additions & 1 deletion front/src/modules/pathfinding/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import { getPointCoordinates } from 'utils/geometry';
import getStepLocation from './helpers/getStepLocation';

export const formatSuggestedOperationalPoints = (
operationalPoints: NonNullable<Required<PathProperties['operational_points']>>,
operationalPoints: Array<
NonNullable<Required<PathProperties['operational_points']>>[number] & {
metadata?: NonNullable<SuggestedOP['metadata']>;
}
>,
geometry: GeoJsonLineString,
pathLength: number
): SuggestedOP[] =>
Expand Down

0 comments on commit a5c1dad

Please sign in to comment.