fifty-shades-of-bully/src/pages/vistualNovelHandler.jsx

86 lines
2.5 KiB
React
Raw Normal View History

2025-05-06 15:36:55 +07:00
// src/pages/vistualNovelHandler.jsx
import { useEffect, useState } from 'react';
import { useNavigate, useParams } from 'react-router-dom';
import '../css/global.css';
import { fetchYamlData } from './components/fetchYamlData';
import StoryPage from './storyPage';
2025-05-06 17:08:03 +07:00
import ConversationPage from './conversationPage.jsx';
2025-05-10 22:18:49 +07:00
import OptionPage from './optionPage.jsx';
2025-05-06 15:36:55 +07:00
import LoadingScene from './components/loadingScene.jsx';
2025-05-13 22:51:54 +07:00
import TransitionPage from './transitionPage.jsx';
2025-05-10 22:18:49 +07:00
2025-05-06 15:36:55 +07:00
function VitualNovelHandler() {
const [data, setData] = useState(null);
const [error, setError] = useState(null);
const [currentStep, setCurrentStep] = useState(0);
const navigate = useNavigate();
const { step } = useParams();
useEffect(() => {
const loadData = async () => {
try {
const parsedData = await fetchYamlData({ src: '/yml/DialogPorsche.yml' });
if (!Array.isArray(parsedData)) {
throw new Error('YAML data is not an array');
}
setData(parsedData);
} catch (error) {
setError(error.message);
}
};
loadData();
}, []);
useEffect(() => {
// Update the current step from the URL parameter
if (step && !isNaN(step)) {
setCurrentStep(parseInt(step, 10));
}
}, [step]);
if (error) {
return <div>Error loading YAML data: {error}</div>;
}
if (!data) {
return <div>Loading...</div>;
}
const handleNextStep = (nextstep) => {
// console.log(data);
console.log(nextstep)
2025-05-24 21:53:10 +07:00
if (typeof nextstep !== 'number' || Number.isNaN(nextstep)) {
navigate(`/end`);//44 is last page
}
else{
setCurrentStep(nextstep);
navigate(`/vs/${nextstep}`);
}
2025-05-06 15:36:55 +07:00
};
2025-05-24 21:53:10 +07:00
const currentStepData = data[currentStep] === null ? "end" : data[currentStep];
2025-05-06 15:36:55 +07:00
const renderComponent = (type) => {
switch (type) {
case "story":
return <StoryPage data={currentStepData} onClicked={handleNextStep}/>;
case "conversation":
2025-05-06 17:08:03 +07:00
return <ConversationPage data={currentStepData} onClicked={handleNextStep}/>;
2025-05-06 15:36:55 +07:00
case "option":
2025-05-10 22:18:49 +07:00
return <OptionPage data={currentStepData} onClicked={handleNextStep}/>;
2025-05-13 22:51:54 +07:00
case "transition":
return <TransitionPage data={currentStepData} onChanged={handleNextStep}/>
2025-05-06 15:36:55 +07:00
default:
return <LoadingScene />;
}
};
return (
2025-05-06 17:08:03 +07:00
<div style={{ width: "100svw", height: "100svh" }}>
2025-05-24 21:53:10 +07:00
{renderComponent(currentStepData.type === null ? "end" : currentStepData.type)}
2025-05-06 15:36:55 +07:00
</div>
);
}
export default VitualNovelHandler;