80e510225c
- 实现录制状态存储到本地storage,包括isRecording、recordingTabId、events和startTime - 添加loadRecorderState和saveRecorderState函数用于状态管理 - 监听Tab切换事件,当切换离开录制Tab时发出警告并通知popup - 监听Tab关闭事件,自动停止录制并清理状态 - 添加录制状态同步检查,确保content script状态一致 - 在开始录制前检查是否已在录制状态,避免重复录制 - 添加错误处理返回详细错误信息 - 优化回放HTML样式和脚本加载方式 - 添加Tab切换消息通信协议支持
78 lines
2.0 KiB
TypeScript
78 lines
2.0 KiB
TypeScript
import { useEffect, useState, useMemo } from 'react';
|
|
import { AppState } from '../types';
|
|
import { sendMessage, onMessage } from '@/utils/messages';
|
|
import { Button, Container, Stack } from '@mui/material';
|
|
|
|
const RecordeReplayPage = () => {
|
|
const [status, setStatus] = useState<AppState>(AppState.READ);
|
|
const isRecording = useMemo(() => status === AppState.RECORDING, [status]);
|
|
|
|
useEffect(() => {
|
|
const unlistenStarted = onMessage('popup:started', () => {
|
|
console.log('[popup] Received started message');
|
|
setStatus(AppState.RECORDING);
|
|
});
|
|
|
|
const unlistenStopped = onMessage('popup:stopped', () => {
|
|
setStatus(AppState.READ);
|
|
});
|
|
|
|
const unlistenReady = onMessage('popup:ready', () => {
|
|
setStatus(AppState.READ);
|
|
});
|
|
|
|
sendMessage('popup:check-status', undefined)
|
|
.then((res) => {
|
|
if (res?.active) {
|
|
setStatus(AppState.RECORDING);
|
|
} else {
|
|
setStatus(AppState.READ);
|
|
}
|
|
})
|
|
.catch((err) => {
|
|
console.error(err);
|
|
});
|
|
|
|
return () => {
|
|
unlistenStarted();
|
|
unlistenStopped();
|
|
unlistenReady();
|
|
};
|
|
}, []);
|
|
|
|
const toggleRecording = async () => {
|
|
try {
|
|
if (isRecording) {
|
|
const result = await sendMessage('popup:stop', undefined);
|
|
if (!result?.ok) {
|
|
return;
|
|
}
|
|
} else {
|
|
const result = await sendMessage('popup:start', undefined);
|
|
if (!result?.ok) {
|
|
return;
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Error toggling recording:', error);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Container maxWidth={false} sx={{ py: 2.5 }}>
|
|
<Stack direction="row" spacing={1.25} sx={{ mb: 2.5 }}>
|
|
<Button
|
|
variant="contained"
|
|
size="medium"
|
|
color={isRecording ? 'error' : 'primary'}
|
|
onClick={toggleRecording}
|
|
>
|
|
{isRecording ? '停止录制' : '开始录制'}
|
|
</Button>
|
|
</Stack>
|
|
</Container>
|
|
);
|
|
};
|
|
|
|
export default RecordeReplayPage;
|