aboutsummaryrefslogtreecommitdiffstats
path: root/apps/Youtube/components/App.js
blob: d902346508ea6c51fdbe35d637441295d0dfe21d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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 (
    <>
      <div className='window__submenu'>
        <div>
          {[
            'youtube_videos',
            'youtube_music',
            'youtube_live',
            'youtube_channels',
            'youtube_playlists'
          ].map(y => (
            <div
              className={y === type ? 'active' : ''}
              onClick={() => { setType(y); setResults() }}
              key={y}
            >
              {t(y)}
            </div>
          ))}
          <span />
          <div
            className={enqueue ? '' : 'off'}
            onClick={() => { setEnqueue(e => !e) }}
          >
            {t('youtube_enqueue')}
          </div>
        </div>
      </div>
      <div className={styles.results}>
        <form onSubmit={handleSearch}>
          <input type='text' name='quote' />
          <input type='submit' className='window__button' value={t('search')} />
        </form>
        <div className='window__scroll'>
          {
            searching
              ? (
                <Splash />
                )
              : (
                <ul>
                  {
                    results && results.length > 0 && results.map(r => (
                      <li key={r.id} onClick={() => handlePlay(r.link)}>
                        <img loading='lazy' src={r.thumbnail} width={96} height={72} />
                        <p>{time(r.duration)}</p>
                        <div>
                          <p>{r.title}</p>
                          <p>Views: {r.views}, uploaded: {r.uploaded || '-'}</p>
                          <p>{r.channel?.name}</p>
                        </div>
                      </li>
                    ))
                  }
                  {sending && <Splash fixed />}
                </ul>
                )
          }
        </div>
      </div>
    </>
  )
}

export default App