The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | AuthOrchestrationOperateScope, |
| 3 | EDITORS, |
| 4 | EditorId, |
| 5 | EnvironmentAuthorizationError, |
| 6 | EnvironmentId, |
| 7 | } from "@t3tools/contracts"; |
| 8 | import { |
| 9 | mapAtomCommandResult, |
| 10 | type AtomCommandFailure, |
| 11 | type AtomCommandResult, |
| 12 | } from "@t3tools/client-runtime/state/runtime"; |
| 13 | import { parseFileUrlHref, splitFilePathPosition } from "@t3tools/shared/fileLinks"; |
| 14 | import { safeDecodeURIComponent } from "@t3tools/shared/markdownLinks"; |
| 15 | import * as Cause from "effect/Cause"; |
| 16 | import * as Schema from "effect/Schema"; |
| 17 | import { AsyncResult } from "effect/reactivity"; |
| 18 | import { msg } from "gt-react"; |
| 19 | import { getLocalStorageItem, setLocalStorageItem, useLocalStorage } from "./hooks/useLocalStorage"; |
| 20 | import { m } from "./i18n/messages"; |
| 21 | import { shellEnvironment } from "./state/shell"; |
| 22 | import { useAtomCommand } from "./state/use-atom-command"; |
| 23 | import { readEnvironmentScope } from "./state/session"; |
| 24 | |
| 25 | const LAST_EDITOR_KEY = "t3code:last-editor"; |
| 26 | |
| 27 | export class PreferredEditorEnvironmentRequiredError extends Schema.TaggedError<PreferredEditorEnvironmentRequiredError>()( |
| 28 | "PreferredEditorEnvironmentRequiredError", |
| 29 | { |
| 30 | targetPath: Schema.String, |
| 31 | }, |
| 32 | ) { |
| 33 | override get message(): string { |
| 34 | return m( |
| 35 | msg("Cannot open {targetPath} because no environment is selected.", { |
| 36 | targetPath: this.targetPath, |
| 37 | }), |
| 38 | ); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | export class PreferredEditorUnavailableError extends Schema.TaggedError<PreferredEditorUnavailableError>()( |
| 43 | "PreferredEditorUnavailableError", |
| 44 | { |
| 45 | environmentId: EnvironmentId, |
| 46 | targetPath: Schema.String, |
| 47 | availableEditorIds: Schema.Array(EditorId), |
| 48 | }, |
| 49 | ) { |
| 50 | override get message(): string { |
| 51 | return m( |
| 52 | msg("No available editor can open {targetPath} in environment {environmentId}.", { |
| 53 | targetPath: this.targetPath, |
| 54 | environmentId: this.environmentId, |
| 55 | }), |
| 56 | ); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | export function usePreferredEditor(availableEditors: ReadonlyArray<EditorId>) { |
| 61 | const [lastEditor, setLastEditor] = useLocalStorage(LAST_EDITOR_KEY, null, EditorId); |
| 62 | |
| 63 | const effectiveEditor = |
| 64 | lastEditor && availableEditors.includes(lastEditor) |
| 65 | ? lastEditor |
| 66 | : (EDITORS.find((editor) => availableEditors.includes(editor.id))?.id ?? null); |
| 67 | |
| 68 | return [effectiveEditor, setLastEditor] as const; |
| 69 | } |
| 70 | |
| 71 | function resolveAndPersistPreferredEditor(availableEditors: readonly EditorId[]): EditorId | null { |
| 72 | const availableEditorIds = new Set(availableEditors); |
| 73 | let stored: EditorId | null = null; |
| 74 | try { |
| 75 | stored = getLocalStorageItem(LAST_EDITOR_KEY, EditorId); |
| 76 | } catch (error) { |
| 77 | console.warn("Could not read the preferred editor.", error); |
| 78 | } |
| 79 | if (stored && availableEditorIds.has(stored)) return stored; |
| 80 | const editor = EDITORS.find((editor) => availableEditorIds.has(editor.id))?.id ?? null; |
| 81 | if (editor) { |
| 82 | try { |
| 83 | setLocalStorageItem(LAST_EDITOR_KEY, editor, EditorId); |
| 84 | } catch (error) { |
| 85 | console.warn("Could not save the preferred editor.", error); |
| 86 | } |
| 87 | } |
| 88 | return editor ?? null; |
| 89 | } |
| 90 | |
| 91 | export function useOpenInPreferredEditor( |
| 92 | environmentId: EnvironmentId | null, |
| 93 | availableEditors: readonly EditorId[], |
| 94 | projectRoot?: string, |
| 95 | ) { |
| 96 | const openInEditor = useAtomCommand(shellEnvironment.openInEditor, { |
| 97 | reportFailure: false, |
| 98 | }); |
| 99 | type OpenInEditorError = AtomCommandFailure<Awaited<ReturnType<typeof openInEditor>>>; |
| 100 | |
| 101 | return async ( |
| 102 | targetPath: string, |
| 103 | ): Promise< |
| 104 | AtomCommandResult< |
| 105 | EditorId, |
| 106 | | OpenInEditorError |
| 107 | | PreferredEditorEnvironmentRequiredError |
| 108 | | PreferredEditorUnavailableError |
| 109 | | EnvironmentAuthorizationError |
| 110 | > |
| 111 | > => { |
| 112 | if (environmentId === null) { |
| 113 | return AsyncResult.failure( |
| 114 | Cause.fail( |
| 115 | new PreferredEditorEnvironmentRequiredError({ |
| 116 | targetPath, |
| 117 | }), |
| 118 | ), |
| 119 | ); |
| 120 | } |
| 121 | if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) { |
| 122 | return AsyncResult.failure( |
| 123 | Cause.fail( |
| 124 | new EnvironmentAuthorizationError({ |
| 125 | requiredScope: AuthOrchestrationOperateScope, |
| 126 | message: m(msg("This connection cannot open an editor on this environment.")), |
| 127 | }), |
| 128 | ), |
| 129 | ); |
| 130 | } |
| 131 | const editor = resolveAndPersistPreferredEditor(availableEditors); |
| 132 | if (!editor) { |
| 133 | return AsyncResult.failure( |
| 134 | Cause.fail( |
| 135 | new PreferredEditorUnavailableError({ |
| 136 | environmentId, |
| 137 | targetPath, |
| 138 | availableEditorIds: availableEditors, |
| 139 | }), |
| 140 | ), |
| 141 | ); |
| 142 | } |
| 143 | const uri = /^file:\/\//iu.test(targetPath) ? parseFileUrlHref(targetPath) : null; |
| 144 | const { line, column } = uri ? splitFilePathPosition("", uri.hash) : {}; |
| 145 | const result = await openInEditor({ |
| 146 | environmentId, |
| 147 | input: { |
| 148 | cwd: uri ? safeDecodeURIComponent(uri.path) : targetPath, |
| 149 | ...(uri ? { position: { line, column } } : {}), |
| 150 | editor, |
| 151 | ...(projectRoot ? { projectRoot } : {}), |
| 152 | }, |
| 153 | }); |
| 154 | return mapAtomCommandResult(result, () => editor); |
| 155 | }; |
| 156 | } |