@luziyang2026/dsh-question-nav 0.5.0 → 0.7.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/README.md +25 -7
- package/README.zh.md +19 -5
- package/lib/client.js +295 -45
- package/lib/client.js.map +1 -1
- package/lib/types/core/focus.d.ts +58 -0
- package/lib/types/core/time.d.ts +19 -0
- package/package.json +2 -2
- package/src/client/QuestionNavStrip.tsx +261 -46
- package/src/client/question-nav.module.css +123 -19
- package/src/core/focus.ts +96 -0
- package/src/core/time.ts +39 -0
package/src/core/time.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Question sent-time formatting for the question-nav surface. Pure: no DSH
|
|
3
|
+
* imports, deterministic for a given (time, now) pair — unit-testable in
|
|
4
|
+
* isolation and safe to inline into the client bundle.
|
|
5
|
+
*
|
|
6
|
+
* @module dsh-question-nav/time
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
function pad2(n: number): string {
|
|
10
|
+
return n < 10 ? `0${n}` : String(n)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Whether `time` falls on the same calendar day as `now`. */
|
|
14
|
+
export function isSameDay(time: number, now: number): boolean {
|
|
15
|
+
const a = new Date(time)
|
|
16
|
+
const b = new Date(now)
|
|
17
|
+
return a.getFullYear() === b.getFullYear()
|
|
18
|
+
&& a.getMonth() === b.getMonth()
|
|
19
|
+
&& a.getDate() === b.getDate()
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Smart question sent-time, relative to `now`:
|
|
24
|
+
* - same calendar day → `HH:MM`
|
|
25
|
+
* - same calendar year → `MM-DD HH:MM`
|
|
26
|
+
* - otherwise → `YYYY-MM-DD HH:MM`
|
|
27
|
+
*
|
|
28
|
+
* Returns `''` for a missing/invalid timestamp (e.g. a live node that never
|
|
29
|
+
* reported one), so callers can hide the time line without branching.
|
|
30
|
+
*/
|
|
31
|
+
export function formatQuestionTime(time: number, now: number): string {
|
|
32
|
+
if (!Number.isFinite(time) || time <= 0) return ''
|
|
33
|
+
const d = new Date(time)
|
|
34
|
+
const hm = `${pad2(d.getHours())}:${pad2(d.getMinutes())}`
|
|
35
|
+
if (isSameDay(time, now)) return hm
|
|
36
|
+
const md = `${pad2(d.getMonth() + 1)}-${pad2(d.getDate())}`
|
|
37
|
+
if (d.getFullYear() === new Date(now).getFullYear()) return `${md} ${hm}`
|
|
38
|
+
return `${d.getFullYear()}-${md} ${hm}`
|
|
39
|
+
}
|