Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions apps/web/src/editorPreferences.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,24 @@ it("checks revocation and regrant when a retained editor callback is invoked", a
});
expect(state.setPreference).toHaveBeenCalledTimes(1);
});

it.each(["read", "write"] as const)(
"opens an available editor when its preference %s fails",
async (operation) => {
state.allowed.add(secondary);
const preference = operation === "read" ? state.getPreference : state.setPreference;
preference.mockImplementationOnce(() => {
throw new Error("Preference storage unavailable");
});
using _warnings = vi.spyOn(console, "warn").mockImplementation(() => undefined);
const open = useOpenInPreferredEditor(secondary, ["vscode"]);
await expect(open("/work/readme.md:5")).resolves.toMatchObject({
_tag: "Success",
value: "vscode",
});
expect(state.run).toHaveBeenCalledExactlyOnceWith({
environmentId: secondary,
input: { cwd: "/work/readme.md:5", editor: "vscode" },
});
},
);
15 changes: 13 additions & 2 deletions apps/web/src/editorPreferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,21 @@ export function usePreferredEditor(availableEditors: ReadonlyArray<EditorId>) {

function resolveAndPersistPreferredEditor(availableEditors: readonly EditorId[]): EditorId | null {
const availableEditorIds = new Set(availableEditors);
const stored = getLocalStorageItem(LAST_EDITOR_KEY, EditorId);
let stored: EditorId | null = null;
try {
stored = getLocalStorageItem(LAST_EDITOR_KEY, EditorId);
} catch (error) {
console.warn("Could not read the preferred editor.", error);
}
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);
if (editor) {
try {
setLocalStorageItem(LAST_EDITOR_KEY, editor, EditorId);
} catch (error) {
console.warn("Could not save the preferred editor.", error);
}
}
return editor ?? null;
}

Expand Down
Loading