diff --git a/src/hooks/useCurrentLine.ts b/src/hooks/useCurrentLine.ts index ce3466f2f..597f296f9 100644 --- a/src/hooks/useCurrentLine.ts +++ b/src/hooks/useCurrentLine.ts @@ -14,9 +14,13 @@ export const useCurrentLine = () => { (selectedDirection === 'INBOUND' ? stations.slice().reverse() : stations - ).find((rs) => rs.id === currentStation?.id), - [currentStation?.id, selectedDirection, stations] + ).find( + (rs) => + rs.groupId === currentStation?.groupId ?? + rs.line?.id === selectedLine?.id + ), + [currentStation?.groupId, selectedDirection, selectedLine?.id, stations] ) - return actualCurrentStation?.line || selectedLine + return actualCurrentStation?.line } diff --git a/src/hooks/useCurrentStation.ts b/src/hooks/useCurrentStation.ts index 63e87a072..1fb26d195 100644 --- a/src/hooks/useCurrentStation.ts +++ b/src/hooks/useCurrentStation.ts @@ -1,5 +1,6 @@ import { useMemo } from 'react' import { useRecoilValue } from 'recoil' +import lineState from '../store/atoms/line' import stationState from '../store/atoms/station' import getIsPass from '../utils/isPass' @@ -12,11 +13,17 @@ export const useCurrentStation = ( station: stationFromState, selectedDirection, } = useRecoilValue(stationState) + const { selectedLine } = useRecoilValue(lineState) // NOTE: 選択した路線と現在の駅の路線を一致させる const station = useMemo( - () => stations.find((s) => s.id === stationFromState?.id) ?? null, - [stationFromState?.id, stations] + () => + stations.find( + (s) => + s.groupId === stationFromState?.groupId ?? + s.line?.id === selectedLine?.id + ), + [selectedLine?.id, stationFromState?.groupId, stations] ) const withTrainTypeStation = useMemo(() => { diff --git a/src/hooks/usePreviousStation.ts b/src/hooks/usePreviousStation.ts index bc8fc0ea5..97ea6aa6e 100644 --- a/src/hooks/usePreviousStation.ts +++ b/src/hooks/usePreviousStation.ts @@ -9,6 +9,7 @@ import { useCurrentStation } from './useCurrentStation' const usePreviousStation = (skipPass = true): Station | undefined => { const { stations: stationsFromState, selectedDirection } = useRecoilValue(stationState) + const station = useCurrentStation(true) const stations = useMemo( () => @@ -18,7 +19,6 @@ const usePreviousStation = (skipPass = true): Station | undefined => { [selectedDirection, skipPass, stationsFromState] ) - const station = useCurrentStation(true) const reversedStations = useMemo( () => selectedDirection === 'INBOUND' ? stations : stations.slice().reverse(),