Files
testing-tool/components/TimestampExecution.tsx
T
雨霖铃 80e510225c feat(background): 添加录制状态持久化和Tab切换处理
- 实现录制状态存储到本地storage,包括isRecording、recordingTabId、events和startTime
- 添加loadRecorderState和saveRecorderState函数用于状态管理
- 监听Tab切换事件,当切换离开录制Tab时发出警告并通知popup
- 监听Tab关闭事件,自动停止录制并清理状态
- 添加录制状态同步检查,确保content script状态一致
- 在开始录制前检查是否已在录制状态,避免重复录制
- 添加错误处理返回详细错误信息
- 优化回放HTML样式和脚本加载方式
- 添加Tab切换消息通信协议支持
2026-02-21 15:09:07 +08:00

107 lines
3.0 KiB
TypeScript

import { useEffect, useState, useCallback, JSX } from 'react';
import CopyButton from './CopyButton';
import { Button, Paper, Typography, Stack, Box } from '@mui/material';
/**
* 时间戳显示和执行组件
*/
export function TimestampExecution(): JSX.Element {
const [currentTimestamp, setCurrentTimestamp] = useState(() => Math.floor(Date.now()));
const [showMilliseconds, setShowMilliseconds] = useState(true);
const [isRunningTimestamp, setIsRunningTimestamp] = useState(true);
const displayTimestamp = showMilliseconds
? currentTimestamp
: Math.floor(currentTimestamp / 1000);
const unitText = showMilliseconds ? '毫秒' : '秒';
useEffect(() => {
let timer: number;
if (isRunningTimestamp) {
const interval = showMilliseconds ? 100 : 1000;
timer = window.setInterval(() => {
setCurrentTimestamp(Math.floor(Date.now()));
}, interval);
}
return () => clearInterval(timer);
}, [isRunningTimestamp, showMilliseconds]);
const toggleUnit = useCallback(() => {
setShowMilliseconds((prev) => !prev);
}, []);
const toggleTimestamp = useCallback(() => {
setIsRunningTimestamp((prev) => !prev);
}, []);
const unitButtonLabel = showMilliseconds ? '切换为秒显示' : '切换为毫秒显示';
const toggleButtonLabel = isRunningTimestamp ? '停止时间戳自动更新' : '开始时间戳自动更新';
const toggleButtonText = isRunningTimestamp ? '停止' : '开始';
return (
<Paper elevation={3} sx={{ p: 2, my: 2, borderRadius: 2 }}>
<Box
sx={{
display: 'flex',
alignItems: 'baseline',
justifyContent: 'center',
gap: 1,
mb: 2,
overflowX: 'auto',
pb: 1,
}}
>
<Typography
variant="h5"
component="span"
sx={{
fontFamily: 'monospace',
fontWeight: 'bold',
color: 'primary.main',
whiteSpace: 'nowrap',
}}
>
{displayTimestamp}
</Typography>
<Typography
variant="h6"
component="span"
sx={{ color: 'text.secondary', whiteSpace: 'nowrap' }}
>
{unitText}
</Typography>
</Box>
<Stack direction="row" spacing={2} justifyContent="center" flexWrap="wrap">
<Button
variant="contained"
color="primary"
onClick={toggleUnit}
aria-label={unitButtonLabel}
title={unitButtonLabel}
>
切换单位
</Button>
<CopyButton
textToCopy={String(currentTimestamp)}
buttonText="复制"
aria-label="复制当前时间戳到剪贴板"
color="primary"
/>
<Button
variant="contained"
color={isRunningTimestamp ? 'error' : 'primary'}
onClick={toggleTimestamp}
aria-label={toggleButtonLabel}
title={toggleButtonLabel}
>
{toggleButtonText}
</Button>
</Stack>
</Paper>
);
}