bugfix when multiple simulators have the same url start

pull/2524/head
Jérémy Rialland 2023-02-28 11:57:57 +01:00 committed by Jérémy Rialland
parent 27cfe59840
commit d81f84a219
1 changed files with 16 additions and 6 deletions

View File

@ -1,5 +1,6 @@
import { useLocation } from 'react-router-dom'
import { useIsEmbedded } from '@/components/utils/useIsEmbedded'
import { Merge, ToOptional } from '@/types/utils'
import useSimulatorsData, { SimulatorDataValues } from './useSimulatorsData'
@ -11,14 +12,23 @@ export type MergedSimulatorDataValues = ToOptional<Merge<SimulatorDataValues>>
*/
export const useCurrentSimulatorData = () => {
const simulatorsData = useSimulatorsData()
const isEmbedded = useIsEmbedded()
const pathname = decodeURI(useLocation().pathname)
const entries = Object.entries(simulatorsData)
const [key, data] =
Object.entries(simulatorsData).find(
([, data]) =>
pathname.startsWith(data.path) ||
pathname.startsWith('/iframes/' + data.iframePath)
) ?? []
(!isEmbedded
? entries
.sort((a, b) => b[1].path.length - a[1].path.length)
.find(([, data]) => pathname.startsWith(data.path))
: entries
.sort((a, b) => b[1].iframePath.length - a[1].iframePath.length)
.find(([, data]) =>
pathname.startsWith('/iframes/' + data.iframePath)
)) ?? []
return { key, currentSimulatorData: data as MergedSimulatorDataValues }
return {
key,
currentSimulatorData: data as MergedSimulatorDataValues | undefined,
}
}