Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HelpTrigger to open in drawer #3436

Merged
merged 4 commits into from
Feb 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions client/app/assets/less/ant.less
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
@import '~antd/lib/tag/style/index';
@import '~antd/lib/grid/style/index';
@import '~antd/lib/switch/style/index';
@import '~antd/lib/drawer/style/index';
@import 'inc/ant-variables';

// Remove bold in labels for Ant checkboxes and radio buttons
Expand Down Expand Up @@ -204,4 +205,8 @@
color: @text-color-secondary;
font-weight: normal;
margin-top: 4px;
}

.ant-popover {
z-index: 1000; // make sure it doesn't cover drawer
}
134 changes: 125 additions & 9 deletions client/app/services/HelpTrigger.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import React from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
arikfr marked this conversation as resolved.
Show resolved Hide resolved
import Tooltip from 'antd/lib/tooltip';
import Icon from 'antd/lib/icon';
import Drawer from 'antd/lib/drawer';
import { BigMessage } from '@/components/BigMessage';

const BASE_URL = 'https://redash.io/help/user-guide/';
import './HelpTrigger.less';

const DOMAIN = 'https://redash.io';
const HELP_PATH = '/help/user-guide/';
const MESSAGES = {
HELP_DRAWER_READY: 'drawer_ready',
HELP_DRAWER_REQUEST: 'drawer_request',
};
const IFRAME_TIMEOUT = 5000;
const TYPES = {
VALUE_SOURCE_OPTIONS: [
'querying/query-parameters#Value-Source-Options',
Expand All @@ -20,16 +31,121 @@ export default class HelpTrigger extends React.PureComponent {
type: PropTypes.oneOf(Object.keys(TYPES)).isRequired,
}

iframeRef = null

iframeLoadingTimeout = null;

constructor(props) {
super(props);
this.iframeRef = React.createRef();
}

state = {
visible: false,
loading: false,
error: false,
}

componentWillUnmount() {
this.onDrawerClosed();
gabrieldutra marked this conversation as resolved.
Show resolved Hide resolved
}

get helpUrl() {
const [pagePath] = TYPES[this.props.type];
return DOMAIN + HELP_PATH + pagePath;
}

onIframeLoaded = () => {
const { contentWindow } = this.iframeRef.current;
contentWindow.postMessage(MESSAGES.HELP_DRAWER_REQUEST, DOMAIN);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The weird thing about this is that if the window doesn't match the domain (try loading a different url in the iframe), it'll fire an UNCATCHABLE error!

}

onIframeError = () => {
this.setState({ error: true, loading: false });
}

onIframeReady = () => {
this.setState({ loading: false });
this.removeIframeListeners();
}

onPostMessageReceived = (event) => {
if (event.origin === DOMAIN && event.data === MESSAGES.HELP_DRAWER_READY) {
this.onIframeReady();
}
}

openDrawer = () => {
this.setState({ visible: true, loading: true, error: false });
setTimeout(this.onDrawerOpened, 300); // to prevent animation jank
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is not ideal - but the simplest (and as risky as any other simple solution I could conceive). Open to suggestions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll create a PR for Ant with onAfterOpen perhaps.

}

closeDrawer = () => {
this.setState({ visible: false });
this.removeIframeListeners();
}

onDrawerOpened = () => {
this.addIframeListeners();
this.iframeRef.current.src = this.helpUrl;
}

addIframeListeners = () => {
window.addEventListener('message', this.onPostMessageReceived, DOMAIN);
this.iframeRef.current.addEventListener('load', this.onIframeLoaded);
this.iframeLoadingTimeout = setTimeout(this.onIframeError, IFRAME_TIMEOUT);
}

removeIframeListeners = () => {
window.removeEventListener('message', this.onPostMessageReceived);
this.iframeRef.current.removeEventListener('load', this.onIframeLoaded);
ranbena marked this conversation as resolved.
Show resolved Hide resolved
window.clearTimeout(this.iframeLoadingTimeout);
}

render() {
const [path, tooltip] = TYPES[this.props.type];
const href = BASE_URL + path;
const [, tooltip] = TYPES[this.props.type];
ranbena marked this conversation as resolved.
Show resolved Hide resolved

return (
<Tooltip title={`Guide: ${tooltip}`}>
{/* eslint-disable-next-line react/jsx-no-target-blank */}
<a href={href} target="_blank" rel="noopener">
<Icon type="question-circle" />
</a>
</Tooltip>
<React.Fragment>
<Tooltip title={`Guide: ${tooltip}`}>
<a href="javascript: void(0)" onClick={this.openDrawer}>
<Icon type="question-circle" />
</a>
</Tooltip>
<Drawer
placement="right"
onClose={this.closeDrawer}
visible={this.state.visible}
className="help-drawer"
destroyOnClose
width={450}
>
{/* iframe */}
{!this.state.error && (
<iframe
ref={this.iframeRef}
title="Redash Help"
src="about:blank"
className={cx({ ready: !this.state.loading })}
/>
)}

{/* loading indicator */}
{this.state.loading && (
<BigMessage icon="fa-spinner fa-2x fa-pulse" message="Loading..." className="help-message" />
)}

{/* error message */}
{this.state.error && (
<BigMessage icon="fa-exclamation-circle" className="help-message">
Something went wrong.<br />
{/* eslint-disable-next-line react/jsx-no-target-blank */}
<a href={this.helpUrl + '?s=help_error'} target="_blank" rel="noopener">Click here</a>{' '}
to open the document in a new window.
ranbena marked this conversation as resolved.
Show resolved Hide resolved
</BigMessage>
)}
</Drawer>
</React.Fragment>
);
}
}
21 changes: 21 additions & 0 deletions client/app/services/HelpTrigger.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.help-drawer {
.ant-drawer-body {
padding: 0;
height: 100%; // to allow iframe full dimensions
display: flex;
align-items: center;
justify-content: center;
}

iframe {
width: 0;
visibility: hidden;
}

iframe.ready {
border: 0;
width: 100%;
height: 100%;
visibility: visible;
}
}