Add error handling and support for multiple file paths in createTextFromFile

This commit is contained in:
2026-01-31 00:42:19 +01:00
parent c7af725e39
commit d69bb68cf8

View File

@@ -291,16 +291,30 @@ export default function App() {
const createTextFromFile = useCallback(
async (filePath: string) => {
const filename = filePath.split(/[\\/]/).pop() || DEFAULT_TITLE;
const title = filename.replace(/\.txt$/i, "") || DEFAULT_TITLE;
const contents = await readTextFile(filePath);
const { textId } = await createText(title, contents);
await refreshTexts();
setSelectedTextId(textId);
try {
const filename = filePath.split(/[\/]/).pop() || DEFAULT_TITLE;
const title = filename.replace(/\.txt$/i, "") || DEFAULT_TITLE;
const contents = await readTextFile(filePath);
const { textId } = await createText(title, contents);
await refreshTexts();
setSelectedTextId(textId);
} catch (error) {
console.error("Failed to open text file", error);
}
},
[refreshTexts]
);
const handleFilePaths = useCallback(
async (paths: string[]) => {
const txtPaths = paths.filter((path) => path.toLowerCase().endsWith(".txt"));
for (const path of txtPaths) {
await createTextFromFile(path);
}
},
[createTextFromFile]
);
const handleOpenText = useCallback(async () => {
const baseDir = await appDataDir();
const path = await open({