123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- import {Component, Fragment} from 'react';
- import styled from '@emotion/styled';
- import {ModalRenderProps, openModal} from 'sentry/actionCreators/modal';
- import Button from 'sentry/components/button';
- import ButtonBar from 'sentry/components/buttonBar';
- import {IconClose} from 'sentry/icons';
- import {t} from 'sentry/locale';
- import space from 'sentry/styles/space';
- import {callIfFunction} from 'sentry/utils/callIfFunction';
- export type TourStep = {
- body: React.ReactNode;
- title: string;
- actions?: React.ReactNode;
- image?: React.ReactNode;
- };
- type ChildProps = {
- showModal: () => void;
- };
- type Props = {
- children: (props: ChildProps) => React.ReactNode;
- /**
- * The list of tour steps.
- * The FeatureTourModal will manage state on the active step.
- */
- steps: TourStep[];
- /**
- * Customize the text shown on the done button.
- */
- doneText?: string;
- /**
- * Provide a URL for the done state to open in a new tab.
- */
- doneUrl?: string;
- /**
- * Triggered when the tour is advanced.
- */
- onAdvance?: (currentIndex: number, durationOpen: number) => void;
- /**
- * Triggered when the tour is closed by completion or IconClose
- */
- onCloseModal?: (currentIndex: number, durationOpen: number) => void;
- };
- type State = {
- /**
- * The last known step
- */
- current: number;
- /**
- * The timestamp when the modal was shown.
- * Used to calculate how long the modal was open
- */
- openedAt: number;
- };
- const defaultProps = {
- doneText: t('Done'),
- };
- /**
- * Provide a showModal action to the child function that lets
- * a tour be triggered.
- *
- * Once active this component will track when the tour was started and keep
- * a last known step state. Ideally the state would live entirely in this component.
- * However, once the modal has been opened state changes in this component don't
- * trigger re-renders in the modal contents. This requires a bit of duplicate state
- * to be managed around the current step.
- */
- class FeatureTourModal extends Component<Props, State> {
- static defaultProps = defaultProps;
- state: State = {
- openedAt: 0,
- current: 0,
- };
- // Record the step change and call the callback this component was given.
- handleAdvance = (current: number, duration: number) => {
- this.setState({current});
- callIfFunction(this.props.onAdvance, current, duration);
- };
- handleShow = () => {
- this.setState({openedAt: Date.now()}, () => {
- const modalProps = {
- steps: this.props.steps,
- onAdvance: this.handleAdvance,
- openedAt: this.state.openedAt,
- doneText: this.props.doneText,
- doneUrl: this.props.doneUrl,
- };
- openModal(deps => <ModalContents {...deps} {...modalProps} />, {
- onClose: this.handleClose,
- });
- });
- };
- handleClose = () => {
- // The bootstrap modal and modal store both call this callback.
- // We use the state flag to deduplicate actions to upstream components.
- if (this.state.openedAt === 0) {
- return;
- }
- const {onCloseModal} = this.props;
- const duration = Date.now() - this.state.openedAt;
- callIfFunction(onCloseModal, this.state.current, duration);
- // Reset the state now that the modal is closed, used to deduplicate close actions.
- this.setState({openedAt: 0, current: 0});
- };
- render() {
- const {children} = this.props;
- return <Fragment>{children({showModal: this.handleShow})}</Fragment>;
- }
- }
- export default FeatureTourModal;
- type ContentsProps = ModalRenderProps &
- Pick<Props, 'steps' | 'doneText' | 'doneUrl' | 'onAdvance'> &
- Pick<State, 'openedAt'>;
- type ContentsState = {
- current: number;
- openedAt: number;
- };
- class ModalContents extends Component<ContentsProps, ContentsState> {
- static defaultProps = defaultProps;
- state: ContentsState = {
- current: 0,
- openedAt: Date.now(),
- };
- handleAdvance = () => {
- const {onAdvance, openedAt} = this.props;
- this.setState(
- prevState => ({current: prevState.current + 1}),
- () => {
- const duration = Date.now() - openedAt;
- callIfFunction(onAdvance, this.state.current, duration);
- }
- );
- };
- render() {
- const {Body, steps, doneText, doneUrl, closeModal} = this.props;
- const {current} = this.state;
- const step = steps[current] !== undefined ? steps[current] : steps[steps.length - 1];
- const hasNext = steps[current + 1] !== undefined;
- return (
- <Body data-test-id="feature-tour">
- <CloseButton
- borderless
- size="zero"
- onClick={closeModal}
- icon={<IconClose />}
- aria-label={t('Close tour')}
- />
- <TourContent>
- {step.image}
- <TourHeader>{step.title}</TourHeader>
- {step.body}
- <TourButtonBar gap={1}>
- {step.actions && step.actions}
- {hasNext && (
- <Button priority="primary" onClick={this.handleAdvance}>
- {t('Next')}
- </Button>
- )}
- {!hasNext && (
- <Button
- external
- href={doneUrl}
- onClick={closeModal}
- priority="primary"
- aria-label={t('Complete tour')}
- >
- {doneText}
- </Button>
- )}
- </TourButtonBar>
- <StepCounter>{t('%s of %s', current + 1, steps.length)}</StepCounter>
- </TourContent>
- </Body>
- );
- }
- }
- const CloseButton = styled(Button)`
- position: absolute;
- top: -${space(2)};
- right: -${space(1)};
- `;
- const TourContent = styled('div')`
- display: flex;
- flex-direction: column;
- align-items: center;
- margin: ${space(3)} ${space(4)} ${space(1)} ${space(4)};
- `;
- const TourHeader = styled('h4')`
- margin-bottom: ${space(1)};
- `;
- const TourButtonBar = styled(ButtonBar)`
- margin-bottom: ${space(3)};
- `;
- const StepCounter = styled('div')`
- text-transform: uppercase;
- font-size: ${p => p.theme.fontSizeSmall};
- font-weight: bold;
- color: ${p => p.theme.gray300};
- `;
- // Styled components that can be used to build tour content.
- export const TourText = styled('p')`
- text-align: center;
- margin-bottom: ${space(4)};
- `;
- export const TourImage = styled('img')`
- height: 200px;
- margin-bottom: ${space(4)};
- /** override styles in less files */
- max-width: 380px !important;
- box-shadow: none !important;
- border: 0 !important;
- border-radius: 0 !important;
- `;
|