Piped/src/components/SearchResults.vue

154 lines
5.7 KiB
Vue
Raw Normal View History

2020-12-09 13:33:29 +00:00
<template>
2022-07-21 20:51:41 +00:00
<h1 class="text-center my-2" v-text="$route.query.search_query" />
2020-12-09 13:33:29 +00:00
2021-12-27 22:33:55 +00:00
<label for="ddlSearchFilters">
<strong v-text="`${$t('actions.filter')}:`" />
</label>
<select id="ddlSearchFilters" v-model="selectedFilter" default="all" class="select w-auto" @change="updateFilter()">
<option v-for="filter in availableFilters" :key="filter" :value="filter" v-t="`search.${filter}`" />
2021-06-14 19:45:19 +00:00
</select>
<hr />
2022-01-14 02:54:27 +00:00
<div v-if="results && results.corrected">
<i18n-t keypath="search.did_you_mean" tag="div" class="text-lg">
<router-link :to="{ name: 'SearchResults', query: { search_query: results.suggestion } }">
<em v-text="results.suggestion" />
</router-link>
</i18n-t>
</div>
2021-12-27 14:46:22 +00:00
<div v-if="results" class="video-grid">
<template v-for="result in results.items" :key="result.url">
2021-09-03 19:27:22 +00:00
<VideoItem v-if="shouldUseVideoItem(result)" :video="result" height="94" width="168" />
2022-01-12 12:13:04 +00:00
<div v-if="!shouldUseVideoItem(result)">
<router-link :to="result.url">
2021-12-27 14:46:26 +00:00
<div class="relative">
2022-01-12 11:59:50 +00:00
<img class="w-full" :src="result.thumbnail" loading="lazy" />
</div>
2021-05-28 19:32:10 +00:00
<p>
2021-12-27 22:33:55 +00:00
<span v-text="result.name" />
<font-awesome-icon class="ml-1.5" v-if="result.verified" icon="check" />
2021-05-28 19:32:10 +00:00
</p>
2020-12-09 13:33:29 +00:00
</router-link>
2021-12-27 16:29:25 +00:00
<p v-if="result.description" v-text="result.description" />
2022-01-12 12:13:04 +00:00
<router-link v-if="result.uploaderUrl" class="link" :to="result.uploaderUrl">
2021-05-28 19:32:10 +00:00
<p>
2021-12-27 22:33:55 +00:00
<span v-text="result.uploader" />
<font-awesome-icon class="ml-1.5" v-if="result.uploaderVerified" icon="check" />
2021-05-28 19:32:10 +00:00
</p>
2020-12-09 13:33:29 +00:00
</router-link>
2021-05-06 17:40:32 +00:00
2022-01-12 12:13:04 +00:00
<a v-if="result.uploaderName" class="link" v-text="result.uploaderName" />
2021-12-28 01:13:55 +00:00
<template v-if="result.videos >= 0">
<br v-if="result.uploaderName" />
<strong v-text="`${result.videos} ${$t('video.videos')}`" />
</template>
2021-05-28 19:32:10 +00:00
2021-05-06 17:40:32 +00:00
<br />
2020-12-09 13:33:29 +00:00
</div>
</template>
2020-12-09 13:33:29 +00:00
</div>
</template>
<script>
2022-04-08 15:46:49 +00:00
import VideoItem from "./VideoItem.vue";
2021-09-03 19:27:22 +00:00
2020-12-09 13:33:29 +00:00
export default {
components: {
VideoItem,
},
2020-12-09 13:33:29 +00:00
data() {
return {
results: null,
2021-06-14 19:45:19 +00:00
availableFilters: [
"all",
"videos",
"channels",
"playlists",
"music_songs",
"music_videos",
"music_albums",
"music_playlists",
],
selectedFilter: this.$route.query.filter ?? "all",
2020-12-09 13:33:29 +00:00
};
},
mounted() {
if (this.handleRedirect()) return;
2020-12-09 13:33:29 +00:00
this.updateResults();
2022-09-11 18:01:58 +00:00
this.saveQueryToHistory();
},
activated() {
this.handleRedirect();
2020-12-14 07:15:09 +00:00
window.addEventListener("scroll", this.handleScroll);
},
deactivated() {
2020-12-14 07:15:09 +00:00
window.removeEventListener("scroll", this.handleScroll);
2020-12-09 13:33:29 +00:00
},
unmounted() {
window.removeEventListener("scroll", this.handleScroll);
},
2020-12-09 13:33:29 +00:00
methods: {
async fetchResults() {
return await await this.fetchJson(this.apiUrl() + "/search", {
q: this.$route.query.search_query,
filter: this.$route.query.filter ?? "all",
});
2020-12-09 13:33:29 +00:00
},
async updateResults() {
2020-12-14 07:15:09 +00:00
document.title = this.$route.query.search_query + " - Piped";
this.results = this.fetchResults().then(json => (this.results = json));
2020-12-14 07:15:09 +00:00
},
updateFilter() {
this.$router.replace({
query: {
search_query: this.$route.query.search_query,
filter: this.selectedFilter,
},
});
},
2020-12-14 07:15:09 +00:00
handleScroll() {
if (this.loading || !this.results || !this.results.nextpage) return;
if (window.innerHeight + window.scrollY >= document.body.offsetHeight - window.innerHeight) {
2020-12-14 07:15:09 +00:00
this.loading = true;
this.fetchJson(this.apiUrl() + "/nextpage/search", {
nextpage: this.results.nextpage,
q: this.$route.query.search_query,
filter: this.$route.query.filter ?? "all",
}).then(json => {
2021-02-24 09:35:41 +00:00
this.results.nextpage = json.nextpage;
this.results.id = json.id;
this.loading = false;
json.items.map(stream => this.results.items.push(stream));
});
2020-12-14 07:15:09 +00:00
}
},
2021-09-03 19:27:22 +00:00
shouldUseVideoItem(item) {
return item.title;
},
handleRedirect() {
const query = this.$route.query.search_query;
const url =
/(?:http(?:s)?:\/\/)?(?:www\.)?youtube\.com(\/[/a-zA-Z0-9_?=&-]*)/gm.exec(query)?.[1] ??
/(?:http(?:s)?:\/\/)?(?:www\.)?youtu\.be\/(?:watch\?v=)?([/a-zA-Z0-9_?=&-]*)/gm
.exec(query)?.[1]
.replace(/^/, "/watch?v=");
if (url) {
this.$router.push(url);
return true;
}
},
2022-09-11 18:01:58 +00:00
saveQueryToHistory() {
if (!this.getPreferenceBoolean("searchHistory", false)) return;
const query = this.$route.query.search_query;
if (!query) return;
const searchHistory = JSON.parse(localStorage.getItem("search_history")) ?? [];
if (!searchHistory.includes(query)) searchHistory.push(query);
if (searchHistory.length > 3) searchHistory.shift();
localStorage.setItem("search_history", JSON.stringify(searchHistory));
},
2021-09-03 19:27:22 +00:00
},
2020-12-09 13:33:29 +00:00
};
</script>