Add file opening functionality in App.tsx

This commit is contained in:
2026-01-31 00:37:25 +01:00
parent e3052754b5
commit d8e05411a2

View File

@@ -1,6 +1,8 @@
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react"; import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { save } from "@tauri-apps/plugin-dialog"; import { open, save } from "@tauri-apps/plugin-dialog";
import { writeTextFile } from "@tauri-apps/plugin-fs"; import { readTextFile, writeTextFile } from "@tauri-apps/plugin-fs";
import { getCurrentWindow } from "@tauri-apps/api/window";
import { appDataDir } from "@tauri-apps/api/path";
import historyIcon from "./assets/history.png"; import historyIcon from "./assets/history.png";
import { import {
createText, createText,
@@ -265,6 +267,30 @@ export default function App() {
setSelectedTextId(textId); setSelectedTextId(textId);
}, [refreshTexts]); }, [refreshTexts]);
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);
},
[refreshTexts]
);
const handleOpenText = useCallback(async () => {
const baseDir = await appDataDir();
const path = await open({
multiple: false,
directory: false,
filters: [{ name: "Text", extensions: ["txt"] }],
defaultPath: baseDir
});
if (!path || Array.isArray(path)) return;
await createTextFromFile(path);
}, [createTextFromFile]);
const handleDeleteText = useCallback( const handleDeleteText = useCallback(
async (promptId: string) => { async (promptId: string) => {
await deleteText(promptId); await deleteText(promptId);