~bigbes/lethe

ref: 20e5301a02603e4b525a946a0b077aab86f94f99 lethe/web/src/routes/index.tsx -rw-r--r-- 3.7 KiB
20e5301a — Eugene Blikh docs: plan lethe collector implementation 24 days 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import React, { useEffect, useCallback } from 'react'
import { createFileRoute, useNavigate } from '@tanstack/react-router'
import { SubBar } from '../shell/SubBar'
import { Sub } from '../primitives'
import { AuthGate } from '../shell/AuthGate'
import { FilterChips } from '../features/home/FilterChips'
import { SessionsTable } from '../features/home/SessionsTable'
import { useSessions } from '../features/home/useSessions'
import { useHomeCursor } from '../features/home/useHomeCursor'
import { useKeyboardCursor } from './__root'
import { AuthError, APIError } from '../api/client'
import type { HomeFilters } from '../features/home/useSessions'
import type { Session } from '../api/adapters'
import '../styles/home.css'

type HomeSearch = {
  since?: '1d' | '7d' | '30d' | '90d' | 'all'
  tool?: string
  host?: string
}

const VALID_SINCE = new Set<string>(['1d', '7d', '30d', '90d', 'all'])

export const Route = createFileRoute('/')({
  validateSearch: (search: Record<string, unknown>): HomeSearch => {
    const since = typeof search['since'] === 'string' && VALID_SINCE.has(search['since'])
      ? (search['since'] as HomeSearch['since'])
      : undefined
    const tool = typeof search['tool'] === 'string' && search['tool'] !== '' ? search['tool'] : undefined
    const host = typeof search['host'] === 'string' && search['host'] !== '' ? search['host'] : undefined
    return { since, tool, host }
  },
  component: HomeRoute,
})

function HomeRoute(): React.JSX.Element {
  const navigate = useNavigate()
  const search = Route.useSearch()

  const filters: HomeFilters = {
    since: search.since,
    tool: search.tool,
    host: search.host,
  }

  const { data: sessions, isLoading, error } = useSessions(filters)

  const handleOpen = useCallback((s: Session) => {
    void navigate({ to: '/session/$tool/$host/$id', params: { tool: s.tool, host: s.host, id: s.sessionId } })
  }, [navigate])

  const { cursor, move, activate, jumpTo } = useHomeCursor(
    sessions?.length ?? 0,
    useCallback((idx: number) => {
      if (sessions && sessions[idx]) handleOpen(sessions[idx])
    }, [sessions, handleOpen]),
  )

  // Wire our cursor into the global keyboard controller
  const cursorRef = useKeyboardCursor()
  useEffect(() => {
    cursorRef.current = { move, activate }
    return () => { cursorRef.current = { move: (_d: 1 | -1) => { /* no-op */ }, activate: () => { /* no-op */ } } }
  }, [cursorRef, move, activate])

  function handleFilterChange(next: HomeFilters) {
    void navigate({
      to: '/',
      search: {
        since: next.since,
        tool: next.tool,
        host: next.host,
      },
    })
  }

  if (isLoading) {
    return (
      <>
        <SubBar>
          <FilterChips value={filters} onChange={handleFilterChange} />
        </SubBar>
        <div className="body body-pad">
          <Sub>loading</Sub>
        </div>
      </>
    )
  }

  if (error != null && !(error instanceof AuthError)) {
    const detail = error instanceof APIError ? error.message : String(error)
    return (
      <div className="body body-pad" style={{ display: 'flex', justifyContent: 'center', paddingTop: 60 }}>
        <div className="card" style={{ padding: '24px 32px', textAlign: 'center' }}>
          <div className="uppercase-mono" style={{ marginBottom: 8 }}>error</div>
          <div className="muted">{detail}</div>
        </div>
      </div>
    )
  }

  return (
    <AuthGate>
      <>
        <SubBar>
          <FilterChips value={filters} onChange={handleFilterChange} />
        </SubBar>
        <SessionsTable
          sessions={sessions ?? []}
          cursor={cursor}
          onCursor={jumpTo}
          onOpen={handleOpen}
        />
      </>
    </AuthGate>
  )
}