@opencode/ui 0.0.0-dev-800 → 0.0.0-dev-823

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.
@@ -0,0 +1,159 @@
1
+ import { createMemo, createSignal, onCleanup, onMount, untrack } from "solid-js"
2
+ import {
3
+ selectMcpSignInView,
4
+ startMcpSignIn,
5
+ transitionMcpSignIn,
6
+ type McpSignInAttemptStatus,
7
+ type McpSignInEffect,
8
+ type McpSignInEvent,
9
+ } from "./machine"
10
+
11
+ export type { McpSignInAttemptStatus, McpSignInFailure, McpSignInView } from "./machine"
12
+
13
+ /** The server and host calls of one MCP sign-in; the app and the details plugin bind them to their own SDK. */
14
+ export type McpSignInHost = {
15
+ /** The authorization URL the browser opens. */
16
+ url: string
17
+ /** The attempt's lifetime in milliseconds, from the server's `time`; the two clocks may disagree. */
18
+ lifetime: number
19
+ /** Polls the attempt's status. */
20
+ status: (signal: AbortSignal) => Promise<McpSignInAttemptStatus>
21
+ /** Sends a redirect URL pasted from the browser. */
22
+ complete: (code: string, signal: AbortSignal) => Promise<void>
23
+ /** Ends the attempt on the server. */
24
+ cancel: () => Promise<void>
25
+ /** Reloads the MCP server and resource lists after the sign-in succeeded. */
26
+ sync: () => Promise<void>
27
+ openExternal: (url: string) => void
28
+ /** Closes the dialog once the lists reloaded. */
29
+ close: () => void
30
+ /** The text shown for a failed request. The SDK and the data layer reject with an `Error`. */
31
+ message: (error: Error) => string
32
+ }
33
+
34
+ /**
35
+ * Drives an MCP sign-in dialog's machine for the owner's lifetime: polls the attempt, sends a pasted redirect URL,
36
+ * reloads the MCP lists once it succeeded, and cancels an attempt still open when the owner is disposed.
37
+ */
38
+ export function createMcpSignIn(host: McpSignInHost) {
39
+ const initial = startMcpSignIn({ url: host.url, lifetimeMs: host.lifetime })
40
+ const [signInState, setSignInState] = createSignal(initial.state)
41
+ const view = createMemo(() => selectMcpSignInView(signInState()))
42
+ // The machine decides when each of these starts and stops; the driver only holds them.
43
+ let deadlineTimer: ReturnType<typeof setTimeout> | undefined
44
+ let pollTimer: ReturnType<typeof setTimeout> | undefined
45
+ let syncTimer: ReturnType<typeof setTimeout> | undefined
46
+ let pollController: AbortController | undefined
47
+ let submitController: AbortController | undefined
48
+
49
+ const describe = (error: Error | string) => host.message(error instanceof Error ? error : new Error(error))
50
+
51
+ const dispatch = (event: McpSignInEvent) => {
52
+ const previous = untrack(signInState)
53
+ const next = transitionMcpSignIn(previous, event)
54
+
55
+ if (next.state !== previous) setSignInState(next.state)
56
+ next.effects.forEach((effect) => void Promise.try(() => run(effect)).catch((cause) => console.error(cause)))
57
+ }
58
+
59
+ const run = (effect: McpSignInEffect) => {
60
+ if (effect.type === "deadline.start") {
61
+ clearTimeout(deadlineTimer)
62
+ deadlineTimer = setTimeout(() => dispatch({ type: "waiting.timed-out" }), effect.delayMs)
63
+
64
+ return
65
+ }
66
+
67
+ if (effect.type === "deadline.clear") return clearTimeout(deadlineTimer)
68
+
69
+ if (effect.type === "poll") {
70
+ abortPoll()
71
+
72
+ if (effect.delayMs <= 0) return poll(effect.pollAttempt)
73
+ pollTimer = setTimeout(() => poll(effect.pollAttempt), effect.delayMs)
74
+
75
+ return
76
+ }
77
+
78
+ if (effect.type === "poll.abort") return abortPoll()
79
+
80
+ if (effect.type === "submit") {
81
+ submitController?.abort()
82
+ const controller = new AbortController()
83
+ submitController = controller
84
+ void Promise.try(() => host.complete(effect.code, controller.signal)).then(
85
+ () => {
86
+ if (!controller.signal.aborted) dispatch({ type: "submit.succeeded", submitAttempt: effect.submitAttempt })
87
+ },
88
+ (error) => {
89
+ if (controller.signal.aborted) return
90
+ dispatch({ type: "submit.failed", submitAttempt: effect.submitAttempt, message: describe(error) })
91
+ },
92
+ )
93
+
94
+ return
95
+ }
96
+
97
+ if (effect.type === "submit.abort") {
98
+ submitController?.abort()
99
+ submitController = undefined
100
+
101
+ return
102
+ }
103
+
104
+ if (effect.type === "sync") {
105
+ clearTimeout(syncTimer)
106
+ syncTimer = setTimeout(
107
+ () => dispatch({ type: "sync.timed-out", syncAttempt: effect.syncAttempt }),
108
+ effect.timeoutMs,
109
+ )
110
+ void Promise.try(host.sync).then(
111
+ () => dispatch({ type: "sync.succeeded", syncAttempt: effect.syncAttempt }),
112
+ (error) => dispatch({ type: "sync.failed", syncAttempt: effect.syncAttempt, message: describe(error) }),
113
+ )
114
+
115
+ return
116
+ }
117
+
118
+ if (effect.type === "sync.clear") return clearTimeout(syncTimer)
119
+
120
+ if (effect.type === "refresh") return void Promise.try(host.sync).catch(() => undefined)
121
+
122
+ if (effect.type === "open-external") return host.openExternal(effect.url)
123
+
124
+ if (effect.type === "cancel") return void Promise.try(host.cancel).catch(() => undefined)
125
+
126
+ host.close()
127
+ }
128
+
129
+ const poll = (pollAttempt: number) => {
130
+ const controller = new AbortController()
131
+ pollController = controller
132
+ void Promise.try(() => host.status(controller.signal)).then(
133
+ (status) => {
134
+ if (controller.signal.aborted) return
135
+ dispatch({
136
+ type: "polled",
137
+ pollAttempt,
138
+ outcome:
139
+ status.status === "failed" ? { status: "failed", message: status.message } : { status: status.status },
140
+ })
141
+ },
142
+ (error) => {
143
+ if (controller.signal.aborted) return
144
+ dispatch({ type: "polled", pollAttempt, outcome: { status: "unreachable", message: describe(error) } })
145
+ },
146
+ )
147
+ }
148
+
149
+ const abortPoll = () => {
150
+ clearTimeout(pollTimer)
151
+ pollController?.abort()
152
+ pollController = undefined
153
+ }
154
+
155
+ onMount(() => initial.effects.forEach(run))
156
+ onCleanup(() => dispatch({ type: "disposed" }))
157
+
158
+ return { view, dispatch }
159
+ }
@@ -0,0 +1,90 @@
1
+ /** Matches the opacity crossfade in `text-shimmer.css`; the sweep keeps running until the shimmer layer has faded. */
2
+ export const TEXT_SHIMMER_SWAP_MS = 220
3
+
4
+ /**
5
+ * `active` shows the shimmer layer and runs its sweep. `fading` has already crossfaded back to the plain text and only
6
+ * keeps the sweep running until the fade ends, so the layer never freezes mid-fade. `inactive` runs nothing.
7
+ */
8
+ export type TextShimmerState =
9
+ | { readonly status: "inactive"; readonly attempts: number }
10
+ | { readonly status: "active"; readonly attempts: number }
11
+ | { readonly status: "fading"; readonly attempt: number }
12
+ | { readonly status: "disposed" }
13
+
14
+ export type TextShimmerEvent =
15
+ | { readonly type: "sync"; readonly active: boolean; readonly reducedMotion: boolean }
16
+ | { readonly type: "settled"; readonly attempt: number }
17
+ | { readonly type: "disposed" }
18
+
19
+ // Every timeline row mounts a shimmer, so mounting, syncing and disposing reuse these instead of allocating.
20
+ export const INITIAL_INACTIVE_TEXT_SHIMMER_STATE: TextShimmerState = Object.freeze({ status: "inactive", attempts: 0 })
21
+
22
+ export const INITIAL_ACTIVE_TEXT_SHIMMER_STATE: TextShimmerState = Object.freeze({ status: "active", attempts: 0 })
23
+
24
+ export const DISPOSED_TEXT_SHIMMER_STATE: TextShimmerState = Object.freeze({ status: "disposed" })
25
+
26
+ export const DISPOSED_TEXT_SHIMMER_EVENT: TextShimmerEvent = Object.freeze({ type: "disposed" })
27
+
28
+ const SYNC_ACTIVE: TextShimmerEvent = Object.freeze({ type: "sync", active: true, reducedMotion: false })
29
+
30
+ const SYNC_ACTIVE_REDUCED: TextShimmerEvent = Object.freeze({ type: "sync", active: true, reducedMotion: true })
31
+
32
+ const SYNC_INACTIVE: TextShimmerEvent = Object.freeze({ type: "sync", active: false, reducedMotion: false })
33
+
34
+ const SYNC_INACTIVE_REDUCED: TextShimmerEvent = Object.freeze({ type: "sync", active: false, reducedMotion: true })
35
+
36
+ export function syncTextShimmerEvent(active: boolean, reducedMotion: boolean): TextShimmerEvent {
37
+ if (active) return reducedMotion ? SYNC_ACTIVE_REDUCED : SYNC_ACTIVE
38
+
39
+ return reducedMotion ? SYNC_INACTIVE_REDUCED : SYNC_INACTIVE
40
+ }
41
+
42
+ export function createTextShimmerState(active: boolean): TextShimmerState {
43
+ return active ? INITIAL_ACTIVE_TEXT_SHIMMER_STATE : INITIAL_INACTIVE_TEXT_SHIMMER_STATE
44
+ }
45
+
46
+ export function transitionTextShimmer(state: TextShimmerState, event: TextShimmerEvent): TextShimmerState {
47
+ if (state.status === "disposed") return state
48
+
49
+ if (event.type === "disposed") return DISPOSED_TEXT_SHIMMER_STATE
50
+
51
+ if (event.type === "settled") {
52
+ if (state.status !== "fading" || event.attempt !== state.attempt) return state
53
+
54
+ return { status: "inactive", attempts: state.attempt }
55
+ }
56
+
57
+ if (state.status === "inactive") {
58
+ if (!event.active) return state
59
+
60
+ return state.attempts === 0 ? INITIAL_ACTIVE_TEXT_SHIMMER_STATE : { status: "active", attempts: state.attempts }
61
+ }
62
+
63
+ if (state.status === "active") {
64
+ if (event.active) return state
65
+
66
+ // Reduced motion turns the crossfade off, so there is nothing to wait for.
67
+ if (event.reducedMotion)
68
+ return state.attempts === 0
69
+ ? INITIAL_INACTIVE_TEXT_SHIMMER_STATE
70
+ : { status: "inactive", attempts: state.attempts }
71
+
72
+ return { status: "fading", attempt: state.attempts + 1 }
73
+ }
74
+
75
+ if (event.active) return { status: "active", attempts: state.attempt }
76
+
77
+ if (event.reducedMotion) return { status: "inactive", attempts: state.attempt }
78
+
79
+ return state
80
+ }
81
+
82
+ /** Whether the shimmer layer is shown; drives the opacity crossfade. */
83
+ export function selectTextShimmerActive(state: TextShimmerState) {
84
+ return state.status === "active"
85
+ }
86
+
87
+ /** Whether the sweep animation runs; it outlives `active` by the crossfade. */
88
+ export function selectTextShimmerRunning(state: TextShimmerState) {
89
+ return state.status === "active" || state.status === "fading"
90
+ }
@@ -1,5 +1,16 @@
1
- import { createEffect, createMemo, createSignal, onCleanup, type ValidComponent } from "solid-js"
1
+ import { createEffect, createMemo, createSignal, on, onCleanup, untrack, type ValidComponent } from "solid-js"
2
2
  import { Dynamic } from "solid-js/web"
3
+ import { prefersReducedMotion } from "../../motion/reduced"
4
+ import {
5
+ createTextShimmerState,
6
+ DISPOSED_TEXT_SHIMMER_EVENT,
7
+ selectTextShimmerActive,
8
+ selectTextShimmerRunning,
9
+ syncTextShimmerEvent,
10
+ TEXT_SHIMMER_SWAP_MS,
11
+ transitionTextShimmer,
12
+ type TextShimmerEvent,
13
+ } from "./machine"
3
14
  import "./text-shimmer.css"
4
15
 
5
16
  export const TextShimmer = <T extends ValidComponent = "span">(props: {
@@ -11,51 +22,54 @@ export const TextShimmer = <T extends ValidComponent = "span">(props: {
11
22
  }) => {
12
23
  const text = createMemo(() => props.text ?? "")
13
24
  const active = createMemo(() => props.active ?? true)
14
- const offset = createMemo(() => props.offset ?? 0)
15
- const [run, setRun] = createSignal(active())
16
- const swap = 220
25
+ const [state, setState] = createSignal(createTextShimmerState(untrack(active)))
17
26
  let timer: ReturnType<typeof setTimeout> | undefined
18
27
 
19
- createEffect(() => {
20
- if (timer) {
21
- clearTimeout(timer)
22
- timer = undefined
23
- }
28
+ const dispatch = (event: TextShimmerEvent) => {
29
+ const previous = untrack(state)
30
+ const next = transitionTextShimmer(previous, event)
24
31
 
25
- if (active()) {
26
- setRun(true)
32
+ if (next === previous) return
33
+ clearTimeout(timer)
34
+ timer = undefined
27
35
 
28
- return
29
- }
36
+ if (next.status === "disposed") return
37
+ setState(next)
30
38
 
39
+ if (next.status !== "fading") return
40
+ const attempt = next.attempt
31
41
  timer = setTimeout(() => {
32
42
  timer = undefined
33
- setRun(false)
34
- }, swap)
35
- })
43
+ dispatch({ type: "settled", attempt })
44
+ }, TEXT_SHIMMER_SWAP_MS)
45
+ }
36
46
 
37
- onCleanup(() => {
38
- if (!timer) return
39
- clearTimeout(timer)
40
- })
47
+ // The first state comes from the initial prop, so a mount dispatches nothing and arms no timer.
48
+ createEffect(on(active, (value) => dispatch(syncTextShimmerEvent(value, prefersReducedMotion())), { defer: true }))
49
+
50
+ onCleanup(() => dispatch(DISPOSED_TEXT_SHIMMER_EVENT))
41
51
 
42
52
  return (
43
53
  <Dynamic
44
54
  component={props.as ?? "span"}
45
55
  data-component="text-shimmer"
46
- data-active={active() ? "true" : "false"}
56
+ data-active={selectTextShimmerActive(state()) ? "true" : "false"}
47
57
  class={props.class}
48
58
  aria-label={text()}
49
59
  style={{
50
- "--text-shimmer-swap": `${swap}ms`,
51
- "--text-shimmer-index": `${offset()}`,
60
+ "--text-shimmer-swap": `${TEXT_SHIMMER_SWAP_MS}ms`,
61
+ "--text-shimmer-index": `${props.offset ?? 0}`,
52
62
  }}
53
63
  >
54
64
  <span data-slot="text-shimmer-char">
55
65
  <span data-slot="text-shimmer-char-base" aria-hidden="true">
56
66
  {text()}
57
67
  </span>
58
- <span data-slot="text-shimmer-char-shimmer" data-run={run() ? "true" : "false"} aria-hidden="true">
68
+ <span
69
+ data-slot="text-shimmer-char-shimmer"
70
+ data-run={selectTextShimmerRunning(state()) ? "true" : "false"}
71
+ aria-hidden="true"
72
+ >
59
73
  {text()}
60
74
  <span data-slot="text-shimmer-sweep">
61
75
  <span data-slot="text-shimmer-sweep-text">{text()}</span>