Files
jrshikoku/components/Apps/FixedPositionBox/hooks/useDestinationStation.ts
harukin-expo-dev-env 18979f2b24 第4弾
2025-12-05 08:13:40 +00:00

48 lines
1.4 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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,
};
};