Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add refresh button to document viewer #1087

Merged
merged 2 commits into from
Jul 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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";

Expand Down Expand Up @@ -75,27 +80,40 @@ export const SimpleDocumentViewer = <FetchType,>({

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 = (
<CodeEditorControl
icon={<UndoIcon />}
aria-label="refresh-task"
tooltipProps={{ content: "Refresh" }}
onClick={() => {
documentId && fetchDocument(documentId);
}}
isVisible={code !== ""}
/>
);

return (
<CodeEditor
Expand Down Expand Up @@ -128,6 +146,7 @@ export const SimpleDocumentViewer = <FetchType,>({
</div>
}
customControls={[
refreshControl,
<div
className={css(
editorStyles.codeEditorTab,
Expand Down