forked from patternfly/react-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectWithRemoteViewer.tsx
More file actions
136 lines (123 loc) · 4.69 KB
/
ConnectWithRemoteViewer.tsx
File metadata and controls
136 lines (123 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { useState } from 'react';
import { Button, ExpandableSection } from '@patternfly/react-core';
import { constants } from '../common/constants';
import { MoreInformationDefaultContent } from './MoreInformationDefaultContent';
import { MoreInformationDefaultRDPContent } from './MoreInformationDefaultRDPContent';
import {
generateDescriptorFile,
downloadFile,
onGenerateFunctionType,
onDownloadFunctionType
} from './consoleDescriptorGenerator';
import { ConsoleDetailPropType } from './ConsoleDetailPropType';
const {
VNC_CONSOLE_TYPE,
SPICE_CONSOLE_TYPE,
RDP_CONSOLE_TYPE,
DEFAULT_VV_FILENAME,
DEFAULT_VV_MIMETYPE,
DEFAULT_RDP_FILENAME,
DEFAULT_RDP_MIMETYPE
} = constants;
export interface ConnectWithRemoteViewerProps extends React.HTMLProps<HTMLDivElement> {
/** Custom content of more-info section */
children?: React.ReactNode;
/** Connection details for Spice */
spice?: ConsoleDetailPropType;
/** Connection details for VNC */
vnc?: ConsoleDetailPropType;
/** Connection details for RDP */
rdp?: ConsoleDetailPropType;
/** Callback function. Generate content of .vv file.
* Parameters: ({ _console, type }) => ({
* content, // required string value
* mimeType, // optional, default application/x-virt-viewer
* fileName // optional, default: console.vv
* })
*/
onGenerate?: onGenerateFunctionType;
/** Callback function. Perform file download.
* Default implementation is usually good enough, but i.e. in case of environment with tight
* content security policy set, this might be required.
*
* Examples for alternative file-download implementations:
* - use of iframe
* - use of http-server
*
* Parameters: (fileName, content, mimeType) => {}
*/
onDownload?: onDownloadFunctionType;
textConnectWithRemoteViewer?: string;
textMoreInfo?: string;
textMoreInfoContent?: string | React.ReactNode;
textConnectWithRDP?: string;
textMoreRDPInfo?: string;
textMoreRDPInfoContent?: string | React.ReactNode;
}
export const ConnectWithRemoteViewer: React.FunctionComponent<ConnectWithRemoteViewerProps> = ({
onGenerate = generateDescriptorFile,
onDownload = downloadFile,
spice = null,
vnc = null,
rdp = null,
textConnectWithRemoteViewer = 'Launch Remote Viewer',
textConnectWithRDP = 'Launch Remote Desktop',
textMoreInfo = 'Remote Viewer Details',
textMoreInfoContent = '',
textMoreRDPInfo = 'Remote Desktop Details',
textMoreRDPInfoContent = ''
}: ConnectWithRemoteViewerProps) => {
const [isExpandedDefault, setIsExpandedDefault] = useState(false);
const [isExpandedRDP, setIsExpandedRDP] = useState(false);
const _console = spice || vnc; // strictly prefer spice over vnc
const onClickVV = () => {
const type = spice ? SPICE_CONSOLE_TYPE : VNC_CONSOLE_TYPE;
if (_console) {
const vv = onGenerate(_console, type);
return onDownload(DEFAULT_VV_FILENAME, vv.content, vv.mimeType || DEFAULT_VV_MIMETYPE);
}
};
const onClickRDP = () => {
const rdpFile = onGenerate(rdp, RDP_CONSOLE_TYPE);
return onDownload(DEFAULT_RDP_FILENAME, rdpFile.content, rdpFile.mimeType || DEFAULT_RDP_MIMETYPE);
};
// RDP button is rendered only if the protocol is available
// If none of Spice or VNC is available, the .vv button is disabled (but rendered)
return (
<div className="pf-v6-c-console__remote-viewer">
<div className="pf-v6-c-console__remote-viewer-launch">
<Button className="pf-v6-c-console__remote-viewer-launch-vv" onClick={onClickVV} isDisabled={!_console}>
{textConnectWithRemoteViewer}
</Button>
{!!rdp && (
<Button onClick={onClickRDP} className="pf-v6-c-console__remote-viewer-launch-rdp">
{textConnectWithRDP}
</Button>
)}
</div>
{!!_console && (
<ExpandableSection
toggleText={textMoreInfo}
toggleId="remote-viewer-details-toggle"
contentId="remote-viewer-details-content"
isExpanded={isExpandedDefault}
onToggle={(_event, isExpanded) => setIsExpandedDefault(isExpanded)}
>
<MoreInformationDefaultContent textMoreInfoContent={textMoreInfoContent} />
</ExpandableSection>
)}
{!!rdp && (
<ExpandableSection
toggleText={textMoreRDPInfo}
isExpanded={isExpandedRDP}
toggleId="remote-desktop-details-toggle"
contentId="remote-desktop-details-content"
onToggle={(_event, isExpanded) => setIsExpandedRDP(isExpanded)}
>
<MoreInformationDefaultRDPContent textMoreRDPInfoContent={textMoreRDPInfoContent} />
</ExpandableSection>
)}
</div>
);
};
ConnectWithRemoteViewer.displayName = 'ConnectWithRemoteViewer';