F #3951: Add debug log component (#493)

This commit is contained in:
Sergio Betanzos authored and GitHub committed 2020-11-27 13:05:38 +01:00
1 parent 4ad14daf2e
commit 4355f40f2e
4 files changed
+268 -13

No files matched your search

@@ -0,0 +1,144 @@
import React, { memo, Children, useEffect, useRef, useState } from 'react'
import PropTypes from 'prop-types'
import clsx from 'clsx'
import { makeStyles, Chip, Slide } from '@material-ui/core'
import ArrowBottomIcon from '@material-ui/icons/VerticalAlignBottom'
const useStyles = makeStyles(theme => ({
wrapperButton: {
top: 5,
position: 'sticky',
textAlign: 'center'
},
button: { padding: theme.spacing(0, 2) },
svg: {
color: '#fff !important',
backgroundColor: 'transparent !important'
}
}))
const baseClass = 'react-auto-scroll'
const AutoScrollBox = memo(({
children,
className,
height,
autoButtonText,
preventInteraction,
scrollBehavior,
showOption
}) => {
const classes = useStyles()
const [autoScroll, setAutoScroll] = useState(true)
const containerElement = useRef(null)
const style = {
padding: 8,
height,
overflowY: 'scroll',
scrollBehavior: 'auto',
pointerEvents: preventInteraction ? 'none' : 'auto'
}
// Handle mousewheel events on the scroll container.
const onWheel = () => {
const { current } = containerElement
if (current && showOption) {
setAutoScroll(
current.scrollTop + current.offsetHeight === current.scrollHeight
)
}
}
// Apply the scroll behavior property after the first render,
// so that the initial render is scrolled all the way to the bottom.
useEffect(() => {
setTimeout(() => {
const { current } = containerElement
if (current) {
current.style.scrollBehavior = scrollBehavior
}
}, 0)
}, [containerElement, scrollBehavior])
// When the children are updated, scroll the container to the bottom.
useEffect(() => {
if (!autoScroll) {
return
}
const { current } = containerElement
if (current) {
current.scrollTop = current.scrollHeight
}
}, [children, containerElement, autoScroll])
return (
<div style={{ height }}
className={clsx(baseClass, className, {
[`${baseClass}--empty`]: Children.count(children) === 0,
[`${baseClass}--prevent-interaction`]: preventInteraction,
[`${baseClass}--showOption`]: showOption
})}
>
<div
className={`${baseClass}__scroll-container`}
onWheel={onWheel}
ref={containerElement}
style={style}
>
<Slide in={!autoScroll} direction="down" mountOnEnter unmountOnExit>
<div className={classes.wrapperButton}>
<Chip
avatar={<ArrowBottomIcon />}
color="primary"
classes={{ avatarColorPrimary: classes.svg }}
className={classes.button}
label={autoButtonText}
onClick={() => setAutoScroll(true)}
/>
</div>
</Slide>
{children}
</div>
</div>
)
})
AutoScrollBox.propTypes = {
// Children to render in the scroll container.
children: PropTypes.node.isRequired,
// Extra CSS class names.
className: PropTypes.object,
// Height value of the scroll container.
height: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
]),
// Text to use for the auto scroll option.
autoButtonText: PropTypes.string,
// Prevent all mouse interaction with the scroll conatiner.
preventInteraction: PropTypes.bool,
// Ability to disable the smooth scrolling behavior.
scrollBehavior: PropTypes.oneOf(['smooth', 'auto']),
// Show the auto scroll option.
showOption: PropTypes.bool
}
AutoScrollBox.defaultProps = {
children: undefined,
className: undefined,
height: '100%',
autoButtonText: 'Auto scroll',
preventInteraction: false,
scrollBehavior: 'smooth',
showOption: true
}
AutoScrollBox.displayName = 'AutoScrollBox'
export default AutoScrollBox
@@ -0,0 +1,102 @@
import React, { useEffect, useState, memo } from 'react'
import PropTypes from 'prop-types'
import { makeStyles, lighten, Box } from '@material-ui/core'
import AutoScrollBox from 'client/components/AutoScrollBox'
const useStyles = makeStyles(theme => ({
message: ({ color = '#fafafa' }) => ({
color,
cursor: 'default',
fontFamily: 'monospace',
padding: theme.spacing(0.5, 1),
'&:hover': { backgroundColor: lighten('#1d1f21', 0.02) }
})
}))
// --------------------------------------------
// MESSAGE COMPONENT
// --------------------------------------------
const Message = memo(({ log, message, index }) => {
const color = () => {
const lastMessage = log[index - 1]
if (message.includes('WARNING')) return 'yellow'
else if (message.includes('WARNING')) return 'yellow'
else if (
message.includes('ERROR') || lastMessage?.includes('ERROR')
) return 'red'
else return '#fafafa'
}
const classes = useStyles({ color: color() })
return (
<div key={index} className={classes.message}>
{message}
</div>
)
}, (prev, next) => prev.index === next.index)
Message.propTypes = {
log: PropTypes.array,
message: PropTypes.string,
index: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
])
}
Message.defaultProps = { log: [], message: '', index: 0 }
Message.displayName = 'Message'
// --------------------------------------------
// DEBUG LOG COMPONENT
// --------------------------------------------
const DebugLog = memo(({ uuid, socket }) => {
const [log, setLog] = useState([])
useEffect(() => {
socket.on(socketData =>
socketData?.id === uuid && setLog(prev => [...prev, socketData?.data])
)
return socket.off
}, [])
return (
<Box borderRadius={5} bgcolor={'#1d1f21'} width={1} height={1}>
<AutoScrollBox scrollBehavior="auto">
{log?.map((message, index) => {
const isString = typeof message === 'string'
const stringMessage = isString ? message : JSON.stringify(message)
return (
<Message key={index} log={log} message={stringMessage} index={index} />
)
})}
</AutoScrollBox>
</Box>
)
}, (prev, next) => prev.uuid === next.uuid)
DebugLog.propTypes = {
uuid: PropTypes.string.isRequired,
socket: PropTypes.shape({
on: PropTypes.func.isRequired,
off: PropTypes.func.isRequired
}).isRequired
}
DebugLog.defaultProps = {
uuid: undefined,
socket: {
on: () => undefined,
off: () => undefined
}
}
DebugLog.displayName = 'DebugLog'
export default DebugLog
@@ -1,4 +1,4 @@
import React from 'react'
import React, { useState } from 'react'
import { useHistory } from 'react-router'
import { Container } from '@material-ui/core'
@@ -8,15 +8,21 @@ import { yupResolver } from '@hookform/resolvers'
import FormStepper from 'client/components/FormStepper'
import Steps from 'client/containers/Provisions/Form/Create/Steps'
import { PATH } from 'client/router/provision'
import useProvision from 'client/hooks/useProvision'
import useSocket from 'client/hooks/useSocket'
import useGeneral from 'client/hooks/useGeneral'
import { set } from 'client/utils'
import useProvision from 'client/hooks/useProvision'
import DebugLog from 'client/components/DebugLog'
import { PATH } from 'client/router/provision'
import { set, mapUserInputs } from 'client/utils'
function ProvisionCreateForm () {
const [uuid, setUuid] = useState(undefined)
const history = useHistory()
const { showError } = useGeneral()
const { getProvision } = useSocket()
const { createProvision, provisionsTemplates } = useProvision()
const { steps, defaultValues, resolvers } = Steps()
const methods = useForm({
@@ -42,14 +48,18 @@ function ProvisionCreateForm () {
set(provisionTemplate, 'defaults.provision.provider', providerSelected)
const parseInputs = mapUserInputs(inputs)
const formatData = {
...provisionTemplate,
inputs: provisionTemplate?.inputs
?.map(input => ({ ...input, value: `${inputs[input?.name]}` }))
?.map(input => ({ ...input, value: `${parseInputs[input?.name]}` }))
}
createProvision({ data: formatData })
.then(() => history.push(PATH.PROVISIONS.LIST))
createProvision({ data: formatData }).then(res => res && setUuid(res))
}
if (uuid) {
return <DebugLog uuid={uuid} socket={getProvision} />
}
return (
@@ -153,13 +153,12 @@ export default function useOpennebula () {
({ data }) =>
serviceProvision
.createProvision({ data })
.then(doc => dispatch(
enqueueSuccess(`Provision created - ID: ${doc?.ID}`))
)
.then(doc => {
dispatch(enqueueSuccess('Provision created'))
return doc.data
})
.catch(err => {
dispatch(
enqueueError(err?.message ?? 'Error CREATE Provision')
)
dispatch(enqueueError(err?.message ?? 'Error CREATE Provision'))
}),
[dispatch, provisions]
)