101 lines
3.2 KiB
TypeScript
101 lines
3.2 KiB
TypeScript
import { useState, useEffect } from 'react';
|
|
import { lineListPair, stationIDPair } from '@/lib/getStationList';
|
|
import { useStationList } from '@/stateBox/useStationList';
|
|
|
|
export const useThroughStations = (trainData) => {
|
|
const { originalStationList, stationList } = useStationList();
|
|
const [trainDataWithThrough, setTrainDataWithThrough] = useState([]);
|
|
const [haveThrough, setHaveThrough] = useState(false);
|
|
|
|
useEffect(() => {
|
|
if (!trainData.length) {
|
|
setTrainDataWithThrough([]);
|
|
return;
|
|
}
|
|
|
|
const isCancel = [];
|
|
const stopStationList = trainData.map((item, index, array) => {
|
|
const [station, se] = item.split(',');
|
|
const [, nextSe] = array[index + 1]?.split(',') || [];
|
|
|
|
if (nextSe) {
|
|
const isCanceled =
|
|
(se.includes('休') && nextSe.includes('休')) ||
|
|
(se.includes('着') && nextSe.includes('休')) ||
|
|
(se.includes('休') && nextSe.includes('発'));
|
|
isCancel.push(isCanceled);
|
|
}
|
|
|
|
if (se === '通編') setHaveThrough(true);
|
|
|
|
return stationList.map((a) => a.filter((d) => d.StationName === station));
|
|
});
|
|
|
|
const allThroughStationList = stopStationList.map((firstItem, index, array) => {
|
|
if (index === array.length - 1) return [];
|
|
|
|
const secondItem = array[index + 1];
|
|
let betweenStationLine = '';
|
|
let baseStationNumberFirst = '';
|
|
let baseStationNumberSecond = '';
|
|
|
|
Object.keys(stationIDPair).forEach((lineName, lineIndex) => {
|
|
if (!lineName) return;
|
|
const haveFirst = firstItem[lineIndex];
|
|
const haveSecond = secondItem[lineIndex];
|
|
|
|
if (haveFirst?.length && haveSecond?.length) {
|
|
betweenStationLine = lineName;
|
|
baseStationNumberFirst = haveFirst[0].StationNumber;
|
|
baseStationNumberSecond = haveSecond[0].StationNumber;
|
|
}
|
|
});
|
|
|
|
if (!betweenStationLine) return [];
|
|
|
|
const allThroughStation = [];
|
|
let reverse = false;
|
|
|
|
originalStationList[lineListPair[stationIDPair[betweenStationLine]]]?.forEach((station) => {
|
|
const throughStatus = isCancel[index] ? '通休編' : '通過';
|
|
|
|
if (
|
|
station.StationNumber > baseStationNumberFirst &&
|
|
station.StationNumber < baseStationNumberSecond
|
|
) {
|
|
allThroughStation.push(`${station.Station_JP},${throughStatus},`);
|
|
setHaveThrough(true);
|
|
reverse = false;
|
|
} else if (
|
|
station.StationNumber < baseStationNumberFirst &&
|
|
station.StationNumber > baseStationNumberSecond
|
|
) {
|
|
allThroughStation.push(`${station.Station_JP},${throughStatus},`);
|
|
setHaveThrough(true);
|
|
reverse = true;
|
|
}
|
|
});
|
|
|
|
if (reverse) allThroughStation.reverse();
|
|
return allThroughStation;
|
|
});
|
|
|
|
let mainArray = [...trainData];
|
|
let offset = 0;
|
|
|
|
trainData.forEach((_, index) => {
|
|
offset += 1;
|
|
const throughStations = allThroughStationList[index];
|
|
|
|
if (!throughStations?.length) return;
|
|
|
|
mainArray.splice(offset, 0, ...throughStations);
|
|
offset += throughStations.length;
|
|
});
|
|
|
|
setTrainDataWithThrough(mainArray);
|
|
}, [trainData, stationList, originalStationList]);
|
|
|
|
return { trainDataWithThrough, haveThrough };
|
|
};
|