Fix file loading dialog fallback
Some checks failed
Build Windows App / build-windows (push) Has been cancelled
Some checks failed
Build Windows App / build-windows (push) Has been cancelled
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import { app, BrowserWindow, Menu, dialog, ipcMain } from "electron";
|
import { app, BrowserWindow, Menu, dialog, ipcMain } from "electron";
|
||||||
|
import type { OpenDialogOptions } from "electron";
|
||||||
import { readFile, writeFile } from "node:fs/promises";
|
import { readFile, writeFile } from "node:fs/promises";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { fileURLToPath } from "node:url";
|
import { fileURLToPath } from "node:url";
|
||||||
@@ -31,11 +32,17 @@ async function createWindow() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ipcMain.handle("envhelper:open-file", async () => {
|
ipcMain.handle("envhelper:open-file", async (event) => {
|
||||||
const result = await dialog.showOpenDialog({
|
const owner = BrowserWindow.fromWebContents(event.sender);
|
||||||
|
const options = {
|
||||||
properties: ["openFile"],
|
properties: ["openFile"],
|
||||||
filters: [{ name: "Environment files", extensions: ["env", "txt", "*"] }]
|
filters: [
|
||||||
});
|
{ name: "Environment files", extensions: ["env", "txt"] },
|
||||||
|
{ name: "All files", extensions: ["*"] }
|
||||||
|
]
|
||||||
|
} satisfies OpenDialogOptions;
|
||||||
|
|
||||||
|
const result = owner ? await dialog.showOpenDialog(owner, options) : await dialog.showOpenDialog(options);
|
||||||
|
|
||||||
if (result.canceled || result.filePaths.length === 0) {
|
if (result.canceled || result.filePaths.length === 0) {
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
45
src/App.tsx
45
src/App.tsx
@@ -441,6 +441,33 @@ function addUnique(values: string[], value: string): string[] {
|
|||||||
return values.some((entry) => normalizeKey(entry) === normalized) ? values : [...values, value];
|
return values.some((entry) => normalizeKey(entry) === normalized) ? values : [...values, value];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function openFileWithBrowserPicker(): Promise<{ name: string; content: string } | null> {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
const input = document.createElement("input");
|
||||||
|
input.type = "file";
|
||||||
|
input.accept = ".env,.txt,text/plain";
|
||||||
|
|
||||||
|
input.addEventListener(
|
||||||
|
"change",
|
||||||
|
async () => {
|
||||||
|
const file = input.files?.[0];
|
||||||
|
if (!file) {
|
||||||
|
resolve(null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve({
|
||||||
|
name: file.name,
|
||||||
|
content: await file.text()
|
||||||
|
});
|
||||||
|
},
|
||||||
|
{ once: true }
|
||||||
|
);
|
||||||
|
|
||||||
|
input.click();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export default function App() {
|
export default function App() {
|
||||||
const [input, setInput] = useState("");
|
const [input, setInput] = useState("");
|
||||||
const [loadedPath, setLoadedPath] = useState<string | null>(null);
|
const [loadedPath, setLoadedPath] = useState<string | null>(null);
|
||||||
@@ -480,10 +507,24 @@ export default function App() {
|
|||||||
}, [ignoredAutoDefaults]);
|
}, [ignoredAutoDefaults]);
|
||||||
|
|
||||||
async function openFile() {
|
async function openFile() {
|
||||||
const file = await window.envHelper?.openFile();
|
try {
|
||||||
|
if (window.envHelper?.openFile) {
|
||||||
|
const file = await window.envHelper.openFile();
|
||||||
|
if (file) {
|
||||||
|
setInput(file.content);
|
||||||
|
setLoadedPath(file.path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.warn("Native file dialog failed, falling back to browser file input.", error);
|
||||||
|
}
|
||||||
|
|
||||||
|
const file = await openFileWithBrowserPicker();
|
||||||
if (file) {
|
if (file) {
|
||||||
setInput(file.content);
|
setInput(file.content);
|
||||||
setLoadedPath(file.path);
|
setLoadedPath(file.name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user