@opencode/ui 2.2.5 → 2.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/actions/button/button.d.ts +2 -2
- package/dist/actions/icon-button/icon-button.d.ts +1 -1
- package/dist/components/animated-number-machine.d.ts +95 -0
- package/dist/components/file-icon-machine.d.ts +40 -0
- package/dist/components/file-icon.d.ts +0 -2
- package/dist/components/file-icons/choose-icon-name.d.ts +2 -0
- package/dist/components/image-flight-machine.d.ts +135 -0
- package/dist/components/image-zoom-machine.d.ts +95 -0
- package/dist/components/image-zoom.d.ts +6 -3
- package/dist/components/list.d.ts +2 -6
- package/dist/components/menu-scroll.d.ts +9 -3
- package/dist/components/popover-machine.d.ts +96 -0
- package/dist/components/popover.d.ts +2 -2
- package/dist/components/resize-handle-machine.d.ts +71 -0
- package/dist/components/resize-handle.d.ts +2 -0
- package/dist/components/scroll-view-machine.d.ts +68 -0
- package/dist/components/scroll-view.d.ts +2 -2
- package/dist/components/spinner.d.ts +11 -3
- package/dist/components/text-field-machine.d.ts +62 -0
- package/dist/components/text-reveal-machine.d.ts +90 -0
- package/dist/components/typewriter-machine.d.ts +62 -0
- package/dist/context/dialog-machine.d.ts +125 -0
- package/dist/context/dialog.d.ts +17 -16
- package/dist/data-display/line-comment/machine.d.ts +157 -0
- package/dist/data-display/project-avatar/animation-machine.d.ts +62 -0
- package/dist/data-display/project-avatar/animation.d.ts +1 -1
- package/dist/feedback/activity-ring/activity-ring.d.ts +2 -1
- package/dist/feedback/toast/machine.d.ts +57 -0
- package/dist/feedback/toast/toast.d.ts +1 -1
- package/dist/forms/checkbox/checkbox.d.ts +6 -1
- package/dist/forms/select/machine.d.ts +45 -0
- package/dist/forms/select/select.d.ts +4 -0
- package/dist/forms/typeahead/machine.d.ts +85 -0
- package/dist/hooks/auto-scroll-machine.d.ts +89 -0
- package/dist/hooks/create-auto-scroll.d.ts +1 -1
- package/dist/hooks/list-machine.d.ts +150 -0
- package/dist/hooks/use-filtered-list.d.ts +21 -35
- package/dist/i18n/en.d.ts +5 -0
- package/dist/icons/catalog.d.ts +8 -0
- package/dist/navigation/menu/machine.d.ts +49 -0
- package/dist/navigation/menu/menu.d.ts +5 -3
- package/dist/navigation/segmented-control/machine.d.ts +46 -0
- package/dist/overlays/mcp-sign-in/machine.d.ts +184 -0
- package/dist/overlays/mcp-sign-in/mcp-sign-in.d.ts +40 -0
- package/dist/overlays/tooltip/machine.d.ts +146 -0
- package/dist/theme/context.d.ts +38 -14
- package/dist/theme/machine.d.ts +174 -0
- package/dist/typography/text-shimmer/machine.d.ts +48 -0
- package/package.json +19 -3
- package/src/actions/button/button.tsx +11 -6
- package/src/actions/icon-button/icon-button.tsx +17 -4
- package/src/components/animated-number-machine.ts +214 -0
- package/src/components/animated-number.tsx +134 -83
- package/src/components/file-icon-machine.ts +78 -0
- package/src/components/file-icon.tsx +96 -579
- package/src/components/file-icons/choose-icon-name.ts +574 -0
- package/src/components/image-flight-machine.ts +247 -0
- package/src/components/image-preview.css +5 -2
- package/src/components/image-preview.tsx +390 -252
- package/src/components/image-zoom-machine.ts +191 -0
- package/src/components/image-zoom.ts +22 -14
- package/src/components/list.tsx +46 -137
- package/src/components/menu-scroll.css +19 -8
- package/src/components/menu-scroll.tsx +111 -34
- package/src/components/popover-machine.ts +198 -0
- package/src/components/popover.tsx +157 -89
- package/src/components/resize-handle-machine.ts +174 -0
- package/src/components/resize-handle.tsx +116 -67
- package/src/components/scroll-view-machine.ts +244 -0
- package/src/components/scroll-view.tsx +163 -116
- package/src/components/spinner.css +18 -5
- package/src/components/spinner.tsx +28 -14
- package/src/components/text-field-machine.ts +102 -0
- package/src/components/text-field.tsx +78 -24
- package/src/components/text-reveal-machine.ts +184 -0
- package/src/components/text-reveal.tsx +96 -93
- package/src/components/typewriter-machine.ts +134 -0
- package/src/components/typewriter.tsx +50 -39
- package/src/context/dialog-machine.ts +280 -0
- package/src/context/dialog.tsx +168 -177
- package/src/data-display/line-comment/line-comment.tsx +157 -178
- package/src/data-display/line-comment/machine.ts +391 -0
- package/src/data-display/project-avatar/animation-machine.ts +182 -0
- package/src/data-display/project-avatar/animation.ts +67 -38
- package/src/feedback/activity-ring/activity-ring.tsx +1 -1
- package/src/feedback/toast/machine.ts +115 -0
- package/src/feedback/toast/toast.tsx +55 -59
- package/src/forms/checkbox/checkbox-compact.css +169 -0
- package/src/forms/checkbox/checkbox.tsx +93 -31
- package/src/forms/search-list/search-list.css +5 -1
- package/src/forms/search-list/search-list.tsx +4 -4
- package/src/forms/select/machine.ts +81 -0
- package/src/forms/select/select.css +59 -17
- package/src/forms/select/select.tsx +67 -31
- package/src/forms/typeahead/machine.ts +253 -0
- package/src/forms/typeahead/typeahead.tsx +70 -62
- package/src/hooks/auto-scroll-machine.ts +261 -0
- package/src/hooks/create-auto-scroll.tsx +110 -205
- package/src/hooks/list-machine.ts +359 -0
- package/src/hooks/use-filtered-list.tsx +101 -112
- package/src/i18n/en.ts +6 -1
- package/src/icons/catalog.ts +8 -0
- package/src/navigation/menu/machine.ts +109 -0
- package/src/navigation/menu/menu.tsx +130 -22
- package/src/navigation/segmented-control/machine.ts +72 -0
- package/src/navigation/segmented-control/segmented-control.tsx +46 -46
- package/src/overlays/dialog/dialog.tsx +7 -1
- package/src/overlays/mcp-sign-in/machine.ts +343 -0
- package/src/overlays/mcp-sign-in/mcp-sign-in.ts +159 -0
- package/src/overlays/tooltip/machine.ts +303 -0
- package/src/overlays/tooltip/tooltip.tsx +157 -92
- package/src/styles/theme.css +10 -2
- package/src/theme/context.tsx +192 -269
- package/src/theme/machine.ts +508 -0
- package/src/typography/text-shimmer/machine.ts +90 -0
- package/src/typography/text-shimmer/text-shimmer.css +71 -39
- package/src/typography/text-shimmer/text-shimmer.tsx +41 -24
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
export const TYPEAHEAD_REQUEST_TIMEOUT_MS = 15_000
|
|
2
|
+
|
|
3
|
+
export type TypeaheadInitial<T> = { readonly items: T[]; readonly failed?: boolean }
|
|
4
|
+
|
|
5
|
+
type TypeaheadLive<T> = {
|
|
6
|
+
/** The input's text; the query is this text trimmed. */
|
|
7
|
+
readonly input: string
|
|
8
|
+
/** The user has typed, so later result changes animate the list height. */
|
|
9
|
+
readonly searched: boolean
|
|
10
|
+
/** Some answer has arrived, so the empty row may show. */
|
|
11
|
+
readonly answered: boolean
|
|
12
|
+
/** The parent's opening results, shown for the empty query. */
|
|
13
|
+
readonly initial: TypeaheadInitial<T> | undefined
|
|
14
|
+
readonly cache: ReadonlyMap<string, T[]>
|
|
15
|
+
readonly shown: T[]
|
|
16
|
+
/** `shown` holds rows the pending query does not match; they stay dimmed instead of collapsing the list. */
|
|
17
|
+
readonly stale: boolean
|
|
18
|
+
/** The last attempt issued, so the next request gets a fresh one. */
|
|
19
|
+
readonly issued: number
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* `debouncing` waits for typing to pause; `loading` has a request in flight. Both name their `attempt`, and a timer or
|
|
24
|
+
* result from another attempt is ignored. `failed` is the current query's failure only.
|
|
25
|
+
*/
|
|
26
|
+
export type TypeaheadState<T> =
|
|
27
|
+
| (TypeaheadLive<T> & { readonly status: "ready" | "failed" })
|
|
28
|
+
| (TypeaheadLive<T> & { readonly status: "debouncing" | "loading"; readonly attempt: number })
|
|
29
|
+
| { readonly status: "disposed" }
|
|
30
|
+
|
|
31
|
+
export type TypeaheadEvent<T> =
|
|
32
|
+
| { readonly type: "initial.synced"; readonly initial: TypeaheadInitial<T> | undefined }
|
|
33
|
+
| {
|
|
34
|
+
readonly type: "input.changed"
|
|
35
|
+
readonly input: string
|
|
36
|
+
readonly delayMs: number
|
|
37
|
+
readonly match: (item: T, query: string) => boolean
|
|
38
|
+
}
|
|
39
|
+
| { readonly type: "debounce.elapsed"; readonly attempt: number }
|
|
40
|
+
| { readonly type: "retry.requested" }
|
|
41
|
+
| { readonly type: "request.succeeded"; readonly attempt: number; readonly items: T[] }
|
|
42
|
+
| { readonly type: "request.failed"; readonly attempt: number }
|
|
43
|
+
| { readonly type: "request.timed-out"; readonly attempt: number }
|
|
44
|
+
| { readonly type: "disposed" }
|
|
45
|
+
|
|
46
|
+
/** Each effect first cancels the pending debounce or request, then starts its own work. */
|
|
47
|
+
export type TypeaheadEffect =
|
|
48
|
+
| { readonly type: "cancel" }
|
|
49
|
+
| { readonly type: "debounce"; readonly attempt: number; readonly delayMs: number }
|
|
50
|
+
| { readonly type: "request"; readonly attempt: number; readonly query: string }
|
|
51
|
+
|
|
52
|
+
export type TypeaheadTransition<T> = {
|
|
53
|
+
readonly state: TypeaheadState<T>
|
|
54
|
+
readonly effects: readonly TypeaheadEffect[]
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
type Live<T> = Exclude<TypeaheadState<T>, { status: "disposed" }>
|
|
58
|
+
|
|
59
|
+
const NONE: readonly TypeaheadEffect[] = Object.freeze([])
|
|
60
|
+
|
|
61
|
+
const CANCEL: readonly TypeaheadEffect[] = Object.freeze([Object.freeze({ type: "cancel" })])
|
|
62
|
+
|
|
63
|
+
const DISPOSED = Object.freeze({ status: "disposed" as const })
|
|
64
|
+
|
|
65
|
+
const EMPTY: never[] = []
|
|
66
|
+
|
|
67
|
+
export function createTypeaheadState<T>(initial: TypeaheadInitial<T> | undefined): TypeaheadTransition<T> {
|
|
68
|
+
const live = {
|
|
69
|
+
input: "",
|
|
70
|
+
searched: false,
|
|
71
|
+
answered: initial !== undefined,
|
|
72
|
+
initial,
|
|
73
|
+
cache: new Map<string, T[]>(),
|
|
74
|
+
shown: initial && !initial.failed ? initial.items : [],
|
|
75
|
+
stale: false,
|
|
76
|
+
issued: 0,
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (initial) return { state: { ...live, status: initial.failed ? "failed" : "ready" }, effects: NONE }
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
state: { ...live, issued: 1, status: "loading", attempt: 1 },
|
|
83
|
+
effects: [{ type: "request", attempt: 1, query: "" }],
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function transitionTypeahead<T>(state: TypeaheadState<T>, event: TypeaheadEvent<T>): TypeaheadTransition<T> {
|
|
88
|
+
if (state.status === "disposed") return { state, effects: NONE }
|
|
89
|
+
|
|
90
|
+
switch (event.type) {
|
|
91
|
+
case "disposed":
|
|
92
|
+
return { state: DISPOSED, effects: busy(state) ? CANCEL : NONE }
|
|
93
|
+
case "initial.synced":
|
|
94
|
+
return syncInitial(state, event.initial)
|
|
95
|
+
case "input.changed":
|
|
96
|
+
return changeInput(state, event)
|
|
97
|
+
case "debounce.elapsed":
|
|
98
|
+
if (state.status !== "debouncing" || state.attempt !== event.attempt) return { state, effects: NONE }
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
state: { ...state, status: "loading" },
|
|
102
|
+
effects: [{ type: "request", attempt: state.attempt, query: query(state) }],
|
|
103
|
+
}
|
|
104
|
+
case "retry.requested": {
|
|
105
|
+
if (state.status !== "failed") return { state, effects: NONE }
|
|
106
|
+
const attempt = state.issued + 1
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
state: { ...live(state), issued: attempt, status: "loading", attempt },
|
|
110
|
+
effects: [{ type: "request", attempt, query: query(state) }],
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
case "request.succeeded":
|
|
115
|
+
if (state.status !== "loading" || state.attempt !== event.attempt) return { state, effects: NONE }
|
|
116
|
+
|
|
117
|
+
return {
|
|
118
|
+
state: {
|
|
119
|
+
...live(state),
|
|
120
|
+
cache: new Map(state.cache).set(query(state), event.items),
|
|
121
|
+
shown: event.items,
|
|
122
|
+
stale: false,
|
|
123
|
+
answered: true,
|
|
124
|
+
status: "ready",
|
|
125
|
+
},
|
|
126
|
+
effects: CANCEL,
|
|
127
|
+
}
|
|
128
|
+
case "request.failed":
|
|
129
|
+
case "request.timed-out":
|
|
130
|
+
if (state.status !== "loading" || state.attempt !== event.attempt) return { state, effects: NONE }
|
|
131
|
+
|
|
132
|
+
return {
|
|
133
|
+
state: { ...live(state), shown: EMPTY, stale: false, answered: true, status: "failed" },
|
|
134
|
+
effects: CANCEL,
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function selectTypeaheadInput<T>(state: TypeaheadState<T>) {
|
|
140
|
+
return state.status === "disposed" ? "" : state.input
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function selectTypeaheadPending<T>(state: TypeaheadState<T>) {
|
|
144
|
+
return state.status === "debouncing" || state.status === "loading"
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function selectTypeaheadFailed<T>(state: TypeaheadState<T>) {
|
|
148
|
+
return state.status === "failed"
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function selectTypeaheadItems<T>(state: TypeaheadState<T>): T[] {
|
|
152
|
+
return state.status === "disposed" ? EMPTY : state.shown
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function selectTypeaheadStale<T>(state: TypeaheadState<T>) {
|
|
156
|
+
return state.status !== "disposed" && state.stale
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function selectTypeaheadAnimateHeight<T>(state: TypeaheadState<T>) {
|
|
160
|
+
return state.status !== "disposed" && state.searched && state.shown.length > 0
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function selectTypeaheadAnswered<T>(state: TypeaheadState<T>) {
|
|
164
|
+
return state.status !== "disposed" && state.answered
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// The parent owns the opening list, so a refresh reaches an open panel for the empty query; a typed query keeps its
|
|
168
|
+
// own results and picks the new list up when it is cleared.
|
|
169
|
+
function syncInitial<T>(state: Live<T>, initial: TypeaheadInitial<T> | undefined): TypeaheadTransition<T> {
|
|
170
|
+
if (initial === state.initial) return { state, effects: NONE }
|
|
171
|
+
|
|
172
|
+
// A request for the empty query is already on its way and answers for itself.
|
|
173
|
+
if (!initial || query(state) !== "" || state.status === "loading")
|
|
174
|
+
return { state: { ...state, initial }, effects: NONE }
|
|
175
|
+
const cache = new Map(state.cache)
|
|
176
|
+
cache.delete("")
|
|
177
|
+
|
|
178
|
+
return {
|
|
179
|
+
state: {
|
|
180
|
+
...live(state),
|
|
181
|
+
initial,
|
|
182
|
+
cache,
|
|
183
|
+
shown: initial.failed ? EMPTY : initial.items,
|
|
184
|
+
stale: false,
|
|
185
|
+
answered: true,
|
|
186
|
+
status: initial.failed ? "failed" : "ready",
|
|
187
|
+
},
|
|
188
|
+
effects: NONE,
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// A cached or empty query answers at once; a typed one waits for `delayMs`. Rows narrow while it is pending, and
|
|
193
|
+
// broadening waits for an answer so speculative rows never flash.
|
|
194
|
+
function changeInput<T>(
|
|
195
|
+
state: Live<T>,
|
|
196
|
+
event: Extract<TypeaheadEvent<T>, { type: "input.changed" }>,
|
|
197
|
+
): TypeaheadTransition<T> {
|
|
198
|
+
const next = event.input.trim()
|
|
199
|
+
|
|
200
|
+
if (next === query(state)) {
|
|
201
|
+
if (event.input === state.input && state.searched) return { state, effects: NONE }
|
|
202
|
+
|
|
203
|
+
return { state: { ...state, input: event.input, searched: true }, effects: NONE }
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const base = { ...live(state), input: event.input, searched: true }
|
|
207
|
+
const cancel = busy(state) ? CANCEL : NONE
|
|
208
|
+
const opening = next === "" ? state.initial : undefined
|
|
209
|
+
const cached = state.cache.get(next) ?? (opening && !opening.failed ? opening.items : undefined)
|
|
210
|
+
|
|
211
|
+
if (cached)
|
|
212
|
+
return { state: { ...base, shown: cached, stale: false, answered: true, status: "ready" }, effects: cancel }
|
|
213
|
+
|
|
214
|
+
if (opening?.failed)
|
|
215
|
+
return { state: { ...base, shown: EMPTY, stale: false, answered: true, status: "failed" }, effects: cancel }
|
|
216
|
+
const attempt = state.issued + 1
|
|
217
|
+
const narrowed = state.shown.filter((item) => event.match(item, next))
|
|
218
|
+
|
|
219
|
+
const rows =
|
|
220
|
+
narrowed.length > 0 ? { shown: narrowed, stale: false } : { shown: state.shown, stale: state.shown.length > 0 }
|
|
221
|
+
|
|
222
|
+
if (next === "" || event.delayMs <= 0)
|
|
223
|
+
return {
|
|
224
|
+
state: { ...base, ...rows, issued: attempt, status: "loading", attempt },
|
|
225
|
+
effects: [{ type: "request", attempt, query: next }],
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return {
|
|
229
|
+
state: { ...base, ...rows, issued: attempt, status: "debouncing", attempt },
|
|
230
|
+
effects: [{ type: "debounce", attempt, delayMs: event.delayMs }],
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function query<T>(state: Live<T>) {
|
|
235
|
+
return state.input.trim()
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function busy<T>(state: Live<T>) {
|
|
239
|
+
return state.status === "debouncing" || state.status === "loading"
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function live<T>(state: Live<T>): TypeaheadLive<T> {
|
|
243
|
+
return {
|
|
244
|
+
input: state.input,
|
|
245
|
+
searched: state.searched,
|
|
246
|
+
answered: state.answered,
|
|
247
|
+
initial: state.initial,
|
|
248
|
+
cache: state.cache,
|
|
249
|
+
shown: state.shown,
|
|
250
|
+
stale: state.stale,
|
|
251
|
+
issued: state.issued,
|
|
252
|
+
}
|
|
253
|
+
}
|
|
@@ -1,12 +1,25 @@
|
|
|
1
1
|
import { createResizeObserver } from "@solid-primitives/resize-observer"
|
|
2
|
-
import { createEffect,
|
|
3
|
-
import { createStore } from "solid-js/store"
|
|
2
|
+
import { createEffect, createSignal, For, type JSX, on, onCleanup, Show, untrack } from "solid-js"
|
|
4
3
|
import { Icon } from "@opencode/ui/icon"
|
|
5
4
|
import { abort } from "@opencode/util/abort"
|
|
6
5
|
import { useI18n } from "../../context/i18n"
|
|
7
6
|
import { Loader } from "../../feedback/loader/loader"
|
|
8
7
|
import { MenuScroll } from "../../components/menu-scroll"
|
|
9
8
|
import { createTypeahead } from "./controller"
|
|
9
|
+
import {
|
|
10
|
+
createTypeaheadState,
|
|
11
|
+
selectTypeaheadAnimateHeight,
|
|
12
|
+
selectTypeaheadAnswered,
|
|
13
|
+
selectTypeaheadFailed,
|
|
14
|
+
selectTypeaheadInput,
|
|
15
|
+
selectTypeaheadItems,
|
|
16
|
+
selectTypeaheadPending,
|
|
17
|
+
selectTypeaheadStale,
|
|
18
|
+
transitionTypeahead,
|
|
19
|
+
TYPEAHEAD_REQUEST_TIMEOUT_MS,
|
|
20
|
+
type TypeaheadEffect,
|
|
21
|
+
type TypeaheadEvent,
|
|
22
|
+
} from "./machine"
|
|
10
23
|
|
|
11
24
|
export { createTypeahead } from "./controller"
|
|
12
25
|
|
|
@@ -29,77 +42,72 @@ export function Typeahead<T>(props: {
|
|
|
29
42
|
children: (item: T) => JSX.Element
|
|
30
43
|
}) {
|
|
31
44
|
const i18n = useI18n()
|
|
32
|
-
|
|
33
|
-
const [
|
|
34
|
-
|
|
35
|
-
searched: boolean
|
|
36
|
-
answered?: string
|
|
37
|
-
failed?: boolean
|
|
38
|
-
loading?: boolean
|
|
39
|
-
}>({ query: "", searched: false, answered: props.initial ? "" : undefined })
|
|
40
|
-
|
|
41
|
-
const cache = new Map<string, T[]>()
|
|
45
|
+
const created = createTypeaheadState(props.initial)
|
|
46
|
+
const [machine, setMachine] = createSignal(created.state)
|
|
47
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
42
48
|
let request: AbortController | undefined
|
|
43
49
|
let input: HTMLInputElement | undefined
|
|
44
|
-
onCleanup(() => abort(request))
|
|
45
50
|
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
+
const dispatch = (event: TypeaheadEvent<T>) => {
|
|
52
|
+
const prev = untrack(machine)
|
|
53
|
+
const next = transitionTypeahead(prev, event)
|
|
54
|
+
|
|
55
|
+
if (next.state !== prev) setMachine(next.state)
|
|
56
|
+
next.effects.forEach(run)
|
|
57
|
+
}
|
|
51
58
|
|
|
52
|
-
const
|
|
59
|
+
const run = (effect: TypeaheadEffect) => {
|
|
60
|
+
clearTimeout(timer)
|
|
61
|
+
timer = undefined
|
|
53
62
|
abort(request)
|
|
63
|
+
request = undefined
|
|
64
|
+
|
|
65
|
+
if (effect.type === "cancel") return
|
|
66
|
+
const attempt = effect.attempt
|
|
67
|
+
|
|
68
|
+
if (effect.type === "debounce") {
|
|
69
|
+
timer = setTimeout(() => dispatch({ type: "debounce.elapsed", attempt }), effect.delayMs)
|
|
70
|
+
|
|
71
|
+
return
|
|
72
|
+
}
|
|
54
73
|
|
|
55
|
-
if (cache.has(query)) return setStore({ answered: query, failed: false, loading: false })
|
|
56
74
|
const current = (request = new AbortController())
|
|
57
|
-
|
|
58
|
-
|
|
75
|
+
// A source that ignores the signal or stalls must still end in a state the user can retry from.
|
|
76
|
+
timer = setTimeout(() => dispatch({ type: "request.timed-out", attempt }), TYPEAHEAD_REQUEST_TIMEOUT_MS)
|
|
77
|
+
props.source(effect.query, current.signal).then(
|
|
59
78
|
(items) => {
|
|
60
|
-
if (current.signal.aborted)
|
|
61
|
-
|
|
62
|
-
|
|
79
|
+
if (!current.signal.aborted) dispatch({ type: "request.succeeded", attempt, items })
|
|
80
|
+
},
|
|
81
|
+
() => {
|
|
82
|
+
if (!current.signal.aborted) dispatch({ type: "request.failed", attempt })
|
|
63
83
|
},
|
|
64
|
-
() => !current.signal.aborted && setStore({ answered: query, failed: true, loading: false }),
|
|
65
84
|
)
|
|
66
85
|
}
|
|
67
86
|
|
|
68
|
-
|
|
87
|
+
created.effects.forEach(run)
|
|
88
|
+
|
|
69
89
|
createEffect(
|
|
70
90
|
on(
|
|
71
|
-
|
|
72
|
-
(
|
|
73
|
-
|
|
74
|
-
abort(request)
|
|
75
|
-
const timer = setTimeout(() => search(next), props.delay)
|
|
76
|
-
onCleanup(() => clearTimeout(timer))
|
|
77
|
-
},
|
|
78
|
-
{ defer: !!props.initial },
|
|
91
|
+
() => props.initial,
|
|
92
|
+
(initial) => dispatch({ type: "initial.synced", initial }),
|
|
93
|
+
{ defer: true },
|
|
79
94
|
),
|
|
80
95
|
)
|
|
81
96
|
|
|
82
|
-
|
|
83
|
-
const results = createMemo<{ items: T[]; stale: boolean }>((previous) => {
|
|
84
|
-
const next = query()
|
|
85
|
-
const preloaded = opening()
|
|
86
|
-
|
|
87
|
-
if (preloaded) return { items: preloaded.failed ? [] : preloaded.items, stale: false }
|
|
97
|
+
onCleanup(() => dispatch({ type: "disposed" }))
|
|
88
98
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (cache.has(next) || cached.length > 0) return { items: cached, stale: false }
|
|
93
|
-
|
|
94
|
-
return { items: previous?.items ?? [], stale: !!previous?.items.length }
|
|
95
|
-
})
|
|
99
|
+
const changeInput = (value: string) =>
|
|
100
|
+
dispatch({ type: "input.changed", input: value, delayMs: props.delay, match: props.match })
|
|
96
101
|
|
|
97
|
-
const
|
|
102
|
+
const items = () => selectTypeaheadItems(machine())
|
|
103
|
+
const stale = () => selectTypeaheadStale(machine())
|
|
104
|
+
const pending = () => selectTypeaheadPending(machine())
|
|
105
|
+
const animateHeight = () => selectTypeaheadAnimateHeight(machine())
|
|
98
106
|
|
|
99
107
|
const typeahead = createTypeahead<T>({
|
|
100
|
-
items
|
|
108
|
+
items,
|
|
101
109
|
key: (item) => props.key(item),
|
|
102
|
-
stale
|
|
110
|
+
stale,
|
|
103
111
|
onSelect: (item) => props.onSelect(item),
|
|
104
112
|
})
|
|
105
113
|
|
|
@@ -131,21 +139,21 @@ export function Typeahead<T>(props: {
|
|
|
131
139
|
aria-activedescendant={typeahead.activeID()}
|
|
132
140
|
aria-label={props.placeholder}
|
|
133
141
|
placeholder={props.placeholder}
|
|
134
|
-
value={
|
|
142
|
+
value={selectTypeaheadInput(machine())}
|
|
135
143
|
autocomplete="off"
|
|
136
144
|
spellcheck={false}
|
|
137
145
|
class="h-8 min-w-0 flex-1 outline-none placeholder:text-v2-text-text-faint"
|
|
138
|
-
onInput={(event) =>
|
|
146
|
+
onInput={(event) => changeInput(event.currentTarget.value)}
|
|
139
147
|
onKeyDown={typeahead.onKeyDown}
|
|
140
148
|
/>
|
|
141
|
-
<Show when={
|
|
149
|
+
<Show when={selectTypeaheadInput(machine())}>
|
|
142
150
|
<button
|
|
143
151
|
type="button"
|
|
144
152
|
aria-label={i18n.t("ui.list.clearFilter")}
|
|
145
153
|
class="flex size-5 shrink-0 items-center justify-center rounded-sm text-v2-icon-icon-faint outline-none hover:text-v2-icon-icon-base focus-visible:text-v2-icon-icon-base"
|
|
146
154
|
onPointerDown={(event) => event.preventDefault()}
|
|
147
155
|
onClick={() => {
|
|
148
|
-
|
|
156
|
+
changeInput("")
|
|
149
157
|
input?.focus()
|
|
150
158
|
}}
|
|
151
159
|
>
|
|
@@ -171,9 +179,9 @@ export function Typeahead<T>(props: {
|
|
|
171
179
|
role="listbox"
|
|
172
180
|
class="max-h-56"
|
|
173
181
|
viewportClass="p-1"
|
|
174
|
-
classList={{ hidden:
|
|
182
|
+
classList={{ hidden: items().length === 0 }}
|
|
175
183
|
>
|
|
176
|
-
<For each={
|
|
184
|
+
<For each={items()}>
|
|
177
185
|
{(item) => (
|
|
178
186
|
<div
|
|
179
187
|
id={typeahead.optionID(item)}
|
|
@@ -181,7 +189,7 @@ export function Typeahead<T>(props: {
|
|
|
181
189
|
aria-selected={typeahead.activeItem() === item}
|
|
182
190
|
aria-current={props.selected === props.key(item) ? "true" : undefined}
|
|
183
191
|
class="flex h-7 min-w-0 cursor-default select-none items-center gap-2 rounded-sm px-2 transition-opacity duration-150 aria-selected:bg-v2-overlay-simple-overlay-hover"
|
|
184
|
-
classList={{ "opacity-50":
|
|
192
|
+
classList={{ "opacity-50": stale() }}
|
|
185
193
|
onMouseMove={(event) => {
|
|
186
194
|
// A static cursor must not steal the active row while the keyboard scrolls the list.
|
|
187
195
|
if (event.movementX !== 0 || event.movementY !== 0) typeahead.setActive(props.key(item))
|
|
@@ -198,17 +206,17 @@ export function Typeahead<T>(props: {
|
|
|
198
206
|
</For>
|
|
199
207
|
</MenuScroll>
|
|
200
208
|
{/* Stays mounted while a retry or a newer query is pending, so the list never collapses to nothing. */}
|
|
201
|
-
<Show when={
|
|
209
|
+
<Show when={items().length === 0}>
|
|
202
210
|
<div
|
|
203
211
|
class="mx-1 flex h-9 items-center px-2 py-1 text-v2-text-text-faint"
|
|
204
|
-
classList={{ invisible:
|
|
212
|
+
classList={{ invisible: !selectTypeaheadAnswered(machine()) }}
|
|
205
213
|
>
|
|
206
|
-
<Show when={
|
|
214
|
+
<Show when={selectTypeaheadFailed(machine())} fallback={props.emptyLabel}>
|
|
207
215
|
<button
|
|
208
216
|
type="button"
|
|
209
217
|
class="-mx-2 flex h-7 flex-1 items-center rounded-sm px-2 text-start text-v2-text-text-base outline-none hover:bg-v2-overlay-simple-overlay-hover focus-visible:bg-v2-overlay-simple-overlay-hover"
|
|
210
218
|
onMouseDown={(event) => event.preventDefault()}
|
|
211
|
-
onClick={() =>
|
|
219
|
+
onClick={() => dispatch({ type: "retry.requested" })}
|
|
212
220
|
>
|
|
213
221
|
{props.retryLabel}
|
|
214
222
|
</button>
|