import * as Tooltip from '@radix-ui/react-tooltip'; import { open } from '@tauri-apps/api/dialog'; import { Body, fetch } from '@tauri-apps/api/http'; import { useState } from 'react'; import { LoaderIcon, MediaIcon } from '@shared/icons'; import { createBlobFromFile } from '@utils/createBlobFromFile'; export function MediaUploader({ setState }: { setState: any }) { const [loading, setLoading] = useState(false); const openFileDialog = async () => { const selected: any = await open({ multiple: false, filters: [ { name: 'Image & Video', extensions: ['png', 'jpeg', 'jpg', 'gif', 'mp4', 'mov'], }, ], }); if (Array.isArray(selected)) { // user selected multiple files } else if (selected === null) { // user cancelled the selection } else { // start loading setLoading(true); const filename = selected.split('/').pop(); const file = await createBlobFromFile(selected); const buf = await file.arrayBuffer(); const res: { data: { file: { id: string } } } = await fetch( 'https://void.cat/upload?cli=false', { method: 'POST', timeout: 5, headers: { accept: '*/*', 'Content-Type': 'application/octet-stream', 'V-Filename': filename, 'V-Description': 'Upload from https://lume.nu', 'V-Strip-Metadata': 'true', }, body: Body.bytes(buf), } ); const image = `https://void.cat/d/${res.data.file.id}.webp`; // update state setState((prev: string) => `${prev}\n${image}`); // stop loading setLoading(false); } }; return ( Upload media ); }