import { NDKEvent } from '@nostr-dev-kit/ndk'; import { message, open } from '@tauri-apps/plugin-dialog'; import { readBinaryFile } from '@tauri-apps/plugin-fs'; import { useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { toast } from 'sonner'; import { useNDK } from '@libs/ndk/provider'; import { LoaderIcon } from '@shared/icons'; export function NewFileScreen() { const { ndk } = useNDK(); const navigate = useNavigate(); const [loading, setLoading] = useState(false); const [isPublish, setIsPublish] = useState(false); const [metadata, setMetadata] = useState(null); const [caption, setCaption] = useState(''); const uploadFile = async () => { try { setLoading(true); const selected = await open({ multiple: false, filters: [ { name: 'Media', extensions: [ 'png', 'jpeg', 'jpg', 'gif', 'mp4', 'mp3', 'webm', 'mkv', 'avi', 'mov', ], }, ], }); if (!selected) { setLoading(false); return; } const file = await readBinaryFile(selected.path); 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) { const json = await res.json(); const data = json.data[0]; setMetadata([ ['url', data.url], ['m', data.mime ?? 'application/octet-stream'], ['x', data.sha256 ?? ''], ['size', data.size.toString() ?? '0'], ['dim', `${data.dimensions.width}x${data.dimensions.height}` ?? '0'], ['blurhash', data.blurhash ?? ''], ['thumb', data.thumbnail ?? ''], ]); // stop loading setLoading(false); } } catch (e) { // stop loading setLoading(false); await message(`Upload failed, error: ${e}`, { title: 'Lume', type: 'error' }); } }; const submit = async () => { try { if (!ndk.signer) return navigate('/new/privkey'); setIsPublish(true); const event = new NDKEvent(ndk); event.content = caption; event.kind = 1063; event.tags = metadata; const publishedRelays = await event.publish(); if (publishedRelays) { setMetadata(null); setIsPublish(false); toast.success(`Broadcasted to ${publishedRelays.size} relays successfully.`); } } catch (e) { setIsPublish(false); toast.error(e); } }; return (
{metadata ? (
{metadata.map((item, index) => (
{item[0]}

{item[1]}

))}
setCaption(e.target.value)} spellCheck={false} autoComplete="off" autoCorrect="off" autoCapitalize="off" placeholder="Caption (Optional)..." className="h-11 w-full rounded-lg bg-neutral-200 px-3 placeholder:text-neutral-500 dark:bg-neutral-900 dark:placeholder:text-neutral-400" />
) : null}
); }