Files
jrshikoku/components/Apps/FixedPositionBox/hooks/useDestinationStation.ts
2025-12-05 08:13:40 +00:00

48 lines
1.4 KiB
TypeScript

import { useState, useEffect } from "react";
import { useStationList } from "@/stateBox/useStationList";
import { StationProps } from "@/lib/CommonTypes";
interface CustomData {
to_data?: string;
}
/**
* 行先駅データを管理するカスタムフック
* カスタムデータまたは列車データから行先を取得
*/
export const useDestinationStation = (
customData: CustomData,
trainDataWithThrough: string[]
) => {
const { getStationDataFromName } = useStationList();
const [toData, setToData] = useState("");
const [destinationStation, setDestinationStation] = useState<StationProps[]>([]);
// 行先駅名の設定
useEffect(() => {
if (customData.to_data && customData.to_data != "") {
setToData(customData.to_data);
} else {
if (trainDataWithThrough.length == 0) return;
// 最後から2番目の駅名を取得(最後は空文字の可能性があるため)
const lastStation = trainDataWithThrough[trainDataWithThrough.length - 2];
if (lastStation) {
setToData(lastStation.split(",")[0]);
}
}
}, [customData, trainDataWithThrough]);
// 行先駅データの取得
useEffect(() => {
if (!toData) return;
const data = getStationDataFromName(toData);
setDestinationStation(data);
}, [toData, getStationDataFromName]);
return {
toData,
destinationStation,
};
};