6
6
} from "@patternfly/react-code-editor" ;
7
7
import {
8
8
Button ,
9
+ Checkbox ,
9
10
EmptyState ,
10
11
EmptyStateIcon ,
11
12
EmptyStateVariant ,
@@ -22,24 +23,17 @@ import CodeIcon from "@patternfly/react-icons/dist/esm/icons/code-icon";
22
23
import UndoIcon from "@patternfly/react-icons/dist/esm/icons/undo-icon" ;
23
24
24
25
import "./SimpleDocumentViewer.css" ;
26
+ import { useFetchTaskByID } from "@app/queries/tasks" ;
25
27
26
28
export { Language } from "@patternfly/react-code-editor" ;
27
29
28
- interface FetchFunction < FetchType > {
29
- /** Fetch a yaml document for the given document */
30
- ( documentId : number , format : Language . yaml ) : Promise < string > ;
31
-
32
- /** Fetch a JSON document as a `FetchType` object for the given document */
33
- ( documentId : number , format : Language . json ) : Promise < FetchType > ;
34
- }
35
-
36
30
/** The subset of MonacoEditor component functions we want to use. */
37
31
type ControlledEditor = {
38
32
focus : ( ) => void ;
39
33
setPosition : ( position : object ) => void ;
40
34
} ;
41
35
42
- export interface ISimpleDocumentViewerProps < FetchType > {
36
+ export interface ISimpleDocumentViewerProps {
43
37
/** The id of the document to display, or `undefined` to display the empty state. */
44
38
documentId : number | undefined ;
45
39
@@ -57,59 +51,60 @@ export interface ISimpleDocumentViewerProps<FetchType> {
57
51
* vertical space. Defaults to "450px".
58
52
*/
59
53
height ?: string | "full" ;
60
-
61
- /** Function that will fetch the document to display. */
62
- fetch : FetchFunction < FetchType > ;
63
54
}
64
55
65
56
/**
66
57
* Fetch and then use the `@patternfly/react-code-editor` to display a document in
67
58
* read-only mode with language highlighting applied.
68
59
*/
69
- export const SimpleDocumentViewer = < FetchType , > ( {
60
+ export const SimpleDocumentViewer = ( {
70
61
documentId,
71
62
downloadFilename,
72
63
language = Language . yaml ,
73
64
height = "450px" ,
74
- fetch,
75
- } : ISimpleDocumentViewerProps < FetchType > ) => {
65
+ } : ISimpleDocumentViewerProps ) => {
76
66
const editorRef = React . useRef < ControlledEditor > ( ) ;
77
-
78
- const [ code , setCode ] = React . useState < string | undefined > ( undefined ) ;
79
67
const [ currentLanguage , setCurrentLanguage ] = React . useState ( language ) ;
68
+ const [ code , setCode ] = React . useState < string > ( ) ;
69
+ const [ merged , setMerged ] = React . useState ( false ) ;
70
+
71
+ const { task, isFetching, fetchError, refetch } = useFetchTaskByID (
72
+ documentId ,
73
+ currentLanguage === Language . yaml ? "yaml" : "json" ,
74
+ merged
75
+ ) ;
76
+
77
+ const onMergedChange = ( checked : boolean ) => {
78
+ setMerged ( checked ) ;
79
+ refetch ( ) ;
80
+ } ;
80
81
81
82
React . useEffect ( ( ) => {
82
- setCode ( undefined ) ;
83
- documentId && fetchDocument ( documentId ) ;
84
- } , [ documentId , currentLanguage ] ) ;
83
+ if ( task ) {
84
+ const formattedCode =
85
+ currentLanguage === Language . yaml
86
+ ? task . toString ( )
87
+ : JSON . stringify ( task , undefined , 2 ) ;
85
88
86
- const fetchDocument = ( documentId : number ) => {
87
- if ( currentLanguage === Language . yaml ) {
88
- fetch ( documentId , currentLanguage ) . then ( ( yaml ) => {
89
- setCode ( yaml . toString ( ) ) ;
90
- focusAndHomePosition ( ) ;
91
- } ) ;
92
- } else {
93
- fetch ( documentId , currentLanguage ) . then ( ( json ) => {
94
- setCode ( JSON . stringify ( json , undefined , 2 ) ) ;
95
- focusAndHomePosition ( ) ;
96
- } ) ;
89
+ setCode ( formattedCode ) ;
90
+ focusAndHomePosition ( ) ;
97
91
}
98
- } ;
92
+ } , [ task , currentLanguage ] ) ;
99
93
100
94
const focusAndHomePosition = ( ) => {
101
95
if ( editorRef . current ) {
102
96
editorRef . current . focus ( ) ;
103
97
editorRef . current . setPosition ( { column : 0 , lineNumber : 1 } ) ;
104
98
}
105
99
} ;
100
+
106
101
const refreshControl = (
107
102
< CodeEditorControl
108
103
icon = { < UndoIcon /> }
109
104
aria-label = "refresh-task"
110
105
tooltipProps = { { content : "Refresh" } }
111
106
onClick = { ( ) => {
112
- documentId && fetchDocument ( documentId ) ;
107
+ refetch ( ) ;
113
108
} }
114
109
isVisible = { code !== "" }
115
110
/>
@@ -147,6 +142,15 @@ export const SimpleDocumentViewer = <FetchType,>({
147
142
}
148
143
customControls = { [
149
144
refreshControl ,
145
+ < Checkbox
146
+ className = "merged-checkbox"
147
+ key = "merged"
148
+ id = "merged"
149
+ label = "Merged"
150
+ isChecked = { merged }
151
+ onChange = { ( e , checked ) => onMergedChange ( checked ) }
152
+ aria-label = "Merged Checkbox"
153
+ /> ,
150
154
< div
151
155
className = { css (
152
156
editorStyles . codeEditorTab ,
@@ -193,8 +197,8 @@ export const SimpleDocumentViewer = <FetchType,>({
193
197
) ;
194
198
} ;
195
199
196
- export interface ISimpleDocumentViewerModalProps < FetchType >
197
- extends ISimpleDocumentViewerProps < FetchType > {
200
+ export interface ISimpleDocumentViewerModalProps
201
+ extends ISimpleDocumentViewerProps {
198
202
/** Simple text content of the modal header. */
199
203
title ?: string ;
200
204
@@ -220,14 +224,14 @@ export interface ISimpleDocumentViewerModalProps<FetchType>
220
224
* displayed if the `documentId` is set. If `documentId` is `undefined`, the modal is
221
225
* closed.
222
226
*/
223
- export const SimpleDocumentViewerModal = < FetchType , > ( {
227
+ export const SimpleDocumentViewerModal = ( {
224
228
title,
225
229
documentId,
226
230
onClose,
227
231
position = "top" ,
228
232
isFullHeight = true ,
229
233
...rest
230
- } : ISimpleDocumentViewerModalProps < FetchType > ) => {
234
+ } : ISimpleDocumentViewerModalProps ) => {
231
235
const isOpen = documentId !== undefined ;
232
236
233
237
return (
@@ -248,7 +252,7 @@ export const SimpleDocumentViewerModal = <FetchType,>({
248
252
</ Button > ,
249
253
] }
250
254
>
251
- < SimpleDocumentViewer < FetchType >
255
+ < SimpleDocumentViewer
252
256
documentId = { documentId }
253
257
height = { isFullHeight ? "full" : undefined }
254
258
{ ...rest }
0 commit comments