Skip to content

Commit

Permalink
console: Refactor admin panel
Browse files Browse the repository at this point in the history
  • Loading branch information
kschiffer committed Jun 29, 2023
1 parent 6e83bc5 commit 7454ed9
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 41 deletions.
47 changes: 25 additions & 22 deletions pkg/webui/console/components/panel-view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import React from 'react'
import React, { useCallback } from 'react'
import { Col, Container, Row } from 'react-grid-system'
import { NavLink, Switch, Route, useRouteMatch } from 'react-router-dom'
import { useRoutes, NavLink, Navigate } from 'react-router-dom'
import classNames from 'classnames'

import Icon from '@ttn-lw/components/icon'

import Message from '@ttn-lw/lib/components/message'
import NotFoundRoute from '@ttn-lw/lib/components/not-found-route'
import { ErrorView } from '@ttn-lw/lib/components/error-view'
import GenericNotFound from '@ttn-lw/lib/components/full-view-error/not-found'

import SubViewError from '@console/views/sub-view-error'

Expand All @@ -30,57 +30,60 @@ import PropTypes from '@ttn-lw/lib/prop-types'
import style from './panel-view.styl'

const PanelView = ({ children, className }) => {
const { path } = useRouteMatch()
const firstChild = React.Children.toArray(children)[0]
const childrenArray = React.Children.toArray(children)
const firstChild = childrenArray[0]

const routes = useRoutes([
{ path: '', element: <Navigate to={firstChild.props.path} replace /> },
...childrenArray.map(child => ({
path: `${child.props.path}/*`,
element: React.cloneElement(child, { _isRoute: false }),
})),
{ path: '*', element: <GenericNotFound /> },
])

return (
<Container className={classNames(className, style.panelView)}>
<Row>
<Col className={style.menu} lg={3} xl={2}>
{React.Children.toArray(children).map(child =>
React.cloneElement(child, { path: `${path}${child.props.path}`, _isRoute: true }),
{childrenArray.map(child =>
React.cloneElement(child, { path: child.props.path, _isRoute: true }),
)}
</Col>
<Col className={style.panelViewMenuItem} lg={9} xl={10}>
<ErrorView errorRender={SubViewError}>
<Switch>
<Route exact path={path} redirect={`${path}${firstChild.path}`} />
{React.Children.toArray(children).map(child =>
React.cloneElement(child, { path: `${path}${child.props.path}`, _isRoute: false }),
)}
<NotFoundRoute />
</Switch>
</ErrorView>
<ErrorView errorRender={SubViewError}>{routes}</ErrorView>
</Col>
</Row>
</Container>
)
}

const PanelViewItem = ({ icon, title, path, exact, component: Component, _isRoute }) => {
const PanelViewItem = ({ icon, title, path, Component, _isRoute }) => {
const className = useCallback(
({ isActive }) => classNames(style.link, { [style.active]: isActive }),
[],
)
if (_isRoute) {
return (
<NavLink to={path} className={style.link} activeClassName={style.active} exact={exact}>
<NavLink to={path} className={className}>
{icon && <Icon icon={icon} className="mr-cs-xs" />} <Message content={title} />
</NavLink>
)
}

return <Route path={path} component={Component} exact={exact} />
return React.createElement(Component)
}

PanelViewItem.propTypes = {
Component: PropTypes.func.isRequired,
_isRoute: PropTypes.bool,
component: PropTypes.func.isRequired,
exact: PropTypes.bool,
icon: PropTypes.string.isRequired,
path: PropTypes.string.isRequired,
title: PropTypes.message.isRequired,
}

PanelViewItem.defaultProps = {
_isRoute: false,
exact: false,
}

PanelView.propTypes = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ class PayloadFormattersForm extends React.Component {
formikRef={this.formRef}
id="payload-formatter-form"
>
{({ touched }) => (
{() => (
<>
<Form.SubTitle title={m.setupSubTitle} />
<Form.Field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ const componentMap = {
js: sharedMessages.componentJs,
edtc: sharedMessages.componentEdtc,
qrg: sharedMessages.componentQrg,
gcs: sharedMessages.claimGateway,
dcs: sharedMessages.claimDevice,
noc: sharedMessages.noc,
gcs: sharedMessages.componentGcs,
dcs: sharedMessages.componentDcs,
}

const DeploymentComponentStatus = () => (
Expand Down
12 changes: 6 additions & 6 deletions pkg/webui/console/views/admin-panel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,25 @@ const AdminPanel = () => {
<PanelView.Item
title={m.networkInformation}
icon="view_compact"
path="/network-information"
component={NetworkInformation}
path="network-information"
Component={NetworkInformation}
exact
/>
{showUserManagement && (
<PanelView.Item
title={m.userManagement}
icon="user_management"
path="/user-management"
component={UserManagement}
path="user-management"
Component={UserManagement}
condition={showUserManagement}
/>
)}
{showPacketBroker && (
<PanelView.Item
title={m.peeringSettings}
icon="packet_broker"
path="/packet-broker"
component={PacketBrokerRouter}
path="packet-broker"
Component={PacketBrokerRouter}
condition={showPacketBroker}
/>
)}
Expand Down
4 changes: 2 additions & 2 deletions pkg/webui/console/views/admin-user-management/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

import React from 'react'
import { Routes, Route } from 'react-router-dom'
import GenericNotFound from '@ttn-lw/lib/components/full-view-error/not-found'
import ValidateRouteParam from '@ttn-lw/lib/components/validate-route-param'

import { useBreadcrumbs } from '@ttn-lw/components/breadcrumbs/context'
import Breadcrumb from '@ttn-lw/components/breadcrumbs/breadcrumb'

import GenericNotFound from '@ttn-lw/lib/components/full-view-error/not-found'
import ValidateRouteParam from '@ttn-lw/lib/components/validate-route-param'
import IntlHelmet from '@ttn-lw/lib/components/intl-helmet'

import Require from '@console/lib/components/require'
Expand Down
2 changes: 1 addition & 1 deletion pkg/webui/console/views/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class ConsoleApp extends React.PureComponent {
<Route path="/applications/*" Component={Applications} />
<Route path="/gateways/*" Component={Gateways} />
<Route path="/organizations/*" Component={Organizations} />
<Route path="/admin-panel/*" component={AdminPanel} />
<Route path="/admin-panel/*" Component={AdminPanel} />
<Route path="/user/" Component={User} />
<Route path="*" Component={GenericNotFound} />
</Routes>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2021 The Things Network Foundation, The Things Industries B.V.
// Copyright © 2022 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -26,8 +26,6 @@ import RequireRequest from '@ttn-lw/lib/components/require-request'

import WebhookEdit from '@console/containers/webhook-edit'

import Require from '@console/lib/components/require'

import sharedMessages from '@ttn-lw/lib/shared-messages'
import { getWebhookTemplateId } from '@ttn-lw/lib/selectors/id'

Expand Down
2 changes: 2 additions & 0 deletions pkg/webui/lib/shared-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ export default defineMessages({
componentJs: 'Join Server',
componentNs: 'Network Server',
componentQrg: 'QR Code Generator',
componentDcs: 'Device Claiming Server',
componentGcs: 'Gateway Claiming Server',
confirm: 'Confirm',
confirmPassword: 'Confirm password',
connected: 'Connected',
Expand Down
6 changes: 3 additions & 3 deletions pkg/webui/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -975,8 +975,6 @@
"console.views.overview.index.createGateway": "Register a gateway",
"console.views.overview.index.gotoApplications": "Go to applications",
"console.views.overview.index.gotoGateways": "Go to gateways",
"console.views.overview.index.needHelp": "Need help? Have a look at our {documentationLink} or {supportLink}.",
"console.views.overview.index.needHelpShort": "Need help? Have a look at our {link}.",
"console.views.overview.index.welcome": "Welcome to the Console!",
"console.views.overview.index.welcomeBack": "Welcome back, {userName}! 👋",
"console.views.overview.index.getStarted": "Get started right away by creating an application or registering a gateway.",
Expand Down Expand Up @@ -1218,7 +1216,7 @@
"lib.shared-messages.collaboratorIdPlaceholder": "collaborator-id",
"lib.shared-messages.collaboratorWarningSelf": "Changing your own rights could result in loss of access",
"lib.shared-messages.collaboratorWarningAdmin": "This user is an administrator that will retain all rights to all entities regardless of collaborator status",
"lib.shared-messages.collaboratorWarningAdminSelf": "As an administrator, you aways retain all rights to all entities regardless of collaborator status",
"lib.shared-messages.collaboratorWarningAdminSelf": "As an administrator, you always retain all rights to all entities regardless of collaborator status",
"lib.shared-messages.collaboratorModalWarning": "Are you sure you want to remove {collaboratorId} as a collaborator?",
"lib.shared-messages.collaboratorModalWarningSelf": "Are you sure you want to remove yourself as a collaborator? Access to this entity will be lost until someone else adds you as a collaborator again.",
"lib.shared-messages.collaboratorRemove": "Collaborator remove",
Expand All @@ -1231,6 +1229,8 @@
"lib.shared-messages.componentJs": "Join Server",
"lib.shared-messages.componentNs": "Network Server",
"lib.shared-messages.componentQrg": "QR Code Generator",
"lib.shared-messages.componentDcs": "Device Claiming Server",
"lib.shared-messages.componentGcs": "Gateway Claiming Server",
"lib.shared-messages.confirm": "Confirm",
"lib.shared-messages.confirmPassword": "Confirm password",
"lib.shared-messages.connected": "Connected",
Expand Down

0 comments on commit 7454ed9

Please sign in to comment.