Files
jrshikoku/components/Apps/FixedPositionBox/hooks/useTrainCurrentPosition.ts
harukin-expo-dev-env 2ed8c17797 第三弾
2025-12-05 07:59:50 +00:00

43 lines
1.3 KiB
TypeScript

import { useState, useEffect } from "react";
import { useCurrentTrain } from "@/stateBox/useCurrentTrain";
import { trainDataType } from "@/lib/trainPositionTextArray";
/**
* 列車の現在位置を計算するカスタムフック
* 伊予駅の特殊処理を含む
*/
export const useTrainCurrentPosition = (
train: trainDataType | null,
stopStationIDList: string[][]
) => {
const { getPosition } = useCurrentTrain();
const [currentPosition, setCurrentPosition] = useState<string[]>([]);
useEffect(() => {
let position = getPosition(train);
if (stopStationIDList.length == 0) return;
if (position) {
if (position.length > 1) {
// 伊予駅の特殊処理
const iyoIndex = stopStationIDList.findIndex((d) => d.includes("U14"));
if (position[0] == "-Iyo") {
position[0] = stopStationIDList[iyoIndex - 1]?.[0];
} else if (position[0] == "+Iyo") {
position[0] = stopStationIDList[iyoIndex + 1]?.[0];
}
if (position[1] == "+Iyo") {
position[1] = stopStationIDList[iyoIndex + 1]?.[0];
} else if (position[1] == "-Iyo") {
position[1] = stopStationIDList[iyoIndex - 1]?.[0];
}
}
setCurrentPosition(position);
}
}, [train, stopStationIDList, getPosition]);
return currentPosition;
};