@njuptlzf/agent-chat-web 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +66 -4
- package/src/agent-chat/AgentChat.tsx +692 -0
- package/src/agent-chat/AgentChatApp.tsx +206 -0
- package/src/agent-chat/Dock.tsx +74 -0
- package/src/agent-chat/PersonasPage.tsx +678 -0
- package/src/agent-chat/adapter.ts +272 -0
- package/src/agent-chat/app-def-check.ts +215 -0
- package/src/agent-chat/app.tsx +327 -0
- package/src/agent-chat/auth.ts +83 -0
- package/src/agent-chat/dock.ts +162 -0
- package/src/agent-chat/docs.ts +179 -0
- package/src/agent-chat/index.ts +17 -0
- package/src/agent-chat/mount.tsx +123 -0
- package/src/agent-chat/qual.ts +55 -0
- package/src/agent-chat/sse.ts +104 -0
- package/src/agent-chat/theme.ts +23 -0
- package/src/agent-chat/ui/CodeCopyHeader.tsx +29 -0
- package/src/agent-chat/ui/ConfirmCard.tsx +32 -0
- package/src/agent-chat/ui/Lightbox.tsx +32 -0
- package/src/agent-chat/ui/QueueBar.tsx +70 -0
- package/src/agent-chat/ui/Splitter.tsx +81 -0
- package/src/agent-chat/ui/ThemeToggle.tsx +25 -0
- package/src/agent-chat/ui/TierPicker.tsx +112 -0
- package/src/agent-chat/ui/UIPromptCard.tsx +68 -0
- package/src/agent-chat/ui/timefmt.ts +110 -0
- package/src/agent-chat/ui/useNarrow.ts +20 -0
- package/src/styles.css +1478 -0
- package/README.md +0 -3
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { useCallback, useRef, useState, type PointerEvent as ReactPointerEvent } from "react";
|
|
2
|
+
|
|
3
|
+
function clamp(v: number, min: number, max: number): number {
|
|
4
|
+
return Math.min(max, Math.max(min, v));
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 解析左栏宽度:记忆值合法才用,否则回落默认。
|
|
9
|
+
* 损坏的 localStorage(NaN/越界/改参后旧值)一律回默认,不会把布局带坏。
|
|
10
|
+
*/
|
|
11
|
+
export function resolveWidth(stored: string | null, fallback: number, min: number, max: number): number {
|
|
12
|
+
const v = Number(stored);
|
|
13
|
+
return stored !== null && stored !== "" && Number.isFinite(v) && v >= min && v <= max ? v : fallback;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 可拖拽竖向分隔条(agentChat 通用能力,零业务知识):拖动调整左栏宽度,
|
|
18
|
+
* 宽度记忆在 localStorage(每浏览器),双击恢复默认。参考 DSH 侧栏拖拽。
|
|
19
|
+
*/
|
|
20
|
+
export function usePaneWidth(storageKey: string, fallback: number, min: number, max: number) {
|
|
21
|
+
const [width, setWidth] = useState(() => {
|
|
22
|
+
let stored: string | null = null;
|
|
23
|
+
try {
|
|
24
|
+
stored = localStorage.getItem(storageKey);
|
|
25
|
+
} catch {
|
|
26
|
+
/* 隐私模式读不到就算了 */
|
|
27
|
+
}
|
|
28
|
+
return resolveWidth(stored, fallback, min, max);
|
|
29
|
+
});
|
|
30
|
+
const drag = useRef<{ startX: number; startW: number } | null>(null);
|
|
31
|
+
|
|
32
|
+
const commit = useCallback(
|
|
33
|
+
(v: number) => {
|
|
34
|
+
const w = clamp(v, min, max);
|
|
35
|
+
setWidth(w);
|
|
36
|
+
try {
|
|
37
|
+
localStorage.setItem(storageKey, String(w));
|
|
38
|
+
} catch {
|
|
39
|
+
/* 写不进去只影响"记忆",不影响本次拖拽 */
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
[storageKey, min, max],
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
width,
|
|
47
|
+
handlers: {
|
|
48
|
+
onPointerDown: (e: ReactPointerEvent<HTMLDivElement>) => {
|
|
49
|
+
e.currentTarget.setPointerCapture(e.pointerId);
|
|
50
|
+
drag.current = { startX: e.clientX, startW: width };
|
|
51
|
+
},
|
|
52
|
+
onPointerMove: (e: ReactPointerEvent<HTMLDivElement>) => {
|
|
53
|
+
const d = drag.current;
|
|
54
|
+
if (d) commit(d.startW + e.clientX - d.startX);
|
|
55
|
+
},
|
|
56
|
+
onPointerUp: () => {
|
|
57
|
+
drag.current = null;
|
|
58
|
+
},
|
|
59
|
+
onDoubleClick: () => commit(fallback),
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
interface SplitterProps {
|
|
65
|
+
onPointerDown?: (e: ReactPointerEvent<HTMLDivElement>) => void;
|
|
66
|
+
onPointerMove?: (e: ReactPointerEvent<HTMLDivElement>) => void;
|
|
67
|
+
onPointerUp?: (e: ReactPointerEvent<HTMLDivElement>) => void;
|
|
68
|
+
onDoubleClick?: () => void;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function Splitter(props: SplitterProps) {
|
|
72
|
+
return (
|
|
73
|
+
<div
|
|
74
|
+
className="splitter"
|
|
75
|
+
role="separator"
|
|
76
|
+
aria-orientation="vertical"
|
|
77
|
+
title="拖动调整宽度,双击恢复默认"
|
|
78
|
+
{...props}
|
|
79
|
+
/>
|
|
80
|
+
);
|
|
81
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
import { applyThemeMode, loadThemeMode, saveThemeMode, type ThemeMode } from "../theme.ts";
|
|
3
|
+
|
|
4
|
+
const ICON: Record<ThemeMode, string> = { auto: "🌗", light: "☀️", dark: "🌙" };
|
|
5
|
+
const LABEL: Record<ThemeMode, string> = { auto: "跟随系统", light: "浅色", dark: "深色" };
|
|
6
|
+
const CYCLE: Record<ThemeMode, ThemeMode> = { auto: "light", light: "dark", dark: "auto" };
|
|
7
|
+
|
|
8
|
+
/** 三态循环:跟随系统 → 浅色 → 深色。自包含:状态、持久化、应用都在这里。 */
|
|
9
|
+
export default function ThemeToggle() {
|
|
10
|
+
const [mode, setMode] = useState<ThemeMode>(loadThemeMode);
|
|
11
|
+
return (
|
|
12
|
+
<button
|
|
13
|
+
className="theme-toggle"
|
|
14
|
+
title={`主题:${LABEL[mode]}(点击切换)`}
|
|
15
|
+
onClick={() => {
|
|
16
|
+
const next = CYCLE[mode];
|
|
17
|
+
setMode(next);
|
|
18
|
+
applyThemeMode(next);
|
|
19
|
+
saveThemeMode(next);
|
|
20
|
+
}}
|
|
21
|
+
>
|
|
22
|
+
{ICON[mode]}
|
|
23
|
+
</button>
|
|
24
|
+
);
|
|
25
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from "react";
|
|
2
|
+
import type { ModelTier } from "@njuptlzf/agent-chat-protocol";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 档位下拉选择器(U1b)。assistant-ui 0.15.28 没有通用下拉/菜单原语
|
|
6
|
+
* (index.d.ts 只有 thread/composer 原语与 unstable trigger-popover 钩子),
|
|
7
|
+
* 按规范自定义。样式对齐宿主截图:触发钮只显示当前档位,弹出列表每行
|
|
8
|
+
* label + 右对齐消耗倍率,顶部结构化描述卡(名称/说明/倍率/单价)。
|
|
9
|
+
* ponytail: 只做点击+Esc+方向键,无焦点陷阱/滚动锁定;升级路径 = 换无头
|
|
10
|
+
* 菜单库(如 radix popover)。
|
|
11
|
+
*/
|
|
12
|
+
export function TierPicker({
|
|
13
|
+
tiers,
|
|
14
|
+
value,
|
|
15
|
+
onChange,
|
|
16
|
+
}: {
|
|
17
|
+
tiers: ModelTier[];
|
|
18
|
+
value?: string;
|
|
19
|
+
onChange: (id: string) => void;
|
|
20
|
+
}) {
|
|
21
|
+
const [open, setOpen] = useState(false);
|
|
22
|
+
const [hover, setHover] = useState<number | null>(null);
|
|
23
|
+
const rootRef = useRef<HTMLDivElement | null>(null);
|
|
24
|
+
|
|
25
|
+
const current = tiers.find((t) => t.id === value) ?? tiers[0];
|
|
26
|
+
const shown = tiers[hover ?? tiers.indexOf(current)] ?? current;
|
|
27
|
+
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
if (!open) return;
|
|
30
|
+
const onDoc = (e: MouseEvent) => {
|
|
31
|
+
if (!rootRef.current?.contains(e.target as Node)) setOpen(false);
|
|
32
|
+
};
|
|
33
|
+
document.addEventListener("mousedown", onDoc);
|
|
34
|
+
return () => document.removeEventListener("mousedown", onDoc);
|
|
35
|
+
}, [open]);
|
|
36
|
+
|
|
37
|
+
const pick = (t: ModelTier) => {
|
|
38
|
+
onChange(t.id);
|
|
39
|
+
setOpen(false);
|
|
40
|
+
setHover(null);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
return (
|
|
44
|
+
<div
|
|
45
|
+
className="tierpick"
|
|
46
|
+
ref={rootRef}
|
|
47
|
+
onKeyDown={(e) => {
|
|
48
|
+
if (e.key === "Escape") {
|
|
49
|
+
setOpen(false);
|
|
50
|
+
setHover(null);
|
|
51
|
+
} else if (open && (e.key === "ArrowDown" || e.key === "ArrowUp")) {
|
|
52
|
+
e.preventDefault();
|
|
53
|
+
const base = hover ?? tiers.indexOf(current);
|
|
54
|
+
setHover(e.key === "ArrowDown" ? Math.min(tiers.length - 1, base + 1) : Math.max(0, base - 1));
|
|
55
|
+
} else if (open && e.key === "Enter" && hover !== null) {
|
|
56
|
+
e.preventDefault();
|
|
57
|
+
const t = tiers[hover];
|
|
58
|
+
if (t) pick(t);
|
|
59
|
+
}
|
|
60
|
+
}}
|
|
61
|
+
>
|
|
62
|
+
{open && (
|
|
63
|
+
<div className="tier-pop" role="listbox" aria-label="回答档位">
|
|
64
|
+
<div className="tier-desc">
|
|
65
|
+
<div className="tier-desc-name">{shown.label}</div>
|
|
66
|
+
{shown.description && <div className="tier-desc-text">{shown.description}</div>}
|
|
67
|
+
<div className="tier-desc-meta">
|
|
68
|
+
<span>消耗倍率</span>
|
|
69
|
+
<span>×{shown.multiplier}</span>
|
|
70
|
+
</div>
|
|
71
|
+
{shown.pricePerThousandTokensCents !== undefined && (
|
|
72
|
+
<div className="tier-desc-meta">
|
|
73
|
+
<span>单价</span>
|
|
74
|
+
<span>{shown.pricePerThousandTokensCents} 分/千字</span>
|
|
75
|
+
</div>
|
|
76
|
+
)}
|
|
77
|
+
</div>
|
|
78
|
+
{tiers.map((t, i) => (
|
|
79
|
+
<button
|
|
80
|
+
type="button"
|
|
81
|
+
key={t.id}
|
|
82
|
+
role="option"
|
|
83
|
+
aria-selected={t.id === current.id}
|
|
84
|
+
className={t.id === current.id ? "tier-row on" : "tier-row"}
|
|
85
|
+
onMouseEnter={() => setHover(i)}
|
|
86
|
+
onClick={() => pick(t)}
|
|
87
|
+
>
|
|
88
|
+
<span>{t.label}</span>
|
|
89
|
+
<span className="tier-mult">×{t.multiplier}</span>
|
|
90
|
+
</button>
|
|
91
|
+
))}
|
|
92
|
+
</div>
|
|
93
|
+
)}
|
|
94
|
+
<button
|
|
95
|
+
type="button"
|
|
96
|
+
className="tier-btn"
|
|
97
|
+
aria-haspopup="listbox"
|
|
98
|
+
aria-expanded={open}
|
|
99
|
+
onKeyDown={(e) => {
|
|
100
|
+
if (!open && (e.key === "ArrowDown" || e.key === "ArrowUp")) {
|
|
101
|
+
e.preventDefault();
|
|
102
|
+
setOpen(true);
|
|
103
|
+
}
|
|
104
|
+
}}
|
|
105
|
+
onClick={() => setOpen((o) => !o)}
|
|
106
|
+
>
|
|
107
|
+
{current.label}
|
|
108
|
+
<span className="tier-caret">▾</span>
|
|
109
|
+
</button>
|
|
110
|
+
</div>
|
|
111
|
+
);
|
|
112
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
import type { UiPromptEvent } from "../adapter.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Card for an extension dialog (`ctx.ui.select/confirm/input`, custom component
|
|
6
|
+
* UI-R3 — assistant-ui has no equivalent primitive for extension UI prompts).
|
|
7
|
+
* The server parks the run inside the extension's await until this answers
|
|
8
|
+
* POST /api/ui-response, so every kind offers an explicit way out: `cancelled`
|
|
9
|
+
* maps to the fail-closed default (false / undefined) if the user dismisses
|
|
10
|
+
* instead of choosing.
|
|
11
|
+
*/
|
|
12
|
+
export function UIPromptCard({
|
|
13
|
+
prompt,
|
|
14
|
+
onAnswer,
|
|
15
|
+
}: {
|
|
16
|
+
prompt: UiPromptEvent;
|
|
17
|
+
onAnswer: (a: { value?: string; confirmed?: boolean; cancelled?: true }) => void;
|
|
18
|
+
}) {
|
|
19
|
+
const [text, setText] = useState("");
|
|
20
|
+
const submitInput = () => {
|
|
21
|
+
if (text.trim()) onAnswer({ value: text });
|
|
22
|
+
};
|
|
23
|
+
return (
|
|
24
|
+
<div className="uidialog" role={prompt.kind === "confirm" ? "alertdialog" : "dialog"} aria-label={prompt.title}>
|
|
25
|
+
<div className="uidialog-title">{prompt.title}</div>
|
|
26
|
+
{prompt.message && <div className="uidialog-msg">{prompt.message}</div>}
|
|
27
|
+
{prompt.kind === "select" && (
|
|
28
|
+
<div className="uidialog-options">
|
|
29
|
+
{(prompt.options ?? []).map((o) => (
|
|
30
|
+
<button key={o} onClick={() => onAnswer({ value: o })}>
|
|
31
|
+
{o}
|
|
32
|
+
</button>
|
|
33
|
+
))}
|
|
34
|
+
<button className="uidialog-dismiss" onClick={() => onAnswer({ cancelled: true })}>
|
|
35
|
+
取消
|
|
36
|
+
</button>
|
|
37
|
+
</div>
|
|
38
|
+
)}
|
|
39
|
+
{prompt.kind === "confirm" && (
|
|
40
|
+
<div className="uidialog-actions">
|
|
41
|
+
<button className="allow" onClick={() => onAnswer({ confirmed: true })}>
|
|
42
|
+
确定
|
|
43
|
+
</button>
|
|
44
|
+
<button className="deny" onClick={() => onAnswer({ cancelled: true })}>
|
|
45
|
+
取消
|
|
46
|
+
</button>
|
|
47
|
+
</div>
|
|
48
|
+
)}
|
|
49
|
+
{prompt.kind === "input" && (
|
|
50
|
+
<div className="uidialog-input">
|
|
51
|
+
<input
|
|
52
|
+
value={text}
|
|
53
|
+
placeholder={prompt.placeholder ?? ""}
|
|
54
|
+
autoFocus
|
|
55
|
+
onChange={(e) => setText(e.target.value)}
|
|
56
|
+
onKeyDown={(e) => e.key === "Enter" && submitInput()}
|
|
57
|
+
/>
|
|
58
|
+
<button onClick={submitInput} disabled={!text.trim()}>
|
|
59
|
+
提交
|
|
60
|
+
</button>
|
|
61
|
+
<button className="uidialog-dismiss" onClick={() => onAnswer({ cancelled: true })}>
|
|
62
|
+
取消
|
|
63
|
+
</button>
|
|
64
|
+
</div>
|
|
65
|
+
)}
|
|
66
|
+
</div>
|
|
67
|
+
);
|
|
68
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session-list time helpers (batch 12, DSH-inspired): human relative labels
|
|
3
|
+
* and calendar-day grouping. Pure and `now`-injected so they are testable
|
|
4
|
+
* without a clock mock.
|
|
5
|
+
*
|
|
6
|
+
* 另外提供**绝对时刻**显示(`fmtTime` / `fmtDate`):
|
|
7
|
+
* 服务端一律存 UTC ISO,页面以前直接 `iso.slice(0,16)` 截出来显示——那是 UTC,
|
|
8
|
+
* +08:00 的运维看审计流水/学习记录会以为「8 小时前」。现在统一转成浏览器本地时区。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const DAY = 86_400_000;
|
|
12
|
+
|
|
13
|
+
function startOfDay(t: number): number {
|
|
14
|
+
const d = new Date(t);
|
|
15
|
+
d.setHours(0, 0, 0, 0);
|
|
16
|
+
return d.getTime();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Whole calendar days between the item's day and today (0 = today, 1 = yesterday). */
|
|
20
|
+
function daysAgo(iso: string, now: Date): number {
|
|
21
|
+
const t = Date.parse(iso);
|
|
22
|
+
if (Number.isNaN(t)) return 0;
|
|
23
|
+
return Math.round((startOfDay(now.getTime()) - startOfDay(t)) / DAY);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** 刚刚 / N 分钟前 / N 小时前 / 昨天 / M月D日(跨年带年份)。 */
|
|
27
|
+
export function relativeTime(iso: string, now: Date): string {
|
|
28
|
+
const t = Date.parse(iso);
|
|
29
|
+
if (Number.isNaN(t)) return "";
|
|
30
|
+
const diff = now.getTime() - t; // negative (clock skew / future) reads as "刚刚"
|
|
31
|
+
if (diff < 60_000) return "刚刚";
|
|
32
|
+
if (diff < 3_600_000) return `${Math.floor(diff / 60_000)} 分钟前`;
|
|
33
|
+
const days = daysAgo(iso, now);
|
|
34
|
+
if (days === 0) return `${Math.floor(diff / 3_600_000)} 小时前`;
|
|
35
|
+
const d = new Date(t);
|
|
36
|
+
if (days === 1) return "昨天";
|
|
37
|
+
const md = `${d.getMonth() + 1}月${d.getDate()}日`;
|
|
38
|
+
return d.getFullYear() === now.getFullYear() ? md : `${d.getFullYear()}年${md}`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Group label for a session row; buckets follow calendar days, not 24h windows. */
|
|
42
|
+
export function groupOf(iso: string, now: Date): string {
|
|
43
|
+
const days = daysAgo(iso, now);
|
|
44
|
+
if (days <= 0) return "今天";
|
|
45
|
+
if (days === 1) return "昨天";
|
|
46
|
+
if (days <= 6) return "近 7 天";
|
|
47
|
+
if (days <= 29) return "近 30 天";
|
|
48
|
+
return "更早";
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Bucket a session list in order. Assumes items are sorted newest-first (the
|
|
53
|
+
* server does that); a newer item after an older one starts a new bucket run
|
|
54
|
+
* rather than being silently merged — same behaviour as DSH's list.
|
|
55
|
+
*/
|
|
56
|
+
export function groupSessions<T extends { updatedAt: string }>(
|
|
57
|
+
items: readonly T[],
|
|
58
|
+
now: Date,
|
|
59
|
+
): Array<{ label: string; items: T[] }> {
|
|
60
|
+
const out: Array<{ label: string; items: T[] }> = [];
|
|
61
|
+
for (const item of items) {
|
|
62
|
+
const label = groupOf(item.updatedAt, now);
|
|
63
|
+
const last = out[out.length - 1];
|
|
64
|
+
if (last && last.label === label) last.items.push(item);
|
|
65
|
+
else out.push({ label, items: [item] });
|
|
66
|
+
}
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 绝对时刻 → 「2026-11-03 09:12」(本地时区)。
|
|
72
|
+
*
|
|
73
|
+
* - `timeZone` 只给测试用(固定时区做断言);页面调用不传 = 用户本地时区。
|
|
74
|
+
* - 空值显示「—」;**解析不了就原样返回**(不吞、不假装是今天)。
|
|
75
|
+
* - 只喂 ISO 时刻(含 Z 或偏移);别喂「2026-11-03」这种纯日期——那会被当 UTC 零点,
|
|
76
|
+
* 在负时区会退到前一天。
|
|
77
|
+
*/
|
|
78
|
+
export function fmtTime(iso: string | null | undefined, timeZone?: string): string {
|
|
79
|
+
const parts = fmtParts(iso, timeZone, true);
|
|
80
|
+
if (!parts) return iso ? String(iso) : "—";
|
|
81
|
+
return `${parts.year}-${parts.month}-${parts.day} ${parts.hour}:${parts.minute}`;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** 绝对时刻 → 「2026-11-03」(本地时区)。语义同 `fmtTime`。 */
|
|
85
|
+
export function fmtDate(iso: string | null | undefined, timeZone?: string): string {
|
|
86
|
+
const parts = fmtParts(iso, timeZone, false);
|
|
87
|
+
if (!parts) return iso ? String(iso) : "—";
|
|
88
|
+
return `${parts.year}-${parts.month}-${parts.day}`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function fmtParts(
|
|
92
|
+
iso: string | null | undefined,
|
|
93
|
+
timeZone: string | undefined,
|
|
94
|
+
withTime: boolean,
|
|
95
|
+
): { year: string; month: string; day: string; hour: string; minute: string } | null {
|
|
96
|
+
if (!iso) return null;
|
|
97
|
+
const d = new Date(iso);
|
|
98
|
+
if (Number.isNaN(d.getTime())) return null;
|
|
99
|
+
const parts = new Intl.DateTimeFormat("zh-CN", {
|
|
100
|
+
...(timeZone ? { timeZone } : {}),
|
|
101
|
+
year: "numeric",
|
|
102
|
+
month: "2-digit",
|
|
103
|
+
day: "2-digit",
|
|
104
|
+
...(withTime ? { hour: "2-digit", minute: "2-digit" } : {}),
|
|
105
|
+
// h23:不用 hour12:false——某些 ICU 版本会把午夜排成 "24:00"。
|
|
106
|
+
hourCycle: withTime ? "h23" : undefined,
|
|
107
|
+
}).formatToParts(d);
|
|
108
|
+
const get = (type: string) => parts.find((p) => p.type === type)?.value ?? "";
|
|
109
|
+
return { year: get("year"), month: get("month"), day: get("day"), hour: get("hour"), minute: get("minute") };
|
|
110
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { useSyncExternalStore } from "react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 视口是否窄于 maxWidth(响应式布局用)。
|
|
5
|
+
*
|
|
6
|
+
* 用 useSyncExternalStore 而不是 useState+useEffect:首帧就能拿到正确值,
|
|
7
|
+
* 不会出现"先按宽屏渲染再跳成窄屏"的闪动。服务端渲染(无 window)回落 false。
|
|
8
|
+
*/
|
|
9
|
+
export function useNarrow(maxWidth: number): boolean {
|
|
10
|
+
const query = `(max-width: ${maxWidth}px)`;
|
|
11
|
+
return useSyncExternalStore(
|
|
12
|
+
(onChange) => {
|
|
13
|
+
const mql = window.matchMedia(query);
|
|
14
|
+
mql.addEventListener("change", onChange);
|
|
15
|
+
return () => mql.removeEventListener("change", onChange);
|
|
16
|
+
},
|
|
17
|
+
() => window.matchMedia(query).matches,
|
|
18
|
+
() => false,
|
|
19
|
+
);
|
|
20
|
+
}
|