Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

種別表示バグ修正 #3582

Merged
merged 6 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/components/TypeChangeNotify.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import useNextTrainType from '../hooks/useNextTrainType'
import stationState from '../store/atoms/station'
import themeState from '../store/atoms/theme'
import { currentLineSelector } from '../store/selectors/currentLine'
import { currentStationSelector } from '../store/selectors/currentStation'
import isTablet from '../utils/isTablet'
import { getIsLocal } from '../utils/trainTypeString'
import truncateTrainType from '../utils/truncateTrainType'
Expand Down Expand Up @@ -724,16 +725,19 @@ const JOBars: React.FC = () => {
)
}
const TypeChangeNotify: React.FC = () => {
const { selectedDirection, stations, selectedBound, station } =
const { selectedDirection, stations, selectedBound } =
useRecoilValue(stationState)
const { theme } = useRecoilValue(themeState)
const station = useRecoilValue(currentStationSelector({}))
const currentLine = useRecoilValue(currentLineSelector)
const nextLine = useNextLine()
const trainType = useCurrentTrainType()
const nextTrainType = useNextTrainType()

const currentTypeStations = stations.filter(
(s) => s.trainType?.typeId === trainType?.typeId
(s) =>
s.trainType?.typeId === trainType?.typeId &&
s.line?.id === currentLine?.id
)

const reversedStations = stations.slice().reverse()
Expand Down
37 changes: 28 additions & 9 deletions src/hooks/useNextTrainType.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import { useMemo } from 'react'
import { useRecoilValue } from 'recoil'
import { TrainType } from '../../gen/proto/stationapi_pb'
import navigationState from '../store/atoms/navigation'
import stationState from '../store/atoms/station'
import { currentStationSelector } from '../store/selectors/currentStation'
import useCurrentTrainType from './useCurrentTrainType'
import useNextLine from './useNextLine'

const useNextTrainType = (): TrainType | null => {
const { stations, selectedDirection } = useRecoilValue(stationState)
const { trainType } = useRecoilValue(navigationState)
const nextLine = useNextLine()
const currentStation = useRecoilValue(currentStationSelector({}))
const trainType = useCurrentTrainType()

// 同じ路線でも種別が変わる場合を想定(小田急線等)
const sameLineNextType = useMemo(() => {
if (
nextLine &&
trainType?.line?.id !== nextLine.id &&
trainType?.line?.company?.id !== nextLine.company?.id
) {
return
}

if (selectedDirection === 'INBOUND') {
const currentIndex = stations.findIndex(
(sta) => sta.groupId === currentStation?.groupId
Expand All @@ -36,16 +44,27 @@ const useNextTrainType = (): TrainType | null => {
.map((sta) => sta.trainType)
.filter((tt) => tt)
.find((tt) => tt?.typeId !== trainType?.typeId)
}, [currentStation?.groupId, selectedDirection, stations, trainType?.typeId])
}, [
currentStation?.groupId,
nextLine,
selectedDirection,
stations,
trainType?.line?.company?.id,
trainType?.line?.id,
trainType?.typeId,
])

const nextLineTrainType = useMemo(
() =>
trainType?.lines?.find((l) => l.id === nextLine?.id)?.trainType ?? null,
[nextLine?.id, trainType?.lines]
)

const nextTrainType = useMemo(() => {
return (
sameLineNextType ??
trainType?.lines?.find((l) => l.id === nextLine?.id)?.trainType
)
}, [nextLine?.id, sameLineNextType, trainType?.lines])
return sameLineNextType ?? nextLineTrainType
}, [nextLineTrainType, sameLineNextType])

return nextTrainType ?? null
return nextTrainType
}

export default useNextTrainType
4 changes: 2 additions & 2 deletions src/hooks/useTypeWillChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export const useTypeWillChange = (): boolean => {
const nextTrainType = useNextTrainType()

const nextTrainTypeIsDifferent = useMemo(() => {
if (!trainType) {
if (!trainType || !nextTrainType) {
return false
}

if (!nextTrainType) {
if (trainType.typeId === nextTrainType.typeId) {
return false
}

Expand Down
Loading