42 lines
1.3 KiB
TypeScript
42 lines
1.3 KiB
TypeScript
import React,{ useEffect, MutableRefObject } from 'react';
|
|
import { LayoutAnimation, ScrollView } from 'react-native';
|
|
|
|
|
|
export const useAutoScroll = (
|
|
points: boolean[] | undefined,
|
|
trainDataWithThrough: string[],
|
|
scrollHandlers: any,
|
|
isJumped: boolean,
|
|
setIsJumped: (value: boolean) => void,
|
|
setShowThrew: (value: boolean) => void
|
|
) => {
|
|
useEffect(() => {
|
|
if (isJumped || !points?.length || !scrollHandlers) return;
|
|
|
|
const currentPositionIndex = points.findIndex((d) => d === true);
|
|
if (currentPositionIndex === -1) return;
|
|
|
|
setShowThrew(true);
|
|
|
|
const isPassingThrough = trainDataWithThrough[currentPositionIndex]?.split(',')[1] === '通過';
|
|
if (isPassingThrough) {
|
|
LayoutAnimation.configureNext({
|
|
duration: 400,
|
|
update: { type: 'easeInEaseOut', springDamping: 0.6 },
|
|
});
|
|
}
|
|
|
|
// 5駅以内の場合はスクロールしない
|
|
if (currentPositionIndex < 5) {
|
|
setIsJumped(true);
|
|
return;
|
|
}
|
|
|
|
const scrollPosition = currentPositionIndex * 44 - 50;
|
|
setTimeout(() => {
|
|
scrollHandlers.ref.current?.scrollTo({ y: scrollPosition, animated: true });
|
|
setIsJumped(true);
|
|
}, 400);
|
|
}, [points, trainDataWithThrough, scrollHandlers, isJumped, setIsJumped, setShowThrew]);
|
|
};
|