2023-04-18 11:56:15 +02:00
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
import { DemoTopics } from './DemoTopics/DemoTopics';
|
|
|
|
import { DemoSteps } from './DemoSteps/DemoSteps';
|
|
|
|
import { createLocalStorage } from 'utils/createLocalStorage';
|
2023-04-19 20:40:29 +02:00
|
|
|
import { TOPICS } from './demo-topics';
|
2023-04-20 17:32:26 +02:00
|
|
|
import { DemoDialogWelcome } from './DemoDialog/DemoDialogWelcome/DemoDialogWelcome';
|
2023-04-21 12:48:44 +02:00
|
|
|
import { DemoDialogFinish } from './DemoDialog/DemoDialogFinish/DemoDialogFinish';
|
2023-04-18 11:56:15 +02:00
|
|
|
|
|
|
|
const defaultProgress = {
|
2023-04-20 17:32:26 +02:00
|
|
|
welcomeOpen: true,
|
2023-04-18 11:56:15 +02:00
|
|
|
expanded: true,
|
2023-04-21 12:48:44 +02:00
|
|
|
topic: -1,
|
2023-04-18 11:56:15 +02:00
|
|
|
steps: [0],
|
|
|
|
};
|
|
|
|
|
|
|
|
const { value: storedProgress, setValue: setStoredProgress } =
|
|
|
|
createLocalStorage('Tutorial:v1', defaultProgress);
|
|
|
|
|
|
|
|
export const Demo = () => {
|
2023-04-20 17:32:26 +02:00
|
|
|
const [welcomeOpen, setWelcomeOpen] = useState(
|
2023-04-21 12:48:44 +02:00
|
|
|
storedProgress.welcomeOpen ?? defaultProgress.welcomeOpen
|
2023-04-20 17:32:26 +02:00
|
|
|
);
|
2023-04-21 12:48:44 +02:00
|
|
|
const [finishOpen, setFinishOpen] = useState(false);
|
2023-04-18 11:56:15 +02:00
|
|
|
|
2023-04-21 12:48:44 +02:00
|
|
|
const [expanded, setExpanded] = useState(
|
|
|
|
storedProgress.expanded ?? defaultProgress.expanded
|
|
|
|
);
|
|
|
|
const [topic, setTopic] = useState(
|
|
|
|
storedProgress.topic ?? defaultProgress.topic
|
|
|
|
);
|
|
|
|
const [steps, setSteps] = useState(
|
|
|
|
storedProgress.steps ?? defaultProgress.steps
|
|
|
|
);
|
2023-04-18 11:56:15 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setStoredProgress({
|
2023-04-20 17:32:26 +02:00
|
|
|
welcomeOpen,
|
2023-04-18 11:56:15 +02:00
|
|
|
expanded,
|
|
|
|
topic,
|
|
|
|
steps,
|
|
|
|
});
|
2023-04-21 12:48:44 +02:00
|
|
|
}, [welcomeOpen, expanded, topic, steps]);
|
|
|
|
|
|
|
|
const onStart = () => {
|
|
|
|
setTopic(0);
|
|
|
|
setSteps([0]);
|
|
|
|
setExpanded(true);
|
|
|
|
};
|
2023-04-18 11:56:15 +02:00
|
|
|
|
2023-04-21 12:48:44 +02:00
|
|
|
const onFinish = () => {
|
|
|
|
const completedSteps = steps.reduce(
|
|
|
|
(acc, step) => acc + (step || 0),
|
|
|
|
1
|
|
|
|
);
|
|
|
|
const totalSteps = TOPICS.flatMap(({ steps }) => steps).length;
|
|
|
|
|
|
|
|
if (completedSteps === totalSteps) {
|
|
|
|
setFinishOpen(true);
|
|
|
|
}
|
|
|
|
};
|
2023-04-18 11:56:15 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2023-04-20 17:32:26 +02:00
|
|
|
<DemoDialogWelcome
|
|
|
|
open={welcomeOpen}
|
|
|
|
onClose={() => {
|
|
|
|
setWelcomeOpen(false);
|
|
|
|
setExpanded(false);
|
|
|
|
}}
|
|
|
|
onStart={() => {
|
|
|
|
setWelcomeOpen(false);
|
2023-04-21 12:48:44 +02:00
|
|
|
onStart();
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<DemoDialogFinish
|
|
|
|
open={finishOpen}
|
|
|
|
onClose={() => {
|
|
|
|
setFinishOpen(false);
|
|
|
|
}}
|
|
|
|
onRestart={() => {
|
|
|
|
setFinishOpen(false);
|
|
|
|
onStart();
|
2023-04-20 17:32:26 +02:00
|
|
|
}}
|
|
|
|
/>
|
2023-04-18 11:56:15 +02:00
|
|
|
<DemoTopics
|
|
|
|
expanded={expanded}
|
|
|
|
setExpanded={setExpanded}
|
|
|
|
steps={steps}
|
|
|
|
currentTopic={topic}
|
|
|
|
setCurrentTopic={(topic: number) => {
|
|
|
|
setTopic(topic);
|
|
|
|
setSteps(steps => {
|
|
|
|
const newSteps = [...steps];
|
|
|
|
newSteps[topic] = 0;
|
|
|
|
return newSteps;
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
topics={TOPICS}
|
2023-04-21 12:48:44 +02:00
|
|
|
onWelcome={() => setWelcomeOpen(true)}
|
2023-04-18 11:56:15 +02:00
|
|
|
/>
|
2023-04-21 12:48:44 +02:00
|
|
|
<DemoSteps
|
|
|
|
setExpanded={setExpanded}
|
|
|
|
steps={steps}
|
|
|
|
setSteps={setSteps}
|
|
|
|
topic={topic}
|
|
|
|
setTopic={setTopic}
|
|
|
|
topics={TOPICS}
|
|
|
|
onFinish={onFinish}
|
2023-04-18 11:56:15 +02:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|