Skip to content

Commit

Permalink
Refactor. Reduced HistoryDrawer funtion length.
Browse files Browse the repository at this point in the history
  • Loading branch information
BigIskander committed Jul 1, 2024
1 parent cc96fc0 commit 23b1ef3
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions app/src/components/Sidebar/HistoryDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ interface Props {
children?: any
}

function dowloadHistoryAsFile(props: Props) {
var filename = "save.txt"
var text = ''
const elementsText = props.items.map((element, index) => (
text.concat('"').concat(element.key).concat('";"').concat(element.value).concat('";\r\n')
))
var element = document.createElement('a')
element.setAttribute('href', 'data:text/csv;charset=utf-8,' + encodeURIComponent(elementsText.join('')))
element.setAttribute('download', filename)
element.style.display = 'none'
document.body.appendChild(element)
element.click()
document.body.removeChild(element)
}

function HistoryDrawer(props: Props) {
const handleCtrlA = selectTextWithCtrlA({ targetSelector: 'pre' })
const [expanded, setExpanded] = useState<boolean | undefined>(undefined)
Expand All @@ -40,6 +55,10 @@ function HistoryDrawer(props: Props) {
event.stopPropagation()
}

const saveHistory = (() => {
dowloadHistoryAsFile(props);
})

function renderHistory() {
const style = (element: HistoryItem) => ({
backgroundColor: element.selected
Expand Down Expand Up @@ -70,21 +89,6 @@ function HistoryDrawer(props: Props) {
</div>
))

const save = (() => {
var filename = "save.txt"
var text = ''
const elementsText = props.items.map((element, index) => (
text.concat('"').concat(element.key).concat('";"').concat(element.value).concat('";\r\n')
))
var element = document.createElement('a')
element.setAttribute('href', 'data:text/csv;charset=utf-8,' + encodeURIComponent(elementsText.join('')))
element.setAttribute('download', filename)
element.style.display = 'none'
document.body.appendChild(element)
element.click()
document.body.removeChild(element)
})

const visible = props.items.length > 0 && !expanded

return (
Expand Down Expand Up @@ -118,7 +122,7 @@ function HistoryDrawer(props: Props) {
backgroundColor: emphasize(props.theme.palette.background.default, 0.15),
}}
>
<Typography component={'span'} onClick={save} style={{ cursor: 'pointer', display: 'flex' }}>
<Typography component={'span'} onClick={saveHistory} style={{ cursor: 'pointer', display: 'flex' }}>
<span style={{ flexGrow: 1 }}>
<Badge
classes={{ badge: props.classes.badge }}
Expand Down

0 comments on commit 23b1ef3

Please sign in to comment.