Skip to content

Commit 33f4dde

Browse files
authored
駅到着直後に次の駅へ一瞬進んでしまうデグレを修正 (#5338)
1 parent 2b75e0c commit 33f4dde

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

src/hooks/useNearestStation.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ export const useNearestStation = (): Station | undefined => {
4949
sta.longitude === nearestCoordinates.longitude
5050
);
5151

52+
// currentStationを優先して返すことで、到着直後にnextStationへ誤って進むのを防ぐ
5253
return (
53-
nearestStations.find(
54-
(s) => s.id === currentStation?.id || s.id === nextStation?.id
55-
) ?? nearestStations[0]
54+
nearestStations.find((s) => s.id === currentStation?.id) ??
55+
nearestStations.find((s) => s.id === nextStation?.id) ??
56+
nearestStations[0]
5657
);
5758
}, [latitude, longitude, validStations, currentStation, nextStation]);
5859

src/hooks/useRefreshStation.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export const useRefreshStation = (): void => {
4444
const approachingNotifiedIdRef = useRef<number | null>(null);
4545
const arrivedNotifiedIdRef = useRef<number | null>(null);
4646
const lastArrivedTimeRef = useRef<number>(0);
47+
const lastArrivedStationIdRef = useRef<number | null>(null);
4748
const { targetStationIds } = useAtomValue(notifyState);
4849

4950
const nearestStation = useNearestStation();
@@ -64,14 +65,17 @@ export const useRefreshStation = (): void => {
6465
const effectiveApproachingThreshold = approachingThreshold + accuracyBonus;
6566

6667
const isArrived = useMemo((): boolean => {
68+
if (latitude == null || longitude == null || !nearestStation) {
69+
return true;
70+
}
71+
72+
// グレース期間は到着を引き起こした駅にのみ適用する
73+
// 別の駅に対してtrueを返すと誤って駅が進んでしまう
6774
const inGracePeriod =
6875
Date.now() - lastArrivedTimeRef.current < ARRIVED_GRACE_PERIOD_MS;
69-
7076
if (
71-
latitude == null ||
72-
longitude == null ||
73-
!nearestStation ||
74-
inGracePeriod
77+
inGracePeriod &&
78+
nearestStation.id === lastArrivedStationIdRef.current
7579
) {
7680
return true;
7781
}
@@ -91,6 +95,7 @@ export const useRefreshStation = (): void => {
9195

9296
if (arrived) {
9397
lastArrivedTimeRef.current = Date.now();
98+
lastArrivedStationIdRef.current = nearestStation.id ?? null;
9499
}
95100
return arrived;
96101
}, [effectiveArrivedThreshold, latitude, longitude, nearestStation]);

0 commit comments

Comments
 (0)