-
Notifications
You must be signed in to change notification settings - Fork 5.5k
fix(web): resolve preferred editor from available editors & introduce useLocalStorage helper
#662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
juliusmarminge
merged 8 commits into
pingdotgg:main
from
mbuvarp:fix/open-editor-without-localstorage
Mar 12, 2026
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
305d4af
fix(web): resolve preferred editor from available editors
mbuvarp 1f1901f
fix(web): guard localStorage access for editor preference
mbuvarp 3b51956
Merge remote-tracking branch 'upstream/main' into fix/open-editor-wit…
mbuvarp 25ca5eb
Merge remote-tracking branch 'upstream/main' into fix/open-editor-wit…
mbuvarp 0c1f1ff
Merge remote-tracking branch 'upstream/main' into fix/open-editor-wit…
mbuvarp 1aea9ae
useLocalStorage
juliusmarminge 02d2df2
fix macro
juliusmarminge 00de43c
fmt
juliusmarminge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { | ||
| readStoredPreferredEditor, | ||
| resolveAndPersistPreferredEditor, | ||
| resolvePreferredEditor, | ||
| } from "./editorPreferences"; | ||
|
|
||
| function createStorage(initial: Record<string, string> = {}) { | ||
| const values = new Map(Object.entries(initial)); | ||
| return { | ||
| getItem(key: string) { | ||
| return values.get(key) ?? null; | ||
| }, | ||
| setItem(key: string, value: string) { | ||
| values.set(key, value); | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| describe("resolvePreferredEditor", () => { | ||
| it("prefers a stored editor when it is available", () => { | ||
| const storage = createStorage({ "t3code:last-editor": "vscode" }); | ||
| expect(resolvePreferredEditor(["cursor", "vscode", "file-manager"], storage)).toBe("vscode"); | ||
| }); | ||
|
|
||
| it("falls back to the first available editor in configured preference order", () => { | ||
| const storage = createStorage(); | ||
| expect(resolvePreferredEditor(["vscode", "file-manager"], storage)).toBe("vscode"); | ||
| }); | ||
|
|
||
| it("returns null when no editors are available", () => { | ||
| const storage = createStorage({ "t3code:last-editor": "cursor" }); | ||
| expect(resolvePreferredEditor([], storage)).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("resolveAndPersistPreferredEditor", () => { | ||
| it("persists the inferred fallback editor", () => { | ||
| const storage = createStorage(); | ||
| expect(resolveAndPersistPreferredEditor(["vscode", "file-manager"], storage)).toBe("vscode"); | ||
| expect(readStoredPreferredEditor(storage)).toBe("vscode"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import { EDITORS, type EditorId, type NativeApi } from "@t3tools/contracts"; | ||
|
|
||
| const LAST_EDITOR_KEY = "t3code:last-editor"; | ||
|
|
||
| type StorageLike = Pick<Storage, "getItem" | "setItem">; | ||
|
|
||
| function defaultStorage(): StorageLike | null { | ||
| if (typeof window === "undefined") return null; | ||
| try { | ||
| return window.localStorage; | ||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| function isEditorId(value: string | null): value is EditorId { | ||
| return EDITORS.some((editor) => editor.id === value); | ||
| } | ||
|
|
||
| export function readStoredPreferredEditor( | ||
| storage: StorageLike | null = defaultStorage(), | ||
| ): EditorId | null { | ||
| const stored = storage?.getItem(LAST_EDITOR_KEY) ?? null; | ||
| return isEditorId(stored) ? stored : null; | ||
| } | ||
|
|
||
| export function writeStoredPreferredEditor( | ||
| editor: EditorId, | ||
| storage: StorageLike | null = defaultStorage(), | ||
| ): void { | ||
| storage?.setItem(LAST_EDITOR_KEY, editor); | ||
| } | ||
|
|
||
| export function resolvePreferredEditor( | ||
| availableEditors: readonly EditorId[], | ||
| storage: StorageLike | null = defaultStorage(), | ||
| ): EditorId | null { | ||
| const stored = readStoredPreferredEditor(storage); | ||
| if (stored && availableEditors.includes(stored)) { | ||
| return stored; | ||
| } | ||
|
|
||
| const availableEditorIds = new Set(availableEditors); | ||
| return EDITORS.find((editor) => availableEditorIds.has(editor.id))?.id ?? null; | ||
| } | ||
|
|
||
| export function resolveAndPersistPreferredEditor( | ||
| availableEditors: readonly EditorId[], | ||
| storage: StorageLike | null = defaultStorage(), | ||
| ): EditorId | null { | ||
| const editor = resolvePreferredEditor(availableEditors, storage); | ||
| if (editor) { | ||
| writeStoredPreferredEditor(editor, storage); | ||
| } | ||
| return editor; | ||
| } | ||
|
|
||
| export async function openInPreferredEditor( | ||
| api: Pick<NativeApi, "server" | "shell">, | ||
| targetPath: string, | ||
| storage: StorageLike | null = defaultStorage(), | ||
| ): Promise<EditorId> { | ||
| const { availableEditors } = await api.server.getConfig(); | ||
| const editor = resolveAndPersistPreferredEditor(availableEditors, storage); | ||
| if (!editor) { | ||
| throw new Error("No available editors found."); | ||
| } | ||
|
|
||
| await api.shell.openInEditor(targetPath, editor); | ||
| return editor; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
time to add
useLocalStorageI guess 🙃 (or zustand with persist middleware like we do in other places but feels overkill for a single value here)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Want me to add it in this PR?