Farmbot-Web-App/frontend/help/tour.tsx

89 lines
2.4 KiB
TypeScript
Raw Permalink Normal View History

2018-10-15 17:23:58 -06:00
import * as React from "react";
2019-01-07 17:08:48 -07:00
import Joyride, { Step as TourStep, CallBackProps } from "react-joyride";
2018-10-15 17:23:58 -06:00
import { Color } from "../ui";
import { history } from "../history";
import { TOUR_STEPS, tourPageNavigation } from "./tours";
2019-04-02 13:59:37 -06:00
import { t } from "../i18next_wrapper";
2019-04-23 12:40:32 -06:00
import { Actions } from "../constants";
import { store } from "../redux/store";
2020-02-18 12:21:09 -07:00
import { ErrorBoundary } from "../error_boundary";
2018-10-15 17:23:58 -06:00
const strings = () => ({
back: t("Back"),
close: t("Close"),
last: t("End Tour"),
next: t("Next"),
skip: t("End Tour")
});
const STYLES = {
2018-10-15 17:23:58 -06:00
buttonNext: { backgroundColor: Color.green },
buttonBack: { color: Color.darkGray }
2019-01-07 17:08:48 -07:00
};
2018-10-15 17:23:58 -06:00
interface TourProps {
steps: TourStep[];
}
interface TourState {
run: boolean;
index: number;
2019-06-14 16:59:11 -06:00
returnPath: string;
2018-10-15 17:23:58 -06:00
}
export class Tour extends React.Component<TourProps, TourState> {
2020-02-28 09:35:32 -07:00
state: TourState = { run: false, index: 0, returnPath: "" };
2018-10-15 17:23:58 -06:00
2019-01-07 17:08:48 -07:00
callback = ({ action, index, step, type }: CallBackProps) => {
2018-10-15 17:23:58 -06:00
console.log("Tour debug:", step.target, type, action);
tourPageNavigation(step.target);
if (type === "step:after") {
const increment = action === "prev" ? -1 : 1;
const nextStepIndex = index + increment;
this.setState({ index: nextStepIndex });
const nextStepTarget = nextStepIndex < this.props.steps.length
? this.props.steps[nextStepIndex].target : "";
tourPageNavigation(nextStepTarget);
}
if (type === "tour:end") {
this.setState({ run: false });
2019-06-14 16:59:11 -06:00
history.push(this.state.returnPath);
2019-04-23 12:40:32 -06:00
store.dispatch({ type: Actions.START_TOUR, payload: undefined });
2018-10-15 17:23:58 -06:00
}
};
componentDidMount() {
2019-06-14 16:59:11 -06:00
this.setState({
run: true,
index: 0,
returnPath: history.getCurrentLocation().pathname,
});
2018-10-15 17:23:58 -06:00
}
render() {
const steps = this.props.steps.map(step => {
step.disableBeacon = true;
return step;
});
return <div className="tour">
2020-02-18 12:21:09 -07:00
<ErrorBoundary>
<Joyride
steps={steps}
run={this.state.run}
callback={this.callback}
stepIndex={this.state.index}
showSkipButton={true}
continuous={true}
styles={STYLES}
locale={strings()} />
</ErrorBoundary>
2018-10-15 17:23:58 -06:00
</div>;
}
}
export const RunTour = ({ currentTour }: { currentTour: string | undefined }) => {
2020-02-28 09:34:28 -07:00
return currentTour
? <Tour steps={TOUR_STEPS()[currentTour]} />
: <div className={"tour-inactive"} />;
2018-10-15 17:23:58 -06:00
};