History

[codex] Split preferred editor precondition errors (#3324)

Julius Marminge committed 4 months ago

Commit 71608141 parent bf1a6501 file · +35 −10Browse files

Unified · Split

apps/web/src/editorPreferences.ts+35−10
@@ -1,11 +1,11 @@
1−import { EDITORS, EditorId, type EnvironmentId } from "@t3tools/contracts";
1+import { EDITORS, EditorId, EnvironmentId } from "@t3tools/contracts";
22 import {
33 mapAtomCommandResult,
44 type AtomCommandFailure,
55 type AtomCommandResult,
66 } from "@t3tools/client-runtime/state/runtime";
77 import * as Cause from "effect/Cause";
8−import * as Data from "effect/Data";
8+import * as Schema from "effect/Schema";
99 import { AsyncResult } from "effect/unstable/reactivity";
1010 import { getLocalStorageItem, setLocalStorageItem, useLocalStorage } from "./hooks/useLocalStorage";
1111 import { useCallback, useMemo } from "react";
@@ -14,11 +14,29 @@ import { useAtomCommand } from "./state/use-atom-command"; · 2 unmodified lines
1414
1515 const LAST_EDITOR_KEY = "t3code:last-editor";
1616
17−export class PreferredEditorUnavailableError extends Data.TaggedError(
17+export class PreferredEditorEnvironmentRequiredError extends Schema.TaggedErrorClass<PreferredEditorEnvironmentRequiredError>()(
18+ "PreferredEditorEnvironmentRequiredError",
19+ {
20+ targetPath: Schema.String,
21+ },
22+) {
23+ override get message(): string {
24+ return `Cannot open ${this.targetPath} because no environment is selected.`;
25+ }
26+}
27+
28+export class PreferredEditorUnavailableError extends Schema.TaggedErrorClass<PreferredEditorUnavailableError>()(
1829 "PreferredEditorUnavailableError",
19−)<{
20− readonly message: string;
21−}> {}
30+ {
31+ environmentId: EnvironmentId,
32+ targetPath: Schema.String,
33+ availableEditorIds: Schema.Array(EditorId),
34+ },
35+) {
36+ override get message(): string {
37+ return `No available editor can open ${this.targetPath} in environment ${this.environmentId}.`;
38+ }
39+}
2240
2341 export function usePreferredEditor(availableEditors: ReadonlyArray<EditorId>) {
2442 const [lastEditor, setLastEditor] = useLocalStorage(LAST_EDITOR_KEY, null, EditorId);
@@ -55,13 +73,18 @@ export function useOpenInPreferredEditor( · 30 unmodified lines
5573 async (
5674 targetPath: string,
5775 ): Promise<
58− AtomCommandResult<EditorId, OpenInEditorError | PreferredEditorUnavailableError>
76+ AtomCommandResult<
77+ EditorId,
78+ | OpenInEditorError
79+ | PreferredEditorEnvironmentRequiredError
80+ | PreferredEditorUnavailableError
81+ >
5982 > => {
6083 if (environmentId === null) {
6184 return AsyncResult.failure(
6285 Cause.fail(
63− new PreferredEditorUnavailableError({
64− message: "No environment is selected.",
86+ new PreferredEditorEnvironmentRequiredError({
87+ targetPath,
6588 }),
6689 ),
6790 );
@@ -71,7 +94,9 @@ export function useOpenInPreferredEditor( · 3 unmodified lines
7194 return AsyncResult.failure(
7295 Cause.fail(
7396 new PreferredEditorUnavailableError({
74− message: "No available editors found.",
97+ environmentId,
98+ targetPath,
99+ availableEditorIds: availableEditors,
75100 }),
76101 ),
77102 );

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t