Skip to content

Commit 019aedc

Browse files
CopilotRavelloH
andauthored
ref: move duplicated ISO date-string detection into shared utility (#42)
* Initial plan * refactor: share isDateString utility across admin pages Co-authored-by: RavelloH <68409330+RavelloH@users.noreply.github.com> * chore: align shared isDateString signature with existing usage Co-authored-by: RavelloH <68409330+RavelloH@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: RavelloH <68409330+RavelloH@users.noreply.github.com>
1 parent 1bbe740 commit 019aedc

File tree

3 files changed

+16
-26
lines changed

3 files changed

+16
-26
lines changed

apps/web/src/app/(admin)/admin/pages/PagesTable.tsx

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import GridTable from "@/components/ui/GridTable";
2222
import { useNavigateWithTransition } from "@/components/ui/Link";
2323
import { useBroadcast } from "@/hooks/use-broadcast";
2424
import runWithAuth from "@/lib/client/run-with-auth";
25+
import { isDateString } from "@/lib/shared/date-format";
2526
import { getPageEditorPathByContentType } from "@/lib/shared/page-editor-route";
2627
import { AlertDialog } from "@/ui/AlertDialog";
2728
import { Button } from "@/ui/Button";
@@ -102,19 +103,6 @@ interface PageFormState {
102103
robotsIndex: boolean;
103104
}
104105

105-
// 判断是否为日期字符串(从 SettingsSheet 复制)
106-
const isDateString = (value: unknown): boolean => {
107-
if (typeof value !== "string") return false;
108-
109-
// ISO 8601 格式检测
110-
const isoDateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?Z?$/;
111-
if (!isoDateRegex.test(value)) return false;
112-
113-
// 验证是否为有效日期
114-
const date = new Date(value);
115-
return !isNaN(date.getTime());
116-
};
117-
118106
// 获取页面配置字段输入组件配置(参考 SettingsSheet 的 getInputConfig)
119107
const getPageConfigInputConfig = (
120108
value: unknown,

apps/web/src/app/(admin)/admin/settings/SettingsSheet.tsx

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
} from "@/data/default-configs";
1414
import { useBroadcast } from "@/hooks/use-broadcast";
1515
import runWithAuth from "@/lib/client/run-with-auth";
16+
import { isDateString } from "@/lib/shared/date-format";
1617
import generateGradient from "@/lib/shared/gradient";
1718
import { AutoTransition } from "@/ui/AutoTransition";
1819
import { Button } from "@/ui/Button";
@@ -500,19 +501,6 @@ export default function SettingSheet() {
500501
fetchSettings();
501502
};
502503

503-
// 判断是否为日期字符串
504-
const isDateString = (value: unknown): boolean => {
505-
if (typeof value !== "string") return false;
506-
507-
// ISO 8601 格式检测
508-
const isoDateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?Z?$/;
509-
if (!isoDateRegex.test(value)) return false;
510-
511-
// 验证是否为有效日期
512-
const date = new Date(value);
513-
return !isNaN(date.getTime());
514-
};
515-
516504
// 获取原始显示值(不考虑编辑状态)
517505
const getOriginalDisplayValue = (setting: SettingConfig): string => {
518506
// 提取 default 字段

apps/web/src/lib/shared/date-format.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,17 @@ export function formatDateTimeLocale(date: DateInput, fallback = "-"): string {
9999
return fallback;
100100
}
101101
}
102+
103+
/**
104+
* 判断值是否为 ISO 8601 日期字符串
105+
* @param value 输入值
106+
*/
107+
export function isDateString(value: unknown): boolean {
108+
if (typeof value !== "string") return false;
109+
110+
const isoDateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?Z?$/;
111+
if (!isoDateRegex.test(value)) return false;
112+
113+
const date = new Date(value);
114+
return !isNaN(date.getTime());
115+
}

0 commit comments

Comments
 (0)