Files
testing-tool/hooks/useRecorder.tsx
T
雨霖铃 4213e70697 feat: update package.json to remove lint-staged, add eslint configuration and dependencies
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
2026-01-31 18:00:01 +08:00

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,
};
};