Feature/channel view content (#150)

* ChannelView feeds

* Fixes for web app

* Add request id to error message
This commit is contained in:
Brahim Hadriche authored and GitHub committed 2023-09-22 17:52:53 -04:00
1 parent e2df0b524a
commit dab6dc5bc4
20 files changed
+414 -133

No files matched your search

+2 -7
View File
@@ -12,7 +12,6 @@ export class InvidiousApi {
// Note: handlers for authenticated requests are not needed, since they are handled server side
this.responseHandlers = {
"DefaultHandler": (requestData, response) => this.DefaultHandler(requestData, response),
"PlaylistHandler": (requestData, response) => this.PlaylistHandler(requestData, response),
}
}
@@ -91,12 +90,8 @@ export class InvidiousApi {
}
private async DefaultHandler(requestData, response) {
return await response.json();
}
private async PlaylistHandler(requestData, response) {
const playlist = await response.json();
return playlist.videos
const items = await response.json();
return { items };
}
private makeUrl(url: string, params: any) {
@@ -0,0 +1,82 @@
<script lang="ts">
import { PlayletApi } from "lib/Api/PlayletApi";
import { playletStateStore } from "lib/Stores";
export let author: string | undefined = undefined;
export let authorId: string | undefined = undefined;
export let authorThumbnails: any[] | undefined = undefined;
export let videoCount: number | undefined = undefined;
export let subCount: number | undefined = undefined;
// svelte-ignore unused-export-let
export let type: string = undefined;
let modal;
let tvName = "Roku TV";
let invidiousInstance;
playletStateStore.subscribe((value) => {
tvName = value?.device?.friendly_name ?? "Roku TV";
invidiousInstance = value?.invidious?.current_instance;
});
function getThumbnailUrl() {
if (!authorThumbnails || authorThumbnails.length === 0) {
return "";
}
const authorThumbnail = authorThumbnails[authorThumbnails.length - 1];
let url = authorThumbnail.url;
if (url.startsWith("//")) {
url = "https:" + url;
}
return url;
}
async function openChannelOnTv() {
// TODO:P1 open channel on TV
// await PlayletApi.playPlaylist(playlistId, title, videoCount);
}
function openInvidiousInNewTab() {
let url = `${invidiousInstance}/channel/${authorId}`;
window.open(url);
}
</script>
<button class="w-60 p-2" on:click={modal.showModal()}>
<div class="card card-compact bg-base-100 shadow-xl border border-neutral">
<figure class="relative">
<img
class="w-44 rounded-full aspect-square object-cover"
loading="lazy"
width="176"
height="176"
src={getThumbnailUrl()}
alt={author}
/>
</figure>
<div class="card-body">
<h3 class="card-title text-base line-clamp-1 min-h-8">{author}</h3>
<div>{subCount} subscribers</div>
<div>{videoCount} videos</div>
</div>
</div>
</button>
<!-- TODO:P2 a dialog for every video is very slow. Need to reuse the same one -->
<dialog bind:this={modal} id="modal_{authorId}" class="modal">
<form method="dialog" class="modal-box bg-base-100">
<h3 class="text-lg m-5">{author}</h3>
<div class="flex flex-col">
<!-- <button class="btn m-2" on:click={openChannelOnTv}
>Open on {tvName}</button
> -->
<button class="btn m-2" on:click={openInvidiousInNewTab}
>Open in Invidious</button
>
<button class="btn m-2">Cancel</button>
</div>
</form>
<form method="dialog" class="modal-backdrop">
<button>close</button>
</form>
</dialog>
@@ -3,20 +3,24 @@
import { invidiousVideoApiStore, playletStateStore } from "lib/Stores";
import VideoCell from "lib/Screens/Home/VideoCell.svelte";
import PlaylistCell from "lib/Screens/Home/PlaylistCell.svelte";
import ChannelCell from "./ChannelCell.svelte";
export let requestData: any = undefined;
export let videos = undefined;
let itemWidths = [];
let carouselElement;
let scrollStart = 0;
let scrollEnd = 0;
// w-80 p-2: 320px + 16px padding on each side
const itemWidth = 320 + 16 * 2;
// w-80|w-60 p-2: 320px|240px + 16px padding on each side
const videoItemWidth = 320 + 16 * 2;
const channelItemWidth = 240 + 16 * 2;
$: {
if (carouselElement && videos && videos.length) {
if (carouselElement && itemWidths && itemWidths.length) {
recalculateVisibileCells();
}
}
@@ -37,26 +41,49 @@
async function updateRow() {
const result = await invidiousApi.makeRequest(requestData);
if (result) {
result.forEach((item) => {
if (result && result.items) {
result.items.forEach((item) => {
if (item.videoId) {
item.type = "video";
} else if (item.playlistId) {
item.type = "playlist";
}
});
videos = result.filter(
(video) => video.type === "video" || video.type === "playlist"
);
videos = result.items;
itemWidths = videos.map((video) => {
if (video.type === "channel") {
return channelItemWidth;
} else {
return videoItemWidth;
}
});
}
}
function recalculateVisibileCells() {
const scrollLeft = carouselElement.scrollLeft;
let scrollLeft = carouselElement.scrollLeft;
const clientWidth = carouselElement.clientWidth || window.innerWidth;
scrollStart = Math.floor(scrollLeft / itemWidth) - 1;
scrollEnd = Math.ceil((scrollLeft + clientWidth) / itemWidth) + 1;
scrollStart = 0;
while (
scrollStart < itemWidths.length &&
scrollLeft > itemWidths[scrollStart]
) {
scrollLeft -= itemWidths[scrollStart];
scrollStart++;
}
scrollEnd = scrollStart;
while (
scrollEnd < itemWidths.length &&
scrollLeft + clientWidth > itemWidths[scrollEnd]
) {
scrollLeft -= itemWidths[scrollEnd];
scrollEnd++;
}
scrollStart = Math.max(0, scrollStart - 1);
scrollEnd = Math.min(itemWidths.length - 1, scrollEnd + 1);
}
</script>
@@ -70,12 +97,16 @@
on:scroll={recalculateVisibileCells}
>
{#each videos as video, i}
<div class="carousel-item w-80 p-2">
<div
class="carousel-item {video.type === 'channel' ? 'w-60' : 'w-80'} p-2"
>
{#if i >= scrollStart && i <= scrollEnd}
{#if video.type === "video"}
<VideoCell {...video} />
{:else if video.type === "playlist"}
<PlaylistCell {...video} />
{:else if video.type === "channel"}
<ChannelCell {...video} />
{/if}
{/if}
</div>
@@ -5,6 +5,7 @@
import VideoCell from "lib/Screens/Home/VideoCell.svelte";
import PlaylistCell from "lib/Screens/Home/PlaylistCell.svelte";
import SearchThinIcon from "assets/search-thin-icon.svg.svelte";
import ChannelCell from "./Home/ChannelCell.svelte";
export let visibility: boolean;
@@ -156,6 +157,8 @@
<VideoCell {...video} />
{:else if video.type === "playlist"}
<PlaylistCell {...video} />
{:else if video.type === "channel"}
<ChannelCell {...video} />
{/if}
{/each}
</div>