From 452cd571c0121ac1e79751afabdd4bc4943aae6e Mon Sep 17 00:00:00 2001 From: Ian Bolton Date: Wed, 5 Jul 2023 14:42:03 -0400 Subject: [PATCH] :sparkles: Add refresh button to document viewer (#1087) Adds a refresh button for the custom code editor. This will allow the refresh of analysis details without refreshing the browser or relying on polling. Screenshot 2023-07-03 at 4 27 42 PM Closes https://github.com/konveyor/tackle2-ui/issues/1086 Signed-off-by: ibolton336 --- .../simple-document-viewer.tsx | 47 +++++++++++++------ 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/client/src/app/shared/components/simple-task-viewer/simple-document-viewer.tsx b/client/src/app/shared/components/simple-task-viewer/simple-document-viewer.tsx index 326c0318d..5dfc0664d 100644 --- a/client/src/app/shared/components/simple-task-viewer/simple-document-viewer.tsx +++ b/client/src/app/shared/components/simple-task-viewer/simple-document-viewer.tsx @@ -1,5 +1,9 @@ import * as React from "react"; -import { CodeEditor, Language } from "@patternfly/react-code-editor"; +import { + CodeEditor, + CodeEditorControl, + Language, +} from "@patternfly/react-code-editor"; import { Button, EmptyState, @@ -15,6 +19,7 @@ import { import { css } from "@patternfly/react-styles"; import editorStyles from "@patternfly/react-styles/css/components/CodeEditor/code-editor"; import CodeIcon from "@patternfly/react-icons/dist/esm/icons/code-icon"; +import UndoIcon from "@patternfly/react-icons/dist/esm/icons/undo-icon"; import "./viewer.css"; @@ -75,27 +80,40 @@ export const SimpleDocumentViewer = ({ React.useEffect(() => { setCode(undefined); - if (documentId) { - if (currentLanguage === Language.yaml) { - fetch(documentId, currentLanguage).then((yaml) => { - setCode(yaml.toString()); - focusAndHomePosition(); - }); - } else { - fetch(documentId, currentLanguage).then((json) => { - setCode(JSON.stringify(json, undefined, 2)); - focusAndHomePosition(); - }); - } - } + documentId && fetchDocument(documentId); }, [documentId, currentLanguage]); + const fetchDocument = (documentId: number) => { + if (currentLanguage === Language.yaml) { + fetch(documentId, currentLanguage).then((yaml) => { + setCode(yaml.toString()); + focusAndHomePosition(); + }); + } else { + fetch(documentId, currentLanguage).then((json) => { + setCode(JSON.stringify(json, undefined, 2)); + focusAndHomePosition(); + }); + } + }; + const focusAndHomePosition = () => { if (editorRef.current) { editorRef.current.focus(); editorRef.current.setPosition({ column: 0, lineNumber: 1 }); } }; + const refreshControl = ( + } + aria-label="refresh-task" + tooltipProps={{ content: "Refresh" }} + onClick={() => { + documentId && fetchDocument(documentId); + }} + isVisible={code !== ""} + /> + ); return ( ({ } customControls={[ + refreshControl,