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( const createTextFromFile = useCallback(
async (filePath: string) => { async (filePath: string) => {
const filename = filePath.split(/[\\/]/).pop() || DEFAULT_TITLE; try {
const title = filename.replace(/\.txt$/i, "") || DEFAULT_TITLE; const filename = filePath.split(/[\/]/).pop() || DEFAULT_TITLE;
const contents = await readTextFile(filePath); const title = filename.replace(/\.txt$/i, "") || DEFAULT_TITLE;
const { textId } = await createText(title, contents); const contents = await readTextFile(filePath);
await refreshTexts(); const { textId } = await createText(title, contents);
setSelectedTextId(textId); await refreshTexts();
setSelectedTextId(textId);
} catch (error) {
console.error("Failed to open text file", error);
}
}, },
[refreshTexts] [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 handleOpenText = useCallback(async () => {
const baseDir = await appDataDir(); const baseDir = await appDataDir();
const path = await open({ const path = await open({