Skip to content

Commit

Permalink
feat(log): added download chunk and entire log
Browse files Browse the repository at this point in the history
  • Loading branch information
YuryShkoda committed Apr 21, 2023
1 parent 57b7f95 commit a38a9f9
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 59 deletions.
47 changes: 35 additions & 12 deletions web/src/containers/Studio/internal/components/log/logChunk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { ErrorOutline, Warning } from '@mui/icons-material'
import ContentCopyIcon from '@mui/icons-material/ContentCopy'
import ExpandMoreIcon from '@mui/icons-material/ExpandMore'
import CheckIcon from '@mui/icons-material/Check'
import FileDownloadIcon from '@mui/icons-material/FileDownload'
import { makeStyles } from '@mui/styles'
import {
defaultChunkSize,
parseErrorsAndWarnings,
LogInstance,
clearErrorsAndWarningsHtmlWrapping
clearErrorsAndWarningsHtmlWrapping,
download
} from '../../../../../utils'

const useStyles: any = makeStyles((theme: any) => ({
Expand Down Expand Up @@ -44,7 +46,11 @@ interface LogChunkProps {
}

const LogChunk = (props: LogChunkProps) => {
const { id, text, logLineCount, scrollToLogInstance } = props
const { id, text, logLineCount } = props
const [scrollToLogInstance, setScrollToLogInstance] = useState(
props.scrollToLogInstance
)
const rowText = clearErrorsAndWarningsHtmlWrapping(text)
const classes = useStyles()
const [expanded, setExpanded] = useState(props.expanded)
const [copied, setCopied] = useState(false)
Expand Down Expand Up @@ -78,6 +84,13 @@ const LogChunk = (props: LogChunkProps) => {

const { errors, warnings } = parseErrorsAndWarnings(text)

const getLineRange = (separator = ' ... ') =>
`${id * defaultChunkSize}${separator}${
(id + 1) * defaultChunkSize < logLineCount
? (id + 1) * defaultChunkSize
: logLineCount
}`

return (
<div onClick={(evt) => props.onClick(evt, id)}>
<button
Expand All @@ -103,11 +116,7 @@ const LogChunk = (props: LogChunkProps) => {
alignItems: 'center'
}}
>
<span>{`Lines: ${id * defaultChunkSize} ... ${
(id + 1) * defaultChunkSize < logLineCount
? (id + 1) * defaultChunkSize
: logLineCount
}`}</span>
<span>{`Lines: ${getLineRange()}`}</span>
{copied ? (
<CheckIcon style={{ fontSize: 20, color: 'green' }} />
) : (
Expand All @@ -116,9 +125,7 @@ const LogChunk = (props: LogChunkProps) => {
onClick={(evt: SyntheticEvent) => {
evt.stopPropagation()

navigator.clipboard.writeText(
clearErrorsAndWarningsHtmlWrapping(text)
)
navigator.clipboard.writeText(rowText)

setCopied(true)

Expand All @@ -128,11 +135,27 @@ const LogChunk = (props: LogChunkProps) => {
}}
/>
)}
<FileDownloadIcon
onClick={(evt: SyntheticEvent) => {
download(evt, rowText, `log.${getLineRange('-')}`)
}}
/>
{errors && errors.length !== 0 && (
<ErrorOutline color="error" style={{ fontSize: 20 }} />
<ErrorOutline
color="error"
style={{ fontSize: 20 }}
onClick={() => {
setScrollToLogInstance(errors[0])
}}
/>
)}
{warnings && warnings.length !== 0 && (
<Warning style={{ fontSize: 20, color: 'green' }} />
<Warning
style={{ fontSize: 20, color: 'green' }}
onClick={() => {
setScrollToLogInstance(warnings[0])
}}
/>
)}{' '}
<ExpandMoreIcon
style={{
Expand Down
120 changes: 73 additions & 47 deletions web/src/containers/Studio/internal/components/log/logComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useEffect, useState, SyntheticEvent } from 'react'
import TreeView from '@mui/lab/TreeView'
import TreeItem from '@mui/lab/TreeItem'
import { ChevronRight, ExpandMore } from '@mui/icons-material'
Expand All @@ -7,12 +8,15 @@ import { makeStyles } from '@mui/styles'
import Highlight from 'react-highlight'
import { LogObject, defaultChunkSize } from '../../../../../utils'
import { RunTimeType } from '../../../../../context/appContext'
import { splitIntoChunks, LogInstance } from '../../../../../utils'
import {
splitIntoChunks,
LogInstance,
clearErrorsAndWarningsHtmlWrapping,
download
} from '../../../../../utils'
import LogChunk from './logChunk'
import { useEffect, useState } from 'react'

// TODO:
// link to download log.log
import FileDownloadIcon from '@mui/icons-material/FileDownload'
import { Button } from '@mui/material'

const useStyles: any = makeStyles((theme: any) => ({
expansionDescription: {
Expand Down Expand Up @@ -136,6 +140,7 @@ const LogComponent = (props: LogComponentProps) => {

const hasErrorsOrWarnings =
logObject.errors?.length !== 0 || logObject.warnings?.length !== 0
const logBody = typeof log === 'string' ? log : log.body

return (
<>
Expand Down Expand Up @@ -204,48 +209,50 @@ const LogComponent = (props: LogComponentProps) => {
)}
</div>

{Array.isArray(logChunks) ? (
logChunks.map((chunk: string, id: number) => (
<LogChunk
id={id}
text={chunk}
expanded={logChunksState[id]}
key={`log-chunk-${id}`}
logLineCount={logObject.linesCount}
scrollToLogInstance={scrollToLogInstance}
onClick={(_, chunkNumber) => {
setLogChunksState((prevState) => {
const newState = [...prevState]
const expand = !newState[chunkNumber]

newState[chunkNumber] = expand

if (expand) {
const chunkToCollapse = getChunkToAutoCollapse()

if (chunkToCollapse !== undefined) {
newState[chunkToCollapse] = false
<div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
{Array.isArray(logChunks) ? (
logChunks.map((chunk: string, id: number) => (
<LogChunk
id={id}
text={chunk}
expanded={logChunksState[id]}
key={`log-chunk-${id}`}
logLineCount={logObject.linesCount}
scrollToLogInstance={scrollToLogInstance}
onClick={(_, chunkNumber) => {
setLogChunksState((prevState) => {
const newState = [...prevState]
const expand = !newState[chunkNumber]

newState[chunkNumber] = expand

if (expand) {
const chunkToCollapse = getChunkToAutoCollapse()

if (chunkToCollapse !== undefined) {
newState[chunkToCollapse] = false
}
}
}

return newState
})

setScrollToLogInstance(undefined)
}}
/>
))
) : (
<Typography
id={`log_container`}
variant="h5"
className={classes.expansionDescription}
>
<Highlight className={'html'} innerHTML={true}>
{logChunks}
</Highlight>
</Typography>
)}

return newState
})

setScrollToLogInstance(undefined)
}}
/>
))
) : (
<Typography
id={`log_container`}
variant="h5"
className={classes.expansionDescription}
>
<Highlight className={'html'} innerHTML={true}>
{logChunks}
</Highlight>
</Typography>
)}
</div>
</div>
) : (
<div>
Expand All @@ -254,10 +261,29 @@ const LogComponent = (props: LogComponentProps) => {
id="log"
style={{ overflow: 'auto', height: 'calc(100vh - 220px)' }}
>
{typeof log === 'string' ? log : log.body}
{logBody}
</pre>
</div>
)}
<div
style={{
display: 'flex',
justifyContent: 'center',

marginTop: 10
}}
>
<Button
onClick={(evt: SyntheticEvent) => {
download(evt, clearErrorsAndWarningsHtmlWrapping(logBody))
}}
variant="contained"
color="primary"
startIcon={<FileDownloadIcon />}
>
download entire log
</Button>
</div>
</>
)
}
Expand Down
23 changes: 23 additions & 0 deletions web/src/utils/log.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SyntheticEvent } from 'react'
import { LogInstance } from './'

export const parseErrorsAndWarnings = (log: string) => {
Expand Down Expand Up @@ -100,3 +101,25 @@ export const splitIntoChunks = (log: string, chunkSize = defaultChunkSize) => {

export const clearErrorsAndWarningsHtmlWrapping = (log: string) =>
log.replace(/^<font[^>]*>/gm, '').replace(/<\/font>/gm, '')

export const download = (
evt: SyntheticEvent,
log: string,
fileName = 'log'
) => {
evt.stopPropagation()

const file = new Blob([log])
const url = URL.createObjectURL(file)

const a = document.createElement('a')
a.href = url
a.download = `${fileName}.log`
document.body.appendChild(a)
a.click()

setTimeout(() => {
document.body.removeChild(a)
window.URL.revokeObjectURL(url)
}, 0)
}

0 comments on commit a38a9f9

Please sign in to comment.