Piped/src/components/ChannelPage.vue

262 lines
9.3 KiB
Vue
Raw Normal View History

2020-11-27 06:46:36 +00:00
<template>
<ErrorHandler v-if="channel && channel.error" :message="channel.message" :error="channel.error" />
<LoadingIndicatorPage :show-content="channel != null && !channel.error">
2021-12-27 14:46:30 +00:00
<div class="flex justify-center place-items-center">
<img height="48" width="48" class="rounded-full m-1" :src="channel.avatarUrl" />
2021-12-27 16:29:25 +00:00
<h1 v-text="channel.name" />
<font-awesome-icon class="ml-1.5 !text-3xl" v-if="channel.verified" icon="check" />
2021-12-27 14:46:30 +00:00
</div>
<img v-if="channel.bannerUrl" :src="channel.bannerUrl" class="w-full pb-1.5" loading="lazy" />
<!-- eslint-disable-next-line vue/no-v-html -->
<p class="whitespace-pre-wrap">
2023-01-30 18:42:57 +00:00
<span v-html="purifyHTML(rewriteDescription(channel.description))" />
2021-12-27 22:33:55 +00:00
</p>
2020-11-27 06:46:36 +00:00
2021-12-27 16:29:25 +00:00
<button
class="btn"
@click="subscribeHandler"
v-t="{
path: `actions.${subscribed ? 'unsubscribe' : 'subscribe'}`,
args: { count: numberFormat(channel.subscriberCount) },
}"
></button>
2022-09-08 21:24:33 +00:00
<!-- RSS Feed button -->
<a
aria-label="RSS feed"
title="RSS feed"
role="button"
v-if="channel.id"
2022-09-09 10:40:18 +00:00
:href="`${apiUrl()}/feed/unauthenticated/rss?channels=${channel.id}`"
2022-09-08 21:24:33 +00:00
target="_blank"
class="btn flex-col mx-3"
2022-09-08 21:24:33 +00:00
>
<font-awesome-icon icon="rss" />
</a>
<WatchOnYouTubeButton :link="`https://youtube.com/channel/${this.channel.id}`" />
2022-11-02 16:10:27 +00:00
2022-11-01 11:31:45 +00:00
<div class="flex mt-4 mb-2">
2022-11-01 12:07:31 +00:00
<button
v-for="(tab, index) in tabs"
:key="tab.name"
class="btn mr-2"
@click="loadTab(index)"
:class="{ active: selectedTab == index }"
>
2022-11-01 11:31:45 +00:00
<span v-text="tab.translatedName"></span>
</button>
</div>
2020-11-27 06:46:36 +00:00
<hr />
2021-12-27 14:46:22 +00:00
<div class="video-grid">
2022-11-01 11:31:45 +00:00
<ContentItem
v-for="item in contentItems"
:key="item.url"
2022-11-01 12:12:54 +00:00
:item="item"
height="94"
width="168"
hide-channel
/>
2020-11-27 06:46:36 +00:00
</div>
</LoadingIndicatorPage>
2020-11-27 06:46:36 +00:00
</template>
<script>
2022-04-08 15:46:49 +00:00
import ErrorHandler from "./ErrorHandler.vue";
2022-11-01 11:31:45 +00:00
import ContentItem from "./ContentItem.vue";
import WatchOnYouTubeButton from "./WatchOnYouTubeButton.vue";
import LoadingIndicatorPage from "./LoadingIndicatorPage.vue";
2020-11-27 06:46:36 +00:00
export default {
components: {
ErrorHandler,
2022-11-01 11:31:45 +00:00
ContentItem,
WatchOnYouTubeButton,
LoadingIndicatorPage,
},
2020-11-27 06:46:36 +00:00
data() {
return {
channel: null,
2022-08-02 08:41:35 +00:00
subscribed: false,
2022-11-01 11:31:45 +00:00
tabs: [],
selectedTab: 0,
contentItems: [],
2020-11-27 06:46:36 +00:00
};
},
mounted() {
this.getChannelData();
},
activated() {
if (this.channel && !this.channel.error) document.title = this.channel.name + " - Piped";
2020-11-27 06:46:36 +00:00
window.addEventListener("scroll", this.handleScroll);
if (this.channel && !this.channel.error) this.updateWatched(this.channel.relatedStreams);
2020-11-27 06:46:36 +00:00
},
deactivated() {
2020-11-27 06:46:36 +00:00
window.removeEventListener("scroll", this.handleScroll);
},
unmounted() {
window.removeEventListener("scroll", this.handleScroll);
},
2020-11-27 06:46:36 +00:00
methods: {
async fetchSubscribedStatus() {
2022-08-02 08:41:35 +00:00
if (!this.channel.id) return;
if (!this.authenticated) {
this.subscribed = this.isSubscribedLocally(this.channel.id);
return;
}
this.fetchJson(
this.authApiUrl() + "/subscribed",
{
channelId: this.channel.id,
},
{
headers: {
Authorization: this.getAuthToken(),
},
},
).then(json => {
this.subscribed = json.subscribed;
});
},
2020-11-27 06:46:36 +00:00
async fetchChannel() {
2023-03-11 18:44:07 +00:00
const url = this.$route.path.includes("@")
? this.apiUrl() + "/c/" + this.$route.params.channelId
: this.apiUrl() + "/" + this.$route.params.path + "/" + this.$route.params.channelId;
2021-05-28 18:40:54 +00:00
return await this.fetchJson(url);
2020-11-27 06:46:36 +00:00
},
async getChannelData() {
this.fetchChannel()
.then(data => (this.channel = data))
.then(() => {
if (!this.channel.error) {
document.title = this.channel.name + " - Piped";
2022-11-01 11:31:45 +00:00
this.contentItems = this.channel.relatedStreams;
2022-08-02 08:41:35 +00:00
this.fetchSubscribedStatus();
this.updateWatched(this.channel.relatedStreams);
2022-11-01 11:31:45 +00:00
this.tabs.push({
translatedName: this.$t("video.videos"),
});
2023-01-26 18:36:20 +00:00
const tabQuery = this.$route.query.tab;
2022-11-01 11:31:45 +00:00
for (let i = 0; i < this.channel.tabs.length; i++) {
let tab = this.channel.tabs[i];
tab.translatedName = this.getTranslatedTabName(tab.name);
this.tabs.push(tab);
2023-01-26 18:36:20 +00:00
if (tab.name === tabQuery) this.loadTab(i + 1);
2022-11-01 11:31:45 +00:00
}
}
});
2020-11-27 06:46:36 +00:00
},
handleScroll() {
if (
this.loading ||
!this.channel ||
!this.channel.nextpage ||
(this.selectedTab != 0 && !this.tabs[this.selectedTab].tabNextPage)
)
return;
if (window.innerHeight + window.scrollY >= document.body.offsetHeight - window.innerHeight) {
2020-11-27 06:46:36 +00:00
this.loading = true;
2022-11-01 11:39:01 +00:00
if (this.selectedTab == 0) {
this.fetchChannelNextPage();
} else {
this.fetchChannelTabNextPage();
}
2020-11-27 06:46:36 +00:00
}
},
2022-11-01 11:39:01 +00:00
fetchChannelNextPage() {
this.fetchJson(this.apiUrl() + "/nextpage/channel/" + this.channel.id, {
nextpage: this.channel.nextpage,
}).then(json => {
this.channel.nextpage = json.nextpage;
this.loading = false;
this.updateWatched(json.relatedStreams);
json.relatedStreams.map(stream => this.contentItems.push(stream));
});
},
fetchChannelTabNextPage() {
this.fetchJson(this.apiUrl() + "/channels/tabs", {
data: this.tabs[this.selectedTab].data,
nextpage: this.tabs[this.selectedTab].tabNextPage,
2022-11-01 11:39:01 +00:00
}).then(json => {
this.tabs[this.selectedTab].tabNextPage = json.nextpage;
2022-11-01 11:39:01 +00:00
this.loading = false;
json.content.map(item => this.contentItems.push(item));
this.tabs[this.selectedTab].content = this.contentItems;
2022-11-01 11:39:01 +00:00
});
},
subscribeHandler() {
if (this.authenticated) {
this.fetchJson(this.authApiUrl() + (this.subscribed ? "/unsubscribe" : "/subscribe"), null, {
method: "POST",
body: JSON.stringify({
channelId: this.channel.id,
}),
headers: {
Authorization: this.getAuthToken(),
"Content-Type": "application/json",
},
});
} else {
if (!this.handleLocalSubscriptions(this.channel.id)) return;
}
this.subscribed = !this.subscribed;
},
2022-11-01 11:31:45 +00:00
getTranslatedTabName(tabName) {
let translatedTabName = tabName;
switch (tabName) {
2022-11-15 21:35:56 +00:00
case "livestreams":
2022-11-01 11:31:45 +00:00
translatedTabName = this.$t("titles.livestreams");
break;
2022-11-15 21:35:56 +00:00
case "playlists":
2022-11-01 11:31:45 +00:00
translatedTabName = this.$t("titles.playlists");
break;
2022-11-15 21:35:56 +00:00
case "channels":
2022-11-01 11:31:45 +00:00
translatedTabName = this.$t("titles.channels");
break;
2022-11-15 21:35:56 +00:00
case "shorts":
2022-11-01 11:31:45 +00:00
translatedTabName = this.$t("video.shorts");
break;
2022-11-01 12:22:23 +00:00
default:
console.error(`Tab name "${tabName}" is not translated yet!`);
break;
2022-11-01 11:31:45 +00:00
}
return translatedTabName;
},
loadTab(index) {
2022-11-01 11:39:01 +00:00
this.selectedTab = index;
// update the tab query in the url path
const url = new URL(window.location);
url.searchParams.set("tab", this.tabs[index].name ?? "videos");
window.history.replaceState(window.history.state, "", url);
2022-11-01 11:31:45 +00:00
if (index == 0) {
this.contentItems = this.channel.relatedStreams;
return;
}
if (this.tabs[index].content) {
this.contentItems = this.tabs[index].content;
return;
}
2022-11-01 11:31:45 +00:00
this.fetchJson(this.apiUrl() + "/channels/tabs", {
data: this.tabs[index].data,
}).then(tab => {
this.contentItems = this.tabs[index].content = tab.content;
this.tabs[this.selectedTab].tabNextPage = tab.nextpage;
2022-11-01 11:31:45 +00:00
});
},
},
2020-11-27 06:46:36 +00:00
};
</script>
2022-11-01 12:07:31 +00:00
<style>
.active {
border: 0.1rem outset red;
}
</style>