A clean and minimal youtube frontend, without all the ads and whistles
JavaScript
refactor: extract router, move fns
Christian Fei885a716896b2
diff --git a/client/index.html b/client/index.htmlindex 0f83b97..122a485 100644--- a/client/index.html+++ b/client/index.html@@ -80,6 +80,7 @@</template><script type="module" src="/main.js"></script>+ <script type="module" src="/lib/router.js"></script><script type="module" src="/components/empty-state.js"></script><script type="module" src="/components/video-filters.js"></script><script type="module" src="/components/search-videos.js"></script>diff --git a/client/lib/router.js b/client/lib/router.jsnew file mode 100644index 0000000..e7d5b7b--- /dev/null+++ b/client/lib/router.js@@ -0,0 +1,121 @@++function handleRoute(route = window.location.pathname) {+ console.log('handling route', route)+ if (routes[route]) {+ document.querySelector('main').replaceChildren(routes[route].template.content.cloneNode(true))+ routes[route].initialize && routes[route].initialize()+ } else {+ document.querySelector('main').replaceChildren(routes['/404'].template.content.cloneNode(true))+ routes['/404'].initialize && routes['/404'].initialize()+ }+}++const routes = {+ '/': { template: document.getElementById('main-template'), async initialize() {+ console.log('initialize /')++ let $videosContainer = document.querySelector('.main-videos-container')+ if (!$videosContainer) return+ let videos = await fetch('/videos').then(res => res.json())+ console.log('videos.length', videos.length)+ if (videos.length === 0) {+ document.querySelector('empty-state').style.display = ''+ document.querySelector('channels-list').style.display = 'none'+ document.querySelector('video-filters').style.display = 'none'+ } else {+ document.querySelector('empty-state').style.display = 'none'+ document.querySelector('channels-list').style.display = ''+ document.querySelector('video-filters').style.display = ''+ }++ document.getElementById('search').removeAttribute('disabled')++ $videosContainer.innerHTML = ''+ const ignoredTerms = store.get(store.ignoreTermsKey)+ const showOriginalThumbnail = store.get(store.showOriginalThumbnailKey)++ videos = videos+ .filter(video => video.title.split(' ').every(word => !ignoredTerms.includes(word.toLowerCase().replace(/('s|"|,|:)/,''))))+ .sort((a, b) => +new Date(b.publishedAt) - +new Date(a.publishedAt))+ // .filter((_, i) => i < 1000)++ videos.forEach(video => $videosContainer.appendChild(createVideoElement(video, showOriginalThumbnail)))++ const channelsList = videos.reduce((acc, video) => {+ if (!acc.includes(video.channelName)) acc.push(video.channelName)+ return acc+ }, []).join(',')+ document.querySelector('channels-list').dataset['list'] = channelsList++ applyShowThumbnails(store.get(store.showThumbnailsKey))+ applyShowBigPlayer(store.get(store.showBigPlayerKey))+ } },+ '/settings': { template: document.getElementById('settings-template'), async initialize () {+ console.log('initalize /settings')++ document.getElementById('search').setAttribute('disabled', 'disabled')++ const $showThumbnails = document.getElementById('show-thumbnails')+ store.get(store.showThumbnailsKey) ? $showThumbnails.setAttribute('checked', 'true') : $showThumbnails.removeAttribute('checked')++ $showThumbnails.addEventListener('click', (event) => {+ store.toggle(store.showThumbnailsKey)+ applyShowThumbnails(store.get(store.showThumbnailsKey))+ })++ const $showBigPlayer = document.getElementById('show-big-player')+ store.get(store.showBigPlayerKey) ? $showBigPlayer.setAttribute('checked', 'true') : $showBigPlayer.removeAttribute('checked')++ $showBigPlayer.addEventListener('click', (event) => {+ store.toggle(store.showBigPlayerKey)+ applyShowBigPlayer(store.get(store.showBigPlayerKey))+ })++ const $showOriginalThumbnail = document.getElementById('show-original-thumbnail')+ store.get(store.showOriginalThumbnailKey) ? $showOriginalThumbnail.setAttribute('checked', 'true') : $showOriginalThumbnail.removeAttribute('checked')++ $showOriginalThumbnail.addEventListener('click', (event) => {+ store.toggle(store.showOriginalThumbnailKey)+ })+ } },+ '/404': { template: document.getElementById('not-found-template'), async initialize () {+ document.getElementById('search').setAttribute('disabled', 'disabled')+ } }+}++handleRoute()+window.addEventListener('popstate', (event) => {+ console.log('popstate', window.location.pathname)+ handleRoute()+})+document.querySelectorAll('[href="/"],[href="/settings"]').forEach(($el) => {+ $el.addEventListener('click', (event) => {+ event.preventDefault()+ const path = new URL($el.href, window.location.origin).pathname+ console.log('navigating ->', path)+ window.history.pushState({}, '', path)+ var popStateEvent = new PopStateEvent('popstate', {})+ dispatchEvent(popStateEvent)+ })+})++++function applyShowThumbnails(showThumbnails) {+ if (showThumbnails) {+ document.body.classList.remove('hide-thumbnails')+ } else {+ document.body.classList.add('hide-thumbnails')+ }+ const $showThumbnailsCheckbox = document.getElementById('show-thumbnails')+ if ($showThumbnailsCheckbox) $showThumbnailsCheckbox.checked = showThumbnails+}+function applyShowBigPlayer(showBigPlayer) {+ if (showBigPlayer) {+ document.body.classList.add('show-big-player')+ } else {+ document.body.classList.remove('show-big-player')+ }+ const $showBigPlayerCheckbox = document.getElementById('show-big-player')+ if ($showBigPlayerCheckbox) $showBigPlayerCheckbox.checked = showBigPlayer+}\ No newline at end of filediff --git a/client/main.js b/client/main.jsindex 07e2030..183eb8f 100644--- a/client/main.js+++ b/client/main.js@@ -1,109 +1,9 @@import Store from '/lib/store.js'const store = new Store()window.store = store+window.createVideoElement = createVideoElement--function handleRoute(route = window.location.pathname) {- console.log('handling route', route)- if (routes[route]) {- document.querySelector('main').replaceChildren(routes[route].template.content.cloneNode(true))- routes[route].initialize && routes[route].initialize()- } else {- document.querySelector('main').replaceChildren(routes['/404'].template.content.cloneNode(true))- routes['/404'].initialize && routes['/404'].initialize()- }-}--const routes = {- '/': { template: document.getElementById('main-template'), async initialize() {- console.log('initialize /')-- let $videosContainer = document.querySelector('.main-videos-container')- if (!$videosContainer) return- let videos = await fetch('/videos').then(res => res.json())- console.log('videos.length', videos.length)- if (videos.length === 0) {- document.querySelector('empty-state').style.display = ''- document.querySelector('channels-list').style.display = 'none'- document.querySelector('video-filters').style.display = 'none'- } else {- document.querySelector('empty-state').style.display = 'none'- document.querySelector('channels-list').style.display = ''- document.querySelector('video-filters').style.display = ''- }-- document.getElementById('search').removeAttribute('disabled')-- $videosContainer.innerHTML = ''- const ignoredTerms = store.get(store.ignoreTermsKey)- const showOriginalThumbnail = store.get(store.showOriginalThumbnailKey)-- videos = videos- .filter(video => video.title.split(' ').every(word => !ignoredTerms.includes(word.toLowerCase().replace(/('s|"|,|:)/,''))))- .sort((a, b) => +new Date(b.publishedAt) - +new Date(a.publishedAt))- // .filter((_, i) => i < 1000)-- videos.forEach(video => $videosContainer.appendChild(createVideoElement(video, showOriginalThumbnail)))-- const channelsList = videos.reduce((acc, video) => {- if (!acc.includes(video.channelName)) acc.push(video.channelName)- return acc- }, []).join(',')- document.querySelector('channels-list').dataset['list'] = channelsList-- applyShowThumbnails(store.get(store.showThumbnailsKey))- applyShowBigPlayer(store.get(store.showBigPlayerKey))- } },- '/settings': { template: document.getElementById('settings-template'), async initialize () {- console.log('initalize /settings')-- document.getElementById('search').setAttribute('disabled', 'disabled')-- const $showThumbnails = document.getElementById('show-thumbnails')- store.get(store.showThumbnailsKey) ? $showThumbnails.setAttribute('checked', 'true') : $showThumbnails.removeAttribute('checked')-- $showThumbnails.addEventListener('click', (event) => {- store.toggle(store.showThumbnailsKey)- applyShowThumbnails(store.get(store.showThumbnailsKey))- })-- const $showBigPlayer = document.getElementById('show-big-player')- store.get(store.showBigPlayerKey) ? $showBigPlayer.setAttribute('checked', 'true') : $showBigPlayer.removeAttribute('checked')-- $showBigPlayer.addEventListener('click', (event) => {- store.toggle(store.showBigPlayerKey)- applyShowBigPlayer(store.get(store.showBigPlayerKey))- })-- const $showOriginalThumbnail = document.getElementById('show-original-thumbnail')- store.get(store.showOriginalThumbnailKey) ? $showOriginalThumbnail.setAttribute('checked', 'true') : $showOriginalThumbnail.removeAttribute('checked')-- $showOriginalThumbnail.addEventListener('click', (event) => {- store.toggle(store.showOriginalThumbnailKey)- })- } },- '/404': { template: document.getElementById('not-found-template'), async initialize () {- document.getElementById('search').setAttribute('disabled', 'disabled')- } }-}--handleRoute()-window.addEventListener('popstate', (event) => {- console.log('popstate', window.location.pathname)- handleRoute()-})-document.querySelectorAll('[href="/"],[href="/settings"]').forEach(($el) => {- $el.addEventListener('click', (event) => {- event.preventDefault()- const path = new URL($el.href, window.location.origin).pathname- console.log('navigating ->', path)- window.history.pushState({}, '', path)- var popStateEvent = new PopStateEvent('popstate', {})- dispatchEvent(popStateEvent)- })-})-// app: sse updates and rendersconst eventSource = new window.EventSource('/')let $videosContainer = document.querySelector('.main-videos-container')@@ -228,22 +128,3 @@ function createVideoElement (video, showOriginalThumbnail = false) {// applyIgnoredTerms(store.get(store.ignoreTermsKey))--function applyShowThumbnails(showThumbnails) {- if (showThumbnails) {- document.body.classList.remove('hide-thumbnails')- } else {- document.body.classList.add('hide-thumbnails')- }- const $showThumbnailsCheckbox = document.getElementById('show-thumbnails')- if ($showThumbnailsCheckbox) $showThumbnailsCheckbox.checked = showThumbnails-}-function applyShowBigPlayer(showBigPlayer) {- if (showBigPlayer) {- document.body.classList.add('show-big-player')- } else {- document.body.classList.remove('show-big-player')- }- const $showBigPlayerCheckbox = document.getElementById('show-big-player')- if ($showBigPlayerCheckbox) $showBigPlayerCheckbox.checked = showBigPlayer-}\ No newline at end of filediff --git a/lib/server.js b/lib/server.jsindex ca9df78..1eafd13 100644--- a/lib/server.js+++ b/lib/server.js@@ -58,6 +58,10 @@ export function createServer ({repo, connections = []}) {return fileHandler('client/normalize.css', 'text/css')(req, res)if (url.pathname === '/main.js')return fileHandler('client/main.js', 'application/javascript')(req, res)+ if (url.pathname === '/lib/store.js')+ return fileHandler('client/lib/store.js', 'application/javascript')(req, res)+ if (url.pathname === '/lib/router.js')+ return fileHandler('client/lib/router.js', 'application/javascript')(req, res)if (url.pathname === '/components/video-element.js')return fileHandler('client/components/video-element.js', 'application/javascript')(req, res)if (url.pathname === '/components/add-channel-form.js')@@ -70,8 +74,6 @@ export function createServer ({repo, connections = []}) {return fileHandler('client/components/empty-state.js', 'application/javascript')(req, res)if (url.pathname === '/components/video-filters.js')return fileHandler('client/components/video-filters.js', 'application/javascript')(req, res)- if (url.pathname === '/lib/store.js')- return fileHandler('client/lib/store.js', 'application/javascript')(req, res)return fileHandler('client/index.html', 'text/html')(req, res)})