11import { useCallback , useEffect , useMemo , useRef , useState } from "react" ;
22import { useAtomSet } from "@effect/atom-react" ;
3+ import * as Exit from "effect/Exit" ;
4+ import * as Option from "effect/Option" ;
35
4- import { usePendingSources } from "@executor-js/react/api/optimistic" ;
56import { sourceWriteKeys } from "@executor-js/react/api/reactivity-keys" ;
67import { useScope } from "@executor-js/react/api/scope-context" ;
78import type { SecretPickerSecret } from "@executor-js/react/plugins/secret-picker" ;
@@ -44,7 +45,7 @@ import { FloatActions } from "@executor-js/react/components/float-actions";
4445import { Input } from "@executor-js/react/components/input" ;
4546import { RadioGroup , RadioGroupItem } from "@executor-js/react/components/radio-group" ;
4647import { IOSSpinner , Spinner } from "@executor-js/react/components/spinner" ;
47- import { addGoogleDiscoverySource , probeGoogleDiscovery } from "./atoms" ;
48+ import { addGoogleDiscoverySourceOptimistic , probeGoogleDiscovery } from "./atoms" ;
4849import { GOOGLE_DISCOVERY_OAUTH_POPUP_NAME , googleDiscoveryOAuthStrategy } from "./oauth" ;
4950import { googleDiscoveryPresets , type GoogleDiscoveryPreset } from "../sdk/presets" ;
5051
@@ -203,8 +204,9 @@ export default function AddGoogleDiscoverySource(props: {
203204
204205 const scopeId = useScope ( ) ;
205206 const doProbe = useAtomSet ( probeGoogleDiscovery , { mode : "promise" } ) ;
206- const doAdd = useAtomSet ( addGoogleDiscoverySource , { mode : "promise" } ) ;
207- const { beginAdd } = usePendingSources ( ) ;
207+ const doAdd = useAtomSet ( addGoogleDiscoverySourceOptimistic ( scopeId ) , {
208+ mode : "promiseExit" ,
209+ } ) ;
208210 const secretList = useSecretPickerSecrets ( ) ;
209211 const oauth = useOAuthPopupFlow ( {
210212 popupName : GOOGLE_DISCOVERY_OAUTH_POPUP_NAME ,
@@ -326,38 +328,36 @@ export default function AddGoogleDiscoverySource(props: {
326328 setError ( null ) ;
327329 const displayName = identity . name . trim ( ) || probe . name ;
328330 const namespace = resolvedNamespace ;
329- const placeholder = beginAdd ( {
330- id : namespace ,
331- name : displayName ,
332- kind : "google-discovery" ,
331+ const exit = await doAdd ( {
332+ params : { scopeId } ,
333+ payload : {
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 ] ,
333349 } ) ;
334- try {
335- await doAdd ( {
336- params : { scopeId } ,
337- payload : {
338- name : displayName ,
339- discoveryUrl : discoveryUrl . trim ( ) ,
340- namespace,
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" ) ;
350+ if ( Exit . isFailure ( exit ) ) {
351+ const error = Exit . findErrorOption ( exit ) ;
352+ setError (
353+ Option . isSome ( error ) && error . value instanceof Error
354+ ? error . value . message
355+ : "Failed to add source" ,
356+ ) ;
357357 setAdding ( false ) ;
358- } finally {
359- placeholder . done ( ) ;
358+ return ;
360359 }
360+ props . onComplete ( ) ;
361361 } , [
362362 probe ,
363363 doAdd ,
@@ -367,7 +367,6 @@ export default function AddGoogleDiscoverySource(props: {
367367 oauthAuth ,
368368 props ,
369369 scopeId ,
370- beginAdd ,
371370 resolvedNamespace ,
372371 ] ) ;
373372
0 commit comments