import React, {useCallback, useEffect, useState} from 'react'; import * as api from '../../api' import {useDropzone} from 'react-dropzone' import File from './file.jsx' import purchases from './fake' export default function Media(props) { const [loggedIn, setLoggedIn] = useState(false) const [description, setDescription] = useState('') const [myFiles, setMyFiles] = useState([]) const [files, setFiles] = useState([]) const [searchText, setSearchText] = useState('') const [selectedMedia, setSelectedMedia] = useState(null) const [img, setImg] = useState(null) async function sign() { const ch = await api.media.GET('ask') const res = await api.relay.GET(`signer/${ch.challenge}`) const body = await api.media.POST('verify', { id: ch.id, sig: res.response.sig, pubkey: props.identity_pubkey }) if(body && body.token) { console.log('success!') setLoggedIn(true) } } useEffect(() => { async function load(){ const hasToken = localStorage.getItem(api.media.tokenName) await sign() setLoggedIn(true) const media = await api.media.GET('mymedia') console.log(media) setMyFiles(media) } load() }, []); const onDrop = useCallback(async files => { const ttl = 60*60*24*7 // one week const file = files[0] || {} var start = new Date(); const r = await api.media.UPLOAD('file',file,{ name: file.name||'filename', description: description||'description', ttl: ttl, }) var end = new Date(); var time = end.getTime() - start.getTime(); console.log('Upload Timer: finished in', time, 'ms'); await api.relay.POST('attachment', { muid: r.muid, file_name: r.filename, chat_id: 13, ttl: ttl, }) }, [description]) // subscribe to "description" async function search(){ const medias = await api.media.GET(`search/${searchText}`) console.log(medias) setFiles(medias) } async function buy(){ const {contacts, chats} = props console.log(contacts, chats) const contact = contacts && contacts.find(c=>!c.is_owner) const chat = chats.find(ch=> ch.contact_ids.includes(parseInt(contact.id))) if(!contact || !chat) return const p = await api.relay.POST('purchase',{ chat_id: chat.id, contact_id: contact.id, amount: selectedMedia.price, muid: selectedMedia.muid }) console.log(p) } const {getRootProps, getInputProps, isDragActive} = useDropzone({onDrop}) if(!loggedIn){ return