Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

editorPreferences.ts

115 lines3.7 KB

1import { EDITORS, EditorId, EnvironmentId } from "@t3tools/contracts";
2import {
3 mapAtomCommandResult,
4 type AtomCommandFailure,
5 type AtomCommandResult,
6} from "@t3tools/client-runtime/state/runtime";
7import * as Cause from "effect/Cause";
8import * as Schema from "effect/Schema";
9import { AsyncResult } from "effect/unstable/reactivity";
10import { getLocalStorageItem, setLocalStorageItem, useLocalStorage } from "./hooks/useLocalStorage";
11import { useCallback, useMemo } from "react";
12import { shellEnvironment } from "./state/shell";
13import { useAtomCommand } from "./state/use-atom-command";
14​
15const LAST_EDITOR_KEY = "t3code:last-editor";
16​
17export 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 }
27​
28export class PreferredEditorUnavailableError extends Schema.TaggedErrorClass<PreferredEditorUnavailableError>()(
29 "PreferredEditorUnavailableError",
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 }
40​
41export function usePreferredEditor(availableEditors: ReadonlyArray<EditorId>) {
42 const [lastEditor, setLastEditor] = useLocalStorage(LAST_EDITOR_KEY, null, EditorId);
43​
44 const effectiveEditor = useMemo(() => {
45 if (lastEditor && availableEditors.includes(lastEditor)) return lastEditor;
46 return EDITORS.find((editor) => availableEditors.includes(editor.id))?.id ?? null;
47 }, [lastEditor, availableEditors]);
48​
49 return [effectiveEditor, setLastEditor] as const;
51​
52export function resolveAndPersistPreferredEditor(
53 availableEditors: readonly EditorId[],
54): EditorId | null {
55 const availableEditorIds = new Set(availableEditors);
56 const stored = getLocalStorageItem(LAST_EDITOR_KEY, EditorId);
57 if (stored && availableEditorIds.has(stored)) return stored;
58 const editor = EDITORS.find((editor) => availableEditorIds.has(editor.id))?.id ?? null;
59 if (editor) setLocalStorageItem(LAST_EDITOR_KEY, editor, EditorId);
60 return editor ?? null;
62​
63export function useOpenInPreferredEditor(
64 environmentId: EnvironmentId | null,
65 availableEditors: readonly EditorId[],
66) {
67 const openInEditor = useAtomCommand(shellEnvironment.openInEditor, {
68 reportFailure: false,
69 });
70 type OpenInEditorError = AtomCommandFailure<Awaited<ReturnType<typeof openInEditor>>>;
71​
72 return useCallback(
73 async (
74 targetPath: string,
75 ): Promise<
76 AtomCommandResult<
77 EditorId,
78 | OpenInEditorError
79 | PreferredEditorEnvironmentRequiredError
80 | PreferredEditorUnavailableError
81 >
82 > => {
83 if (environmentId === null) {
84 return AsyncResult.failure(
85 Cause.fail(
86 new PreferredEditorEnvironmentRequiredError({
87 targetPath,
88 }),
89 ),
90 );
91 }
92 const editor = resolveAndPersistPreferredEditor(availableEditors);
93 if (!editor) {
94 return AsyncResult.failure(
95 Cause.fail(
96 new PreferredEditorUnavailableError({
97 environmentId,
98 targetPath,
99 availableEditorIds: availableEditors,
100 }),
101 ),
102 );
103 }
104 const result = await openInEditor({
105 environmentId,
106 input: {
107 cwd: targetPath,
108 editor,
109 },
110 });
111 return mapAtomCommandResult(result, () => editor);
112 },
113 [availableEditors, environmentId, openInEditor],
114 );