Skip to content

armgit / my-yt

Public Imported from GitHub
0

A clean and minimal youtube frontend, without all the ads and whistles

JavaScript

feat: filter on client wip

Christian Fei9d5914ae0032
diff --git a/client/components/search-videos.js b/client/components/search-videos.js
index 5b9725e..9435e6f 100644
--- a/client/components/search-videos.js
+++ b/client/components/search-videos.js
@@ -25,13 +25,7 @@ class SearchVideos extends HTMLElement {
event.preventDefault()
let searchTerm = event.target.value.trim()
const $status = document.querySelector('#filter-results-status')
- const $videos = document.querySelectorAll('.video')
- if (!searchTerm) {
- if ($status) $status.innerText = ''
- $videos.forEach($video => $video.style.display = '')
- document.body.classList.remove('searching')
- return console.log('no search term')
- }
+ $status.innerText = ''
if (searchTerm.match('https?://')) {
fetch('/api/download-video', {
@@ -47,24 +41,27 @@ class SearchVideos extends HTMLElement {
searchTerm = searchTerm.toLowerCase()
document.body.classList.add('searching')
- let filteredCount = 0
- $videos.forEach($video => {
- const videoData = JSON.parse($video.dataset['data'])
- if (
- videoData.title.toLowerCase().includes(searchTerm)
- || videoData.summary?.toLowerCase().includes(searchTerm)
- || videoData.channelName?.toLowerCase().includes(searchTerm)
- ) {
- $video.style.display = ''
- filteredCount++
- } else {
- $video.style.display = 'none'
- }
- })
- if ($status) {
- if (filteredCount > 0) $status.innerText = `Found ${filteredCount} videos`
+
+ fetch(`/api/videos?filter=${encodeURIComponent(searchTerm)}`)
+ .then(res => res.json())
+ .then((videos) => {
+ const $videosContainer = document.querySelector('.main-videos-container')
+ if (!$videosContainer) return
+ $videosContainer.innerHTML = ''
+ const showOriginalThumbnail = window.store.get(store.showOriginalThumbnailKey)
+ videos.forEach(video =>
+ $videosContainer.appendChild(window.createVideoElement(video, showOriginalThumbnail))
+ )
+
+ if (videos.length > 0) $status.innerText = `Found ${videos.length} videos`
else $status.innerText = `No videos found`
- }
+ })
+ .catch(err => {
+ console.error(err)
+ })
+ .finally(() => {
+ document.body.classList.remove('searching')
+ })
}
}
customElements.define('search-videos', SearchVideos)
diff --git a/client/main.css b/client/main.css
index e6efb54..836b323 100644
--- a/client/main.css
+++ b/client/main.css
@@ -394,9 +394,9 @@ details {
body.searching [data-ignored="true"] {
display: block;
}
-[data-ignored="true"] {
+/* [data-ignored="true"] {
display: none;
-}
+} */
#show-summarized-videos.active, #show-downloaded-videos.active, #show-ignored-videos.active {
background-color: #545454;
color: #f0f0f0;
diff --git a/lib/repository.js b/lib/repository.js
index 18d60dd..60d8ed0 100644
--- a/lib/repository.js
+++ b/lib/repository.js
@@ -76,13 +76,15 @@ export default class Repository {
.sort((a, b) => +new Date(b.publishedAt) - +new Date(a.publishedAt))
if (isFiltering) {
+ console.log({query})
const searchTerm = query.filter.toLowerCase()
videos = videos.filter(v => {
return v.channelName.toLowerCase().includes(searchTerm)
- || v.title.toLowerCase().includes(searchTerm)
- || v.description.toLowerCase().includes(searchTerm)
+ || v.title?.toLowerCase().includes(searchTerm)
+ || v.description?.toLowerCase().includes(searchTerm)
})
}
+ videos = videos.filter((_, i) => i < 100)
return videos
}
diff --git a/lib/server.js b/lib/server.js
index 9032379..9ff5ed6 100644
--- a/lib/server.js
+++ b/lib/server.js
@@ -315,7 +315,6 @@ function watchVideoHandler(req, res, repo) {
export function searchVideosHandler(req, res, repo) {
const query = getQuery(req)
- console.log(query)
const videos = repo.getVideos(query)
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(videos))