1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-06 00:07:44 +01:00
unleash.unleash/frontend/src/component/demo/Demo.tsx
Nuno Góis 710b2a6d5e
feat: demo guide improvements (#3676)
https://linear.app/unleash/issue/2-986/feedback-from-sebastian

Implements the items mentioned in the task:
 - Refactors logic to track completion separately;
- When finishing a topic, jumps to the next unfinished topic it can
find;
- Shows the finish dialog when finishing a topic, as long as completion
is 100%;
- Changes the guide overlay behavior and implements the necessary
changes to adapt to light and dark mode;
- Fixes an issue where some guide dialogs would close when clicking
outside;
- Added a final "toggle" step for each topic (still needs alignment,
different task);
- Improve navigation logic to hopefully fix the feature toggle name
sorting;


![image](https://user-images.githubusercontent.com/14320932/236003007-6e441acc-f933-4eb0-93a4-4b6c15a45b96.png)

Relates to [roadmap](https://github.com/orgs/Unleash/projects/10) item:
#3537
2023-05-03 21:47:35 +03:00

180 lines
5.4 KiB
TypeScript

import { useEffect, useState } from 'react';
import { DemoTopics } from './DemoTopics/DemoTopics';
import { DemoSteps } from './DemoSteps/DemoSteps';
import { createLocalStorage } from 'utils/createLocalStorage';
import { TOPICS } from './demo-topics';
import { DemoDialogWelcome } from './DemoDialog/DemoDialogWelcome/DemoDialogWelcome';
import { DemoDialogFinish } from './DemoDialog/DemoDialogFinish/DemoDialogFinish';
import { DemoDialogPlans } from './DemoDialog/DemoDialogPlans/DemoDialogPlans';
import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig';
import { DemoBanner } from './DemoBanner/DemoBanner';
import { usePlausibleTracker } from 'hooks/usePlausibleTracker';
const defaultProgress = {
welcomeOpen: true,
expanded: true,
topic: -1,
step: 0,
stepsCompletion: Array(TOPICS.length).fill(0),
};
interface IDemoProps {
children: JSX.Element;
}
export const Demo = ({ children }: IDemoProps): JSX.Element => {
const { uiConfig } = useUiConfig();
const { trackEvent } = usePlausibleTracker();
const { value: storedProgress, setValue: setStoredProgress } =
createLocalStorage('Tutorial:v1.1', defaultProgress);
const [welcomeOpen, setWelcomeOpen] = useState(
storedProgress.welcomeOpen ?? defaultProgress.welcomeOpen
);
const [finishOpen, setFinishOpen] = useState(false);
const [plansOpen, setPlansOpen] = useState(false);
const [expanded, setExpanded] = useState(
storedProgress.expanded ?? defaultProgress.expanded
);
const [topic, setTopic] = useState(
storedProgress.topic ?? defaultProgress.topic
);
const [step, setStep] = useState(
storedProgress.step ?? defaultProgress.step
);
const [stepsCompletion, setStepsCompletion] = useState(
storedProgress.stepsCompletion ?? defaultProgress.stepsCompletion
);
useEffect(() => {
setStoredProgress({
welcomeOpen,
expanded,
topic,
step,
stepsCompletion,
});
}, [welcomeOpen, expanded, topic, step, stepsCompletion]);
const onStart = () => {
setTopic(0);
setStep(0);
setStepsCompletion(Array(TOPICS.length).fill(0));
setExpanded(true);
};
const onFinish = () => {
setFinishOpen(true);
trackEvent('demo', {
props: {
eventType: 'finish',
},
});
};
if (!uiConfig.flags.demo) return children;
return (
<>
<DemoBanner
onPlans={() => {
setPlansOpen(true);
trackEvent('demo', {
props: {
eventType: 'see_plans',
},
});
}}
/>
{children}
<DemoDialogWelcome
open={welcomeOpen}
onClose={() => {
setWelcomeOpen(false);
setExpanded(false);
trackEvent('demo', {
props: {
eventType: 'close',
topic: 'start',
},
});
}}
onStart={() => {
setWelcomeOpen(false);
onStart();
trackEvent('demo', {
props: {
eventType: 'start',
},
});
}}
/>
<DemoDialogFinish
open={finishOpen}
onClose={() => {
setFinishOpen(false);
setPlansOpen(true);
}}
onRestart={() => {
setFinishOpen(false);
onStart();
trackEvent('demo', {
props: {
eventType: 'restart',
},
});
}}
/>
<DemoDialogPlans
open={plansOpen}
onClose={() => setPlansOpen(false)}
/>
<DemoTopics
expanded={expanded}
setExpanded={setExpanded}
stepsCompletion={stepsCompletion}
currentTopic={topic}
setCurrentTopic={(topic: number) => {
setTopic(topic);
setStep(0);
trackEvent('demo', {
props: {
eventType: 'start_topic',
step: TOPICS[topic].title,
},
});
}}
topics={TOPICS}
onWelcome={() => {
setWelcomeOpen(true);
trackEvent('demo', {
props: {
eventType: 'view_demo_link',
},
});
}}
/>
<DemoSteps
setExpanded={setExpanded}
step={step}
setStep={setStep}
stepsCompletion={stepsCompletion}
setStepsCompletion={setStepsCompletion}
topic={topic}
setTopic={setTopic}
topics={TOPICS}
onFinish={onFinish}
/>
</>
);
};