Add error handling and support for multiple file paths in createTextFromFile
This commit is contained in:
16
src/App.tsx
16
src/App.tsx
@@ -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 filename = filePath.split(/[\/]/).pop() || DEFAULT_TITLE;
|
||||||
const title = filename.replace(/\.txt$/i, "") || DEFAULT_TITLE;
|
const title = filename.replace(/\.txt$/i, "") || DEFAULT_TITLE;
|
||||||
const contents = await readTextFile(filePath);
|
const contents = await readTextFile(filePath);
|
||||||
const { textId } = await createText(title, contents);
|
const { textId } = await createText(title, contents);
|
||||||
await refreshTexts();
|
await refreshTexts();
|
||||||
setSelectedTextId(textId);
|
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({
|
||||||
|
|||||||
Reference in New Issue
Block a user