From 4f1a78ebb5fee548be2e4a26d070a791bdd43574 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=A8=E9=9C=96=E9=93=83?= Date: Fri, 19 Dec 2025 00:40:02 +0800 Subject: [PATCH] =?UTF-8?q?refactor(timestamp):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E6=88=B3=E9=A1=B5=E9=9D=A2=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E5=92=8CUI=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除未使用的 ActionsPage 页面及相关路由配置 - 简化 TimestampPage 的导入和状态管理逻辑 - 替换自定义日期格式化函数为工具类方法 - 重构定时器逻辑,提升代码可读性和稳定性 - 更新按钮交互功能,增强无障碍访问支持 - 优化输入控件结构和事件处理方式 - 改进时间戳转换逻辑并增加空值校验 - 调整DOM结构和样式类名以提高一致性 --- .prettierrc | 19 ++++++++ src/App.js | 2 - src/pages/ActionsPage.js | 26 ---------- src/pages/TimestampPage.js | 98 ++++++++++++++++++++------------------ src/utils/timeUtils.js | 11 +++++ 5 files changed, 82 insertions(+), 74 deletions(-) create mode 100644 .prettierrc delete mode 100644 src/pages/ActionsPage.js create mode 100644 src/utils/timeUtils.js diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..af61745 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,19 @@ +{ + "printWidth": 100, + "tabWidth": 2, + "useTabs": false, + "semi": true, + "singleQuote": false, + "quoteProps": "as-needed", + "jsxSingleQuote": false, + "trailingComma": "none", + "bracketSpacing": true, + "jsxBracketSameLine": false, + "arrowParens": "always", + "requirePragma": false, + "insertPragma": false, + "proseWrap": "preserve", + "htmlWhitespaceSensitivity": "ignore", + "endOfLine": "auto" +} + diff --git a/src/App.js b/src/App.js index ade4a5e..1b5526c 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,4 @@ import {HashRouter as Router, Routes, Route} from 'react-router-dom'; -import ActionsPage from './pages/ActionsPage'; import TimestampPage from './pages/TimestampPage'; import './App.css'; @@ -9,7 +8,6 @@ function App() {
}/> - }/> }/>
diff --git a/src/pages/ActionsPage.js b/src/pages/ActionsPage.js deleted file mode 100644 index a55999c..0000000 --- a/src/pages/ActionsPage.js +++ /dev/null @@ -1,26 +0,0 @@ -import { Link } from 'react-router-dom'; - -const ActionsPage = () => { - return ( -
-

Actions Page

-

Perform user actions here (e.g., add, edit, delete).

- -
- - -
- - {/* Link back to UserListPage */} - - Back to User List - - - - Go to Timestamp - -
- ); -}; - -export default ActionsPage; diff --git a/src/pages/TimestampPage.js b/src/pages/TimestampPage.js index e46ef4c..a22310f 100644 --- a/src/pages/TimestampPage.js +++ b/src/pages/TimestampPage.js @@ -1,55 +1,48 @@ -import {useState, useEffect, useRef, useCallback, useMemo} from 'react'; +import {useState, useEffect} from 'react'; import CopyButton from "../components/CopyButton"; - -function formatDate(value) { - return (new Intl.DateTimeFormat('zh-CN', { - year: 'numeric', - month: 'numeric', - day: 'numeric', - hour: 'numeric', - minute: 'numeric', - second: 'numeric', - })).format(value); -} +import {formatDate} from "../utils/timeUtils"; const TimestampPage = () => { const [currentTimestamp, setCurrentTimestamp] = useState(Math.floor(Date.now())); const [showMilliseconds, setShowMilliseconds] = useState(true); const [isRunningTimestamp, setIsRunningTimestamp] = useState(true); - const intervalRef = useRef(null); const [timestampValue, setTimestampValue] = useState(Date.now()); const [dateValue, setDateValue] = useState(formatDate(Date.now())); const [timestampResult, setTimestampResult] = useState(''); const [dateResult, setDateResult] = useState(''); + // 定时更新时间戳 useEffect(() => { + let timerId = null; + if (isRunningTimestamp) { - intervalRef.current = setInterval(() => { + timerId = setInterval(() => { setCurrentTimestamp(Math.floor(Date.now())); }, 1000); } return () => { - if (intervalRef.current) { - clearInterval(intervalRef.current); + if (timerId) { + clearInterval(timerId); + timerId = null; } } }, [isRunningTimestamp]); - function handleChangeUnit() { - setShowMilliseconds(!showMilliseconds); + function toggleUnit() { + setShowMilliseconds(prev => !prev); } - const handleToggleTimestampRunning = useCallback(() => { - const newStatus = !isRunningTimestamp; - setIsRunningTimestamp(newStatus); - console.log(newStatus ? '开始时间戳' : '停止时间戳'); - }, [isRunningTimestamp]); - useMemo(() => ({ - backgroundColor: isRunningTimestamp ? 'red' : 'green' - }), [isRunningTimestamp]); + function toggleTimestamp() { + setIsRunningTimestamp((prev => !prev)); + } function handleConvertTimestampToDate() { + if (!timestampValue) { + setTimestampResult('请输入时间戳'); + return; + } + setTimestampResult(formatDate(new Date(Number(timestampValue)))); } @@ -71,17 +64,27 @@ const TimestampPage = () => { return (
-
+

当前时间戳

- {Math.floor(currentTimestamp / (showMilliseconds ? 1 : 1000))} - {showMilliseconds ? '毫秒' : '秒'} + {Math.floor(currentTimestamp / (showMilliseconds ? 1 : 1000))} + {showMilliseconds ? '毫秒' : '秒'}

- + -
@@ -89,32 +92,35 @@ const TimestampPage = () => {

时间戳转日期时间

-
-
- setTimestampValue(e.target.value)}/> - +
+
+ setTimestampValue(Number(e.target.value))}/> +
-
-
-
+
- +
diff --git a/src/utils/timeUtils.js b/src/utils/timeUtils.js new file mode 100644 index 0000000..ac78ed7 --- /dev/null +++ b/src/utils/timeUtils.js @@ -0,0 +1,11 @@ +export function formatDate(value) { + return (new Intl.DateTimeFormat('zh-CN', { + year: 'numeric', + month: 'numeric', + day: 'numeric', + hour: 'numeric', + minute: 'numeric', + second: 'numeric', + })).format(value); +} +