@@ -14,7 +14,6 @@ import * as Cause from "effect/Cause";
1414import * as Schema from "effect/Schema" ;
1515import { AsyncResult } from "effect/reactivity" ;
1616import { getLocalStorageItem , setLocalStorageItem , useLocalStorage } from "./hooks/useLocalStorage" ;
17- import { useCallback , useMemo } from "react" ;
1817import { shellEnvironment } from "./state/shell" ;
1918import { useAtomCommand } from "./state/use-atom-command" ;
2019import { readEnvironmentScope } from "./state/session" ;
@@ -48,10 +47,10 @@ export class PreferredEditorUnavailableError extends Schema.TaggedError<Preferre
4847export function usePreferredEditor ( availableEditors : ReadonlyArray < EditorId > ) {
4948 const [ lastEditor , setLastEditor ] = useLocalStorage ( LAST_EDITOR_KEY , null , EditorId ) ;
5049
51- const effectiveEditor = useMemo ( ( ) => {
52- if ( lastEditor && availableEditors . includes ( lastEditor ) ) return lastEditor ;
53- return EDITORS . find ( ( editor ) => availableEditors . includes ( editor . id ) ) ?. id ?? null ;
54- } , [ lastEditor , availableEditors ] ) ;
50+ const effectiveEditor =
51+ lastEditor && availableEditors . includes ( lastEditor )
52+ ? lastEditor
53+ : ( EDITORS . find ( ( editor ) => availableEditors . includes ( editor . id ) ) ?. id ?? null ) ;
5554
5655 return [ effectiveEditor , setLastEditor ] as const ;
5756}
@@ -74,58 +73,55 @@ export function useOpenInPreferredEditor(
7473 } ) ;
7574 type OpenInEditorError = AtomCommandFailure < Awaited < ReturnType < typeof openInEditor > > > ;
7675
77- return useCallback (
78- async (
79- targetPath : string ,
80- ) : Promise <
81- AtomCommandResult <
82- EditorId ,
83- | OpenInEditorError
84- | PreferredEditorEnvironmentRequiredError
85- | PreferredEditorUnavailableError
86- | EnvironmentAuthorizationError
87- >
88- > => {
89- if ( environmentId === null ) {
90- return AsyncResult . failure (
91- Cause . fail (
92- new PreferredEditorEnvironmentRequiredError ( {
93- targetPath,
94- } ) ,
95- ) ,
96- ) ;
97- }
98- if ( ! readEnvironmentScope ( environmentId , AuthOrchestrationOperateScope ) ) {
99- return AsyncResult . failure (
100- Cause . fail (
101- new EnvironmentAuthorizationError ( {
102- requiredScope : AuthOrchestrationOperateScope ,
103- message : "This connection cannot open an editor on this environment." ,
104- } ) ,
105- ) ,
106- ) ;
107- }
108- const editor = resolveAndPersistPreferredEditor ( availableEditors ) ;
109- if ( ! editor ) {
110- return AsyncResult . failure (
111- Cause . fail (
112- new PreferredEditorUnavailableError ( {
113- environmentId,
114- targetPath,
115- availableEditorIds : availableEditors ,
116- } ) ,
117- ) ,
118- ) ;
119- }
120- const result = await openInEditor ( {
121- environmentId,
122- input : {
123- cwd : targetPath ,
124- editor,
125- } ,
126- } ) ;
127- return mapAtomCommandResult ( result , ( ) => editor ) ;
128- } ,
129- [ availableEditors , environmentId , openInEditor ] ,
130- ) ;
76+ return async (
77+ targetPath : string ,
78+ ) : Promise <
79+ AtomCommandResult <
80+ EditorId ,
81+ | OpenInEditorError
82+ | PreferredEditorEnvironmentRequiredError
83+ | PreferredEditorUnavailableError
84+ | EnvironmentAuthorizationError
85+ >
86+ > => {
87+ if ( environmentId === null ) {
88+ return AsyncResult . failure (
89+ Cause . fail (
90+ new PreferredEditorEnvironmentRequiredError ( {
91+ targetPath,
92+ } ) ,
93+ ) ,
94+ ) ;
95+ }
96+ if ( ! readEnvironmentScope ( environmentId , AuthOrchestrationOperateScope ) ) {
97+ return AsyncResult . failure (
98+ Cause . fail (
99+ new EnvironmentAuthorizationError ( {
100+ requiredScope : AuthOrchestrationOperateScope ,
101+ message : "This connection cannot open an editor on this environment." ,
102+ } ) ,
103+ ) ,
104+ ) ;
105+ }
106+ const editor = resolveAndPersistPreferredEditor ( availableEditors ) ;
107+ if ( ! editor ) {
108+ return AsyncResult . failure (
109+ Cause . fail (
110+ new PreferredEditorUnavailableError ( {
111+ environmentId,
112+ targetPath,
113+ availableEditorIds : availableEditors ,
114+ } ) ,
115+ ) ,
116+ ) ;
117+ }
118+ const result = await openInEditor ( {
119+ environmentId,
120+ input : {
121+ cwd : targetPath ,
122+ editor,
123+ } ,
124+ } ) ;
125+ return mapAtomCommandResult ( result , ( ) => editor ) ;
126+ } ;
131127}
0 commit comments