~bigbes/lethe

ref: dcc2805a6e14ca0169751edb755aeac42d086d17 lethe/web/src/features/settings/SavedSearchesSection.tsx -rw-r--r-- 6.6 KiB
dcc2805a — Eugene Blikh docs: record collector final fixes 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import React, { useState } from 'react'
import { EmptyState, Sub } from '../../primitives'
import { AuthGate } from '../../shell/AuthGate'
import { AuthError, APIError } from '../../api/client'
import type { SavedSearch } from '../../api/adapters'
import {
  useSavedSearches,
  useCreateSavedSearch,
  useUpdateSavedSearch,
  useDeleteSavedSearch,
} from './useSavedSearches'

// ── Inline "Add new" form ─────────────────────────────────────────────────────

function AddForm(): React.JSX.Element {
  const [name, setName] = useState('')
  const [query, setQuery] = useState('')
  const createMutation = useCreateSavedSearch()

  function handleSave(e: React.FormEvent) {
    e.preventDefault()
    if (!name.trim() || !query.trim()) return
    createMutation.mutate(
      { name: name.trim(), query: query.trim() },
      {
        onSuccess: () => {
          setName('')
          setQuery('')
        },
      },
    )
  }

  return (
    <form className="saved-search-form" onSubmit={handleSave}>
      <input
        className="saved-search-input"
        placeholder="name"
        value={name}
        onChange={(e) => setName(e.target.value)}
        autoComplete="off"
      />
      <input
        className="saved-search-input saved-search-input-mono"
        placeholder="query"
        value={query}
        onChange={(e) => setQuery(e.target.value)}
        autoComplete="off"
      />
      <button
        className="saved-search-btn"
        type="submit"
        disabled={createMutation.isPending}
      >
        save
      </button>
      {createMutation.isError && (
        <span className="saved-search-error">
          {createMutation.error instanceof APIError
            ? createMutation.error.message
            : String(createMutation.error)}
        </span>
      )}
    </form>
  )
}

// ── Per-row inline edit form ──────────────────────────────────────────────────

interface RowProps {
  row: SavedSearch
}

function SavedSearchRow({ row }: RowProps): React.JSX.Element {
  const [editing, setEditing] = useState(false)
  const [name, setName] = useState(row.name)
  const [query, setQuery] = useState(row.query)
  const updateMutation = useUpdateSavedSearch()
  const deleteMutation = useDeleteSavedSearch()

  function handleSave(e: React.FormEvent) {
    e.preventDefault()
    updateMutation.mutate(
      { oldName: row.name, name: name.trim() || undefined, query: query.trim() || undefined },
      { onSuccess: () => setEditing(false) },
    )
  }

  function handleDelete() {
    deleteMutation.mutate({ name: row.name })
  }

  if (editing) {
    return (
      <div className="saved-searches-row saved-searches-row-editing">
        <form className="saved-search-form" onSubmit={handleSave}>
          <input
            className="saved-search-input"
            value={name}
            onChange={(e) => setName(e.target.value)}
            autoComplete="off"
          />
          <input
            className="saved-search-input saved-search-input-mono"
            value={query}
            onChange={(e) => setQuery(e.target.value)}
            autoComplete="off"
          />
          <button className="saved-search-btn" type="submit" disabled={updateMutation.isPending}>
            save
          </button>
          <button
            className="saved-search-btn"
            type="button"
            onClick={() => {
              setEditing(false)
              setName(row.name)
              setQuery(row.query)
            }}
          >
            cancel
          </button>
        </form>
        {updateMutation.isError && (
          <span className="saved-search-error">
            {updateMutation.error instanceof APIError
              ? updateMutation.error.message
              : String(updateMutation.error)}
          </span>
        )}
      </div>
    )
  }

  return (
    <div className="saved-searches-row">
      <span className="saved-searches-cell saved-searches-name">{row.name}</span>
      <span className="saved-searches-cell saved-searches-query mono">{row.query}</span>
      <span className="saved-searches-cell saved-searches-updated muted">
        {new Date(row.updatedAt).toLocaleDateString()}
      </span>
      <span className="saved-searches-cell saved-searches-actions">
        <button
          className="saved-search-btn"
          type="button"
          onClick={() => setEditing(true)}
        >
          edit
        </button>
        <button
          className="saved-search-btn saved-search-btn-danger"
          type="button"
          onClick={handleDelete}
          disabled={deleteMutation.isPending}
        >
          delete
        </button>
      </span>
      {deleteMutation.isError && (
        <span className="saved-search-error">
          {deleteMutation.error instanceof APIError
            ? deleteMutation.error.message
            : String(deleteMutation.error)}
        </span>
      )}
    </div>
  )
}

// ── Main section ──────────────────────────────────────────────────────────────

export function SavedSearchesSection(): React.JSX.Element {
  const { data, isLoading, error } = useSavedSearches()

  if (isLoading) {
    return <Sub>loading</Sub>
  }

  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>
      <div className="saved-searches-section">
        <div className="saved-searches-heading">Saved searches</div>
        <AddForm />
        {data != null && data.length === 0 ? (
          <EmptyState glyph="∅" copy="no saved searches yet" />
        ) : (
          <div className="saved-searches-table">
            <div className="saved-searches-thead">
              <span>Name</span>
              <span>Query</span>
              <span>Updated</span>
              <span></span>
            </div>
            {data?.map((row) => (
              <SavedSearchRow key={row.name} row={row} />
            ))}
          </div>
        )}
      </div>
    </AuthGate>
  )
}