~bigbes/lethe

ref: 7cffe38a672c0f5fc825235f496c72bebc9ee2b3 lethe/web/src/features/projects/ProjectsTable.tsx -rw-r--r-- 3.1 KiB
7cffe38a — Eugene Blikh web: add display preference modules a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import React from 'react'
import { EmptyState, ToolDot, Spark } from '../../primitives'
import type { Project } from '../../api/adapters'

interface ProjectsTableProps {
  projects: Project[]
  cursor: number
  onCursor: (i: number) => void
  onOpen: (p: Project) => void
}

const COLS = '1fr 90px 90px 110px 110px 90px'

// Format TOK: 12400 → "12.4k"; small numbers unchanged
function formatTok(n: number): string {
  if (n >= 1000) {
    return (n / 1000).toFixed(1) + 'k'
  }
  return String(n)
}

// Format lastActive ISO string: recent = "Apr 25 11:08"; empty = "—"
function formatLastActive(iso: string): string {
  if (!iso) return '—'
  const d = new Date(iso)
  const now = new Date()
  const diffMs = now.getTime() - d.getTime()
  const h24 = 24 * 60 * 60 * 1000

  if (diffMs <= h24) {
    return d.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', hour12: false })
  }
  return (
    d.toLocaleDateString('en-US', { month: 'short', day: 'numeric' }) +
    ' ' +
    d.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', hour12: false })
  )
}

export function ProjectsTable({ projects, cursor, onCursor, onOpen }: ProjectsTableProps): React.JSX.Element {
  if (projects.length === 0) {
    return (
      <div className="body body-pad">
        <EmptyState glyph="∅" copy="no projects match these filters" />
      </div>
    )
  }

  const maxSessions = Math.max(...projects.map(p => p.sessions), 1)

  return (
    <div className="projects-table body">
      <div className="projects-thead projects-cols" style={{ gridTemplateColumns: COLS }}>
        <span>cwd</span>
        <span className="right">sessions</span>
        <span className="right">tok</span>
        <span className="right">last</span>
        <span>top tool</span>
        <span className="right">activity</span>
      </div>
      {projects.map((p, i) => {
        const isCursor = i === cursor
        // Build a minimal sparkline: normalise sessions relative to max, scaled to 0–12px height
        const sparkPoints = [0, (p.sessions / maxSessions) * 12]
        return (
          <div
            key={p.cwd}
            className={'projects-row projects-cols' + (isCursor ? ' cursor' : '')}
            style={{ gridTemplateColumns: COLS }}
            onClick={() => { onCursor(i); onOpen(p) }}
            onMouseEnter={() => onCursor(i)}
          >
            <span className="mono truncate">{p.cwd}</span>
            <span className="right mono">{p.sessions}</span>
            <span className="right mono muted">{formatTok(p.tokensIn + p.tokensOut)}</span>
            <span className="right mono muted">{formatLastActive(p.lastActive)}</span>
            <span className="mono">
              {p.topTool ? (
                <>
                  <ToolDot tool={p.topTool} />
                  {' '}{p.topTool}
                </>
              ) : (
                <span className="muted"></span>
              )}
            </span>
            <span className="right">
              <Spark points={sparkPoints} w={70} h={12} accent={i === 0} />
            </span>
          </div>
        )
      })}
    </div>
  )
}