Skip to content

The open alternative to Salesforce, designed for AI.

2.2 KB
Raw Blame
import { LEGEND_HIGHLIGHT_DIMMED_OPACITY } from '@/page-layout/widgets/graph/constants/LegendHighlightDimmedOpacity';
import { graphWidgetHighlightedLegendIdComponentState } from '@/page-layout/widgets/graph/states/graphWidgetHighlightedLegendIdComponentState';
import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateValue';
import { useAnimatedPath } from '@nivo/core';
import {
  type ComputedSeries,
  type LineCustomSvgLayerProps,
  type LineSeries,
} from '@nivo/line';
import { animated } from '@react-spring/web';
import { isDefined } from 'twenty-shared/utils';
import { doNotTranslate } from 'twenty-shared/i18n';

type CustomLinesLayerProps = {
  series: readonly ComputedSeries<LineSeries>[];
  lineGenerator: LineCustomSvgLayerProps<LineSeries>['lineGenerator'];
  lineWidth: number;
};

type AnimatedLinePathProps = {
  id: string;
  path: string;
  color: string;
  lineWidth: number;
};

const AnimatedLinePath = ({
  id,
  path,
  color,
  lineWidth,
}: AnimatedLinePathProps) => {
  const animatedPath = useAnimatedPath(path);

  const graphWidgetHighlightedLegendId = useAtomComponentStateValue(
    graphWidgetHighlightedLegendIdComponentState,
  );

  const isDimmed =
    isDefined(graphWidgetHighlightedLegendId) &&
    graphWidgetHighlightedLegendId !== id;

  return (
    <animated.path
      d={animatedPath}
      fill="none"
      stroke={color}
      strokeWidth={lineWidth}
      opacity={isDimmed ? LEGEND_HIGHLIGHT_DIMMED_OPACITY : 1}
      style={{
        transition: doNotTranslate('opacity 0.15s ease-in-out'),
      }}
    />
  );
};

export const CustomLinesLayer = ({
  series,
  lineGenerator,
  lineWidth,
}: CustomLinesLayerProps) => {
  return (
    <g>
      {[...series].reverse().map((seriesItem) => {
        const path = lineGenerator(
          seriesItem.data.map((point) => point.position),
        );

        if (!isDefined(path)) {
          return null;
        }

        return (
          <AnimatedLinePath
            key={seriesItem.id}
            id={String(seriesItem.id)}
            path={path}
            color={seriesItem.color}
            lineWidth={lineWidth}
          />
        );
      })}
    </g>
  );
};