From 1d9561194d02c4194e995124fe69db9a825c9ca0 Mon Sep 17 00:00:00 2001 From: reya <123083837+reyamir@users.noreply.github.com> Date: Sun, 4 Aug 2024 12:50:26 +0700 Subject: [PATCH] feat: add attach media button --- package.json | 1 + pnpm-lock.yaml | 10 +++++ src-tauri/Cargo.lock | 1 + src-tauri/Cargo.toml | 1 + src-tauri/capabilities/default.json | 3 +- src-tauri/src/main.rs | 1 + src/commons.ts | 55 +++++++++++++++++++++++++- src/routes/$account.chats.$id.lazy.tsx | 44 +++++++++++++++++---- 8 files changed, 107 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index e0d3d5c..c35c173 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "@tauri-apps/api": "2.0.0-beta.15", "@tauri-apps/plugin-clipboard-manager": "2.1.0-beta.5", "@tauri-apps/plugin-dialog": "2.0.0-beta.7", + "@tauri-apps/plugin-fs": "2.0.0-beta.7", "@tauri-apps/plugin-os": "2.0.0-beta.7", "@tauri-apps/plugin-process": "2.0.0-beta.7", "@tauri-apps/plugin-shell": "2.0.0-beta.8", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1c21618..742e6fd 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -38,6 +38,9 @@ importers: '@tauri-apps/plugin-dialog': specifier: 2.0.0-beta.7 version: 2.0.0-beta.7 + '@tauri-apps/plugin-fs': + specifier: 2.0.0-beta.7 + version: 2.0.0-beta.7 '@tauri-apps/plugin-os': specifier: 2.0.0-beta.7 version: 2.0.0-beta.7 @@ -914,6 +917,9 @@ packages: '@tauri-apps/plugin-dialog@2.0.0-beta.7': resolution: {integrity: sha512-myywwpsKbquDDzl5zaOmmLLv5O8EJ/GgHDAoVSPwO97R4iWzkDvj3HFF91tNh7i25Tu/bP6jYPAdZA1NCRxxtg==} + '@tauri-apps/plugin-fs@2.0.0-beta.7': + resolution: {integrity: sha512-hsZyhzvy+xtRfbrKI2rmU1ZfmgbSs7Zu/6a12MFlJKVzqXmUwJvggcjYdm1cEFdLbnOOsszENXbwMnkzmxtirA==} + '@tauri-apps/plugin-os@2.0.0-beta.7': resolution: {integrity: sha512-CHo09ecxUU0NFkAqctXeQzdaXw02EXulqcaZnbjrBfRJ2ulmGq7zaUCsHihfcqWcdnmNwmP9Wh/gyznMc1JF9Q==} @@ -2427,6 +2433,10 @@ snapshots: dependencies: '@tauri-apps/api': 2.0.0-beta.15 + '@tauri-apps/plugin-fs@2.0.0-beta.7': + dependencies: + '@tauri-apps/api': 2.0.0-beta.15 + '@tauri-apps/plugin-os@2.0.0-beta.7': dependencies: '@tauri-apps/api': 2.0.0-beta.15 diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 1c7e1c4..510e7ca 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -949,6 +949,7 @@ dependencies = [ "tauri-plugin-decorum", "tauri-plugin-devtools", "tauri-plugin-dialog", + "tauri-plugin-fs", "tauri-plugin-os", "tauri-plugin-prevent-default", "tauri-plugin-process", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 71dad55..7eae105 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -30,6 +30,7 @@ tauri-plugin-dialog = "2.0.0-beta" tauri-plugin-shell = "2.0.0-beta" tauri-plugin-updater = "2.0.0-beta" tauri-plugin-process = "2.0.0-beta" +tauri-plugin-fs = "2.0.0-beta" tauri-plugin-decorum = "0.1.5" serde = { version = "1", features = ["derive"] } serde_json = "1" diff --git a/src-tauri/capabilities/default.json b/src-tauri/capabilities/default.json index de34e89..444e06c 100644 --- a/src-tauri/capabilities/default.json +++ b/src-tauri/capabilities/default.json @@ -32,6 +32,7 @@ "updater:allow-check", "updater:allow-download-and-install", "clipboard-manager:allow-write-text", - "clipboard-manager:allow-read-text" + "clipboard-manager:allow-read-text", + "fs:allow-read-file" ] } diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 774fa72..5434a02 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -117,6 +117,7 @@ fn main() { Ok(()) }) .enable_macos_default_menu(false) + .plugin(tauri_plugin_fs::init()) .plugin(tauri_plugin_prevent_default::init()) .plugin(tauri_plugin_process::init()) .plugin(tauri_plugin_updater::Builder::new().build()) diff --git a/src/commons.ts b/src/commons.ts index af767a4..14125c5 100644 --- a/src/commons.ts +++ b/src/commons.ts @@ -1,4 +1,5 @@ -import { ask, message } from "@tauri-apps/plugin-dialog"; +import { ask, message, open } from "@tauri-apps/plugin-dialog"; +import { readFile } from "@tauri-apps/plugin-fs"; import { relaunch } from "@tauri-apps/plugin-process"; import { check } from "@tauri-apps/plugin-updater"; import { type ClassValue, clsx } from "clsx"; @@ -138,3 +139,55 @@ export async function checkForAppUpdates(silent: boolean) { return; } } + +export async function upload() { + const allowExts = [ + "png", + "jpeg", + "jpg", + "gif", + "mp4", + "mp3", + "webm", + "mkv", + "avi", + "mov", + ]; + + const selected = await open({ + multiple: false, + filters: [ + { + name: "Media", + extensions: allowExts, + }, + ], + }); + + // User cancelled action + if (!selected) return null; + + try { + const selectedPath = selected.path; + const file = await readFile(selectedPath); + const blob = new Blob([file]); + + const data = new FormData(); + data.append("fileToUpload", blob); + data.append("submit", "Upload Image"); + + const res = await fetch("https://nostr.build/api/v2/upload/files", { + method: "POST", + body: data, + }); + + if (!res.ok) return null; + + const json = await res.json(); + const content = json.data[0]; + + return content.url as string; + } catch (e) { + return null; + } +} diff --git a/src/routes/$account.chats.$id.lazy.tsx b/src/routes/$account.chats.$id.lazy.tsx index d947487..159bfe6 100644 --- a/src/routes/$account.chats.$id.lazy.tsx +++ b/src/routes/$account.chats.$id.lazy.tsx @@ -1,5 +1,5 @@ import { commands } from "@/commands"; -import { cn, getReceivers, groupEventByDate, time } from "@/commons"; +import { cn, getReceivers, groupEventByDate, time, upload } from "@/commons"; import { Spinner } from "@/components/spinner"; import { User } from "@/components/user"; import { CoopIcon } from "@/icons/coop"; @@ -11,6 +11,8 @@ import { listen } from "@tauri-apps/api/event"; import { message } from "@tauri-apps/plugin-dialog"; import type { NostrEvent } from "nostr-tools"; import { + type Dispatch, + type SetStateAction, useCallback, useLayoutEffect, useRef, @@ -317,12 +319,7 @@ function Form() { ) : (
-
- -
+
); } + +function AttachMedia({ + callback, +}: { callback: Dispatch> }) { + const [isPending, startTransition] = useTransition(); + + const attach = async () => { + startTransition(async () => { + const file = await upload(); + + if (file) { + callback((prev) => `${prev} ${file}`); + } else { + return; + } + }); + }; + + return ( + + ); +}