83 lines
2.6 KiB
TypeScript
83 lines
2.6 KiB
TypeScript
import { useBranchingQuiz } from "./FlowControlLogic/useBranchingQuiz";
|
||
import { useLinearQuiz } from "./FlowControlLogic/useLinearQuiz";
|
||
import { useAIQuiz } from "./FlowControlLogic/useAIQuiz";
|
||
import { useFirstFCQuiz } from "./FlowControlLogic/useFirstFCQuiz";
|
||
import { Status } from "@/model/settingsData";
|
||
import { useQuizStore } from "@/stores/useQuizStore";
|
||
|
||
interface StatusData {
|
||
status: Status;
|
||
haveRoot: string | null;
|
||
}
|
||
|
||
// выбор способа управления в зависимости от статуса
|
||
let cachedManager: () => ReturnType<typeof useLinearQuiz>;
|
||
export let statusOfQuiz: "line" | "branch" | "ai" | "FC";
|
||
let isInitialized = false;
|
||
|
||
function analyicStatus({ status, haveRoot }: StatusData) {
|
||
let isCrutch13112025 = window.location.pathname === "/d557133f-26b6-4b0b-93da-c538758a65d4";
|
||
|
||
if (isCrutch13112025) {
|
||
statusOfQuiz = "FC";
|
||
console.log("statusOfQuiz: " + statusOfQuiz);
|
||
return;
|
||
}
|
||
if (status === "ai") {
|
||
statusOfQuiz = "ai";
|
||
console.log("statusOfQuiz: " + statusOfQuiz);
|
||
return;
|
||
}
|
||
if (status === "start") {
|
||
// Если есть ветвление, то settings.cfg.haveRoot будет заполнен
|
||
if (haveRoot) statusOfQuiz = "branch";
|
||
else statusOfQuiz = "line";
|
||
console.log("statusOfQuiz: " + statusOfQuiz);
|
||
return;
|
||
}
|
||
throw new Error("quiz is inactive");
|
||
}
|
||
|
||
export const initDataManager = (data: StatusData) => {
|
||
analyicStatus(data);
|
||
switch (statusOfQuiz) {
|
||
case "line":
|
||
cachedManager = useLinearQuiz;
|
||
break;
|
||
case "branch":
|
||
cachedManager = useBranchingQuiz;
|
||
break;
|
||
case "ai":
|
||
cachedManager = useAIQuiz;
|
||
break;
|
||
case "FC":
|
||
cachedManager = useFirstFCQuiz;
|
||
break;
|
||
}
|
||
isInitialized = true;
|
||
};
|
||
|
||
// Главный хук (интерфейс для потребителей)
|
||
export const useQuestionFlowControl = () => {
|
||
if (!cachedManager || !isInitialized) {
|
||
// Попытка автоматической инициализации на основе текущих настроек
|
||
const { settings } = useQuizStore.getState();
|
||
if (settings && settings.status) {
|
||
initDataManager({
|
||
status: settings.status,
|
||
haveRoot: settings.cfg.haveRoot,
|
||
});
|
||
} else {
|
||
throw new Error("DataManager not initialized! Call initDataManager() first.");
|
||
}
|
||
}
|
||
return cachedManager();
|
||
};
|
||
|
||
// Функция для сброса состояния (полезна для HMR)
|
||
export const resetDataManager = () => {
|
||
cachedManager = null as any;
|
||
isInitialized = false;
|
||
statusOfQuiz = null as any;
|
||
};
|