Skip to content

Commit

Permalink
front: fix exported function, no longer divided between two components
Browse files Browse the repository at this point in the history
Signed-off-by: Theo Macron <[email protected]>
  • Loading branch information
Akctarus committed Jan 8, 2025
1 parent f74d1fb commit 0d2c156
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
34 changes: 33 additions & 1 deletion ui-manchette-with-spacetimechart/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { InteractiveWaypoint, Waypoint } from '@osrd-project/ui-manchette/dist/types';
import type { OperationalPoint } from '@osrd-project/ui-spacetimechart/dist/lib/types';
import { filterVisibleElements } from '@osrd-project/ui-speedspacechart/src/components/utils';
import { clamp } from 'lodash';

import {
Expand All @@ -14,6 +13,39 @@ import { calcTotalDistance, getHeightWithoutLastWaypoint } from './utils';

type WaypointsOptions = { isProportional: boolean; yZoom: number; height: number };

type VisibilityFilterOptions<T> = {
elements: T[];
getPosition: (element: T) => number;
getWeight: (element: T) => number | undefined;
minSpace: number;
};

export const filterVisibleElements = <T>({
elements,
getPosition,
getWeight,
minSpace,
}: VisibilityFilterOptions<T>): T[] => {
const sortedElements = [...elements].sort((a, b) => (getWeight(b) ?? 0) - (getWeight(a) ?? 0));
const displayedElements: { element: T; position: number }[] = [];

for (const element of sortedElements) {
const position = getPosition(element);

const hasSpace = !displayedElements.some(
(displayed) => Math.abs(position - displayed.position) < minSpace
);

if (hasSpace) {
displayedElements.push({ element, position });
}
}

return displayedElements
.sort((a, b) => getPosition(a.element) - getPosition(b.element))
.map(({ element }) => element);
};

export const getDisplayedWaypoints = (
waypoints: Waypoint[],
{ height, isProportional, yZoom }: WaypointsOptions
Expand Down
1 change: 0 additions & 1 deletion ui-speedspacechart/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
],
"exports": {
"./dist/theme.css": "./dist/theme.css",
"./src/components/utils": "./src/components/utils",
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.esm.js"
Expand Down

0 comments on commit 0d2c156

Please sign in to comment.