import styles from '../styles/Youtube.module.scss' import { useState } from 'react' import fetchJson from 'helpers/fetchJson' import useApps from 'hooks/useApps' import useSettings from 'hooks/useSettings' import { open } from 'helpers/windowActions' import appList from 'configs/appList' import Splash from 'components/Splash' const time = t => new Date(t * 1000).toISOString().substr(11, 8) const App = () => { const { apps, setApps } = useApps() const [results, setResults] = useState() const [searching, setSearching] = useState(false) const [sending, setSending] = useState(false) const [enqueue, setEnqueue] = useState(false) const [type, setType] = useState('youtube_videos') const { t } = useSettings() const handleSearch = async e => { e.preventDefault() e.stopPropagation() setSearching(true) const quote = e.currentTarget.quote.value const results = await fetchJson('/api/youtube/search', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ quote }) }) results?.videos && setResults(results.videos) setSearching(false) } const handlePlay = async url => { setSending(true) const data = await fetchJson('/api/youtube/player', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ url }) }) const list = { items: [{ id: data.videoDetails.videoId, title: data.videoDetails.title, type: 'youtube-video', sources: data.formats.filter(v => v.hasAudio).sort((a, b) => a.bitrate > b.bitrate) }], enqueue } apps && apps.length > 0 && apps.some(a => a && a.name === 'Player') ? setApps(prev => prev.map(a => a.name === 'Player' ? { ...a, props: { list } } : a)) : open({ appName: 'Player', ...appList.Player }, setApps, { list }) setSending(false) } return ( <>
{time(r.duration)}
{r.title}
Views: {r.views}, uploaded: {r.uploaded || '-'}
{r.channel?.name}