diff --git a/site/source/components/ShareSimulationBanner/index.tsx b/site/source/components/ShareSimulationBanner/index.tsx index 180fadaf6..7529c4068 100644 --- a/site/source/components/ShareSimulationBanner/index.tsx +++ b/site/source/components/ShareSimulationBanner/index.tsx @@ -1,9 +1,13 @@ import { Grid } from '@mui/material' import Emoji from 'Components/utils/Emoji' +import { SitePathsContext } from 'Components/utils/SitePathsContext' import { Button } from 'DesignSystem/buttons' import { Spacing } from 'DesignSystem/layout' import PopoverWithTrigger from 'DesignSystem/PopoverWithTrigger' -import { useContext } from 'react' +import useSimulatorsData, { + SimulatorId, +} from '../../pages/Simulateurs/metadata' +import { useContext, useMemo } from 'react' import { Trans, useTranslation } from 'react-i18next' import { useSelector } from 'react-redux' import { situationSelector } from 'Selectors/simulationSelectors' @@ -15,10 +19,29 @@ import { ShareSimulationPopup } from './ShareSimulationPopup' export function useUrl() { const situation = useSelector(situationSelector) const searchParams = useParamsFromSituation(situation) + const sitePaths = useContext(SitePathsContext) + const simulatorData = useSimulatorsData() + + type KeyofSimulator = keyof typeof sitePaths.simulateurs + + const simulatorPath = useMemo(() => { + const iframePath = window.location.pathname.split(/[/?]/).slice(-1)[0] + const simulatorKey = ( + Object.keys(sitePaths.simulateurs) as KeyofSimulator[] + ).find( + (key) => + key in simulatorData && + simulatorData[key as SimulatorId]?.iframePath === iframePath + ) + + return simulatorKey ? sitePaths.simulateurs[simulatorKey] : '' + }, [simulatorData, sitePaths.simulateurs]) + searchParams.set('utm_source', 'sharing') return [ window.location.origin, - window.location.pathname.replace('iframes/', ''), + window.location.pathname.replace(/\/iframes\/.+/, ''), + simulatorPath, '?', searchParams.toString(), ].join('')