|
| 1 | +import React, { FunctionComponent } from "react"; |
| 2 | +import Joyride, { CallBackProps, STATUS } from "react-joyride"; |
| 3 | +import { IconButton, Avatar } from "@material-ui/core"; |
| 4 | +import { HelpOutline } from "@material-ui/icons"; |
| 5 | +import { useHelpState } from "../contexts"; |
| 6 | + |
| 7 | +const GuidedTour: FunctionComponent = () => { |
| 8 | + const [run, setRun] = React.useState(false); |
| 9 | + const { helpSteps } = useHelpState(); |
| 10 | + |
| 11 | + const getHelpSteps = React.useCallback(() => { |
| 12 | + const firstStep = helpSteps[0]; |
| 13 | + //Below line is to prevent application breaking if element is not present for any reason (e.g. if the user deletes build or if there is no data.) |
| 14 | + if ( |
| 15 | + firstStep && |
| 16 | + document.getElementById(firstStep.target.toString().slice(1)) |
| 17 | + ) { |
| 18 | + helpSteps.forEach((e) => { |
| 19 | + e.disableBeacon = true; |
| 20 | + e.hideCloseButton = true; |
| 21 | + }); |
| 22 | + return helpSteps; |
| 23 | + } |
| 24 | + return []; |
| 25 | + }, [helpSteps]); |
| 26 | + |
| 27 | + const handleJoyrideCallback = (data: CallBackProps) => { |
| 28 | + const { status } = data; |
| 29 | + const finishedStatuses: string[] = [STATUS.FINISHED, STATUS.SKIPPED]; |
| 30 | + |
| 31 | + if (finishedStatuses.includes(status)) { |
| 32 | + setRun(false); |
| 33 | + } |
| 34 | + }; |
| 35 | + |
| 36 | + const handleClickStart = (e: React.MouseEvent<HTMLElement>) => { |
| 37 | + e.preventDefault(); |
| 38 | + setRun(true); |
| 39 | + }; |
| 40 | + |
| 41 | + return ( |
| 42 | + <React.Fragment> |
| 43 | + <IconButton onClick={handleClickStart}> |
| 44 | + <Avatar> |
| 45 | + <HelpOutline /> |
| 46 | + <Joyride |
| 47 | + callback={handleJoyrideCallback} |
| 48 | + continuous={true} |
| 49 | + run={run} |
| 50 | + scrollToFirstStep={true} |
| 51 | + showProgress={true} |
| 52 | + showSkipButton={true} |
| 53 | + steps={getHelpSteps()} |
| 54 | + disableCloseOnEsc={true} |
| 55 | + styles={{ |
| 56 | + options: { |
| 57 | + zIndex: 10000, |
| 58 | + }, |
| 59 | + buttonNext: { color: "#3f51b5", backgroundColor: "" }, |
| 60 | + buttonBack: { color: "#3f51b5" }, |
| 61 | + }} |
| 62 | + /> |
| 63 | + </Avatar> |
| 64 | + </IconButton> |
| 65 | + </React.Fragment> |
| 66 | + ); |
| 67 | +}; |
| 68 | + |
| 69 | +export default GuidedTour; |
0 commit comments