120 lines
3.6 KiB
TypeScript
120 lines
3.6 KiB
TypeScript
import { useState, useEffect } from "react";
|
|
import { useAllTrainDiagram } from "@/stateBox/useAllTrainDiagram";
|
|
import { useStationList } from "@/stateBox/useStationList";
|
|
|
|
interface StationIDPair {
|
|
[key: string]: string;
|
|
}
|
|
|
|
interface LineListPair {
|
|
[key: string]: string;
|
|
}
|
|
|
|
interface OriginalStationList {
|
|
[key: string]: Array<{
|
|
Station_JP: string;
|
|
StationNumber: string;
|
|
}>;
|
|
}
|
|
|
|
/**
|
|
* 通過駅を含む列車データを生成するカスタムフック
|
|
* 停車駅間の通過駅を自動的に挿入する
|
|
*/
|
|
export const useTrainDataWithThrough = (
|
|
trainID: string,
|
|
stationIDPair: StationIDPair,
|
|
lineListPair: LineListPair,
|
|
originalStationList: OriginalStationList
|
|
) => {
|
|
const { allTrainDiagram } = useAllTrainDiagram();
|
|
const { stationList } = useStationList();
|
|
|
|
const [trainDataWithThrough, setTrainDataWithThrough] = useState<string[]>([]);
|
|
|
|
useEffect(() => {
|
|
const trainData = allTrainDiagram[trainID]?.split("#");
|
|
if (!trainData) return;
|
|
|
|
// 各停車駅の情報を取得
|
|
const stopStationList = trainData.map((i) => {
|
|
const [station] = i.split(",");
|
|
return stationList.map((a) => a.filter((d) => d.Station_JP == station));
|
|
});
|
|
|
|
// 各停車駅間の通過駅リストを生成
|
|
const allThroughStationList = stopStationList.map((_, index, array) => {
|
|
if (index == array.length - 1) return [];
|
|
|
|
const firstItem = array[index];
|
|
const secondItem = array[index + 1];
|
|
|
|
// 2つの停車駅が共通して属する路線を検索
|
|
let betweenStationLine = "";
|
|
let baseStationNumberFirst = "";
|
|
let baseStationNumberSecond = "";
|
|
|
|
Object.keys(stationIDPair).forEach((lineName, index2) => {
|
|
if (!lineName) return;
|
|
const haveFirst = firstItem[index2];
|
|
const haveSecond = secondItem[index2];
|
|
|
|
if (haveFirst.length && haveSecond.length) {
|
|
betweenStationLine = lineName;
|
|
baseStationNumberFirst = haveFirst[0].StationNumber;
|
|
baseStationNumberSecond = haveSecond[0].StationNumber;
|
|
}
|
|
});
|
|
|
|
if (!betweenStationLine) return [];
|
|
|
|
// 停車駅間の通過駅を抽出
|
|
let allThroughStation: string[] = [];
|
|
let reverse = false;
|
|
|
|
const lineStations = originalStationList[lineListPair[stationIDPair[betweenStationLine]]];
|
|
if (!lineStations) return [];
|
|
|
|
lineStations.forEach((d) => {
|
|
// 順方向の判定
|
|
if (
|
|
d.StationNumber > baseStationNumberFirst &&
|
|
d.StationNumber < baseStationNumberSecond
|
|
) {
|
|
allThroughStation.push(`${d.Station_JP},通過,`);
|
|
reverse = false;
|
|
}
|
|
// 逆方向の判定
|
|
else if (
|
|
d.StationNumber < baseStationNumberFirst &&
|
|
d.StationNumber > baseStationNumberSecond
|
|
) {
|
|
allThroughStation.push(`${d.Station_JP},通過,`);
|
|
reverse = true;
|
|
}
|
|
});
|
|
|
|
if (reverse) allThroughStation.reverse();
|
|
return allThroughStation;
|
|
});
|
|
|
|
// メインの列車データに通過駅を挿入
|
|
let mainArray = [...trainData];
|
|
let indexOffset = 0;
|
|
|
|
trainData.forEach((_, index) => {
|
|
indexOffset = indexOffset + 1;
|
|
const throughStations = allThroughStationList[index];
|
|
|
|
if (!throughStations || throughStations.length == 0) return;
|
|
|
|
mainArray.splice(indexOffset, 0, ...throughStations);
|
|
indexOffset = indexOffset + throughStations.length;
|
|
});
|
|
|
|
setTrainDataWithThrough(mainArray);
|
|
}, [allTrainDiagram, stationList, trainID, stationIDPair, lineListPair, originalStationList]);
|
|
|
|
return { trainDataWithThrough };
|
|
};
|