Add file opening functionality in App.tsx
This commit is contained in:
30
src/App.tsx
30
src/App.tsx
@@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user