~bigbes/lethe

ref: 13113b8c4674cd767c94798875471f64b11e656d lethe/web/src/routes/projects.tsx -rw-r--r-- 3.9 KiB
13113b8c — Eugene Blikh web: AuthGate consolidates three "not authenticated" cards 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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 { ProjectsTable } from '../features/projects/ProjectsTable'
import { useProjects } from '../features/projects/useProjects'
import { useProjectsCursor } from '../features/projects/useProjectsCursor'
import { useKeyboardCursor } from './__root'
import { AuthError, APIError } from '../api/client'
import type { ProjectFilters } from '../features/projects/useProjects'
import type { Project } from '../api/adapters'
import '../styles/projects.css'

type ProjectsSearch = {
  since?: '7d' | '30d' | '90d' | 'all'
}

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

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

function SinceButtonGroup({
  value,
  onChange,
}: {
  value: string
  onChange: (v: ProjectsSearch['since']) => void
}): React.JSX.Element {
  const options = ['7d', '30d', '90d', 'all'] as const
  return (
    <div className="since-btn-group">
      {options.map(o => (
        <button
          key={o}
          className={'since-btn' + (value === o ? ' active' : '')}
          onClick={() => onChange(o)}
          type="button"
        >
          {o}
        </button>
      ))}
    </div>
  )
}

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

  const since = search.since ?? '30d'

  const filters: ProjectFilters = { since }

  const { data: projects, isLoading, error } = useProjects(filters)

  const handleOpen = useCallback(
    (p: Project) => {
      void navigate({ to: '/project/$', params: { _splat: encodeURIComponent(p.cwd) } })
    },
    [navigate],
  )

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

  const cursorRef = useKeyboardCursor()
  useEffect(() => {
    cursorRef.current = { move, activate }
    return () => {
      cursorRef.current = {
        move: (_d: 1 | -1) => { /* no-op */ },
        activate: () => { /* no-op */ },
      }
    }
  }, [cursorRef, move, activate])

  function handleSinceChange(next: ProjectsSearch['since']) {
    void navigate({ to: '/projects', search: { since: next } })
  }

  const subBar = (
    <SubBar>
      <span className="mono muted">
        {projects != null ? `${projects.length} projects` : '…'} · ranked by recent activity
      </span>
      <span style={{ flex: 1 }} />
      <SinceButtonGroup value={since} onChange={handleSinceChange} />
    </SubBar>
  )

  if (isLoading) {
    return (
      <>
        {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}
        <ProjectsTable
          projects={projects ?? []}
          cursor={cursor}
          onCursor={jumpTo}
          onOpen={handleOpen}
        />
      </>
    </AuthGate>
  )
}