Skip to content

Commit 323f3c9

Browse files
committed
feat: transit journal updating of active segment
1 parent 49d15f6 commit 323f3c9

1 file changed

Lines changed: 26 additions & 4 deletions

File tree

app/src/contexts/TransitJournalContext.tsx

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ export function TransitJournalProvider({ children }: { children: ReactNode }) {
7777
const [showNextSegmentModal, setShowNextSegmentModal] = useState(false);
7878
const [showTripFinishedModal, setShowTripFinishedModal] = useState(false);
7979
const [followUser, setFollowUser] = useState(true);
80+
const [lastSegmentChangeTime, setLastSegmentChangeTime] = useState<number>(Date.now());
81+
const [lastTripFinishedTime, setLastTripFinishedTime] = useState<number>(Date.now());
8082

8183
// Check if the user has a transit journal
8284
useEffect(() => {
@@ -177,17 +179,37 @@ export function TransitJournalProvider({ children }: { children: ReactNode }) {
177179
lineRef.current.update(activeRoutes);
178180
circleRef.current.update(activeRoutes);
179181

180-
// Show transfer modal everytime segment changes
181-
if (activeSegments[0]?.id !== _currentSegment.id) {
182+
// Show transfer modal if segment changes and enough time has passed
183+
const now = Date.now();
184+
const MIN_SEGMENT_CHANGE_INTERVAL = 5000; // 5 seconds
185+
186+
if (
187+
activeSegments[0]?.id !== _currentSegment.id &&
188+
now - lastSegmentChangeTime >= MIN_SEGMENT_CHANGE_INTERVAL
189+
) {
190+
setLastSegmentChangeTime(now);
182191
setShowTripFinishedModal(false);
183192
setShowNextSegmentModal(true);
193+
setActiveSegments(segments.slice(segmentIndex));
184194
}
185-
195+
186196
// check if currentLocation is near destination
187197
const destination = segments[segments.length - 1].endCoords;
188-
if (isNearLocation(newLocation, destination, 20)) {
198+
const FINISHED_MODAL_COOLDOWN = 30000; // 30 seconds
199+
200+
if (
201+
isNearLocation(newLocation, destination, 20) &&
202+
now - lastTripFinishedTime >= FINISHED_MODAL_COOLDOWN
203+
) {
204+
setLastTripFinishedTime(now);
189205
setShowNextSegmentModal(false);
190206
setShowTripFinishedModal(true);
207+
} else {
208+
if (now - lastTripFinishedTime < FINISHED_MODAL_COOLDOWN) {
209+
console.log('⏱ Trip finished cooldown active');
210+
} else {
211+
console.log('🛣 Still far from destination');
212+
}
191213
}
192214

193215
// update the active segments and step information

0 commit comments

Comments
 (0)