From 23b1ef3fbe991c80eeb284dc1e79f5a6d3ba637c Mon Sep 17 00:00:00 2001 From: BigIskander Date: Mon, 1 Jul 2024 14:23:33 +0300 Subject: [PATCH] Refactor. Reduced HistoryDrawer funtion length. --- app/src/components/Sidebar/HistoryDrawer.tsx | 36 +++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/app/src/components/Sidebar/HistoryDrawer.tsx b/app/src/components/Sidebar/HistoryDrawer.tsx index a39675de..2551a802 100644 --- a/app/src/components/Sidebar/HistoryDrawer.tsx +++ b/app/src/components/Sidebar/HistoryDrawer.tsx @@ -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(undefined) @@ -40,6 +55,10 @@ function HistoryDrawer(props: Props) { event.stopPropagation() } + const saveHistory = (() => { + dowloadHistoryAsFile(props); + }) + function renderHistory() { const style = (element: HistoryItem) => ({ backgroundColor: element.selected @@ -70,21 +89,6 @@ function HistoryDrawer(props: Props) { )) - 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 ( @@ -118,7 +122,7 @@ function HistoryDrawer(props: Props) { backgroundColor: emphasize(props.theme.palette.background.default, 0.15), }} > - +