* Add remove in-use toasts * Use effect-atom optimistic resources
Unified · Split
| @@ -3,7 +3,7 @@ import { useEffect, useRef, useState } from "react"; · 2 unmodified lines | |||
| 3 | import { useAtomValue, useAtomSet } from "@effect/atom-react"; | 3 | import { useAtomValue, useAtomSet } from "@effect/atom-react"; |
| 4 | import * as AsyncResult from "effect/unstable/reactivity/AsyncResult"; | 4 | import * as AsyncResult from "effect/unstable/reactivity/AsyncResult"; |
| 5 | import * as Exit from "effect/Exit"; | 5 | import * as Exit from "effect/Exit"; |
| 6 | import { useSourcesWithPending } from "@executor-js/react/api/optimistic"; | 6 | import { sourcesOptimisticAtom } from "@executor-js/react/api/atoms"; |
| 7 | import { useScope } from "@executor-js/react/api/scope-context"; | 7 | import { useScope } from "@executor-js/react/api/scope-context"; |
| 8 | import { Button } from "@executor-js/react/components/button"; | 8 | import { Button } from "@executor-js/react/components/button"; |
| 9 | import { Skeleton } from "@executor-js/react/components/skeleton"; | 9 | import { Skeleton } from "@executor-js/react/components/skeleton"; |
| @@ -60,7 +60,7 @@ function NavItem(props: { to: string; label: string; active: boolean; onNavigate · 50 unmodified lines | |||
| 60 | 60 | ||
| 61 | function SourceList(props: { pathname: string; onNavigate?: () => void }) { | 61 | function SourceList(props: { pathname: string; onNavigate?: () => void }) { |
| 62 | const scopeId = useScope(); | 62 | const scopeId = useScope(); |
| 63 | const sources = useSourcesWithPending(scopeId); | 63 | const sources = useAtomValue(sourcesOptimisticAtom(scopeId)); |
| 64 | 64 | ||
| 65 | return AsyncResult.match(sources, { | 65 | return AsyncResult.match(sources, { |
| 66 | onInitial: () => ( | 66 | onInitial: () => ( |
| @@ -1,9 +1,8 @@ | |||
| 1 | import { Link, Outlet, useLocation } from "@tanstack/react-router"; | 1 | import { Link, Outlet, useLocation } from "@tanstack/react-router"; |
| 2 | import { useCallback, useEffect, useRef, useState } from "react"; | 2 | import { useCallback, useEffect, useRef, useState } from "react"; |
| 3 | import { useAtomRefresh } from "@effect/atom-react"; | 3 | import { useAtomRefresh, useAtomValue } from "@effect/atom-react"; |
| 4 | import * as AsyncResult from "effect/unstable/reactivity/AsyncResult"; | 4 | import * as AsyncResult from "effect/unstable/reactivity/AsyncResult"; |
| 5 | import { sourcesAtom, toolsAtom } from "@executor-js/react/api/atoms"; | 5 | import { sourcesAtom, sourcesOptimisticAtom, toolsAtom } from "@executor-js/react/api/atoms"; |
| 6 | import { useSourcesWithPending } from "@executor-js/react/api/optimistic"; | ||
| 7 | import { useScope, useScopeInfo } from "@executor-js/react/api/scope-context"; | 6 | import { useScope, useScopeInfo } from "@executor-js/react/api/scope-context"; |
| 8 | import { Button } from "@executor-js/react/components/button"; | 7 | import { Button } from "@executor-js/react/components/button"; |
| 9 | import { SourceFavicon } from "@executor-js/react/components/source-favicon"; | 8 | import { SourceFavicon } from "@executor-js/react/components/source-favicon"; |
| @@ -261,7 +260,7 @@ function PluginNav(props: { pathname: string; onNavigate?: () => void }) { · 251 unmodified lines | |||
| 261 | 260 | ||
| 262 | function SourceList(props: { pathname: string; onNavigate?: () => void }) { | 261 | function SourceList(props: { pathname: string; onNavigate?: () => void }) { |
| 263 | const scopeId = useScope(); | 262 | const scopeId = useScope(); |
| 264 | const sources = useSourcesWithPending(scopeId); | 263 | const sources = useAtomValue(sourcesOptimisticAtom(scopeId)); |
| 265 | 264 | ||
| 266 | return AsyncResult.match(sources, { | 265 | return AsyncResult.match(sources, { |
| 267 | onInitial: () => <div className="px-2.5 py-2 text-xs text-muted-foreground">Loading…</div>, | 266 | onInitial: () => <div className="px-2.5 py-2 text-xs text-muted-foreground">Loading…</div>, |
| @@ -1,7 +1,8 @@ | |||
| 1 | import { useCallback, useEffect, useMemo, useRef, useState } from "react"; | 1 | import { useCallback, useEffect, useMemo, useRef, useState } from "react"; |
| 2 | import { useAtomSet } from "@effect/atom-react"; | 2 | import { useAtomSet } from "@effect/atom-react"; |
| 3 | import * as Exit from "effect/Exit"; | ||
| 4 | import * as Option from "effect/Option"; | ||
| 3 | 5 | ||
| 4 | import { usePendingSources } from "@executor-js/react/api/optimistic"; | ||
| 5 | import { sourceWriteKeys } from "@executor-js/react/api/reactivity-keys"; | 6 | import { sourceWriteKeys } from "@executor-js/react/api/reactivity-keys"; |
| 6 | import { useScope } from "@executor-js/react/api/scope-context"; | 7 | import { useScope } from "@executor-js/react/api/scope-context"; |
| 7 | import type { SecretPickerSecret } from "@executor-js/react/plugins/secret-picker"; | 8 | import type { SecretPickerSecret } from "@executor-js/react/plugins/secret-picker"; |
| @@ -44,7 +45,7 @@ import { FloatActions } from "@executor-js/react/components/float-actions"; · 36 unmodified lines | |||
| 44 | import { Input } from "@executor-js/react/components/input"; | 45 | import { Input } from "@executor-js/react/components/input"; |
| 45 | import { RadioGroup, RadioGroupItem } from "@executor-js/react/components/radio-group"; | 46 | import { RadioGroup, RadioGroupItem } from "@executor-js/react/components/radio-group"; |
| 46 | import { IOSSpinner, Spinner } from "@executor-js/react/components/spinner"; | 47 | import { IOSSpinner, Spinner } from "@executor-js/react/components/spinner"; |
| 47 | import { addGoogleDiscoverySource, probeGoogleDiscovery } from "./atoms"; | 48 | import { addGoogleDiscoverySourceOptimistic, probeGoogleDiscovery } from "./atoms"; |
| 48 | import { GOOGLE_DISCOVERY_OAUTH_POPUP_NAME, googleDiscoveryOAuthStrategy } from "./oauth"; | 49 | import { GOOGLE_DISCOVERY_OAUTH_POPUP_NAME, googleDiscoveryOAuthStrategy } from "./oauth"; |
| 49 | import { googleDiscoveryPresets, type GoogleDiscoveryPreset } from "../sdk/presets"; | 50 | import { googleDiscoveryPresets, type GoogleDiscoveryPreset } from "../sdk/presets"; |
| 50 | 51 | ||
| @@ -203,8 +204,9 @@ export default function AddGoogleDiscoverySource(props: { · 152 unmodified lines | |||
| 203 | 204 | ||
| 204 | const scopeId = useScope(); | 205 | const scopeId = useScope(); |
| 205 | const doProbe = useAtomSet(probeGoogleDiscovery, { mode: "promise" }); | 206 | const doProbe = useAtomSet(probeGoogleDiscovery, { mode: "promise" }); |
| 206 | const doAdd = useAtomSet(addGoogleDiscoverySource, { mode: "promise" }); | 207 | const doAdd = useAtomSet(addGoogleDiscoverySourceOptimistic(scopeId), { |
| 207 | const { beginAdd } = usePendingSources(); | 208 | mode: "promiseExit", |
| 209 | }); | ||
| 208 | const secretList = useSecretPickerSecrets(); | 210 | const secretList = useSecretPickerSecrets(); |
| 209 | const oauth = useOAuthPopupFlow({ | 211 | const oauth = useOAuthPopupFlow({ |
| 210 | popupName: GOOGLE_DISCOVERY_OAUTH_POPUP_NAME, | 212 | popupName: GOOGLE_DISCOVERY_OAUTH_POPUP_NAME, |
| @@ -326,38 +328,36 @@ export default function AddGoogleDiscoverySource(props: { · 115 unmodified lines | |||
| 326 | setError(null); | 328 | setError(null); |
| 327 | const displayName = identity.name.trim() || probe.name; | 329 | const displayName = identity.name.trim() || probe.name; |
| 328 | const namespace = resolvedNamespace; | 330 | const namespace = resolvedNamespace; |
| 329 | const placeholder = beginAdd({ | 331 | const exit = await doAdd({ |
| 330 | id: namespace, | 332 | params: { scopeId }, |
| 331 | name: displayName, | 333 | payload: { |
| 332 | kind: "google-discovery", | 334 | name: displayName, |
| 335 | discoveryUrl: discoveryUrl.trim(), | ||
| 336 | namespace, | ||
| 337 | auth: | ||
| 338 | authKind === "oauth2" && oauthAuth | ||
| 339 | ? { | ||
| 340 | kind: "oauth2" as const, | ||
| 341 | connectionId: oauthAuth.connectionId, | ||
| 342 | clientIdSecretId: oauthAuth.clientIdSecretId, | ||
| 343 | clientSecretSecretId: oauthAuth.clientSecretSecretId, | ||
| 344 | scopes: oauthAuth.scopes, | ||
| 345 | } | ||
| 346 | : { kind: "none" as const }, | ||
| 347 | }, | ||
| 348 | reactivityKeys: [...sourceWriteKeys], | ||
| 333 | }); | 349 | }); |
| 334 | try { | 350 | if (Exit.isFailure(exit)) { |
| 335 | await doAdd({ | 351 | const error = Exit.findErrorOption(exit); |
| 336 | params: { scopeId }, | 352 | setError( |
| 337 | payload: { | 353 | Option.isSome(error) && error.value instanceof Error |
| 338 | name: displayName, | 354 | ? error.value.message |
| 339 | discoveryUrl: discoveryUrl.trim(), | 355 | : "Failed to add source", |
| 340 | namespace, | 356 | ); |
| 341 | auth: | ||
| 342 | authKind === "oauth2" && oauthAuth | ||
| 343 | ? { | ||
| 344 | kind: "oauth2" as const, | ||
| 345 | connectionId: oauthAuth.connectionId, | ||
| 346 | clientIdSecretId: oauthAuth.clientIdSecretId, | ||
| 347 | clientSecretSecretId: oauthAuth.clientSecretSecretId, | ||
| 348 | scopes: oauthAuth.scopes, | ||
| 349 | } | ||
| 350 | : { kind: "none" as const }, | ||
| 351 | }, | ||
| 352 | reactivityKeys: [...sourceWriteKeys], | ||
| 353 | }); | ||
| 354 | props.onComplete(); | ||
| 355 | } catch (e) { | ||
| 356 | setError(e instanceof Error ? e.message : "Failed to add source"); | ||
| 357 | setAdding(false); | 357 | setAdding(false); |
| 358 | } finally { | 358 | return; |
| 359 | placeholder.done(); | ||
| 360 | } | 359 | } |
| 360 | props.onComplete(); | ||
| 361 | }, [ | 361 | }, [ |
| 362 | probe, | 362 | probe, |
| 363 | doAdd, | 363 | doAdd, |
| @@ -367,7 +367,6 @@ export default function AddGoogleDiscoverySource(props: { · 3 unmodified lines | |||
| 367 | oauthAuth, | 367 | oauthAuth, |
| 368 | props, | 368 | props, |
| 369 | scopeId, | 369 | scopeId, |
| 370 | beginAdd, | ||
| 371 | resolvedNamespace, | 370 | resolvedNamespace, |
| 372 | ]); | 371 | ]); |
| 373 | 372 | ||
| @@ -1,4 +1,7 @@ | |||
| 1 | import type { ScopeId } from "@executor-js/sdk/core"; | 1 | import type { ScopeId } from "@executor-js/sdk/core"; |
| 2 | import * as Atom from "effect/unstable/reactivity/Atom"; | ||
| 3 | import * as AsyncResult from "effect/unstable/reactivity/AsyncResult"; | ||
| 4 | import { sourcesOptimisticAtom } from "@executor-js/react/api/atoms"; | ||
| 2 | import { ReactivityKey } from "@executor-js/react/api/reactivity-keys"; | 5 | import { ReactivityKey } from "@executor-js/react/api/reactivity-keys"; |
| 3 | import { GoogleDiscoveryClient } from "./client"; | 6 | import { GoogleDiscoveryClient } from "./client"; |
| 4 | 7 | ||
| @@ -25,6 +28,32 @@ export const addGoogleDiscoverySource = GoogleDiscoveryClient.mutation( · 20 unmodified lines | |||
| 25 | "googleDiscovery", | 28 | "googleDiscovery", |
| 26 | "addSource", | 29 | "addSource", |
| 27 | ); | 30 | ); |
| 31 | export const addGoogleDiscoverySourceOptimistic = Atom.family((scopeId: ScopeId) => | ||
| 32 | sourcesOptimisticAtom(scopeId).pipe( | ||
| 33 | Atom.optimisticFn({ | ||
| 34 | reducer: (current, arg) => | ||
| 35 | AsyncResult.map(current, (rows) => { | ||
| 36 | const id = arg.payload.namespace ?? `pending-${Math.random().toString(36).slice(2)}`; | ||
| 37 | const source = { | ||
| 38 | id, | ||
| 39 | scopeId, | ||
| 40 | kind: "googleDiscovery", | ||
| 41 | pluginId: "google-discovery", | ||
| 42 | name: arg.payload.name, | ||
| 43 | url: arg.payload.discoveryUrl, | ||
| 44 | canRemove: false, | ||
| 45 | canRefresh: false, | ||
| 46 | canEdit: false, | ||
| 47 | runtime: false, | ||
| 48 | }; | ||
| 49 | return [source, ...rows.filter((row) => row.id !== id)].sort((a, b) => | ||
| 50 | a.name.localeCompare(b.name), | ||
| 51 | ); | ||
| 52 | }), | ||
| 53 | fn: addGoogleDiscoverySource, | ||
| 54 | }), | ||
| 55 | ), | ||
| 56 | ); | ||
| 28 | export const updateGoogleDiscoverySource = GoogleDiscoveryClient.mutation( | 57 | export const updateGoogleDiscoverySource = GoogleDiscoveryClient.mutation( |
| 29 | "googleDiscovery", | 58 | "googleDiscovery", |
| 30 | "updateSource", | 59 | "updateSource", |
| @@ -1,9 +1,10 @@ | |||
| 1 | import { useCallback, useState } from "react"; | 1 | import { useCallback, useState } from "react"; |
| 2 | import { useAtomSet } from "@effect/atom-react"; | 2 | import { useAtomSet } from "@effect/atom-react"; |
| 3 | import * as Exit from "effect/Exit"; | ||
| 4 | import * as Option from "effect/Option"; | ||
| 3 | 5 | ||
| 4 | import { useScope } from "@executor-js/react/api/scope-context"; | 6 | import { useScope } from "@executor-js/react/api/scope-context"; |
| 5 | import { sourceWriteKeys } from "@executor-js/react/api/reactivity-keys"; | 7 | import { sourceWriteKeys } from "@executor-js/react/api/reactivity-keys"; |
| 6 | import { usePendingSources } from "@executor-js/react/api/optimistic"; | ||
| 7 | import { | 8 | import { |
| 8 | HttpCredentialsEditor, | 9 | HttpCredentialsEditor, |
| 9 | httpCredentialsValid, | 10 | httpCredentialsValid, |
| @@ -33,7 +34,7 @@ import { · 23 unmodified lines | |||
| 33 | import { FloatActions } from "@executor-js/react/components/float-actions"; | 34 | import { FloatActions } from "@executor-js/react/components/float-actions"; |
| 34 | import { Input } from "@executor-js/react/components/input"; | 35 | import { Input } from "@executor-js/react/components/input"; |
| 35 | import { Spinner } from "@executor-js/react/components/spinner"; | 36 | import { Spinner } from "@executor-js/react/components/spinner"; |
| 36 | import { addGraphqlSource } from "./atoms"; | 37 | import { addGraphqlSourceOptimistic } from "./atoms"; |
| 37 | import { initialGraphqlCredentials } from "./defaults"; | 38 | import { initialGraphqlCredentials } from "./defaults"; |
| 38 | import type { HeaderValue } from "../sdk/types"; | 39 | import type { HeaderValue } from "../sdk/types"; |
| 39 | 40 | ||
| @@ -55,8 +56,7 @@ export default function AddGraphqlSource(props: { · 15 unmodified lines | |||
| 55 | const [tokens, setTokens] = useState<OAuthCompletionPayload | null>(null); | 56 | const [tokens, setTokens] = useState<OAuthCompletionPayload | null>(null); |
| 56 | 57 | ||
| 57 | const scopeId = useScope(); | 58 | const scopeId = useScope(); |
| 58 | const doAdd = useAtomSet(addGraphqlSource, { mode: "promise" }); | 59 | const doAdd = useAtomSet(addGraphqlSourceOptimistic(scopeId), { mode: "promiseExit" }); |
| 59 | const { beginAdd } = usePendingSources(); | ||
| 60 | const secretList = useSecretPickerSecrets(); | 60 | const secretList = useSecretPickerSecrets(); |
| 61 | const oauth = useOAuthPopupFlow({ | 61 | const oauth = useOAuthPopupFlow({ |
| 62 | popupName: "graphql-oauth", | 62 | popupName: "graphql-oauth", |
| @@ -112,41 +112,38 @@ export default function AddGraphqlSource(props: { · 49 unmodified lines | |||
| 112 | const { headers: headerMap, queryParams } = serializeHttpCredentials(credentials); | 112 | const { headers: headerMap, queryParams } = serializeHttpCredentials(credentials); |
| 113 | 113 | ||
| 114 | const { trimmedEndpoint, namespace, displayName } = sourceIdentity(); | 114 | const { trimmedEndpoint, namespace, displayName } = sourceIdentity(); |
| 115 | const placeholder = beginAdd({ | 115 | const exit = await doAdd({ |
| 116 | id: namespace, | 116 | params: { scopeId }, |
| 117 | name: displayName, | 117 | payload: { |
| 118 | kind: "graphql", | 118 | endpoint: trimmedEndpoint, |
| 119 | url: trimmedEndpoint || undefined, | 119 | name: displayName, |
| 120 | namespace, | ||
| 121 | ...(Object.keys(headerMap).length > 0 ? { headers: headerMap } : {}), | ||
| 122 | ...(Object.keys(queryParams).length > 0 | ||
| 123 | ? { queryParams: queryParams as Record<string, HeaderValue> } | ||
| 124 | : {}), | ||
| 125 | ...(authMode === "oauth2" && tokens | ||
| 126 | ? { | ||
| 127 | auth: { | ||
| 128 | kind: "oauth2" as const, | ||
| 129 | connectionId: tokens.connectionId, | ||
| 130 | }, | ||
| 131 | } | ||
| 132 | : {}), | ||
| 133 | }, | ||
| 134 | reactivityKeys: sourceWriteKeys, | ||
| 120 | }); | 135 | }); |
| 121 | try { | 136 | if (Exit.isFailure(exit)) { |
| 122 | await doAdd({ | 137 | const error = Exit.findErrorOption(exit); |
| 123 | params: { scopeId }, | 138 | setAddError( |
| 124 | payload: { | 139 | Option.isSome(error) && error.value instanceof Error |
| 125 | endpoint: trimmedEndpoint, | 140 | ? error.value.message |
| 126 | name: identity.name.trim() || undefined, | 141 | : "Failed to add source", |
| 127 | namespace: slugifyNamespace(identity.namespace) || undefined, | 142 | ); |
| 128 | ...(Object.keys(headerMap).length > 0 ? { headers: headerMap } : {}), | ||
| 129 | ...(Object.keys(queryParams).length > 0 | ||
| 130 | ? { queryParams: queryParams as Record<string, HeaderValue> } | ||
| 131 | : {}), | ||
| 132 | ...(authMode === "oauth2" && tokens | ||
| 133 | ? { | ||
| 134 | auth: { | ||
| 135 | kind: "oauth2" as const, | ||
| 136 | connectionId: tokens.connectionId, | ||
| 137 | }, | ||
| 138 | } | ||
| 139 | : {}), | ||
| 140 | }, | ||
| 141 | reactivityKeys: sourceWriteKeys, | ||
| 142 | }); | ||
| 143 | props.onComplete(); | ||
| 144 | } catch (e) { | ||
| 145 | setAddError(e instanceof Error ? e.message : "Failed to add source"); | ||
| 146 | setAdding(false); | 143 | setAdding(false); |
| 147 | } finally { | 144 | return; |
| 148 | placeholder.done(); | ||
| 149 | } | 145 | } |
| 146 | props.onComplete(); | ||
| 150 | }; | 147 | }; |
| 151 | 148 | ||
| 152 | return ( | 149 | return ( |
| @@ -1,4 +1,7 @@ | |||
| 1 | import type { ScopeId } from "@executor-js/sdk/core"; | 1 | import type { ScopeId } from "@executor-js/sdk/core"; |
| 2 | import * as Atom from "effect/unstable/reactivity/Atom"; | ||
| 3 | import * as AsyncResult from "effect/unstable/reactivity/AsyncResult"; | ||
| 4 | import { sourcesOptimisticAtom } from "@executor-js/react/api/atoms"; | ||
| 2 | import { ReactivityKey } from "@executor-js/react/api/reactivity-keys"; | 5 | import { ReactivityKey } from "@executor-js/react/api/reactivity-keys"; |
| 3 | import { GraphqlClient } from "./client"; | 6 | import { GraphqlClient } from "./client"; |
| 4 | 7 | ||
| @@ -19,4 +22,31 @@ export const graphqlSourceAtom = (scopeId: ScopeId, namespace: string) => · 14 unmodified lines | |||
| 19 | 22 | ||
| 20 | export const addGraphqlSource = GraphqlClient.mutation("graphql", "addSource"); | 23 | export const addGraphqlSource = GraphqlClient.mutation("graphql", "addSource"); |
| 21 | 24 | ||
| 25 | export const addGraphqlSourceOptimistic = Atom.family((scopeId: ScopeId) => | ||
| 26 | sourcesOptimisticAtom(scopeId).pipe( | ||
| 27 | Atom.optimisticFn({ | ||
| 28 | reducer: (current, arg) => | ||
| 29 | AsyncResult.map(current, (rows) => { | ||
| 30 | const id = arg.payload.namespace ?? `pending-${Math.random().toString(36).slice(2)}`; | ||
| 31 | const source = { | ||
| 32 | id, | ||
| 33 | scopeId, | ||
| 34 | kind: "graphql", | ||
| 35 | pluginId: "graphql", | ||
| 36 | name: arg.payload.name ?? id, | ||
| 37 | url: arg.payload.endpoint, | ||
| 38 | canRemove: false, | ||
| 39 | canRefresh: false, | ||
| 40 | canEdit: false, | ||
| 41 | runtime: false, | ||
| 42 | }; | ||
| 43 | return [source, ...rows.filter((row) => row.id !== id)].sort((a, b) => | ||
| 44 | a.name.localeCompare(b.name), | ||
| 45 | ); | ||
| 46 | }), | ||
| 47 | fn: addGraphqlSource, | ||
| 48 | }), | ||
| 49 | ), | ||
| 50 | ); | ||
| 51 | |||
| 22 | export const updateGraphqlSource = GraphqlClient.mutation("graphql", "updateSource"); | 52 | export const updateGraphqlSource = GraphqlClient.mutation("graphql", "updateSource"); |
| @@ -1,5 +1,7 @@ | |||
| 1 | import { useReducer, useCallback, useEffect, useRef, useState, type ReactNode } from "react"; | 1 | import { useReducer, useCallback, useEffect, useRef, useState, type ReactNode } from "react"; |
| 2 | import { useAtomSet } from "@effect/atom-react"; | 2 | import { useAtomSet } from "@effect/atom-react"; |
| 3 | import * as Exit from "effect/Exit"; | ||
| 4 | import * as Option from "effect/Option"; | ||
| 3 | 5 | ||
| 4 | import { useScope } from "@executor-js/react/api/scope-context"; | 6 | import { useScope } from "@executor-js/react/api/scope-context"; |
| 5 | import { Button } from "@executor-js/react/components/button"; | 7 | import { Button } from "@executor-js/react/components/button"; |
| @@ -49,8 +51,7 @@ import { · 43 unmodified lines | |||
| 49 | 51 | ||
| 50 | type RemoteAuthMode = "none" | "header" | "oauth2"; | 52 | type RemoteAuthMode = "none" | "header" | "oauth2"; |
| 51 | import { sourceWriteKeys } from "@executor-js/react/api/reactivity-keys"; | 53 | import { sourceWriteKeys } from "@executor-js/react/api/reactivity-keys"; |
| 52 | import { usePendingSources } from "@executor-js/react/api/optimistic"; | 54 | import { probeMcpEndpoint, addMcpSourceOptimistic } from "./atoms"; |
| 53 | import { probeMcpEndpoint, addMcpSource } from "./atoms"; | ||
| 54 | import { mcpPresets, type McpPreset } from "../sdk/presets"; | 55 | import { mcpPresets, type McpPreset } from "../sdk/presets"; |
| 55 | 56 | ||
| 56 | // --------------------------------------------------------------------------- | 57 | // --------------------------------------------------------------------------- |
| @@ -269,8 +270,7 @@ export default function AddMcpSource(props: { · 212 unmodified lines | |||
| 269 | 270 | ||
| 270 | const scopeId = useScope(); | 271 | const scopeId = useScope(); |
| 271 | const doProbe = useAtomSet(probeMcpEndpoint, { mode: "promise" }); | 272 | const doProbe = useAtomSet(probeMcpEndpoint, { mode: "promise" }); |
| 272 | const doAdd = useAtomSet(addMcpSource, { mode: "promise" }); | 273 | const doAdd = useAtomSet(addMcpSourceOptimistic(scopeId), { mode: "promiseExit" }); |
| 273 | const { beginAdd } = usePendingSources(); | ||
| 274 | const secretList = useSecretPickerSecrets(); | 274 | const secretList = useSecretPickerSecrets(); |
| 275 | const oauth = useOAuthPopupFlow<OAuthCompletionPayload>({ | 275 | const oauth = useOAuthPopupFlow<OAuthCompletionPayload>({ |
| 276 | popupName: "mcp-oauth", | 276 | popupName: "mcp-oauth", |
| @@ -466,40 +466,33 @@ export default function AddMcpSource(props: { · 189 unmodified lines | |||
| 466 | }; | 466 | }; |
| 467 | const displayName = remoteIdentity.name.trim() || probe.serverName || probe.name; | 467 | const displayName = remoteIdentity.name.trim() || probe.serverName || probe.name; |
| 468 | const slugNamespace = slugifyNamespace(remoteIdentity.namespace); | 468 | const slugNamespace = slugifyNamespace(remoteIdentity.namespace); |
| 469 | const placeholderId = slugNamespace || `pending:${crypto.randomUUID()}`; | 469 | const exit = await doAdd({ |
| 470 | const placeholder = beginAdd({ | 470 | params: { scopeId }, |
| 471 | id: placeholderId, | 471 | payload: { |
| 472 | name: displayName, | 472 | transport: "remote" as const, |
| 473 | kind: "mcp", | 473 | name: displayName, |
| 474 | url: state.url.trim(), | 474 | namespace: slugNamespace || undefined, |
| 475 | endpoint: state.url.trim(), | ||
| 476 | auth, | ||
| 477 | ...(Object.keys(remoteRequestHeaders).length > 0 ? { headers: remoteRequestHeaders } : {}), | ||
| 478 | ...(Object.keys(credentials.queryParams).length > 0 | ||
| 479 | ? { queryParams: credentials.queryParams } | ||
| 480 | : {}), | ||
| 481 | }, | ||
| 482 | reactivityKeys: sourceWriteKeys, | ||
| 475 | }); | 483 | }); |
| 476 | try { | 484 | if (Exit.isFailure(exit)) { |
| 477 | await doAdd({ | 485 | const error = Exit.findErrorOption(exit); |
| 478 | params: { scopeId }, | ||
| 479 | payload: { | ||
| 480 | transport: "remote" as const, | ||
| 481 | name: displayName, | ||
| 482 | namespace: slugNamespace || undefined, | ||
| 483 | endpoint: state.url.trim(), | ||
| 484 | auth, | ||
| 485 | ...(Object.keys(remoteRequestHeaders).length > 0 | ||
| 486 | ? { headers: remoteRequestHeaders } | ||
| 487 | : {}), | ||
| 488 | ...(Object.keys(credentials.queryParams).length > 0 | ||
| 489 | ? { queryParams: credentials.queryParams } | ||
| 490 | : {}), | ||
| 491 | }, | ||
| 492 | reactivityKeys: sourceWriteKeys, | ||
| 493 | }); | ||
| 494 | props.onComplete(); | ||
| 495 | } catch (e) { | ||
| 496 | dispatch({ | 486 | dispatch({ |
| 497 | type: "add-fail", | 487 | type: "add-fail", |
| 498 | error: e instanceof Error ? e.message : "Failed to add source", | 488 | error: |
| 489 | Option.isSome(error) && error.value instanceof Error | ||
| 490 | ? error.value.message | ||
| 491 | : "Failed to add source", | ||
| 499 | }); | 492 | }); |
| 500 | } finally { | 493 | return; |
| 501 | placeholder.done(); | ||
| 502 | } | 494 | } |
| 495 | props.onComplete(); | ||
| 503 | }, [ | 496 | }, [ |
| 504 | probe, | 497 | probe, |
| 505 | remoteAuthMode, | 498 | remoteAuthMode, |
| @@ -512,7 +505,6 @@ export default function AddMcpSource(props: { · 6 unmodified lines | |||
| 512 | doAdd, | 505 | doAdd, |
| 513 | props, | 506 | props, |
| 514 | scopeId, | 507 | scopeId, |
| 515 | beginAdd, | ||
| 516 | ]); | 508 | ]); |
| 517 | 509 | ||
| 518 | // ---- Stdio actions ---- | 510 | // ---- Stdio actions ---- |
| @@ -547,33 +539,30 @@ export default function AddMcpSource(props: { · 28 unmodified lines | |||
| 547 | setStdioError(null); | 539 | setStdioError(null); |
| 548 | const displayName = stdioIdentity.name.trim() || cmd; | 540 | const displayName = stdioIdentity.name.trim() || cmd; |
| 549 | const slugNamespace = slugifyNamespace(stdioIdentity.namespace); | 541 | const slugNamespace = slugifyNamespace(stdioIdentity.namespace); |
| 550 | const placeholderId = slugNamespace || `pending:${crypto.randomUUID()}`; | 542 | const exit = await doAdd({ |
| 551 | const placeholder = beginAdd({ | 543 | params: { scopeId }, |
| 552 | id: placeholderId, | 544 | payload: { |
| 553 | name: displayName, | 545 | transport: "stdio" as const, |
| 554 | kind: "mcp", | 546 | name: displayName, |
| 547 | namespace: slugNamespace || undefined, | ||
| 548 | command: cmd, | ||
| 549 | args: parseStdioArgs(stdioArgs), | ||
| 550 | env: parseStdioEnv(stdioEnv), | ||
| 551 | }, | ||
| 552 | reactivityKeys: sourceWriteKeys, | ||
| 555 | }); | 553 | }); |
| 556 | try { | 554 | if (Exit.isFailure(exit)) { |
| 557 | await doAdd({ | 555 | const error = Exit.findErrorOption(exit); |
| 558 | params: { scopeId }, | 556 | setStdioError( |
| 559 | payload: { | 557 | Option.isSome(error) && error.value instanceof Error |
| 560 | transport: "stdio" as const, | 558 | ? error.value.message |
| 561 | name: displayName, | 559 | : "Failed to add source", |
| 562 | namespace: slugNamespace || undefined, | 560 | ); |
| 563 | command: cmd, | ||
| 564 | args: parseStdioArgs(stdioArgs), | ||
| 565 | env: parseStdioEnv(stdioEnv), | ||
| 566 | }, | ||
| 567 | reactivityKeys: sourceWriteKeys, | ||
| 568 | }); | ||
| 569 | props.onComplete(); | ||
| 570 | } catch (e) { | ||
| 571 | setStdioError(e instanceof Error ? e.message : "Failed to add source"); | ||
| 572 | setStdioAdding(false); | 561 | setStdioAdding(false); |
| 573 | } finally { | 562 | return; |
| 574 | placeholder.done(); | ||
| 575 | } | 563 | } |
| 576 | }, [stdioCommand, stdioArgs, stdioEnv, stdioIdentity, doAdd, scopeId, props, beginAdd]); | 564 | props.onComplete(); |
| 565 | }, [stdioCommand, stdioArgs, stdioEnv, stdioIdentity, doAdd, scopeId, props]); | ||
| 577 | 566 | ||
| 578 | // ---- Render ---- | 567 | // ---- Render ---- |
| 579 | 568 | ||
| @@ -1,4 +1,7 @@ | |||
| 1 | import type { ScopeId } from "@executor-js/sdk/core"; | 1 | import type { ScopeId } from "@executor-js/sdk/core"; |
| 2 | import * as Atom from "effect/unstable/reactivity/Atom"; | ||
| 3 | import * as AsyncResult from "effect/unstable/reactivity/AsyncResult"; | ||
| 4 | import { sourcesOptimisticAtom } from "@executor-js/react/api/atoms"; | ||
| 2 | import { ReactivityKey } from "@executor-js/react/api/reactivity-keys"; | 5 | import { ReactivityKey } from "@executor-js/react/api/reactivity-keys"; |
| 3 | import { McpClient } from "./client"; | 6 | import { McpClient } from "./client"; |
| 4 | 7 | ||
| @@ -19,6 +22,32 @@ export const mcpSourceAtom = (scopeId: ScopeId, namespace: string) => · 14 unmodified lines | |||
| 19 | 22 | ||
| 20 | export const probeMcpEndpoint = McpClient.mutation("mcp", "probeEndpoint"); | 23 | export const probeMcpEndpoint = McpClient.mutation("mcp", "probeEndpoint"); |
| 21 | export const addMcpSource = McpClient.mutation("mcp", "addSource"); | 24 | export const addMcpSource = McpClient.mutation("mcp", "addSource"); |
| 25 | export const addMcpSourceOptimistic = Atom.family((scopeId: ScopeId) => | ||
| 26 | sourcesOptimisticAtom(scopeId).pipe( | ||
| 27 | Atom.optimisticFn({ | ||
| 28 | reducer: (current, arg) => | ||
| 29 | AsyncResult.map(current, (rows) => { | ||
| 30 | const id = arg.payload.namespace ?? `pending-${Math.random().toString(36).slice(2)}`; | ||
| 31 | const source = { | ||
| 32 | id, | ||
| 33 | scopeId, | ||
| 34 | kind: "mcp", | ||
| 35 | pluginId: "mcp", | ||
| 36 | name: arg.payload.name ?? id, | ||
| 37 | ...(arg.payload.transport === "remote" ? { url: arg.payload.endpoint } : {}), | ||
| 38 | canRemove: false, | ||
| 39 | canRefresh: false, | ||
| 40 | canEdit: false, | ||
| 41 | runtime: false, | ||
| 42 | }; | ||
| 43 | return [source, ...rows.filter((row) => row.id !== id)].sort((a, b) => | ||
| 44 | a.name.localeCompare(b.name), | ||
| 45 | ); | ||
| 46 | }), | ||
| 47 | fn: addMcpSource, | ||
| 48 | }), | ||
| 49 | ), | ||
| 50 | ); | ||
| 22 | export const removeMcpSource = McpClient.mutation("mcp", "removeSource"); | 51 | export const removeMcpSource = McpClient.mutation("mcp", "removeSource"); |
| 23 | export const refreshMcpSource = McpClient.mutation("mcp", "refreshSource"); | 52 | export const refreshMcpSource = McpClient.mutation("mcp", "refreshSource"); |
| 24 | export const updateMcpSource = McpClient.mutation("mcp", "updateSource"); | 53 | export const updateMcpSource = McpClient.mutation("mcp", "updateSource"); |
| @@ -1,6 +1,7 @@ | |||
| 1 | import { useCallback, useEffect, useRef, useState } from "react"; | 1 | import { useCallback, useEffect, useRef, useState } from "react"; |
| 2 | import { useAtomSet } from "@effect/atom-react"; | 2 | import { useAtomSet } from "@effect/atom-react"; |
| 3 | import { Option } from "effect"; | 3 | import * as Exit from "effect/Exit"; |
| 4 | import * as Option from "effect/Option"; | ||
| 4 | 5 | ||
| 5 | import { ConnectionId, ScopeId, SecretId } from "@executor-js/sdk/core"; | 6 | import { ConnectionId, ScopeId, SecretId } from "@executor-js/sdk/core"; |
| 6 | import { startOAuth } from "@executor-js/react/api/atoms"; | 7 | import { startOAuth } from "@executor-js/react/api/atoms"; |
| @@ -14,7 +15,6 @@ import { connectionWriteKeys, sourceWriteKeys } from "@executor-js/react/api/rea · 7 unmodified lines | |||
| 14 | // state without a refresh. | 15 | // state without a refresh. |
| 15 | const addSpecWriteKeys = [...sourceWriteKeys, ...connectionWriteKeys] as const; | 16 | const addSpecWriteKeys = [...sourceWriteKeys, ...connectionWriteKeys] as const; |
| 16 | const bindingWriteKeys = [...sourceWriteKeys, ...connectionWriteKeys] as const; | 17 | const bindingWriteKeys = [...sourceWriteKeys, ...connectionWriteKeys] as const; |
| 17 | import { usePendingSources } from "@executor-js/react/api/optimistic"; | ||
| 18 | import { HeadersList } from "@executor-js/react/plugins/headers-list"; | 18 | import { HeadersList } from "@executor-js/react/plugins/headers-list"; |
| 19 | import { | 19 | import { |
| 20 | HttpCredentialsEditor, | 20 | HttpCredentialsEditor, |
| @@ -65,7 +65,7 @@ import { SourceFavicon } from "@executor-js/react/components/source-favicon"; · 44 unmodified lines | |||
| 65 | import { RadioGroup, RadioGroupItem } from "@executor-js/react/components/radio-group"; | 65 | import { RadioGroup, RadioGroupItem } from "@executor-js/react/components/radio-group"; |
| 66 | import { Skeleton } from "@executor-js/react/components/skeleton"; | 66 | import { Skeleton } from "@executor-js/react/components/skeleton"; |
| 67 | import { IOSSpinner, Spinner } from "@executor-js/react/components/spinner"; | 67 | import { IOSSpinner, Spinner } from "@executor-js/react/components/spinner"; |
| 68 | import { addOpenApiSpec, previewOpenApiSpec, setOpenApiSourceBinding } from "./atoms"; | 68 | import { addOpenApiSpecOptimistic, previewOpenApiSpec, setOpenApiSourceBinding } from "./atoms"; |
| 69 | import type { SpecPreview, HeaderPreset, OAuth2Preset } from "../sdk/preview"; | 69 | import type { SpecPreview, HeaderPreset, OAuth2Preset } from "../sdk/preview"; |
| 70 | import { | 70 | import { |
| 71 | headerBindingSlot, | 71 | headerBindingSlot, |
| @@ -243,10 +243,9 @@ export default function AddOpenApiSource(props: { · 171 unmodified lines | |||
| 243 | const scopeId = useScope(); | 243 | const scopeId = useScope(); |
| 244 | const userScope = useUserScope(); | 244 | const userScope = useUserScope(); |
| 245 | const doPreview = useAtomSet(previewOpenApiSpec, { mode: "promise" }); | 245 | const doPreview = useAtomSet(previewOpenApiSpec, { mode: "promise" }); |
| 246 | const doAdd = useAtomSet(addOpenApiSpec, { mode: "promise" }); | 246 | const doAdd = useAtomSet(addOpenApiSpecOptimistic(scopeId), { mode: "promiseExit" }); |
| 247 | const doStartOAuth = useAtomSet(startOAuth, { mode: "promise" }); | 247 | const doStartOAuth = useAtomSet(startOAuth, { mode: "promise" }); |
| 248 | const doSetBinding = useAtomSet(setOpenApiSourceBinding, { mode: "promise" }); | 248 | const doSetBinding = useAtomSet(setOpenApiSourceBinding, { mode: "promiseExit" }); |
| 249 | const { beginAdd } = usePendingSources(); | ||
| 250 | const secretList = useSecretPickerSecrets(); | 249 | const secretList = useSecretPickerSecrets(); |
| 251 | const oauth = useOAuthPopupFlow<OAuthCompletionPayload>({ | 250 | const oauth = useOAuthPopupFlow<OAuthCompletionPayload>({ |
| 252 | popupName: OPENAPI_OAUTH_POPUP_NAME, | 251 | popupName: OPENAPI_OAUTH_POPUP_NAME, |
| @@ -615,109 +614,146 @@ export default function AddOpenApiSource(props: { · 362 unmodified lines | |||
| 615 | const displayName = | 614 | const displayName = |
| 616 | identity.name.trim() || | 615 | identity.name.trim() || |
| 617 | (preview ? Option.getOrElse(preview.title, () => namespace) : namespace); | 616 | (preview ? Option.getOrElse(preview.title, () => namespace) : namespace); |
| 618 | const placeholder = beginAdd({ | 617 | const exit = await doAdd({ |
| 619 | id: namespace, | 618 | params: { scopeId }, |
| 620 | name: displayName, | 619 | payload: { |
| 621 | kind: "openapi", | 620 | spec: specUrl, |
| 622 | url: resolvedBaseUrl || undefined, | 621 | specFetchCredentials: serializeHttpCredentials(specFetchCredentials), |
| 622 | name: displayName, | ||
| 623 | namespace, | ||
| 624 | baseUrl: resolvedBaseUrl || undefined, | ||
| 625 | ...(hasHeaders ? { headers: configuredHeaders } : {}), | ||
| 626 | ...(Object.keys(serializeHttpCredentials(runtimeCredentials).queryParams).length > 0 | ||
| 627 | ? { queryParams: serializeHttpCredentials(runtimeCredentials).queryParams } | ||
| 628 | : {}), | ||
| 629 | ...(configuredOAuth2 ? { oauth2: configuredOAuth2 } : {}), | ||
| 630 | }, | ||
| 631 | reactivityKeys: addSpecWriteKeys, | ||
| 623 | }); | 632 | }); |
| 624 | try { | 633 | if (Exit.isFailure(exit)) { |
| 625 | const result = await doAdd({ | 634 | const error = Exit.findErrorOption(exit); |
| 635 | setAddError( | ||
| 636 | Option.isSome(error) && error.value instanceof Error | ||
| 637 | ? error.value.message | ||
| 638 | : "Failed to add source", | ||
| 639 | ); | ||
| 640 | setAdding(false); | ||
| 641 | return; | ||
| 642 | } | ||
| 643 | |||
| 644 | const sourceId = exit.value.namespace; | ||
| 645 | const sourceScope = ScopeId.make(scopeId); | ||
| 646 | const bindingScope = ScopeId.make(userScope); | ||
| 647 | |||
| 648 | for (const binding of headerBindings) { | ||
| 649 | const bindingExit = await doSetBinding({ | ||
| 626 | params: { scopeId }, | 650 | params: { scopeId }, |
| 627 | payload: { | 651 | payload: { |
| 628 | spec: specUrl, | 652 | sourceId, |
| 629 | specFetchCredentials: serializeHttpCredentials(specFetchCredentials), | 653 | sourceScope, |
| 630 | name: identity.name.trim() || undefined, | 654 | scope: bindingScope, |
| 631 | namespace: slugifyNamespace(identity.namespace) || undefined, | 655 | slot: binding.slot, |
| 632 | baseUrl: resolvedBaseUrl || undefined, | 656 | value: { |
| 633 | ...(hasHeaders ? { headers: configuredHeaders } : {}), | 657 | kind: "secret", |
| 634 | ...(Object.keys(serializeHttpCredentials(runtimeCredentials).queryParams).length > 0 | 658 | secretId: SecretId.make(binding.secretId), |
| 635 | ? { queryParams: serializeHttpCredentials(runtimeCredentials).queryParams } | 659 | }, |
| 636 | : {}), | ||
| 637 | ...(configuredOAuth2 ? { oauth2: configuredOAuth2 } : {}), | ||
| 638 | }, | 660 | }, |
| 639 | reactivityKeys: addSpecWriteKeys, | 661 | reactivityKeys: bindingWriteKeys, |
| 640 | }); | 662 | }); |
| 641 | 663 | if (Exit.isFailure(bindingExit)) { | |
| 642 | const sourceId = result.namespace; | 664 | const error = Exit.findErrorOption(bindingExit); |
| 643 | const sourceScope = ScopeId.make(scopeId); | 665 | setAddError( |
| 644 | const bindingScope = ScopeId.make(userScope); | 666 | Option.isSome(error) && error.value instanceof Error |
| 645 | 667 | ? error.value.message | |
| 646 | for (const binding of headerBindings) { | 668 | : "Failed to add source", |
| 647 | await doSetBinding({ | 669 | ); |
| 648 | params: { scopeId }, | 670 | setAdding(false); |
| 649 | payload: { | 671 | return; |
| 650 | sourceId, | ||
| 651 | sourceScope, | ||
| 652 | scope: bindingScope, | ||
| 653 | slot: binding.slot, | ||
| 654 | value: { | ||
| 655 | kind: "secret", | ||
| 656 | secretId: SecretId.make(binding.secretId), | ||
| 657 | }, | ||
| 658 | }, | ||
| 659 | reactivityKeys: bindingWriteKeys, | ||
| 660 | }); | ||
| 661 | } | 672 | } |
| 673 | } | ||
| 662 | 674 | ||
| 663 | if (configuredOAuth2 && oauth2ClientIdSecretId) { | 675 | if (configuredOAuth2 && oauth2ClientIdSecretId) { |
| 664 | await doSetBinding({ | 676 | const bindingExit = await doSetBinding({ |
| 665 | params: { scopeId }, | 677 | params: { scopeId }, |
| 666 | payload: { | 678 | payload: { |
| 667 | sourceId, | 679 | sourceId, |
| 668 | sourceScope, | 680 | sourceScope, |
| 669 | scope: bindingScope, | 681 | scope: bindingScope, |
| 670 | slot: configuredOAuth2.clientIdSlot, | 682 | slot: configuredOAuth2.clientIdSlot, |
| 671 | value: { | 683 | value: { |
| 672 | kind: "secret", | 684 | kind: "secret", |
| 673 | secretId: SecretId.make(oauth2ClientIdSecretId), | 685 | secretId: SecretId.make(oauth2ClientIdSecretId), |
| 674 | }, | ||
| 675 | }, | 686 | }, |
| 676 | reactivityKeys: bindingWriteKeys, | 687 | }, |
| 677 | }); | 688 | reactivityKeys: bindingWriteKeys, |
| 689 | }); | ||
| 690 | if (Exit.isFailure(bindingExit)) { | ||
| 691 | const error = Exit.findErrorOption(bindingExit); | ||
| 692 | setAddError( | ||
| 693 | Option.isSome(error) && error.value instanceof Error | ||
| 694 | ? error.value.message | ||
| 695 | : "Failed to add source", | ||
| 696 | ); | ||
| 697 | setAdding(false); | ||
| 698 | return; | ||
| 678 | } | 699 | } |
| 700 | } | ||
| 679 | 701 | ||
| 680 | if (configuredOAuth2?.clientSecretSlot && oauth2ClientSecretSecretId) { | 702 | if (configuredOAuth2?.clientSecretSlot && oauth2ClientSecretSecretId) { |
| 681 | await doSetBinding({ | 703 | const bindingExit = await doSetBinding({ |
| 682 | params: { scopeId }, | 704 | params: { scopeId }, |
| 683 | payload: { | 705 | payload: { |
| 684 | sourceId, | 706 | sourceId, |
| 685 | sourceScope, | 707 | sourceScope, |
| 686 | scope: bindingScope, | 708 | scope: bindingScope, |
| 687 | slot: configuredOAuth2.clientSecretSlot, | 709 | slot: configuredOAuth2.clientSecretSlot, |
| 688 | value: { | 710 | value: { |
| 689 | kind: "secret", | 711 | kind: "secret", |
| 690 | secretId: SecretId.make(oauth2ClientSecretSecretId), | 712 | secretId: SecretId.make(oauth2ClientSecretSecretId), |
| 691 | }, | ||
| 692 | }, | 713 | }, |
| 693 | reactivityKeys: bindingWriteKeys, | 714 | }, |
| 694 | }); | 715 | reactivityKeys: bindingWriteKeys, |
| 716 | }); | ||
| 717 | if (Exit.isFailure(bindingExit)) { | ||
| 718 | const error = Exit.findErrorOption(bindingExit); | ||
| 719 | setAddError( | ||
| 720 | Option.isSome(error) && error.value instanceof Error | ||
| 721 | ? error.value.message | ||
| 722 | : "Failed to add source", | ||
| 723 | ); | ||
| 724 | setAdding(false); | ||
| 725 | return; | ||
| 695 | } | 726 | } |
| 727 | } | ||
| 696 | 728 | ||
| 697 | if (configuredOAuth2 && oauth2Auth) { | 729 | if (configuredOAuth2 && oauth2Auth) { |
| 698 | await doSetBinding({ | 730 | const bindingExit = await doSetBinding({ |
| 699 | params: { scopeId }, | 731 | params: { scopeId }, |
| 700 | payload: { | 732 | payload: { |
| 701 | sourceId, | 733 | sourceId, |
| 702 | sourceScope, | 734 | sourceScope, |
| 703 | scope: bindingScope, | 735 | scope: bindingScope, |
| 704 | slot: configuredOAuth2.connectionSlot, | 736 | slot: configuredOAuth2.connectionSlot, |
| 705 | value: { | 737 | value: { |
| 706 | kind: "connection", | 738 | kind: "connection", |
| 707 | connectionId: ConnectionId.make(oauth2Auth.connectionId), | 739 | connectionId: ConnectionId.make(oauth2Auth.connectionId), |
| 708 | }, | ||
| 709 | }, | 740 | }, |
| 710 | reactivityKeys: bindingWriteKeys, | 741 | }, |
| 711 | }); | 742 | reactivityKeys: bindingWriteKeys, |
| 743 | }); | ||
| 744 | if (Exit.isFailure(bindingExit)) { | ||
| 745 | const error = Exit.findErrorOption(bindingExit); | ||
| 746 | setAddError( | ||
| 747 | Option.isSome(error) && error.value instanceof Error | ||
| 748 | ? error.value.message | ||
| 749 | : "Failed to add source", | ||
| 750 | ); | ||
| 751 | setAdding(false); | ||
| 752 | return; | ||
| 712 | } | 753 | } |
| 713 | |||
| 714 | props.onComplete(); | ||
| 715 | } catch (e) { | ||
| 716 | setAddError(e instanceof Error ? e.message : "Failed to add source"); | ||
| 717 | setAdding(false); | ||
| 718 | } finally { | ||
| 719 | placeholder.done(); | ||
| 720 | } | 754 | } |
| 755 | |||
| 756 | props.onComplete(); | ||
| 721 | }; | 757 | }; |
| 722 | 758 | ||
| 723 | // ---- Render ---- | 759 | // ---- Render ---- |
| @@ -1,4 +1,7 @@ | |||
| 1 | import type { ScopeId } from "@executor-js/sdk/core"; | 1 | import type { ScopeId } from "@executor-js/sdk/core"; |
| 2 | import * as Atom from "effect/unstable/reactivity/Atom"; | ||
| 3 | import * as AsyncResult from "effect/unstable/reactivity/AsyncResult"; | ||
| 4 | import { sourcesOptimisticAtom } from "@executor-js/react/api/atoms"; | ||
| 2 | import { ReactivityKey } from "@executor-js/react/api/reactivity-keys"; | 5 | import { ReactivityKey } from "@executor-js/react/api/reactivity-keys"; |
| 3 | import { OpenApiClient } from "./client"; | 6 | import { OpenApiClient } from "./client"; |
| 4 | 7 | ||
| @@ -32,6 +35,33 @@ export const previewOpenApiSpec = OpenApiClient.mutation("openapi", "previewSpec · 27 unmodified lines | |||
| 32 | 35 | ||
| 33 | export const addOpenApiSpec = OpenApiClient.mutation("openapi", "addSpec"); | 36 | export const addOpenApiSpec = OpenApiClient.mutation("openapi", "addSpec"); |
| 34 | 37 | ||
| 38 | export const addOpenApiSpecOptimistic = Atom.family((scopeId: ScopeId) => | ||
| 39 | sourcesOptimisticAtom(scopeId).pipe( | ||
| 40 | Atom.optimisticFn({ | ||
| 41 | reducer: (current, arg) => | ||
| 42 | AsyncResult.map(current, (rows) => { | ||
| 43 | const id = arg.payload.namespace ?? `pending-${Math.random().toString(36).slice(2)}`; | ||
| 44 | const source = { | ||
| 45 | id, | ||
| 46 | scopeId, | ||
| 47 | kind: "openapi", | ||
| 48 | pluginId: "openapi", | ||
| 49 | name: arg.payload.name ?? id, | ||
| 50 | ...(arg.payload.baseUrl ? { url: arg.payload.baseUrl } : {}), | ||
| 51 | canRemove: false, | ||
| 52 | canRefresh: false, | ||
| 53 | canEdit: false, | ||
| 54 | runtime: false, | ||
| 55 | }; | ||
| 56 | return [source, ...rows.filter((row) => row.id !== id)].sort((a, b) => | ||
| 57 | a.name.localeCompare(b.name), | ||
| 58 | ); | ||
| 59 | }), | ||
| 60 | fn: addOpenApiSpec, | ||
| 61 | }), | ||
| 62 | ), | ||
| 63 | ); | ||
| 64 | |||
| 35 | export const updateOpenApiSource = OpenApiClient.mutation("openapi", "updateSource"); | 65 | export const updateOpenApiSource = OpenApiClient.mutation("openapi", "updateSource"); |
| 36 | 66 | ||
| 37 | export const setOpenApiSourceBinding = OpenApiClient.mutation("openapi", "setSourceBinding"); | 67 | export const setOpenApiSourceBinding = OpenApiClient.mutation("openapi", "setSourceBinding"); |
| @@ -55,7 +55,10 @@ export const sourcesAtom = (scopeId: ScopeId) => · 54 unmodified lines | |||
| 55 | 55 | ||
| 56 | /** Single source by id — derived from the sources list */ | 56 | /** Single source by id — derived from the sources list */ |
| 57 | export const sourceAtom = (sourceId: string, scopeId: ScopeId) => | 57 | export const sourceAtom = (sourceId: string, scopeId: ScopeId) => |
| 58 | Atom.mapResult(sourcesAtom(scopeId), (sources) => sources.find((s) => s.id === sourceId) ?? null); | 58 | Atom.mapResult( |
| 59 | sourcesOptimisticAtom(scopeId), | ||
| 60 | (sources) => sources.find((s) => s.id === sourceId) ?? null, | ||
| 61 | ); | ||
| 59 | 62 | ||
| 60 | export const secretsAtom = (scopeId: ScopeId) => | 63 | export const secretsAtom = (scopeId: ScopeId) => |
| 61 | ExecutorApiClient.query("secrets", "list", { | 64 | ExecutorApiClient.query("secrets", "list", { |
| @@ -85,25 +88,14 @@ export const secretUsagesAtom = (scopeId: ScopeId, secretId: SecretId) => · 23 unmodified lines | |||
| 85 | // Refresh whenever any source / connection / secret changes — adding | 88 | // Refresh whenever any source / connection / secret changes — adding |
| 86 | // an oauth source pulls in a new connection-secret link and we want | 89 | // an oauth source pulls in a new connection-secret link and we want |
| 87 | // the usage list to reflect it. | 90 | // the usage list to reflect it. |
| 88 | reactivityKeys: [ | 91 | reactivityKeys: [ReactivityKey.secrets, ReactivityKey.sources, ReactivityKey.connections], |
| 89 | ReactivityKey.secrets, | ||
| 90 | ReactivityKey.sources, | ||
| 91 | ReactivityKey.connections, | ||
| 92 | ], | ||
| 93 | }); | 92 | }); |
| 94 | 93 | ||
| 95 | export const connectionUsagesAtom = ( | 94 | export const connectionUsagesAtom = (scopeId: ScopeId, connectionId: ConnectionId) => |
| 96 | scopeId: ScopeId, | ||
| 97 | connectionId: ConnectionId, | ||
| 98 | ) => | ||
| 99 | ExecutorApiClient.query("connections", "usages", { | 95 | ExecutorApiClient.query("connections", "usages", { |
| 100 | params: { scopeId, connectionId }, | 96 | params: { scopeId, connectionId }, |
| 101 | timeToLive: "30 seconds", | 97 | timeToLive: "30 seconds", |
| 102 | reactivityKeys: [ | 98 | reactivityKeys: [ReactivityKey.connections, ReactivityKey.sources, ReactivityKey.secrets], |
| 103 | ReactivityKey.connections, | ||
| 104 | ReactivityKey.sources, | ||
| 105 | ReactivityKey.secrets, | ||
| 106 | ], | ||
| 107 | }); | 99 | }); |
| 108 | 100 | ||
| 109 | export const policiesAtom = (scopeId: ScopeId) => | 101 | export const policiesAtom = (scopeId: ScopeId) => |
| @@ -153,6 +145,46 @@ export const updatePolicy = ExecutorApiClient.mutation("policies", "update"); · 43 unmodified lines | |||
| 153 | 145 | ||
| 154 | export const removePolicy = ExecutorApiClient.mutation("policies", "remove"); | 146 | export const removePolicy = ExecutorApiClient.mutation("policies", "remove"); |
| 155 | 147 | ||
| 148 | // --------------------------------------------------------------------------- | ||
| 149 | // Sources — optimistic surface. | ||
| 150 | // --------------------------------------------------------------------------- | ||
| 151 | |||
| 152 | export const sourcesOptimisticAtom = Atom.family((scopeId: ScopeId) => | ||
| 153 | Atom.optimistic(sourcesAtom(scopeId)), | ||
| 154 | ); | ||
| 155 | |||
| 156 | export const removeSourceOptimistic = Atom.family((scopeId: ScopeId) => | ||
| 157 | sourcesOptimisticAtom(scopeId).pipe( | ||
| 158 | Atom.optimisticFn({ | ||
| 159 | reducer: (current, arg) => | ||
| 160 | AsyncResult.map(current, (rows) => | ||
| 161 | rows.filter((source) => source.id !== arg.params.sourceId), | ||
| 162 | ), | ||
| 163 | fn: removeSource, | ||
| 164 | }), | ||
| 165 | ), | ||
| 166 | ); | ||
| 167 | |||
| 168 | // --------------------------------------------------------------------------- | ||
| 169 | // Connections — optimistic removals. | ||
| 170 | // --------------------------------------------------------------------------- | ||
| 171 | |||
| 172 | export const connectionsOptimisticAtom = Atom.family((scopeId: ScopeId) => | ||
| 173 | Atom.optimistic(connectionsAtom(scopeId)), | ||
| 174 | ); | ||
| 175 | |||
| 176 | export const removeConnectionOptimistic = Atom.family((scopeId: ScopeId) => | ||
| 177 | connectionsOptimisticAtom(scopeId).pipe( | ||
| 178 | Atom.optimisticFn({ | ||
| 179 | reducer: (current, arg) => | ||
| 180 | AsyncResult.map(current, (rows) => | ||
| 181 | rows.filter((connection) => connection.id !== arg.params.connectionId), | ||
| 182 | ), | ||
| 183 | fn: removeConnection, | ||
| 184 | }), | ||
| 185 | ), | ||
| 186 | ); | ||
| 187 | |||
| 156 | // --------------------------------------------------------------------------- | 188 | // --------------------------------------------------------------------------- |
| 157 | // Secrets — optimistic removals. | 189 | // Secrets — optimistic removals. |
| 158 | // --------------------------------------------------------------------------- | 190 | // --------------------------------------------------------------------------- |
| @@ -1,213 +0,0 @@ | |||
| 1 | import * as React from "react"; | ||
| 2 | import { useAtomSet, useAtomValue } from "@effect/atom-react"; | ||
| 3 | import * as Atom from "effect/unstable/reactivity/Atom"; | ||
| 4 | import * as AsyncResult from "effect/unstable/reactivity/AsyncResult"; | ||
| 5 | import type { ScopeId } from "@executor-js/sdk"; | ||
| 6 | |||
| 7 | import { connectionsAtom, sourcesAtom } from "./atoms"; | ||
| 8 | |||
| 9 | /** | ||
| 10 | * Client-only optimistic-update layer. | ||
| 11 | * | ||
| 12 | * Mutations that add a row return only after the server roundtrip. The | ||
| 13 | * `pendingResourceAtom` family lets a UI insert a placeholder *before* the | ||
| 14 | * mutation resolves so the sidebar/list updates instantly. Once the server | ||
| 15 | * returns and reactivity refetches the canonical list, callers should remove | ||
| 16 | * the placeholder (the dedupe in `mergePending` filters it anyway, but | ||
| 17 | * explicit removal keeps the pending set bounded on errors). | ||
| 18 | * | ||
| 19 | * Resource keys (`"sources"`, `"secrets"`, …) match the strings in | ||
| 20 | * `ReactivityKey` but are intentionally not coupled — pending state is purely | ||
| 21 | * client-side. | ||
| 22 | */ | ||
| 23 | |||
| 24 | export const PendingResource = { | ||
| 25 | sources: "sources", | ||
| 26 | connectionRemovals: "connection-removals", | ||
| 27 | } as const; | ||
| 28 | |||
| 29 | export interface PendingEntry<T> { | ||
| 30 | readonly id: string; | ||
| 31 | readonly value: T; | ||
| 32 | } | ||
| 33 | |||
| 34 | const pendingFamily = Atom.family((_resource: string) => | ||
| 35 | Atom.make<ReadonlyArray<PendingEntry<unknown>>>([]), | ||
| 36 | ); | ||
| 37 | |||
| 38 | const atomFor = <T,>(resource: string) => | ||
| 39 | pendingFamily(resource) as ReturnType<typeof pendingFamily> & { | ||
| 40 | readonly __t?: T; | ||
| 41 | }; | ||
| 42 | |||
| 43 | export const usePendingResource = <T,>(resource: string) => { | ||
| 44 | const atom = atomFor<T>(resource); | ||
| 45 | const pending = useAtomValue(atom) as ReadonlyArray<PendingEntry<T>>; | ||
| 46 | const setPending = useAtomSet(atom); | ||
| 47 | |||
| 48 | const add = React.useCallback( | ||
| 49 | (entry: PendingEntry<T>) => | ||
| 50 | setPending((prev) => [ | ||
| 51 | ...(prev as ReadonlyArray<PendingEntry<T>>).filter((p) => p.id !== entry.id), | ||
| 52 | entry, | ||
| 53 | ]), | ||
| 54 | [setPending], | ||
| 55 | ); | ||
| 56 | |||
| 57 | const remove = React.useCallback( | ||
| 58 | (id: string) => | ||
| 59 | setPending((prev) => (prev as ReadonlyArray<PendingEntry<T>>).filter((p) => p.id !== id)), | ||
| 60 | [setPending], | ||
| 61 | ); | ||
| 62 | |||
| 63 | const clear = React.useCallback(() => setPending([]), [setPending]); | ||
| 64 | |||
| 65 | return { pending, add, remove, clear }; | ||
| 66 | }; | ||
| 67 | |||
| 68 | /** | ||
| 69 | * Merges pending entries with a server-loaded list. Pending entries whose id | ||
| 70 | * already exists on the server are dropped so we never double-render a row | ||
| 71 | * once the canonical version arrives. | ||
| 72 | */ | ||
| 73 | export const mergePending = <T, R>( | ||
| 74 | pending: ReadonlyArray<PendingEntry<T>>, | ||
| 75 | server: ReadonlyArray<R>, | ||
| 76 | serverId: (row: R) => string, | ||
| 77 | fromPending: (entry: PendingEntry<T>) => R, | ||
| 78 | ): ReadonlyArray<R> => { | ||
| 79 | if (pending.length === 0) return server; | ||
| 80 | const seen = new Set(server.map(serverId)); | ||
| 81 | const extras = pending.filter((p) => !seen.has(p.id)).map(fromPending); | ||
| 82 | if (extras.length === 0) return server; | ||
| 83 | return [...extras, ...server]; | ||
| 84 | }; | ||
| 85 | |||
| 86 | // --------------------------------------------------------------------------- | ||
| 87 | // Sources — convenience wrappers used by sidebar/list views and add forms. | ||
| 88 | // Pending entries are global (not per-scope) because the user only sees one | ||
| 89 | // scope at a time; this keeps the add-form code free of scope plumbing. | ||
| 90 | // --------------------------------------------------------------------------- | ||
| 91 | |||
| 92 | export interface PendingSource { | ||
| 93 | readonly name: string; | ||
| 94 | readonly kind: string; | ||
| 95 | readonly url?: string; | ||
| 96 | } | ||
| 97 | |||
| 98 | /** | ||
| 99 | * Sidebar/list helper. Reads `sourcesAtom(scopeId)` and merges any pending | ||
| 100 | * placeholder rows in, sorting the combined list by name so the placeholder | ||
| 101 | * lands in the same position the canonical row will occupy — no visual jump | ||
| 102 | * when the server confirms. | ||
| 103 | */ | ||
| 104 | export const useSourcesWithPending = (scopeId: ScopeId) => { | ||
| 105 | const result = useAtomValue(sourcesAtom(scopeId)); | ||
| 106 | const { pending } = usePendingResource<PendingSource>(PendingResource.sources); | ||
| 107 | return React.useMemo( | ||
| 108 | () => | ||
| 109 | AsyncResult.map( | ||
| 110 | result, | ||
| 111 | ( | ||
| 112 | sources: ReadonlyArray<{ | ||
| 113 | readonly id: string; | ||
| 114 | readonly name: string; | ||
| 115 | readonly kind: string; | ||
| 116 | readonly url?: string; | ||
| 117 | }>, | ||
| 118 | ) => { | ||
| 119 | const merged = mergePending( | ||
| 120 | pending, | ||
| 121 | sources, | ||
| 122 | (s: { readonly id: string }) => s.id, | ||
| 123 | (p) => ({ | ||
| 124 | id: p.id, | ||
| 125 | name: p.value.name, | ||
| 126 | kind: p.value.kind, | ||
| 127 | url: p.value.url, | ||
| 128 | // The placeholder cannot be removed/refreshed/edited until the | ||
| 129 | // server confirms it, so disable those affordances. | ||
| 130 | canRemove: false, | ||
| 131 | canRefresh: false, | ||
| 132 | canEdit: false, | ||
| 133 | }), | ||
| 134 | ); | ||
| 135 | return [...merged].sort((a, b) => a.name.localeCompare(b.name)); | ||
| 136 | }, | ||
| 137 | ), | ||
| 138 | [result, pending], | ||
| 139 | ); | ||
| 140 | }; | ||
| 141 | |||
| 142 | /** | ||
| 143 | * Hook for an add form. Returns helpers to push and clear a pending source | ||
| 144 | * placeholder. On submit: | ||
| 145 | * const placeholder = beginAdd({ id: namespace, name, kind }); | ||
| 146 | * try { await doAdd(...); } finally { placeholder.done(); } | ||
| 147 | */ | ||
| 148 | export const usePendingSources = () => { | ||
| 149 | const { add, remove } = usePendingResource<PendingSource>(PendingResource.sources); | ||
| 150 | return React.useMemo( | ||
| 151 | () => ({ | ||
| 152 | beginAdd: (entry: { id: string } & PendingSource) => { | ||
| 153 | add({ id: entry.id, value: { name: entry.name, kind: entry.kind, url: entry.url } }); | ||
| 154 | return { done: () => remove(entry.id) }; | ||
| 155 | }, | ||
| 156 | }), | ||
| 157 | [add, remove], | ||
| 158 | ); | ||
| 159 | }; | ||
| 160 | |||
| 161 | // --------------------------------------------------------------------------- | ||
| 162 | // Connections — optimistic removals. | ||
| 163 | // --------------------------------------------------------------------------- | ||
| 164 | |||
| 165 | interface PendingConnectionRemoval { | ||
| 166 | readonly id: string; | ||
| 167 | } | ||
| 168 | |||
| 169 | export const useConnectionsWithPendingRemovals = (scopeId: ScopeId) => { | ||
| 170 | const result = useAtomValue(connectionsAtom(scopeId)); | ||
| 171 | const { pending, remove } = usePendingResource<PendingConnectionRemoval>( | ||
| 172 | PendingResource.connectionRemovals, | ||
| 173 | ); | ||
| 174 | |||
| 175 | React.useEffect(() => { | ||
| 176 | if (!AsyncResult.isSuccess(result) || pending.length === 0) return; | ||
| 177 | |||
| 178 | const serverIds = new Set( | ||
| 179 | result.value.map((connection: { readonly id: string }) => connection.id as string), | ||
| 180 | ); | ||
| 181 | for (const entry of pending) { | ||
| 182 | if (!serverIds.has(entry.id)) remove(entry.id); | ||
| 183 | } | ||
| 184 | }, [result, pending, remove]); | ||
| 185 | |||
| 186 | return React.useMemo( | ||
| 187 | () => | ||
| 188 | AsyncResult.map(result, (connections) => { | ||
| 189 | if (pending.length === 0) return connections; | ||
| 190 | const hiddenIds = new Set(pending.map((entry) => entry.id)); | ||
| 191 | return connections.filter( | ||
| 192 | (connection: { readonly id: string }) => !hiddenIds.has(connection.id as string), | ||
| 193 | ); | ||
| 194 | }), | ||
| 195 | [result, pending], | ||
| 196 | ); | ||
| 197 | }; | ||
| 198 | |||
| 199 | export const usePendingConnectionRemovals = () => { | ||
| 200 | const { add, remove } = usePendingResource<PendingConnectionRemoval>( | ||
| 201 | PendingResource.connectionRemovals, | ||
| 202 | ); | ||
| 203 | |||
| 204 | return React.useMemo( | ||
| 205 | () => ({ | ||
| 206 | beginRemove: (id: string) => { | ||
| 207 | add({ id, value: { id } }); | ||
| 208 | return { undo: () => remove(id) }; | ||
| 209 | }, | ||
| 210 | }), | ||
| 211 | [add, remove], | ||
| 212 | ); | ||
| 213 | }; | ||
| @@ -4,7 +4,7 @@ import { useAtomValue } from "@effect/atom-react"; · 3 unmodified lines | |||
| 4 | import * as AsyncResult from "effect/unstable/reactivity/AsyncResult"; | 4 | import * as AsyncResult from "effect/unstable/reactivity/AsyncResult"; |
| 5 | import { PlusIcon } from "lucide-react"; | 5 | import { PlusIcon } from "lucide-react"; |
| 6 | import { SourceFavicon } from "./source-favicon"; | 6 | import { SourceFavicon } from "./source-favicon"; |
| 7 | import { sourcesAtom } from "../api/atoms"; | 7 | import { sourcesOptimisticAtom } from "../api/atoms"; |
| 8 | import { useScope } from "../hooks/use-scope"; | 8 | import { useScope } from "../hooks/use-scope"; |
| 9 | import { useSourcePlugins } from "@executor-js/sdk/client"; | 9 | import { useSourcePlugins } from "@executor-js/sdk/client"; |
| 10 | import { | 10 | import { |
| @@ -32,7 +32,7 @@ export function CommandPalette() { · 21 unmodified lines | |||
| 32 | const [open, setOpen] = useState(false); | 32 | const [open, setOpen] = useState(false); |
| 33 | const navigate = useNavigate(); | 33 | const navigate = useNavigate(); |
| 34 | const scopeId = useScope(); | 34 | const scopeId = useScope(); |
| 35 | const sourcesResult = useAtomValue(sourcesAtom(scopeId)); | 35 | const sourcesResult = useAtomValue(sourcesOptimisticAtom(scopeId)); |
| 36 | 36 | ||
| 37 | // Toggle with ⌘K / Ctrl+K | 37 | // Toggle with ⌘K / Ctrl+K |
| 38 | useEffect(() => { | 38 | useEffect(() => { |
| @@ -6,8 +6,11 @@ import * as Option from "effect/Option"; · 5 unmodified lines | |||
| 6 | import { ConnectionId, ConnectionInUseError, type ScopeId } from "@executor-js/sdk"; | 6 | import { ConnectionId, ConnectionInUseError, type ScopeId } from "@executor-js/sdk"; |
| 7 | import { toast } from "sonner"; | 7 | import { toast } from "sonner"; |
| 8 | 8 | ||
| 9 | import { connectionUsagesAtom, removeConnection } from "../api/atoms"; | 9 | import { |
| 10 | import { useConnectionsWithPendingRemovals, usePendingConnectionRemovals } from "../api/optimistic"; | 10 | connectionUsagesAtom, |
| 11 | connectionsOptimisticAtom, | ||
| 12 | removeConnectionOptimistic, | ||
| 13 | } from "../api/atoms"; | ||
| 11 | import { connectionWriteKeys } from "../api/reactivity-keys"; | 14 | import { connectionWriteKeys } from "../api/reactivity-keys"; |
| 12 | import { useScope, useScopeStack } from "../hooks/use-scope"; | 15 | import { useScope, useScopeStack } from "../hooks/use-scope"; |
| 13 | import { Badge } from "../components/badge"; | 16 | import { Badge } from "../components/badge"; |
| @@ -59,13 +62,8 @@ const connectionScopeLabel = ( · 45 unmodified lines | |||
| 59 | // don't get a stray "Used by 0" line before any source binds to them. | 62 | // don't get a stray "Used by 0" line before any source binds to them. |
| 60 | // --------------------------------------------------------------------------- | 63 | // --------------------------------------------------------------------------- |
| 61 | 64 | ||
| 62 | function ConnectionUsageFooter(props: { | 65 | function ConnectionUsageFooter(props: { scopeId: ScopeId; connectionId: ConnectionId }) { |
| 63 | scopeId: ScopeId; | 66 | const usages = useAtomValue(connectionUsagesAtom(props.scopeId, props.connectionId)); |
| 64 | connectionId: ConnectionId; | ||
| 65 | }) { | ||
| 66 | const usages = useAtomValue( | ||
| 67 | connectionUsagesAtom(props.scopeId, props.connectionId), | ||
| 68 | ); | ||
| 69 | return AsyncResult.match(usages, { | 67 | return AsyncResult.match(usages, { |
| 70 | onInitial: () => null, | 68 | onInitial: () => null, |
| 71 | onFailure: () => null, | 69 | onFailure: () => null, |
| @@ -161,18 +159,15 @@ function ConnectionRow(props: { · 89 unmodified lines | |||
| 161 | export function ConnectionsPage() { | 159 | export function ConnectionsPage() { |
| 162 | const scopeId = useScope(); | 160 | const scopeId = useScope(); |
| 163 | const scopeStack = useScopeStack(); | 161 | const scopeStack = useScopeStack(); |
| 164 | const connections = useConnectionsWithPendingRemovals(scopeId); | 162 | const connections = useAtomValue(connectionsOptimisticAtom(scopeId)); |
| 165 | const { beginRemove } = usePendingConnectionRemovals(); | 163 | const doRemove = useAtomSet(removeConnectionOptimistic(scopeId), { mode: "promiseExit" }); |
| 166 | const doRemove = useAtomSet(removeConnection, { mode: "promiseExit" }); | ||
| 167 | 164 | ||
| 168 | const handleRemove = async (connectionId: string) => { | 165 | const handleRemove = async (connectionId: string) => { |
| 169 | const pending = beginRemove(connectionId); | ||
| 170 | const exit = await doRemove({ | 166 | const exit = await doRemove({ |
| 171 | params: { scopeId, connectionId: ConnectionId.make(connectionId) }, | 167 | params: { scopeId, connectionId: ConnectionId.make(connectionId) }, |
| 172 | reactivityKeys: connectionWriteKeys, | 168 | reactivityKeys: connectionWriteKeys, |
| 173 | }); | 169 | }); |
| 174 | if (Exit.isFailure(exit)) { | 170 | if (Exit.isFailure(exit)) { |
| 175 | pending.undo(); | ||
| 176 | const error = Exit.findErrorOption(exit); | 171 | const error = Exit.findErrorOption(exit); |
| 177 | if (Option.isSome(error) && error.value instanceof ConnectionInUseError) { | 172 | if (Option.isSome(error) && error.value instanceof ConnectionInUseError) { |
| 178 | const count = error.value.usageCount; | 173 | const count = error.value.usageCount; |
| @@ -6,9 +6,9 @@ import { effectivePolicyFromSorted } from "@executor-js/sdk"; · 5 unmodified lines | |||
| 6 | import { | 6 | import { |
| 7 | policiesOptimisticAtom, | 7 | policiesOptimisticAtom, |
| 8 | sourceToolsAtom, | 8 | sourceToolsAtom, |
| 9 | sourcesAtom, | 9 | sourcesOptimisticAtom, |
| 10 | sourceAtom, | 10 | sourceAtom, |
| 11 | removeSource, | 11 | removeSourceOptimistic, |
| 12 | refreshSource, | 12 | refreshSource, |
| 13 | } from "../api/atoms"; | 13 | } from "../api/atoms"; |
| 14 | import { sourceWriteKeys } from "../api/reactivity-keys"; | 14 | import { sourceWriteKeys } from "../api/reactivity-keys"; |
| @@ -29,9 +29,9 @@ export function SourceDetailPage(props: { namespace: string }) { · 14 unmodified lines | |||
| 29 | const source = useAtomValue(sourceAtom(namespace, scopeId)); | 29 | const source = useAtomValue(sourceAtom(namespace, scopeId)); |
| 30 | const tools = useAtomValue(sourceToolsAtom(namespace, scopeId)); | 30 | const tools = useAtomValue(sourceToolsAtom(namespace, scopeId)); |
| 31 | const policies = useAtomValue(policiesOptimisticAtom(scopeId)); | 31 | const policies = useAtomValue(policiesOptimisticAtom(scopeId)); |
| 32 | const refreshSources = useAtomRefresh(sourcesAtom(scopeId)); | 32 | const refreshSources = useAtomRefresh(sourcesOptimisticAtom(scopeId)); |
| 33 | const refreshTools = useAtomRefresh(sourceToolsAtom(namespace, scopeId)); | 33 | const refreshTools = useAtomRefresh(sourceToolsAtom(namespace, scopeId)); |
| 34 | const doRemove = useAtomSet(removeSource, { mode: "promise" }); | 34 | const doRemove = useAtomSet(removeSourceOptimistic(scopeId), { mode: "promise" }); |
| 35 | const doRefresh = useAtomSet(refreshSource, { mode: "promise" }); | 35 | const doRefresh = useAtomSet(refreshSource, { mode: "promise" }); |
| 36 | const policyActions = usePolicyActions(scopeId); | 36 | const policyActions = usePolicyActions(scopeId); |
| 37 | const navigate = useNavigate(); | 37 | const navigate = useNavigate(); |
| @@ -1,16 +1,11 @@ | |||
| 1 | import { Suspense, useCallback, useMemo, useState } from "react"; | 1 | import { Suspense, useCallback, useMemo, useState } from "react"; |
| 2 | import { Link, useNavigate } from "@tanstack/react-router"; | 2 | import { Link, useNavigate } from "@tanstack/react-router"; |
| 3 | import { useAtomSet } from "@effect/atom-react"; | 3 | import { useAtomSet, useAtomValue } from "@effect/atom-react"; |
| 4 | import * as AsyncResult from "effect/unstable/reactivity/AsyncResult"; | 4 | import * as AsyncResult from "effect/unstable/reactivity/AsyncResult"; |
| 5 | import { PlusIcon } from "lucide-react"; | 5 | import { PlusIcon } from "lucide-react"; |
| 6 | import type { SourceDetectionResult } from "@executor-js/sdk"; | 6 | import type { SourceDetectionResult } from "@executor-js/sdk"; |
| 7 | import { | 7 | import { useSourcePlugins, type SourcePlugin, type SourcePreset } from "@executor-js/sdk/client"; |
| 8 | useSourcePlugins, | 8 | import { detectSource, sourcesOptimisticAtom } from "../api/atoms"; |
| 9 | type SourcePlugin, | ||
| 10 | type SourcePreset, | ||
| 11 | } from "@executor-js/sdk/client"; | ||
| 12 | import { detectSource } from "../api/atoms"; | ||
| 13 | import { useSourcesWithPending } from "../api/optimistic"; | ||
| 14 | import { useScope } from "../hooks/use-scope"; | 9 | import { useScope } from "../hooks/use-scope"; |
| 15 | import { McpInstallCard } from "../components/mcp-install-card"; | 10 | import { McpInstallCard } from "../components/mcp-install-card"; |
| 16 | import { Button } from "../components/button"; | 11 | import { Button } from "../components/button"; |
| @@ -60,7 +55,7 @@ const bestDetection = ( · 43 unmodified lines | |||
| 60 | 55 | ||
| 61 | export function SourcesPage() { | 56 | export function SourcesPage() { |
| 62 | const scopeId = useScope(); | 57 | const scopeId = useScope(); |
| 63 | const sources = useSourcesWithPending(scopeId); | 58 | const sources = useAtomValue(sourcesOptimisticAtom(scopeId)); |
| 64 | const [connectOpen, setConnectOpen] = useState(false); | 59 | const [connectOpen, setConnectOpen] = useState(false); |
| 65 | 60 | ||
| 66 | return ( | 61 | return ( |
| @@ -228,10 +223,7 @@ function ConnectDialog(props: { open: boolean; onOpenChange: (open: boolean) => · 161 unmodified lines | |||
| 228 | className="flex-1" | 223 | className="flex-1" |
| 229 | /> | 224 | /> |
| 230 | {isUrl && ( | 225 | {isUrl && ( |
| 231 | <Button | 226 | <Button onClick={() => void handleDetect()} disabled={detecting || !query.trim()}> |
| 232 | onClick={() => void handleDetect()} | ||
| 233 | disabled={detecting || !query.trim()} | ||
| 234 | > | ||
| 235 | {detecting ? "Detecting..." : "Detect"} | 227 | {detecting ? "Detecting..." : "Detect"} |
| 236 | </Button> | 228 | </Button> |
| 237 | )} | 229 | )} |
| @@ -256,11 +248,7 @@ function ConnectDialog(props: { open: boolean; onOpenChange: (open: boolean) => · 18 unmodified lines | |||
| 256 | </div> | 248 | </div> |
| 257 | </div> | 249 | </div> |
| 258 | 250 | ||
| 259 | <PresetGrid | 251 | <PresetGrid plugins={sourcePlugins} onPick={closeAndReset} searchQuery={presetSearch} /> |
| 260 | plugins={sourcePlugins} | ||
| 261 | onPick={closeAndReset} | ||
| 262 | searchQuery={presetSearch} | ||
| 263 | /> | ||
| 264 | </div> | 252 | </div> |
| 265 | </DialogContent> | 253 | </DialogContent> |
| 266 | </Dialog> | 254 | </Dialog> |
| @@ -324,8 +312,7 @@ function PresetGrid(props: { · 57 unmodified lines | |||
| 324 | const q = (props.searchQuery ?? "").trim().toLowerCase(); | 312 | const q = (props.searchQuery ?? "").trim().toLowerCase(); |
| 325 | if (q.length === 0) return allPresets; | 313 | if (q.length === 0) return allPresets; |
| 326 | return allPresets.filter(({ preset, pluginLabel }) => { | 314 | return allPresets.filter(({ preset, pluginLabel }) => { |
| 327 | const corpus = | 315 | const corpus = `${preset.name} ${preset.summary ?? ""} ${pluginLabel}`.toLowerCase(); |
| 328 | `${preset.name} ${preset.summary ?? ""} ${pluginLabel}`.toLowerCase(); | ||
| 329 | return corpus.includes(q); | 316 | return corpus.includes(q); |
| 330 | }); | 317 | }); |
| 331 | }, [allPresets, props.searchQuery]); | 318 | }, [allPresets, props.searchQuery]); |
| @@ -61,17 +61,13 @@ paths = [ · 60 unmodified lines | |||
| 61 | [[skills]] | 61 | [[skills]] |
| 62 | name = "wrdn-gha-workflows" | 62 | name = "wrdn-gha-workflows" |
| 63 | remote = "getsentry/warden-skills" | 63 | remote = "getsentry/warden-skills" |
| 64 | paths = [ | 64 | paths = [".github/workflows/**/*.yml", ".github/workflows/**/*.yaml"] |
| 65 | ".github/workflows/**/*.yml", | ||
| 66 | ".github/workflows/**/*.yaml", | ||
| 67 | ] | ||
| 68 | 65 | ||
| 69 | # Local skill — no `remote =`, resolved from `.agents/skills/<name>/SKILL.md`. | 66 | # Local skill — no `remote =`, resolved from `.agents/skills/<name>/SKILL.md`. |
| 70 | [[skills]] | 67 | [[skills]] |
| 71 | name = "wrdn-effect-atom-optimistic" | 68 | name = "wrdn-effect-atom-optimistic" |
| 72 | paths = [ | 69 | paths = [ |
| 73 | "packages/react/src/api/atoms.tsx", | 70 | "packages/react/src/api/atoms.tsx", |
| 74 | "packages/react/src/api/optimistic.tsx", | ||
| 75 | "packages/react/src/pages/**/*.tsx", | 71 | "packages/react/src/pages/**/*.tsx", |
| 76 | "packages/react/src/components/**/*.tsx", | 72 | "packages/react/src/components/**/*.tsx", |
| 77 | ] | 73 | ] |