support drag and drop upload in composer

This commit is contained in:
Ren Amamiya 2023-09-05 13:07:21 +07:00
parent 2389ad5fdc
commit 224439f62b

View File

@ -1,6 +1,7 @@
import { message } from '@tauri-apps/api/dialog';
import { UnlistenFn, listen } from '@tauri-apps/api/event';
import { Editor } from '@tiptap/react';
import { useState } from 'react';
import { useEffect, useState } from 'react';
import { MediaIcon } from '@shared/icons';
@ -10,7 +11,7 @@ export function MediaUploader({ editor }: { editor: Editor }) {
const { upload } = useNostr();
const [loading, setLoading] = useState(false);
const uploadImage = async (file?: string) => {
const uploadToNostrBuild = async (file?: string) => {
try {
// start loading
setLoading(true);
@ -26,14 +27,32 @@ export function MediaUploader({ editor }: { editor: Editor }) {
} catch (e) {
// stop loading
setLoading(false);
await message('Upload failed', { title: 'Lume', type: 'error' });
await message(`Upload failed, error: ${e}`, { title: 'Lume', type: 'error' });
}
};
useEffect(() => {
let unlisten: UnlistenFn;
async function listenDnD() {
unlisten = await listen('tauri://file-drop', (event) => {
uploadToNostrBuild(event.payload[0]);
});
}
// start listen drag and drop event
listenDnD();
// clean up
return () => {
unlisten();
};
}, []);
return (
<button
type="button"
onClick={() => uploadImage()}
onClick={() => uploadToNostrBuild()}
className="ml-2 inline-flex h-10 w-max items-center justify-center gap-1.5 rounded-lg px-2 text-sm font-medium text-white/80 hover:bg-white/10 hover:backdrop-blur-xl"
>
<MediaIcon className="h-5 w-5 text-white/80" />