@@ -33,10 +33,9 @@ import {
3333} from "@t3tools/shared/backgroundActivitySettings" ;
3434import * as Arr from "effect/Array" ;
3535import * as Duration from "effect/Duration" ;
36- import * as Equal from "effect/Equal" ;
3736import * as Result from "effect/Result" ;
3837import { PlusIcon } from "lucide-react" ;
39- import { useCallback , useEffect , useMemo , useRef , useState , type ReactNode } from "react" ;
38+ import { useRef , useState , type ReactNode } from "react" ;
4039
4140import { isDesktopLocalConnectionTarget } from "../../connection/desktopLocal" ;
4241import {
@@ -292,10 +291,10 @@ function ProviderSettingsPanelContent(target: ProviderSettingsTarget) {
292291 const { environments, isReady } = useEnvironments ( ) ;
293292 const primaryEnvironmentId = usePrimaryEnvironmentId ( ) ;
294293 const searchTargetId = useSettingsSearchTargetId ( ) ;
295- const options = useMemo (
296- ( ) =>
297- buildProviderEnvironmentOptions ( environments , primaryEnvironmentId , target . environmentIds ) ,
298- [ environments , primaryEnvironmentId , target . environmentIds ] ,
294+ const options = buildProviderEnvironmentOptions (
295+ environments ,
296+ primaryEnvironmentId ,
297+ target . environmentIds ,
299298 ) ;
300299 const [ selectedEnvironmentId , setSelectedEnvironmentId ] = useState < EnvironmentId | null > (
301300 target . environmentId ?? primaryEnvironmentId ,
@@ -330,34 +329,33 @@ function ProviderSettingsPanelContent(target: ProviderSettingsTarget) {
330329 hasServerConfig : true ,
331330 } ) ,
332331 ) ?. environmentId ;
333- useEffect ( ( ) => {
332+ // A search jump to a setting the selected device cannot show switches to one that can.
333+ let searchRedirectEnvironmentId : EnvironmentId | undefined ;
334+ {
334335 if (
335336 ! target . scoped &&
336337 searchTargetId === searchableSetting ( "cursor-keychain-usage" ) . id &&
337338 ( ! selectedEnvironmentCanRenderSettings ||
338339 selectedEnvironment ?. serverConfig ?. environment . platform . os !== "darwin" ) &&
339340 searchableCursorEnvironmentId !== undefined
340341 ) {
341- setSelectedEnvironmentId ( searchableCursorEnvironmentId ) ;
342- return ;
343- }
344- if (
342+ searchRedirectEnvironmentId = searchableCursorEnvironmentId ;
343+ } else if (
345344 ! target . scoped &&
346345 ( searchTargetId === searchableSetting ( "provider-health-check-interval" ) . id ||
347346 searchTargetId === searchableSetting ( "usage-providers" ) . id ) &&
348347 ! selectedEnvironmentCanRenderSettings &&
349348 searchableEnvironmentId !== undefined
350349 ) {
351- setSelectedEnvironmentId ( searchableEnvironmentId ) ;
350+ searchRedirectEnvironmentId = searchableEnvironmentId ;
352351 }
353- } , [
354- searchTargetId ,
355- searchableCursorEnvironmentId ,
356- searchableEnvironmentId ,
357- selectedEnvironment ,
358- selectedEnvironmentCanRenderSettings ,
359- target . scoped ,
360- ] ) ;
352+ }
353+ if (
354+ searchRedirectEnvironmentId !== undefined &&
355+ searchRedirectEnvironmentId !== selectedEnvironmentId
356+ ) {
357+ setSelectedEnvironmentId ( searchRedirectEnvironmentId ) ;
358+ }
361359 const onlyPrimaryDevice =
362360 options . length === 1 && options [ 0 ] ?. entry . target . _tag === "PrimaryConnectionTarget" ;
363361 const deviceTabs =
@@ -538,13 +536,15 @@ function AccessGatedProviderSettings({
538536 ) ;
539537}
540538
539+ const NO_PROJECT_IDS : readonly ProjectId [ ] = [ ] ;
540+
541541export function EnvironmentProviderSettings ( {
542542 environmentId,
543543 environmentLabel,
544544 readOnly = false ,
545545 deviceTabs,
546546 targetInstanceId,
547- projectIds = [ ] ,
547+ projectIds = NO_PROJECT_IDS ,
548548} : {
549549 readonly environmentId : EnvironmentId ;
550550 readonly environmentLabel : string ;
@@ -593,42 +593,38 @@ export function EnvironmentProviderSettings({
593593 const refreshingRef = useRef ( false ) ;
594594 const updatingInstanceIdsRef = useRef < Set < ProviderInstanceId > > ( new Set ( ) ) ;
595595
596- const acceptUrlAuthentication = useCallback (
597- ( instanceId : ProviderInstanceId , action : AcpRegistryUrlAuthAction ) => {
598- void acceptAcpRegistryUrlAuth ( {
599- environmentId ,
600- input : { instanceId , elicitationId : action . elicitationId } ,
601- } ) . then ( ( result ) => {
602- if ( result . _tag === "Success" && ! result . value . accepted ) {
603- toastManager . add ( {
604- type : "warning" ,
605- title : "Authentication request expired" ,
606- description : "Refresh the provider and start the authentication flow again. " ,
607- } ) ;
608- return ;
609- }
610- if ( result . _tag === "Failure" && ! isAtomCommandInterrupted ( result ) ) {
611- const error = squashAtomCommandFailure ( result ) ;
612- toastManager . add ( {
613- type : "error" ,
614- title : "Could not continue authentication" ,
615- description :
616- error instanceof Error ? error . message : "The authentication request expired. " ,
617- } ) ;
618- }
619- } ) ;
620- } ,
621- [ acceptAcpRegistryUrlAuth , environmentId ] ,
622- ) ;
596+ const acceptUrlAuthentication = (
597+ instanceId : ProviderInstanceId ,
598+ action : AcpRegistryUrlAuthAction ,
599+ ) => {
600+ void acceptAcpRegistryUrlAuth ( {
601+ environmentId ,
602+ input : { instanceId , elicitationId : action . elicitationId } ,
603+ } ) . then ( ( result ) => {
604+ if ( result . _tag === "Success" && ! result . value . accepted ) {
605+ toastManager . add ( {
606+ type : "warning " ,
607+ title : "Authentication request expired" ,
608+ description : "Refresh the provider and start the authentication flow again." ,
609+ } ) ;
610+ return ;
611+ }
612+ if ( result . _tag === "Failure" && ! isAtomCommandInterrupted ( result ) ) {
613+ const error = squashAtomCommandFailure ( result ) ;
614+ toastManager . add ( {
615+ type : "error" ,
616+ title : "Could not continue authentication " ,
617+ description :
618+ error instanceof Error ? error . message : "The authentication request expired." ,
619+ } ) ;
620+ }
621+ } ) ;
622+ } ;
623623
624- const providerUpdateCandidateByInstanceId = useMemo (
625- ( ) =>
626- new Map (
627- serverProviders
628- . filter ( isProviderSettingsUpdateCandidate )
629- . map ( ( candidate ) => [ candidate . instanceId , candidate ] ) ,
630- ) ,
631- [ serverProviders ] ,
624+ const providerUpdateCandidateByInstanceId = new Map (
625+ serverProviders
626+ . filter ( isProviderSettingsUpdateCandidate )
627+ . map ( ( candidate ) => [ candidate . instanceId , candidate ] ) ,
632628 ) ;
633629 const visibleProviderSettings = PROVIDER_SETTINGS . filter (
634630 ( providerSettings ) =>
@@ -656,7 +652,7 @@ export function EnvironmentProviderSettings({
656652 )
657653 : null ;
658654
659- const refreshProviders = useCallback ( ( ) => {
655+ const refreshProviders = ( ) => {
660656 if ( refreshingRef . current || ! readEnvironmentScope ( environmentId , AuthOrchestrationReadScope ) )
661657 return ;
662658 refreshingRef . current = true ;
@@ -676,55 +672,52 @@ export function EnvironmentProviderSettings({
676672 } ) ;
677673 }
678674 } ) ( ) ;
679- } , [ environmentId , refreshServerProviders ] ) ;
675+ } ;
680676
681- const runProviderUpdate = useCallback (
682- async (
683- candidate : Pick < ProviderSettingsUpdateCandidate , "driver" | "instanceId" > ,
684- targetVersion ?: string ,
685- ) => {
686- if ( ! readEnvironmentScope ( environmentId , AuthProvidersManageScope ) ) return ;
687- // Ref-based re-entry guard, mirroring refreshProviders: a state updater
688- // may run after this function returns, so it cannot gate the dispatch.
689- if ( updatingInstanceIdsRef . current . has ( candidate . instanceId ) ) {
690- return ;
691- }
692- updatingInstanceIdsRef . current . add ( candidate . instanceId ) ;
693- setUpdatingProviderInstanceIds ( ( previous ) => new Set ( previous ) . add ( candidate . instanceId ) ) ;
677+ const runProviderUpdate = async (
678+ candidate : Pick < ProviderSettingsUpdateCandidate , "driver" | "instanceId" > ,
679+ targetVersion ?: string ,
680+ ) => {
681+ if ( ! readEnvironmentScope ( environmentId , AuthProvidersManageScope ) ) return ;
682+ // Ref-based re-entry guard, mirroring refreshProviders: a state updater
683+ // may run after this function returns, so it cannot gate the dispatch.
684+ if ( updatingInstanceIdsRef . current . has ( candidate . instanceId ) ) {
685+ return ;
686+ }
687+ updatingInstanceIdsRef . current . add ( candidate . instanceId ) ;
688+ setUpdatingProviderInstanceIds ( ( previous ) => new Set ( previous ) . add ( candidate . instanceId ) ) ;
694689
695- const result = await updateProvider ( {
696- environmentId,
697- input : {
698- provider : candidate . driver ,
699- instanceId : candidate . instanceId ,
700- ...( targetVersion ? { targetVersion } : { } ) ,
701- } ,
702- } ) ;
703- if ( result . _tag === "Failure" && ! isAtomCommandInterrupted ( result ) ) {
704- const error = squashAtomCommandFailure ( result ) ;
705- toastManager . add (
706- stackedThreadToast ( {
707- type : "error" ,
708- title : `Could not update ${ PROVIDER_DISPLAY_NAMES [ candidate . driver ] ?? candidate . driver } ` ,
709- description :
710- error instanceof Error
711- ? error . message
712- : "The provider update command could not be started." ,
713- } ) ,
714- ) ;
690+ const result = await updateProvider ( {
691+ environmentId,
692+ input : {
693+ provider : candidate . driver ,
694+ instanceId : candidate . instanceId ,
695+ ...( targetVersion ? { targetVersion } : { } ) ,
696+ } ,
697+ } ) ;
698+ if ( result . _tag === "Failure" && ! isAtomCommandInterrupted ( result ) ) {
699+ const error = squashAtomCommandFailure ( result ) ;
700+ toastManager . add (
701+ stackedThreadToast ( {
702+ type : "error" ,
703+ title : `Could not update ${ PROVIDER_DISPLAY_NAMES [ candidate . driver ] ?? candidate . driver } ` ,
704+ description :
705+ error instanceof Error
706+ ? error . message
707+ : "The provider update command could not be started." ,
708+ } ) ,
709+ ) ;
710+ }
711+ updatingInstanceIdsRef . current . delete ( candidate . instanceId ) ;
712+ setUpdatingProviderInstanceIds ( ( previous ) => {
713+ if ( ! previous . has ( candidate . instanceId ) ) {
714+ return previous ;
715715 }
716- updatingInstanceIdsRef . current . delete ( candidate . instanceId ) ;
717- setUpdatingProviderInstanceIds ( ( previous ) => {
718- if ( ! previous . has ( candidate . instanceId ) ) {
719- return previous ;
720- }
721- const next = new Set ( previous ) ;
722- next . delete ( candidate . instanceId ) ;
723- return next ;
724- } ) ;
725- } ,
726- [ environmentId , updateProvider ] ,
727- ) ;
716+ const next = new Set ( previous ) ;
717+ next . delete ( candidate . instanceId ) ;
718+ return next ;
719+ } ) ;
720+ } ;
728721
729722 interface InstanceRow {
730723 readonly instanceId : ProviderInstanceId ;
0 commit comments