4213e70697
feat: add state management for recording in RecordeReplayPage component refactor: remove bridge and storage services as they are no longer needed chore: update tsconfig.json for improved type checking and module resolution fix: update downloadHtml function to use eventWithTime type and improve formatting fix: add error logging in formatWithZone function for better debugging
42 lines
960 B
TypeScript
42 lines
960 B
TypeScript
import { useRef, useState, useCallback } from 'react';
|
|
|
|
export const useRecorder = () => {
|
|
// 存储停止录制函数
|
|
const stopFnRef = useRef<(() => void) | null>(null);
|
|
const [isRecording, setIsRecording] = useState(false);
|
|
|
|
const startRecord = useCallback(async () => {
|
|
if (isRecording) return;
|
|
|
|
try {
|
|
// await chrome.runtime.sendMessage({ type: 'CREATE_OFFSCREEN' });
|
|
setIsRecording(true);
|
|
|
|
console.log('[content] rrweb started');
|
|
} catch (error) {
|
|
console.error('Failed to start recording:', error);
|
|
setIsRecording(false);
|
|
}
|
|
}, [isRecording]);
|
|
|
|
const stopRecord = () => {
|
|
if (!isRecording) return;
|
|
|
|
setIsRecording(false);
|
|
|
|
// 停止录制
|
|
if (stopFnRef.current) {
|
|
chrome.runtime.sendMessage({ type: 'SAVE_EVENTS' });
|
|
stopFnRef.current?.();
|
|
|
|
console.log('[content] rrweb stopped');
|
|
}
|
|
};
|
|
|
|
return {
|
|
startRecord,
|
|
stopRecord,
|
|
isRecording,
|
|
};
|
|
};
|