-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Migrate Public Dashboard Page to React #4203
Closed
Closed
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2771fbe
Initial React Rendering with useDashboard
gabrieldutra aa66aff
Make sure widgets refresh + useCallback
gabrieldutra 49dffba
Rename collectFilters and add refreshRate
gabrieldutra 2516635
Fix error updates not being rendered
gabrieldutra 6cc5091
Only render widget bottom when queryResults exists
gabrieldutra 7796161
Cleanup
gabrieldutra ecd55f4
Add useCallback to refreshDashboard
gabrieldutra bce0d97
Make sure Promise.all have all promises done
gabrieldutra 8aee2ad
Merge branch 'master' into react-public-dashboard
gabrieldutra 2c31510
Merge branch 'master' into react-public-dashboard
gabrieldutra 77b5ccd
Merge branch 'master' into react-public-dashboard
gabrieldutra c45b2ea
Merge branch 'master' into react-public-dashboard
gabrieldutra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import React from 'react'; | ||
import { isEmpty } from 'lodash'; | ||
import PropTypes from 'prop-types'; | ||
import { react2angular } from 'react2angular'; | ||
import { BigMessage } from '@/components/BigMessage'; | ||
import { PageHeader } from '@/components/PageHeader'; | ||
import { Parameters } from '@/components/Parameters'; | ||
import { DashboardGrid } from '@/components/dashboards/DashboardGrid'; | ||
import { Filters } from '@/components/Filters'; | ||
import { Dashboard } from '@/services/dashboard'; | ||
import { $route as ngRoute } from '@/services/ng'; | ||
import PromiseRejectionError from '@/lib/promise-rejection-error'; | ||
import logoUrl from '@/assets/images/redash_icon_small.png'; | ||
import useDashboard from './useDashboard'; | ||
|
||
import './PublicDashboardPage.less'; | ||
|
||
|
||
function PublicDashboard({ dashboard }) { | ||
const { globalParameters, filters, setFilters, refreshDashboard, | ||
widgets, loadWidget, refreshWidget } = useDashboard(dashboard); | ||
|
||
return ( | ||
<div className="container p-t-10 p-b-20"> | ||
<PageHeader title={dashboard.name} /> | ||
{!isEmpty(globalParameters) && ( | ||
<div className="m-b-10 p-15 bg-white tiled"> | ||
<Parameters parameters={globalParameters} onValuesChange={refreshDashboard} /> | ||
</div> | ||
)} | ||
{!isEmpty(filters) && ( | ||
<div className="m-b-10 p-15 bg-white tiled"> | ||
<Filters filters={filters} onChange={setFilters} /> | ||
</div> | ||
)} | ||
<div id="dashboard-container"> | ||
<DashboardGrid | ||
dashboard={dashboard} | ||
widgets={widgets} | ||
filters={filters} | ||
isEditing={false} | ||
isPublic | ||
onLoadWidget={loadWidget} | ||
onRefreshWidget={refreshWidget} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
PublicDashboard.propTypes = { | ||
dashboard: PropTypes.object.isRequired, // eslint-disable-line react/forbid-prop-types | ||
}; | ||
|
||
class PublicDashboardPage extends React.Component { | ||
state = { | ||
loading: true, | ||
dashboard: null, | ||
}; | ||
|
||
componentDidMount() { | ||
Dashboard.getByToken({ token: ngRoute.current.params.token }).$promise | ||
.then(dashboard => this.setState({ dashboard, loading: false })) | ||
.catch((error) => { throw new PromiseRejectionError(error); }); | ||
} | ||
|
||
render() { | ||
const { loading, dashboard } = this.state; | ||
return ( | ||
<div className="public-dashboard-page"> | ||
{loading ? ( | ||
<div className="container loading-message"> | ||
<BigMessage | ||
className="" | ||
icon="fa-spinner fa-2x fa-pulse" | ||
message="Loading..." | ||
/> | ||
</div> | ||
) : ( | ||
<PublicDashboard dashboard={dashboard} /> | ||
)} | ||
<div id="footer"> | ||
<div className="text-center"> | ||
<a href="https://redash.io"><img alt="Redash Logo" src={logoUrl} width="38" /></a> | ||
</div> | ||
Powered by <a href="https://redash.io/?ref=public-dashboard">Redash</a> | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default function init(ngModule) { | ||
ngModule.component('publicDashboardPage', react2angular(PublicDashboardPage)); | ||
|
||
function session($route, Auth) { | ||
const token = $route.current.params.token; | ||
Auth.setApiKey(token); | ||
return Auth.loadConfig(); | ||
} | ||
|
||
ngModule.config(($routeProvider) => { | ||
$routeProvider.when('/public/dashboards/:token', { | ||
template: '<public-dashboard-page></public-dashboard-page>', | ||
reloadOnSearch: false, | ||
resolve: { | ||
session, | ||
}, | ||
}); | ||
}); | ||
|
||
return []; | ||
} | ||
|
||
init.init = true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
.public-dashboard-page { | ||
> .container { | ||
min-height: calc(100vh - 95px); | ||
} | ||
|
||
.loading-message { | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
} | ||
|
||
#footer { | ||
height: 95px; | ||
text-align: center; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,4 +95,4 @@ export default function init(ngModule) { | |
return []; | ||
} | ||
|
||
init.init = true; | ||
// init.init = true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { useState, useEffect, useMemo, useCallback } from 'react'; | ||
import { isEmpty, isNaN, includes, compact, map } from 'lodash'; | ||
import { $location } from '@/services/ng'; | ||
import { collectDashboardFilters } from '@/services/dashboard'; | ||
|
||
function getAffectedWidgets(widgets, updatedParameters = []) { | ||
return !isEmpty(updatedParameters) ? widgets.filter( | ||
widget => Object.values(widget.getParameterMappings()).filter( | ||
({ type }) => type === 'dashboard-level', | ||
).some( | ||
({ mapTo }) => includes(updatedParameters.map(p => p.name), mapTo), | ||
), | ||
) : widgets; | ||
} | ||
|
||
function getRefreshRateFromUrl() { | ||
const refreshRate = parseFloat($location.search().refresh); | ||
return isNaN(refreshRate) ? null : Math.max(30, refreshRate); | ||
} | ||
|
||
function useDashboard(dashboard) { | ||
const [filters, setFilters] = useState([]); | ||
const [widgets, setWidgets] = useState(dashboard.widgets); | ||
const globalParameters = useMemo(() => dashboard.getParametersDefs(), [dashboard]); | ||
const refreshRate = useMemo(getRefreshRateFromUrl, []); | ||
|
||
const loadWidget = useCallback((widget, forceRefresh = false) => { | ||
widget.getParametersDefs(); // Force widget to read parameters values from URL | ||
setWidgets([...dashboard.widgets]); | ||
return widget.load(forceRefresh).finally(() => setWidgets([...dashboard.widgets])); | ||
}, [dashboard]); | ||
|
||
const refreshWidget = useCallback(widget => loadWidget(widget, true), [loadWidget]); | ||
|
||
const loadDashboard = useCallback((forceRefresh = false, updatedParameters = []) => { | ||
const affectedWidgets = getAffectedWidgets(widgets, updatedParameters); | ||
const loadWidgetPromises = compact(affectedWidgets.map(widget => loadWidget(widget, forceRefresh))); | ||
|
||
return Promise.all(loadWidgetPromises).finally(() => { | ||
const queryResults = compact(map(widgets, widget => widget.getQueryResult())); | ||
const updatedFilters = collectDashboardFilters(dashboard, queryResults, $location.search()); | ||
setFilters(updatedFilters); | ||
}); | ||
}, [dashboard, widgets, loadWidget]); | ||
|
||
const refreshDashboard = updatedParameters => loadDashboard(true, updatedParameters); | ||
|
||
useEffect(() => { | ||
loadDashboard(); | ||
}, [dashboard]); | ||
|
||
useEffect(() => { | ||
if (refreshRate) { | ||
const refreshTimer = setInterval(refreshDashboard, refreshRate * 1000); | ||
return () => clearInterval(refreshTimer); | ||
} | ||
}, [refreshRate]); | ||
|
||
return { | ||
widgets, | ||
globalParameters, | ||
filters, | ||
setFilters, | ||
refreshDashboard, | ||
loadWidget, | ||
refreshWidget, | ||
}; | ||
} | ||
|
||
export default useDashboard; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this has been playing around with me for a while (in here and in the Widget Migration).
It was returning the queryResult before updating
this.data
, which was a weird result when I expected the widgets to render an error and they were not doing it