Skip to content

Commit 937a564

Browse files
committed
Refactor some helper UI functions into misc.ts.
1 parent 1fdbfc5 commit 937a564

File tree

2 files changed

+32
-33
lines changed

2 files changed

+32
-33
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export function timeAgo(date: Date | number): string {
2+
const timestamp = typeof date === 'number' ? date : date.getTime()
3+
const seconds = Math.floor((Date.now() - timestamp) / 1000)
4+
const intervals = [
5+
{ label: 'y', secs: 31536000 },
6+
{ label: 'mo', secs: 2592000 },
7+
{ label: 'w', secs: 604800 },
8+
{ label: 'd', secs: 86400 },
9+
{ label: 'h', secs: 3600 },
10+
{ label: 'm', secs: 60 },
11+
{ label: 's', secs: 1 },
12+
]
13+
for (const i of intervals) {
14+
const v = Math.floor(seconds / i.secs)
15+
if (v >= 1) return `${v}${i.label}`
16+
}
17+
return 'just now'
18+
}
19+
20+
/** Returns all leaf values of an arbitrary object as strings. */
21+
export function* allLeafValues(obj: any, visited = new Set()): Generator<string> {
22+
if (visited.has(obj) || obj == null) return
23+
if (typeof obj === 'string') yield obj
24+
else if (typeof obj === 'number') yield String(obj)
25+
else if (typeof obj === 'object') {
26+
visited.add(obj)
27+
for (const key in obj) {
28+
yield* allLeafValues(obj[key], visited)
29+
}
30+
}
31+
}

browser-extension/tests/playground/claude.tsx

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,12 @@ import { twMerge } from 'tailwind-merge'
44
import Badge from '@/components/Badge'
55
import { badgeCVA } from '@/components/design'
66
import MultiSegment from '@/components/MultiSegment'
7+
import { allLeafValues, timeAgo } from '@/components/misc'
78
import type { CommentTableRow } from '@/entrypoints/background'
89
import type { FilterState } from '@/entrypoints/popup/popup'
910
import { EnhancerRegistry } from '@/lib/registries'
1011
import { generateMockDrafts } from './replicaData'
1112

12-
// Helper function for relative time
13-
function timeAgo(date: Date | number): string {
14-
const timestamp = typeof date === 'number' ? date : date.getTime()
15-
const seconds = Math.floor((Date.now() - timestamp) / 1000)
16-
const intervals = [
17-
{ label: 'y', secs: 31536000 },
18-
{ label: 'mo', secs: 2592000 },
19-
{ label: 'w', secs: 604800 },
20-
{ label: 'd', secs: 86400 },
21-
{ label: 'h', secs: 3600 },
22-
{ label: 'm', secs: 60 },
23-
{ label: 's', secs: 1 },
24-
]
25-
for (const i of intervals) {
26-
const v = Math.floor(seconds / i.secs)
27-
if (v >= 1) return `${v}${i.label}`
28-
}
29-
return 'just now'
30-
}
31-
32-
/** Returns all leaf values of an arbitrary object as strings. */
33-
function* allLeafValues(obj: any, visited = new Set()): Generator<string> {
34-
if (visited.has(obj) || obj == null) return
35-
if (typeof obj === 'string') yield obj
36-
else if (typeof obj === 'number') yield String(obj)
37-
else if (typeof obj === 'object') {
38-
visited.add(obj)
39-
for (const key in obj) {
40-
yield* allLeafValues(obj[key], visited)
41-
}
42-
}
43-
}
44-
4513
export const ClaudePrototype = () => {
4614
const [drafts] = useState(generateMockDrafts())
4715
const [selectedIds, setSelectedIds] = useState(new Set())

0 commit comments

Comments
 (0)