Skip to content
TypeScript

perf(web): the editor preference hooks compile without manual memoization (#20660)

Julius Marminge37db94e52045
diff --git a/apps/web/src/editorPreferences.ts b/apps/web/src/editorPreferences.ts
index 2451aad2d5d..e6aacd32b02 100644
--- a/apps/web/src/editorPreferences.ts
+++ b/apps/web/src/editorPreferences.ts
@@ -14,7 +14,6 @@ import * as Cause from "effect/Cause";
import * as Schema from "effect/Schema";
import { AsyncResult } from "effect/reactivity";
import { getLocalStorageItem, setLocalStorageItem, useLocalStorage } from "./hooks/useLocalStorage";
-import { useCallback, useMemo } from "react";
import { shellEnvironment } from "./state/shell";
import { useAtomCommand } from "./state/use-atom-command";
import { readEnvironmentScope } from "./state/session";
@@ -48,10 +47,10 @@ export class PreferredEditorUnavailableError extends Schema.TaggedError<Preferre
export function usePreferredEditor(availableEditors: ReadonlyArray<EditorId>) {
const [lastEditor, setLastEditor] = useLocalStorage(LAST_EDITOR_KEY, null, EditorId);
- const effectiveEditor = useMemo(() => {
- if (lastEditor && availableEditors.includes(lastEditor)) return lastEditor;
- return EDITORS.find((editor) => availableEditors.includes(editor.id))?.id ?? null;
- }, [lastEditor, availableEditors]);
+ const effectiveEditor =
+ lastEditor && availableEditors.includes(lastEditor)
+ ? lastEditor
+ : (EDITORS.find((editor) => availableEditors.includes(editor.id))?.id ?? null);
return [effectiveEditor, setLastEditor] as const;
}
@@ -74,58 +73,55 @@ export function useOpenInPreferredEditor(
});
type OpenInEditorError = AtomCommandFailure<Awaited<ReturnType<typeof openInEditor>>>;
- return useCallback(
- async (
- targetPath: string,
- ): Promise<
- AtomCommandResult<
- EditorId,
- | OpenInEditorError
- | PreferredEditorEnvironmentRequiredError
- | PreferredEditorUnavailableError
- | EnvironmentAuthorizationError
- >
- > => {
- if (environmentId === null) {
- return AsyncResult.failure(
- Cause.fail(
- new PreferredEditorEnvironmentRequiredError({
- targetPath,
- }),
- ),
- );
- }
- if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) {
- return AsyncResult.failure(
- Cause.fail(
- new EnvironmentAuthorizationError({
- requiredScope: AuthOrchestrationOperateScope,
- message: "This connection cannot open an editor on this environment.",
- }),
- ),
- );
- }
- const editor = resolveAndPersistPreferredEditor(availableEditors);
- if (!editor) {
- return AsyncResult.failure(
- Cause.fail(
- new PreferredEditorUnavailableError({
- environmentId,
- targetPath,
- availableEditorIds: availableEditors,
- }),
- ),
- );
- }
- const result = await openInEditor({
- environmentId,
- input: {
- cwd: targetPath,
- editor,
- },
- });
- return mapAtomCommandResult(result, () => editor);
- },
- [availableEditors, environmentId, openInEditor],
- );
+ return async (
+ targetPath: string,
+ ): Promise<
+ AtomCommandResult<
+ EditorId,
+ | OpenInEditorError
+ | PreferredEditorEnvironmentRequiredError
+ | PreferredEditorUnavailableError
+ | EnvironmentAuthorizationError
+ >
+ > => {
+ if (environmentId === null) {
+ return AsyncResult.failure(
+ Cause.fail(
+ new PreferredEditorEnvironmentRequiredError({
+ targetPath,
+ }),
+ ),
+ );
+ }
+ if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) {
+ return AsyncResult.failure(
+ Cause.fail(
+ new EnvironmentAuthorizationError({
+ requiredScope: AuthOrchestrationOperateScope,
+ message: "This connection cannot open an editor on this environment.",
+ }),
+ ),
+ );
+ }
+ const editor = resolveAndPersistPreferredEditor(availableEditors);
+ if (!editor) {
+ return AsyncResult.failure(
+ Cause.fail(
+ new PreferredEditorUnavailableError({
+ environmentId,
+ targetPath,
+ availableEditorIds: availableEditors,
+ }),
+ ),
+ );
+ }
+ const result = await openInEditor({
+ environmentId,
+ input: {
+ cwd: targetPath,
+ editor,
+ },
+ });
+ return mapAtomCommandResult(result, () => editor);
+ };
}