From 6b5b0b530d22009222552c5a32ea20885f77314c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Rialland?= Date: Wed, 19 Jan 2022 13:50:21 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=F0=9F=9A=91=20correction=20du=20li?= =?UTF-8?q?en=20de=20partage=20dans=20les=20iframes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix #1655 --- .../ShareSimulationBanner/index.tsx | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) 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('')