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([]); 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; };