Open History to see the last commit
1import {
2 AuthOrchestrationOperateScope,
3 EDITORS,
4 EditorId,
5 EnvironmentAuthorizationError,
6 EnvironmentId,
7} from "@t3tools/contracts";
8import {
9 mapAtomCommandResult,
10 type AtomCommandFailure,
11 type AtomCommandResult,
12} from "@t3tools/client-runtime/state/runtime";
13import * as Cause from "effect/Cause";
14import * as Schema from "effect/Schema";
15import { AsyncResult } from "effect/reactivity";
16import { getLocalStorageItem, setLocalStorageItem, useLocalStorage } from "./hooks/useLocalStorage";
17import { shellEnvironment } from "./state/shell";
18import { useAtomCommand } from "./state/use-atom-command";
19import { readEnvironmentScope } from "./state/session";
20
21const LAST_EDITOR_KEY = "t3code:last-editor";
22
23export class PreferredEditorEnvironmentRequiredError extends Schema.TaggedError<PreferredEditorEnvironmentRequiredError>()(
24 "PreferredEditorEnvironmentRequiredError",
25 {
26 targetPath: Schema.String,
27 },
28) {
29 override get message(): string {
30 return `Cannot open ${this.targetPath} because no environment is selected.`;
31 }
32}
33
34export class PreferredEditorUnavailableError extends Schema.TaggedError<PreferredEditorUnavailableError>()(
35 "PreferredEditorUnavailableError",
36 {
37 environmentId: EnvironmentId,
38 targetPath: Schema.String,
39 availableEditorIds: Schema.Array(EditorId),
40 },
41) {
42 override get message(): string {
43 return `No available editor can open ${this.targetPath} in environment ${this.environmentId}.`;
44 }
45}
46
47export function usePreferredEditor(availableEditors: ReadonlyArray<EditorId>) {
48 const [lastEditor, setLastEditor] = useLocalStorage(LAST_EDITOR_KEY, null, EditorId);
49
50 const effectiveEditor =
51 lastEditor && availableEditors.includes(lastEditor)
52 ? lastEditor
53 : (EDITORS.find((editor) => availableEditors.includes(editor.id))?.id ?? null);
54
55 return [effectiveEditor, setLastEditor] as const;
56}
57
58function resolveAndPersistPreferredEditor(availableEditors: readonly EditorId[]): EditorId | null {
59 const availableEditorIds = new Set(availableEditors);
60 const stored = getLocalStorageItem(LAST_EDITOR_KEY, EditorId);
61 if (stored && availableEditorIds.has(stored)) return stored;
62 const editor = EDITORS.find((editor) => availableEditorIds.has(editor.id))?.id ?? null;
63 if (editor) setLocalStorageItem(LAST_EDITOR_KEY, editor, EditorId);
64 return editor ?? null;
65}
66
67export function useOpenInPreferredEditor(
68 environmentId: EnvironmentId | null,
69 availableEditors: readonly EditorId[],
70) {
71 const openInEditor = useAtomCommand(shellEnvironment.openInEditor, {
72 reportFailure: false,
73 });
74 type OpenInEditorError = AtomCommandFailure<Awaited<ReturnType<typeof openInEditor>>>;
75
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 };
127}