From 4355f40f2eb4a917e274b96f116d4e3ceb9b3d0e Mon Sep 17 00:00:00 2001 From: Sergio Betanzos Date: Fri, 27 Nov 2020 13:05:38 +0100 Subject: [PATCH] F #3951: Add debug log component (#493) --- .../client/components/AutoScrollBox/index.js | 144 ++++++++++++++++++ .../src/client/components/DebugLog/index.js | 102 +++++++++++++ .../Provisions/Form/Create/index.js | 24 ++- src/fireedge/src/client/hooks/useProvision.js | 11 +- 4 files changed, 268 insertions(+), 13 deletions(-) create mode 100644 src/fireedge/src/client/components/AutoScrollBox/index.js create mode 100644 src/fireedge/src/client/components/DebugLog/index.js diff --git a/src/fireedge/src/client/components/AutoScrollBox/index.js b/src/fireedge/src/client/components/AutoScrollBox/index.js new file mode 100644 index 0000000000..e80574d799 --- /dev/null +++ b/src/fireedge/src/client/components/AutoScrollBox/index.js @@ -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 ( +
+
+ +
+ } + color="primary" + classes={{ avatarColorPrimary: classes.svg }} + className={classes.button} + label={autoButtonText} + onClick={() => setAutoScroll(true)} + /> +
+
+ {children} +
+
+ ) +}) + +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 diff --git a/src/fireedge/src/client/components/DebugLog/index.js b/src/fireedge/src/client/components/DebugLog/index.js new file mode 100644 index 0000000000..f8d90880b5 --- /dev/null +++ b/src/fireedge/src/client/components/DebugLog/index.js @@ -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 ( +
+ {message} +
+ ) +}, (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 ( + + + {log?.map((message, index) => { + const isString = typeof message === 'string' + const stringMessage = isString ? message : JSON.stringify(message) + + return ( + + ) + })} + + + ) +}, (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 diff --git a/src/fireedge/src/client/containers/Provisions/Form/Create/index.js b/src/fireedge/src/client/containers/Provisions/Form/Create/index.js index 52e935a48d..ca9ef36205 100644 --- a/src/fireedge/src/client/containers/Provisions/Form/Create/index.js +++ b/src/fireedge/src/client/containers/Provisions/Form/Create/index.js @@ -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 } return ( diff --git a/src/fireedge/src/client/hooks/useProvision.js b/src/fireedge/src/client/hooks/useProvision.js index 226803f7b8..00a303e680 100644 --- a/src/fireedge/src/client/hooks/useProvision.js +++ b/src/fireedge/src/client/hooks/useProvision.js @@ -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] )