-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreenAppRecorder.tsx
More file actions
227 lines (199 loc) · 6.19 KB
/
ScreenAppRecorder.tsx
File metadata and controls
227 lines (199 loc) · 6.19 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { useEffect, useRef, useState } from 'react';
// Extend Window interface to include ScreenApp
declare global {
interface Window {
ScreenApp?: new (
apiKey: string,
callback: (data: RecordingData) => void,
options?: ScreenAppOptions
) => ScreenAppInstance;
}
}
interface RecordingData {
id: string;
_id: string;
url: string;
name?: string;
title?: string;
description?: string;
duration?: number;
size?: number;
thumbnailUrl?: string;
createdAt?: string;
}
interface ScreenAppOptions {
uiMode?: 'fullscreen' | 'popup';
}
interface ScreenAppInstance {
mount: (selector: string) => Promise<void>;
unMount: () => void;
}
interface ScreenAppRecorderProps {
apiKey: string; // Plugin token
pluginUrl?: string;
onRecordingComplete?: (recording: RecordingData) => void;
uiMode?: 'fullscreen' | 'popup';
containerClassName?: string;
}
export default function ScreenAppRecorder({
apiKey,
pluginUrl = 'https://embed.screenapp.io/app/plugin-latest.bundle.js',
onRecordingComplete,
uiMode = 'fullscreen',
containerClassName = '',
}: ScreenAppRecorderProps) {
const containerRef = useRef<HTMLDivElement>(null);
const screenAppInstanceRef = useRef<ScreenAppInstance | null>(null);
const containerIdRef = useRef<string>(`screenapp-container-${Math.random().toString(36).substr(2, 9)}`);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [isPluginLoaded, setIsPluginLoaded] = useState(false);
// Load the ScreenApp plugin script
useEffect(() => {
const scriptId = 'screenapp-plugin-script';
// Remove existing script if URL changed
const existingScript = document.getElementById(scriptId);
if (existingScript && existingScript.getAttribute('src') !== pluginUrl) {
existingScript.remove();
setIsPluginLoaded(false);
}
// Check if script with correct URL already exists
if (existingScript && existingScript.getAttribute('src') === pluginUrl) {
setIsPluginLoaded(true);
setIsLoading(false);
return;
}
const script = document.createElement('script');
script.id = scriptId;
script.charset = 'UTF-8';
script.type = 'module';
script.src = pluginUrl;
script.onload = () => {
console.log('ScreenApp plugin loaded successfully from:', pluginUrl);
setIsPluginLoaded(true);
setIsLoading(false);
};
script.onerror = () => {
console.error('Failed to load ScreenApp plugin from:', pluginUrl);
setError(`Failed to load plugin from: ${pluginUrl}`);
setIsLoading(false);
};
document.head.appendChild(script);
return () => {
// Cleanup: Remove script on unmount
const scriptToRemove = document.getElementById(scriptId);
if (scriptToRemove) {
scriptToRemove.remove();
}
};
}, [pluginUrl]);
// Initialize and mount ScreenApp when plugin is loaded
useEffect(() => {
if (!isPluginLoaded || !window.ScreenApp) {
return;
}
const initializeScreenApp = async () => {
try {
setIsLoading(true);
setError(null);
// Wait for next tick to ensure DOM is ready
await new Promise(resolve => setTimeout(resolve, 0));
// Verify container exists
if (!containerRef.current) {
throw new Error('Container element not available');
}
const containerId = containerIdRef.current;
const containerElement = document.getElementById(containerId);
if (!containerElement) {
throw new Error(`Container element #${containerId} not found in DOM`);
}
// Create callback function
const handleRecordingComplete = (recording: RecordingData) => {
console.log('Recording completed:', recording);
if (onRecordingComplete) {
onRecordingComplete(recording);
}
};
// Create ScreenApp instance
const screenApp = new window.ScreenApp!(
apiKey,
handleRecordingComplete,
{ uiMode }
);
// Mount to container
await screenApp.mount(`#${containerId}`);
// Store instance for cleanup
screenAppInstanceRef.current = screenApp;
setIsLoading(false);
} catch (err) {
console.error('Error initializing ScreenApp:', err);
setError(
err instanceof Error
? err.message
: 'Failed to initialize ScreenApp. Please check your plugin token.'
);
setIsLoading(false);
}
};
initializeScreenApp();
// Cleanup on unmount
return () => {
if (screenAppInstanceRef.current) {
screenAppInstanceRef.current.unMount();
screenAppInstanceRef.current = null;
}
};
}, [isPluginLoaded, apiKey, uiMode, onRecordingComplete]);
if (error) {
return (
<div className="screenapp-error" style={{
padding: '20px',
backgroundColor: '#fee',
border: '1px solid #fcc',
borderRadius: '8px',
color: '#c33',
}}>
<h3 style={{ margin: '0 0 10px 0' }}>Error</h3>
<p style={{ margin: 0 }}>{error}</p>
</div>
);
}
return (
<div className={`screenapp-recorder-wrapper ${containerClassName}`}>
{isLoading && (
<div className="screenapp-loading" style={{
padding: '40px',
textAlign: 'center',
backgroundColor: '#f5f5f5',
borderRadius: '8px',
}}>
<div style={{
display: 'inline-block',
width: '40px',
height: '40px',
border: '4px solid #ddd',
borderTopColor: '#4a90e2',
borderRadius: '50%',
animation: 'spin 1s linear infinite',
}} />
<p style={{ marginTop: '16px', color: '#666' }}>
Loading ScreenApp Recorder...
</p>
<style>{`
@keyframes spin {
to { transform: rotate(360deg); }
}
`}</style>
</div>
)}
<div
id={containerIdRef.current}
ref={containerRef}
style={{
minHeight: isLoading ? 0 : '400px',
display: isLoading ? 'none' : 'block',
}}
/>
</div>
);
}