35 lines · 1.5 KB · TypeScript
RawDownload
import { EDITORS, EditorId, NativeApi } from "@t3tools/contracts";import { getLocalStorageItem, setLocalStorageItem, useLocalStorage } from "./hooks/useLocalStorage";import { useMemo } from "react"; const LAST_EDITOR_KEY = "t3code:last-editor"; 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]);   return [effectiveEditor, setLastEditor] as const;} export function resolveAndPersistPreferredEditor(  availableEditors: readonly EditorId[],): EditorId | null {  const availableEditorIds = new Set(availableEditors);  const stored = getLocalStorageItem(LAST_EDITOR_KEY, EditorId);  if (stored && availableEditorIds.has(stored)) return stored;  const editor = EDITORS.find((editor) => availableEditorIds.has(editor.id))?.id ?? null;  if (editor) setLocalStorageItem(LAST_EDITOR_KEY, editor, EditorId);  return editor ?? null;} export async function openInPreferredEditor(api: NativeApi, targetPath: string): Promise<EditorId> {  const { availableEditors } = await api.server.getConfig();  const editor = resolveAndPersistPreferredEditor(availableEditors);  if (!editor) throw new Error("No available editors found.");  await api.shell.openInEditor(targetPath, editor);  return editor;}

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