列車走行位置へのジャンプ機能をuseCurrentTrainへ移動
This commit is contained in:
@@ -8,6 +8,9 @@ import React, {
|
||||
import { HeaderConfig } from "../lib/HeaderConfig";
|
||||
|
||||
import useInterval from "../lib/useInterval";
|
||||
import { useStationList } from "./useStationList";
|
||||
import { checkDuplicateTrainData } from "@/lib/checkDuplicateTrainData";
|
||||
import { getStationID } from "@/lib/eachTrainInfoCoreLib/getStationData";
|
||||
const initialState = {
|
||||
webview: {},
|
||||
currentTrain: [],
|
||||
@@ -16,6 +19,11 @@ const initialState = {
|
||||
setCurrentTrainLoading: () => {},
|
||||
getCurrentTrain: () => {},
|
||||
inject: (i) => {},
|
||||
fixedPosition: null,
|
||||
setFixedPosition: () => {},
|
||||
setInjectData: (e) => {},
|
||||
getCurrentStationData: (e) => {},
|
||||
getPosition: (e) => {},
|
||||
};
|
||||
|
||||
const CurrentTrainContext = createContext(initialState);
|
||||
@@ -27,6 +35,111 @@ export const useCurrentTrain = () => {
|
||||
export const CurrentTrainProvider = ({ children }) => {
|
||||
const [currentTrain, setCurrentTrain] = useState([]); //現在在線中の全列車 { num: 列車番号, delay: 遅延時分(状態), Pos: 位置情報 }
|
||||
const [currentTrainLoading, setCurrentTrainLoading] = useState("loading"); // success, error, loading
|
||||
|
||||
const { getInjectJavascriptAddress, stationList, originalStationList } =
|
||||
useStationList();
|
||||
|
||||
const [fixedPosition, setFixedPosition] = useState(null);
|
||||
const [_, setIntervalState] = useInterval(
|
||||
() => {
|
||||
if (!webview.current) return;
|
||||
inject(fixedPosition);
|
||||
},
|
||||
60000,
|
||||
false
|
||||
);
|
||||
useEffect(() => {
|
||||
if (fixedPosition) {
|
||||
setIntervalState.start();
|
||||
} else {
|
||||
setIntervalState.stop();
|
||||
}
|
||||
}, [fixedPosition]);
|
||||
const getPosition = (currentTrainData) => {
|
||||
console.log(currentTrainData);
|
||||
//currentTrainData.Pos = "鴨川~端岡"; //test
|
||||
if (!currentTrainData) return;
|
||||
if (!currentTrainData?.Pos) return;
|
||||
if (currentTrainData?.Pos.match("~")) {
|
||||
const pos = currentTrainData?.Pos.replace("(下り)", "")
|
||||
.replace("(上り)", "")
|
||||
.replace("(徳島線)", "")
|
||||
.replace("(高徳線)", "")
|
||||
.split("~");
|
||||
const direction = parseInt(currentTrainData?.Direction) || 0;
|
||||
if (pos[0] == "児島" && pos[1] == "宇多津") {
|
||||
return ["M12", "Y09"];
|
||||
} else if (pos[1] == "児島" && pos[0] == "宇多津") {
|
||||
return ["Y09", "M12"];
|
||||
} else if (pos[0] == "伊予若宮" && pos[1] == "伊予白滝") {
|
||||
return ["S18", "S14"];
|
||||
} else if (pos[0] == "伊予白滝" && pos[1] == "伊予若宮") {
|
||||
return ["S14", "S18"];
|
||||
} else if (pos[0] == "伊予大洲" && pos[1] == "伊予若宮") {
|
||||
return ["U14", "U14"];
|
||||
} else if (pos[0] == "伊予若宮" && pos[1] == "伊予大洲") {
|
||||
return ["U14", "U14"];
|
||||
}
|
||||
const currentPosID = Object.keys(originalStationList).map((key) => {
|
||||
let firstStation = false;
|
||||
let firstStationID = "";
|
||||
let secondStation = false;
|
||||
let secondStationID = "";
|
||||
originalStationList[key].forEach((station) => {
|
||||
if (station.Station_JP === pos[0]) {
|
||||
firstStation = true;
|
||||
firstStationID = station.StationNumber;
|
||||
}
|
||||
if (station.Station_JP === pos[1]) {
|
||||
secondStation = true;
|
||||
secondStationID = station.StationNumber;
|
||||
}
|
||||
});
|
||||
if (firstStation && secondStation) {
|
||||
return [firstStationID, secondStationID];
|
||||
} else return false;
|
||||
});
|
||||
const currentPos = currentPosID.filter((d) => d != false)[0];
|
||||
if (currentPos) {
|
||||
return direction == 0 ? currentPos.reverse() : currentPos;
|
||||
} else if (direction == 0) {
|
||||
return [
|
||||
getStationID(pos[1], stationList),
|
||||
getStationID(pos[0], stationList),
|
||||
];
|
||||
} else {
|
||||
return [
|
||||
getStationID(pos[0], stationList),
|
||||
getStationID(pos[1], stationList),
|
||||
];
|
||||
}
|
||||
} else {
|
||||
return [getStationID(currentTrainData?.Pos, stationList)];
|
||||
}
|
||||
};
|
||||
const setInjectData = ({ type, number, fixed }) => {
|
||||
if (type === "station") {
|
||||
const script = getInjectJavascriptAddress(number);
|
||||
inject(script);
|
||||
if (fixed) {
|
||||
setFixedPosition(script);
|
||||
}
|
||||
} else if (type === "station") {
|
||||
const script = getInjectJavascriptAddress(number);
|
||||
inject(script);
|
||||
if (fixed) {
|
||||
setFixedPosition(script);
|
||||
}
|
||||
}
|
||||
};
|
||||
const getCurrentStationData = (e) => {
|
||||
//e:trainNumber
|
||||
if (!currentTrain.length) return;
|
||||
return checkDuplicateTrainData(
|
||||
currentTrain.filter((d) => d.num == e),
|
||||
stationList
|
||||
);
|
||||
};
|
||||
const getCurrentTrain = () => {
|
||||
fetch("https://n8n.haruk.in/webhook/c501550c-7d1b-4e50-927b-4429fe18931a")
|
||||
.then((response) => response.json())
|
||||
@@ -88,6 +201,9 @@ export const CurrentTrainProvider = ({ children }) => {
|
||||
setCurrentTrainLoading,
|
||||
getCurrentTrain,
|
||||
inject,
|
||||
setInjectData,
|
||||
getCurrentStationData,
|
||||
getPosition,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
|
Reference in New Issue
Block a user