// Copyright 2018 Red Hat, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
// The App is the parent component of every pages. Each page content is
// rendered by the Route object according to the current location.
import React from 'react'
import PropTypes from 'prop-types'
import { matchPath, withRouter } from 'react-router'
import { Link, NavLink, Redirect, Route, Switch } from 'react-router-dom'
import { connect } from 'react-redux'
import { withAuth } from 'oidc-react'
import {
TimedToastNotification,
ToastNotificationList,
} from 'patternfly-react'
import * as moment from 'moment'
import {
Brand,
Button,
ButtonVariant,
Dropdown,
DropdownItem,
DropdownToggle,
DropdownSeparator,
KebabToggle,
Modal,
Nav,
NavItem,
NavList,
NotificationBadge,
NotificationDrawer,
NotificationDrawerBody,
NotificationDrawerList,
NotificationDrawerListItem,
NotificationDrawerListItemBody,
NotificationDrawerListItemHeader,
Page,
PageHeader,
PageHeaderTools,
PageHeaderToolsGroup,
PageHeaderToolsItem,
} from '@patternfly/react-core'
import {
BellIcon,
BookIcon,
ChevronDownIcon,
CodeIcon,
ServiceIcon,
UsersIcon,
} from '@patternfly/react-icons'
import AuthContainer from './containers/auth/Auth'
import ErrorBoundary from './containers/ErrorBoundary'
import { Fetching } from './containers/Fetching'
import SelectTz from './containers/timezone/SelectTz'
import ConfigModal from './containers/config/Config'
import logo from './images/logo.svg'
import { clearNotification } from './actions/notifications'
import { fetchConfigErrorsAction, clearConfigErrorsAction } from './actions/configErrors'
import { fetchTenantsIfNeeded } from './actions/tenants'
import { routes } from './routes'
import { setTenantAction } from './actions/tenant'
import { configureAuthFromTenant, configureAuthFromInfo } from './actions/auth'
import { getHomepageUrl } from './api'
import AuthCallbackPage from './pages/AuthCallback'
import AuthRequiredPage from './pages/AuthRequired'
class App extends React.Component {
static propTypes = {
notifications: PropTypes.array,
configErrors: PropTypes.array,
configErrorsReady: PropTypes.bool,
info: PropTypes.object,
tenant: PropTypes.object,
tenants: PropTypes.object,
timezone: PropTypes.string,
location: PropTypes.object,
history: PropTypes.object,
dispatch: PropTypes.func,
isKebabDropdownOpen: PropTypes.bool,
user: PropTypes.object,
auth: PropTypes.object,
signIn: PropTypes.func,
}
state = {
showErrors: false,
isTenantDropdownOpen: false,
}
renderMenu() {
const { tenant } = this.props
if (tenant.name) {
return (
)
} else {
// Return an empty navigation bar in case we don't have an active tenant
return
}
}
isAuthReady() {
const { info, auth, user } = this.props
return !(info.isFetching ||
!auth.info ||
auth.isFetching ||
(user.data && user.data.isFetching) ||
user.isFetching)
}
renderContent = () => {
const { tenant, auth, user } = this.props
const allRoutes = []
if ((window.location.origin + window.location.pathname) ===
(getHomepageUrl() + 'auth_callback')) {
// Sit on the auth callback page until login and token
// validation is complete (it will internally redirect when complete)
return
}
if (!this.isAuthReady()) {
return
}
if (auth.info.read_protected && !user.data) {
console.log('Read-access login required')
const redirect_target = window.location.href.slice(getHomepageUrl().length)
localStorage.setItem('zuul_auth_redirect', redirect_target)
this.props.signIn()
return
}
if (auth.info.read_protected && user.scope.length<1) {
return
}
this.menu
// Do not include '/tenants' route in white-label setup
.filter(item =>
(tenant.whiteLabel && !item.globalRoute) || !tenant.whiteLabel)
.forEach((item, index) => {
// We use react-router's render function to be able to pass custom props
// to our route components (pages):
// https://reactrouter.com/web/api/Route/render-func
// https://learnwithparam.com/blog/how-to-pass-props-in-react-router/
allRoutes.push(
(
)}
exact
/>
)
})
if (tenant.defaultRoute)
allRoutes.push(
)
return (
{allRoutes}
)
}
componentDidUpdate() {
// This method is called when info property is updated
const { tenant, info, auth, user, configErrorsReady } = this.props
if (info.ready) {
let tenantName = null
let whiteLabel
if (info.tenant) {
// White label
whiteLabel = true
tenantName = info.tenant
} else if (!info.tenant) {
// Multi tenant, look for tenant name in url
whiteLabel = false
this.props.dispatch(fetchTenantsIfNeeded())
const match = matchPath(
this.props.location.pathname, { path: '/t/:tenant' })
if (match) {
tenantName = match.params.tenant
}
}
// Set tenant only if it changed to prevent DidUpdate loop
if (tenant.name !== tenantName) {
const tenantAction = setTenantAction(tenantName, whiteLabel)
this.props.dispatch(tenantAction)
if (tenantName) {
this.props.dispatch(clearConfigErrorsAction())
}
if (whiteLabel || !tenantName) {
// The app info endpoint was already a tenant info
// endpoint, so auth info was already provided.
this.props.dispatch(configureAuthFromInfo(info))
} else {
// Query the tenant info endpoint for auth info.
this.props.dispatch(configureAuthFromTenant(tenantName))
}
}
if (tenant && tenant.name && !configErrorsReady && this.isAuthReady() &&
(!auth.info.read_protected || user.data)) {
// This will happen after the tenant action is complete, so we
// can use the "old" tenant now.
this.props.dispatch(fetchConfigErrorsAction(tenant))
}
}
}
constructor() {
super()
this.menu = routes()
}
handleKebabDropdownToggle = (isKebabDropdownOpen) => {
this.setState({
isKebabDropdownOpen
})
}
handleKebabDropdownSelect = () => {
this.setState({
isKebabDropdownOpen: !this.state.isKebabDropdownOpen
})
}
handleComponentsLink = () => {
const { history } = this.props
history.push('/components')
}
handleApiLink = () => {
const { history } = this.props
history.push('/openapi')
}
handleDocumentationLink = () => {
window.open('https://zuul-ci.org/docs', '_blank', 'noopener noreferrer')
}
handleTenantLink = () => {
const { history, tenant } = this.props
history.push(tenant.defaultRoute)
}
handleModalClose = () => {
this.setState({
showErrors: false
})
}
renderNotifications = (notifications) => {
return (
{notifications.map(notification => {
let notificationBody
if (notification.type === 'error') {
notificationBody = (
<>
{notification.text} {notification.status}
{notification.url}
>
)
} else {
notificationBody = ({notification.text})
}
return (
{ this.props.dispatch(clearNotification(notification.id)) }}
>
{notificationBody}
)
}
)}
)
}
renderConfigErrors = (configErrors) => {
const { history } = this.props
const { showErrors } = this.state
const errors = []
configErrors.forEach((item, idx) => {
let error = item.error
let cookie = error.indexOf('The error was:')
if (cookie !== -1) {
error = error.slice(cookie + 18).split('\n')[0]
}
let ctxPath = item.source_context.path
if (item.source_context.branch !== 'master') {
ctxPath += ' (' + item.source_context.branch + ')'
}
errors.push(
{
history.push(this.props.tenant.linkPrefix + '/config-errors')
this.setState({ showErrors: false })
}}
>
{error}
)
})
return (
Config Errors
{errors.length} error(s)
>
}
>
{errors.map(item => (item))}
)
}
renderTenantDropdown() {
const { tenant, tenants } = this.props
const { isTenantDropdownOpen } = this.state
if (tenant.whiteLabel) {
return (
Tenant {tenant.name}
)
} else {
const tenantLink = (_tenant) => {
const currentPath = this.props.location.pathname
let suffix
switch (currentPath) {
case '/t/' + tenant.name + '/projects':
suffix = '/projects'
break
case '/t/' + tenant.name + '/jobs':
suffix = '/jobs'
break
case '/t/' + tenant.name + '/labels':
suffix = '/labels'
break
case '/t/' + tenant.name + '/nodes':
suffix = '/nodes'
break
case '/t/' + tenant.name + '/autoholds':
suffix = '/autoholds'
break
case '/t/' + tenant.name + '/builds':
suffix = '/builds'
break
case '/t/' + tenant.name + '/buildsets':
suffix = '/buildsets'
break
case '/t/' + tenant.name + '/status':
default:
// all other paths point to tenant-specific resources that would most likely result in a 404
suffix = '/status'
break
}
return {_tenant.name}
}
const options = tenants.tenants.filter(
(_tenant) => (_tenant.name !== tenant.name)
).map(
(_tenant, idx) => {
return (
)
})
options.push(
,
Go to tenants page} />
)
return (tenants.isFetching ?
Loading tenants ...
:
<>
{ this.setState({ isTenantDropdownOpen: isOpen }) }}
toggleIndicator={ChevronDownIcon}
>
Tenant {tenant.name}
}
onSelect={() => { this.setState({ isTenantDropdownOpen: !isTenantDropdownOpen }) }}
dropdownItems={options}
/>
>)
}
}
render() {
const { isKebabDropdownOpen } = this.state
const { notifications, configErrors, tenant, info, auth } = this.props
const nav = this.renderMenu()
const kebabDropdownItems = []
if (!info.tenant) {
kebabDropdownItems.push(
this.handleComponentsLink(event)}
>
Components
)
}
kebabDropdownItems.push(
this.handleApiLink(event)}>
API
)
kebabDropdownItems.push(
this.handleDocumentationLink(event)}
>
Documentation
)
if (tenant.name) {
kebabDropdownItems.push(
this.handleTenantLink(event)}
>
Tenants
)
}
const pageHeaderTools = (
{/* The utility navbar is only visible on desktop sizes
and replaced by a kebab dropdown for smaller sizes */}
{ (!info.tenant) &&
}
{tenant.name && (this.renderTenantDropdown())}
{/* this kebab dropdown replaces the icon buttons and is hidden for
desktop sizes */}
}
isOpen={isKebabDropdownOpen}
dropdownItems={kebabDropdownItems}
/>
{configErrors.length > 0 &&
{
e.preventDefault()
this.setState({ showErrors: !this.state.showErrors })
}}
>
}
{auth.info && auth.info.default_realm && ()}
)
// In case we don't have an active tenant, fall back to the root URL
const logoUrl = tenant.name ? tenant.defaultRoute : '/'
const pageHeader = (
}
logoProps={{ to: logoUrl }}
logoComponent={Link}
headerTools={pageHeaderTools}
/>
)
return (
{notifications.length > 0 && this.renderNotifications(notifications)}
{this.renderConfigErrors(configErrors)}
{this.renderContent()}
)
}
}
// This connect the info state from the store to the info property of the App.
export default withRouter(connect(
state => ({
notifications: state.notifications,
configErrors: state.configErrors.errors,
configErrorsReady: state.configErrors.ready,
info: state.info,
tenant: state.tenant,
tenants: state.tenants,
timezone: state.timezone,
user: state.user,
auth: state.auth,
})
)(withAuth(App)))