EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
MeshCentralEmbed.jsx
Go to the documentation of this file.
1import React from 'react';
2import { apiFetch } from '../lib/api';
3import './MeshCentralEmbed.css';
4
5/**
6 * MeshCentral Embed Component
7 * Embeds MeshCentral views directly into the dashboard with automatic authentication
8 *
9 * View Modes:
10 * 10 = General (device overview)
11 * 11 = Desktop (remote desktop)
12 * 12 = Terminal (command line)
13 * 13 = Files (file manager)
14 * 14 = Intel AMT
15 * 15 = Console
16 * 16 = Events
17 * 17 = Details (hardware info)
18 * 19 = Plugins
19 */
20export default function MeshCentralEmbed({ agentId, nodeId, viewMode = 11, title = 'Remote Desktop' }) {
21 const [loading, setLoading] = React.useState(true);
22 const [error, setError] = React.useState(null);
23 const [iframeUrl, setIframeUrl] = React.useState(null);
24 const [sessionInfo, setSessionInfo] = React.useState(null);
25 const iframeRef = React.useRef(null);
26
27 // Load authenticated MeshCentral session
28 React.useEffect(() => {
29 async function createSession() {
30 if (!agentId && !nodeId) {
31 setError('No agent ID or node ID provided');
32 setLoading(false);
33 return;
34 }
35
36 try {
37 setLoading(true);
38 setError(null);
39
40 // Call backend to create authenticated session
41 const response = await apiFetch(`/meshcentral/session/${agentId}`, {
42 method: 'POST',
43 body: JSON.stringify({ viewMode })
44 });
45
46 if (!response.ok) {
47 const data = await response.json();
48 throw new Error(data.message || data.error || 'Failed to create MeshCentral session');
49 }
50
51 const data = await response.json();
52
53 if (!data.iframeUrl) {
54 throw new Error('No iframe URL returned from server');
55 }
56
57 setIframeUrl(data.iframeUrl);
58 setSessionInfo(data);
59 console.log('[MeshCentral] Session created:', data.sessionToken);
60
61 } catch (err) {
62 console.error('[MeshCentral] Failed to create session:', err);
63 setError(err.message || 'Failed to connect to MeshCentral');
64 setLoading(false);
65 }
66 }
67
68 createSession();
69 }, [agentId, nodeId, viewMode]);
70
71 if (!agentId && !nodeId) {
72 return (
73 <div className="meshcentral-error">
74 <div className="error-icon">⚠️</div>
75 <h3>MeshAgent Not Connected</h3>
76 <p>This device is not connected to MeshCentral.</p>
77 <p className="error-hint">
78 Please ensure the MeshAgent is installed and running on this device.
79 </p>
80 </div>
81 );
82 }
83
84 const handleLoad = () => {
85 setLoading(false);
86 setError(null);
87 console.log('[MeshCentral] iframe loaded successfully');
88 };
89
90 const handleError = () => {
91 setError('Failed to load MeshCentral. Please check your connection.');
92 setLoading(false);
93 };
94
95 const openInNewWindow = () => {
96 if (iframeUrl) {
97 window.open(iframeUrl, '_blank', 'width=1400,height=900');
98 }
99 };
100
101 const retry = () => {
102 window.location.reload();
103 };
104
105 return (
106 <div className="meshcentral-embed-container">
107 <div className="meshcentral-header">
108 <h2>{title}</h2>
109 <div className="meshcentral-actions">
110 {sessionInfo && (
111 <span className="session-info">
112 Connected to {sessionInfo.agent?.hostname || 'device'}
113 </span>
114 )}
115 {iframeUrl && (
116 <button onClick={openInNewWindow} className="btn btn-secondary">
117 <span className="material-symbols-outlined">open_in_new</span>
118 Open in New Window
119 </button>
120 )}
121 </div>
122 </div>
123
124 {loading && !error && (
125 <div className="meshcentral-loading">
126 <div className="spinner"></div>
127 <p>Connecting to MeshCentral...</p>
128 <p className="loading-hint">Establishing secure session</p>
129 </div>
130 )}
131
132 {error && (
133 <div className="meshcentral-error">
134 <div className="error-icon">❌</div>
135 <h3>{error}</h3>
136 <div className="error-actions">
137 <button onClick={retry} className="btn btn-primary">
138 Retry
139 </button>
140 {iframeUrl && (
141 <button onClick={openInNewWindow} className="btn btn-secondary">
142 Open in New Window Instead
143 </button>
144 )}
145 </div>
146 </div>
147 )}
148
149 {iframeUrl && (
150 <iframe
151 ref={iframeRef}
152 src={iframeUrl}
153 className="meshcentral-iframe"
154 title={title}
155 sandbox="allow-same-origin allow-scripts allow-forms allow-popups allow-downloads allow-modals"
156 onLoad={handleLoad}
157 onError={handleError}
158 style={{ display: loading || error ? 'none' : 'block' }}
159 />
160 )}
161 </div>
162 );
163}
164
165// Export view mode constants for easy use
166export const VIEWMODE = {
167 GENERAL: 10,
168 DESKTOP: 11,
169 TERMINAL: 12,
170 FILES: 13,
171 INTEL_AMT: 14,
172 CONSOLE: 15,
173 EVENTS: 16,
174 DETAILS: 17,
175 PLUGINS: 19
176};