frontAnswerer/lib/utils/hooks/useQuestionFlowControl.ts

51 lines
1.5 KiB
TypeScript
Raw Normal View History

2025-06-15 09:58:15 +00:00
import { useBranchingQuiz } from "./FlowControlLogic/useBranchingQuiz";
import { useLinearQuiz } from "./FlowControlLogic/useLinearQuiz";
import { useAIQuiz } from "./FlowControlLogic/useAIQuiz";
import { Status } from "@/model/settingsData";
interface StatusData {
status: Status;
haveRoot: string | null;
}
2024-05-06 13:47:19 +00:00
2025-06-15 09:58:15 +00:00
// выбор способа управления в зависимости от статуса
let cachedManager: () => ReturnType<typeof useLinearQuiz>;
export let statusOfQuiz: "line" | "branch" | "ai";
function analyicStatus({ status, haveRoot }: StatusData) {
if (status === "ai") {
statusOfQuiz = "ai";
return;
}
2025-06-15 09:58:15 +00:00
if (status === "start") {
// Если есть ветвление, то settings.cfg.haveRoot будет заполнен
if (haveRoot) statusOfQuiz = "branch";
else statusOfQuiz = "line";
return;
}
throw new Error("quiz is inactive");
2025-06-15 09:58:15 +00:00
}
2025-06-15 09:58:15 +00:00
export const initDataManager = (data: StatusData) => {
analyicStatus(data);
switch (statusOfQuiz) {
case "line":
cachedManager = useLinearQuiz;
break;
case "branch":
cachedManager = useBranchingQuiz;
break;
case "ai":
cachedManager = useAIQuiz;
break;
2024-05-06 13:47:19 +00:00
}
2025-06-15 09:58:15 +00:00
};
2025-06-15 09:58:15 +00:00
// Главный хук (интерфейс для потребителей)
export const useQuestionFlowControl = () => {
if (!cachedManager) {
throw new Error("DataManager not initialized! Call initDataManager() first.");
}
return cachedManager();
};