aboutsummaryrefslogtreecommitdiffstats
path: root/packages/website/ts/components/onboarding/onboarding_flow.tsx
blob: cd6fe2195afb6c49991785eeb9789da1c8e4d74f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import * as _ from 'lodash';
import * as React from 'react';
import Joyride, { CallbackData, Step, StyleOptions } from 'react-joyride';
import { Popper, PopperChildrenProps } from 'react-popper';

import { OnboardingTooltip } from 'ts/components/onboarding/onboarding_tooltip';
import { zIndex } from 'ts/utils/style';

export interface OnboardingFlowProps {
    steps: Step[];
    blacklistedStepIndices: number[];
    stepIndex: number;
    isRunning: boolean;
    onClose: () => void;
    setOnboardingStep: (stepIndex: number) => void;
}

const joyrideStyleOptions: StyleOptions = {
    zIndex: zIndex.overlay,
};

// Wrapper around Joyride with defaults and styles set
export class OnboardingFlow extends React.Component<OnboardingFlowProps> {
    public componentDidMount(): void {
        this._setOnboardingStepBasedOnBlacklist(this.props.stepIndex);
    }

    public componentWillReceiveProps(nextProps: OnboardingFlowProps): void {
        this._setOnboardingStepBasedOnBlacklist(nextProps.stepIndex);
    }

    public render(): React.ReactNode {
        if (!this.props.isRunning) {
            return null;
        }
        return (
            <Popper referenceElement={this._getElementForStep()} placement="right">
                {this._renderPopperChildren.bind(this)}
            </Popper>
        );
    }

    private _setOnboardingStepBasedOnBlacklist(nextIndex: number): void {
        const blacklistedSteps = this.props.blacklistedStepIndices;
        const newStepIndex = this._adjustedStepBasedOnBlacklist(
            this.props.stepIndex,
            nextIndex,
            this.props.steps.length,
            blacklistedSteps,
        );
        if (newStepIndex !== nextIndex) {
            this.props.setOnboardingStep(newStepIndex);
        }
    }

    private _adjustedStepBasedOnBlacklist(
        currentStep: number,
        nextStep: number,
        totalSteps: number,
        blacklistedSteps: number[],
    ): number {
        if (!blacklistedSteps.includes(nextStep)) {
            return nextStep;
        }
        let newStep = nextStep;
        const op = nextStep >= currentStep ? _.add : _.subtract;
        let didSearch = false;
        while (blacklistedSteps.includes(newStep)) {
            newStep = op(newStep, 1);
            if (newStep < 0) {
                if (didSearch) {
                    break;
                }
                newStep = totalSteps - 1;
                didSearch = true;
            }
            if (newStep >= totalSteps) {
                if (didSearch) {
                    break;
                }
                newStep = 0;
                didSearch = true;
            }
        }
        return newStep;
    }

    private _getElementForStep(): Element {
        const step = this.props.steps[this.props.stepIndex];
        return document.querySelector(step.target);
    }

    private _renderPopperChildren(props: any): React.ReactNode {
        const { arrowProps } = props;
        return (
            <div ref={props.ref} style={props.style} data-placement={props.placement}>
                {this._renderToolTip()}
                <div ref={arrowProps.ref} style={arrowProps.style} />
            </div>
        );
    }

    private _renderToolTip(): React.ReactNode {
        const { steps, stepIndex } = this.props;
        const step = steps[stepIndex];
        const isLastStep = steps.length - 1 === stepIndex;
        return (
            <OnboardingTooltip
                title={step.title}
                content={step.content}
                isLastStep={isLastStep}
                onClose={this.props.onClose}
                onClickNext={this._goToNextStep.bind(this)}
                onClickBack={this._goToPrevStep.bind(this)}
            />
        );
    }

    private _goToNextStep(): void {
        const nextStep = this.props.stepIndex + 1;
        if (nextStep < this.props.steps.length) {
            this.props.setOnboardingStep(nextStep);
        } else {
            this.props.onClose();
        }
    }

    private _goToPrevStep(): void {
        const nextStep = this.props.stepIndex - 1;
        if (nextStep >= 0) {
            this.props.setOnboardingStep(nextStep);
        } else {
            this.props.onClose();
        }
    }
}