Refactor casting dialog and add thumbnails (#184)

This commit is contained in:
Brahim Hadriche authored and GitHub committed 2023-10-29 00:28:21 -04:00
1 parent f58137b326
commit af1fa0b401
17 files changed
+525 -393

No files matched your search

@@ -0,0 +1,60 @@
<script lang="ts">
import { PlayletApi } from "lib/Api/PlayletApi";
import { playletStateStore } from "lib/Stores";
import ChannelThumbnail from "./ChannelThumbnail.svelte";
export let author: string | undefined = undefined;
export let authorId: string | undefined = undefined;
export let authorThumbnails: any[] | undefined = undefined;
export function show() {
modal.showModal();
}
export function close() {
modal.close();
}
let modal;
let tvName = "Roku TV";
let invidiousInstance;
playletStateStore.subscribe((value) => {
tvName = value?.device?.friendly_name ?? "Roku TV";
invidiousInstance = value?.invidious?.current_instance;
});
async function openOnTv() {
await PlayletApi.openChannel(authorId);
}
function openInvidious() {
let url = `${invidiousInstance}/channel/${authorId}`;
window.open(url);
}
</script>
<dialog bind:this={modal} class="modal">
<form method="dialog" class="modal-box bg-base-100">
<div class="flex flex-col items-center">
<div class="w-32">
<ChannelThumbnail bind:author bind:authorThumbnails />
</div>
<div class="m-4">{author}</div>
</div>
<div class="flex flex-col">
<div class="join join-vertical m-2">
<button class="btn join-item hover:btn-accent" on:click={openOnTv}>
Open on {tvName}
</button>
<button class="btn join-item hover:btn-accent" on:click={openInvidious}>
Open in Invidious
</button>
<button class="btn join-item hover:btn-accent">Cancel</button>
</div>
</div>
</form>
<form method="dialog" class="modal-backdrop">
<button>Close</button>
</form>
</dialog>
@@ -0,0 +1,32 @@
<script lang="ts">
import ChannelCastDialog from "./ChannelCastDialog.svelte";
import ChannelThumbnail from "./ChannelThumbnail.svelte";
export let author: string | undefined = undefined;
export let authorId: string | undefined = undefined;
export let authorThumbnails: any[] | undefined = undefined;
export let channelHandle: string | undefined = undefined;
export let subCount: number | undefined = undefined;
// svelte-ignore unused-export-let
export let type: string = undefined;
let modal;
</script>
<button class="w-60 p-2" on:click={modal.show()}>
<div class="card card-compact bg-base-100 shadow-xl border border-neutral">
<ChannelThumbnail bind:author bind:authorThumbnails />
<div class="card-body">
<h3 class="card-title text-base line-clamp-1 min-h-8">{author}</h3>
<div>{subCount} subscribers</div>
<div>{channelHandle || ""}</div>
</div>
</div>
</button>
<ChannelCastDialog
bind:this={modal}
bind:author
bind:authorId
bind:authorThumbnails
/>
@@ -0,0 +1,28 @@
<script lang="ts">
export let author: string | undefined = undefined;
export let authorThumbnails: any[] | undefined = undefined;
let thumbnailUrl;
$: {
if (authorThumbnails && authorThumbnails.length > 0) {
const authorThumbnail = authorThumbnails[authorThumbnails.length - 1];
let url = authorThumbnail.url;
if (url.startsWith("//")) {
url = "https:" + url;
}
thumbnailUrl = url;
}
}
</script>
<figure class="relative">
<img
class="w-44 rounded-full aspect-square object-cover"
loading="lazy"
width="176"
height="176"
src={thumbnailUrl}
alt={author}
/>
</figure>
@@ -0,0 +1,83 @@
<script lang="ts">
import { PlayletApi } from "lib/Api/PlayletApi";
import { playletStateStore } from "lib/Stores";
import PlaylistThumbnail from "./PlaylistThumbnail.svelte";
export let title: string | undefined = undefined;
export let playlistId: string | undefined = undefined;
export let videoCount: number | undefined = undefined;
export let playlistThumbnail: string | undefined = undefined;
export let videos: any[] | undefined = undefined;
export function show() {
modal.showModal();
}
export function close() {
modal.close();
}
let modal;
let tvName = "Roku TV";
let invidiousInstance;
playletStateStore.subscribe((value) => {
tvName = value?.device?.friendly_name ?? "Roku TV";
invidiousInstance = value?.invidious?.current_instance;
});
async function playOnTv() {
await PlayletApi.playPlaylist(playlistId, title, videoCount);
}
async function queueOnTv() {
await PlayletApi.queuePlaylist(playlistId, title, videoCount);
}
async function openOnTv() {
await PlayletApi.openPlaylist(playlistId);
}
function openInvidious() {
let url = `${invidiousInstance}/playlist?list=${playlistId}`;
window.open(url);
}
</script>
<dialog bind:this={modal} class="modal">
<form method="dialog" class="modal-box bg-base-100">
<div class="flex flex-col items-center">
<div class="w-64">
<PlaylistThumbnail
bind:title
bind:playlistThumbnail
bind:videoCount
bind:videos
bind:invidiousInstance
/>
</div>
<div class="m-4">{title}</div>
</div>
<div class="flex flex-col">
<div class="join join-vertical m-2">
<button class="btn join-item hover:btn-accent" on:click={playOnTv}>
Play on {tvName}
</button>
<button class="btn join-item hover:btn-accent" on:click={queueOnTv}>
Queue on {tvName}
</button>
<button class="btn join-item hover:btn-accent" on:click={openOnTv}>
Open on {tvName}
</button>
<button class="btn join-item hover:btn-accent" on:click={openInvidious}>
Open in Invidious
</button>
<button class="btn join-item hover:btn-accent">Cancel</button>
</div>
</div>
</form>
<form method="dialog" class="modal-backdrop">
<button>Close</button>
</form>
</dialog>
@@ -0,0 +1,46 @@
<script lang="ts">
import { playletStateStore } from "lib/Stores";
import PlaylistCastDialog from "./PlaylistCastDialog.svelte";
import PlaylistThumbnail from "./PlaylistThumbnail.svelte";
export let title: string | undefined = undefined;
export let author: string | undefined = undefined;
export let playlistId: string | undefined = undefined;
export let playlistThumbnail: string | undefined = undefined;
export let videoCount: number | undefined = undefined;
export let videos: any[] | undefined = undefined;
// svelte-ignore unused-export-let
export let type: string = undefined;
let modal;
let invidiousInstance;
playletStateStore.subscribe((value) => {
invidiousInstance = value?.invidious?.current_instance;
});
</script>
<button class="w-80 p-2" on:click={modal.show()}>
<div class="card card-compact bg-base-100 shadow-xl border border-neutral">
<PlaylistThumbnail
bind:title
bind:playlistThumbnail
bind:videoCount
bind:videos
bind:invidiousInstance
/>
<div class="card-body">
<h3 class="card-title text-base line-clamp-2 min-h-12">{title}</h3>
<div class="font-semibold">{author}</div>
</div>
</div>
</button>
<PlaylistCastDialog
bind:this={modal}
bind:title
bind:playlistId
bind:playlistThumbnail
bind:videoCount
bind:videos
/>
@@ -0,0 +1,52 @@
<script lang="ts">
export let title: string | undefined = undefined;
export let videoCount: number | undefined = undefined;
export let playlistThumbnail: string | undefined = undefined;
export let videos: any[] | undefined = undefined;
export let invidiousInstance;
let thumbnailUrl;
$: {
let url = "";
if (playlistThumbnail) {
url = playlistThumbnail;
if (url.startsWith("/") && invidiousInstance) {
url = invidiousInstance + url;
}
} else if (videos && videos.length) {
const video = videos[0];
if (video.videoThumbnails) {
const videoThumbnail =
video.videoThumbnails.find(
(thumbnail) => thumbnail.quality === "medium"
) || video.videoThumbnails[0];
url = videoThumbnail.url;
}
if (url.startsWith("/") && invidiousInstance) {
url = invidiousInstance + url;
}
}
if (url === "") {
url = `${invidiousInstance}/vi/-----------/mqdefault.jpg`;
}
thumbnailUrl = url;
}
</script>
<figure class="relative">
<img
class="w-full rounded-box aspect-video object-cover"
loading="lazy"
width="320"
height="180"
src={thumbnailUrl}
alt={title}
/>
<div
class="absolute bottom-2 right-0 bg-black/70 text-white text-sm rounded-sm pt-1 pb-1 pr-2 pl-2"
>
{videoCount} videos
</div>
</figure>
@@ -0,0 +1,94 @@
<script lang="ts">
import { PlayletApi } from "lib/Api/PlayletApi";
import { playletStateStore } from "lib/Stores";
import VideoStartAt from "./VideoStartAt.svelte";
import VideoThumbnail from "./VideoThumbnail.svelte";
export let videoId: string | undefined = undefined;
export let title: string | undefined = undefined;
export let videoThumbnails: any[] | undefined = undefined;
export let author: string | undefined = undefined;
export let lengthSeconds: number = undefined;
export let liveNow: boolean = undefined;
export let viewCount: number | undefined = undefined;
export let videoStartAtChecked;
export let videoStartAtTimestamp;
export function show() {
modal.showModal();
}
export function close() {
modal.close();
videoStartAtChecked = false;
videoStartAtTimestamp = 0;
}
let modal;
let tvName = "Roku TV";
let invidiousInstance;
playletStateStore.subscribe((value) => {
tvName = value?.device?.friendly_name ?? "Roku TV";
invidiousInstance = value?.invidious?.current_instance;
});
async function playOnTv() {
const timestamp = videoStartAtChecked ? videoStartAtTimestamp : undefined;
await PlayletApi.playVideo(videoId, timestamp, title, author);
}
async function queueOnTv() {
const timestamp = videoStartAtChecked ? videoStartAtTimestamp : undefined;
await PlayletApi.queueVideo(videoId, timestamp, title, author);
}
function openInvidious() {
let url = `${invidiousInstance}/watch?v=${videoId}`;
if (videoStartAtChecked && videoStartAtTimestamp) {
url += `&t=${videoStartAtTimestamp}`;
}
window.open(url);
}
</script>
<dialog bind:this={modal} class="modal">
<form method="dialog" class="modal-box bg-base-100">
<div class="flex flex-col items-center">
<div class="w-64">
<VideoThumbnail
bind:title
bind:videoThumbnails
bind:liveNow
bind:lengthSeconds
bind:viewCount
bind:invidiousInstance
/>
</div>
<div class="m-4">{title}</div>
</div>
<div class="flex flex-col">
<VideoStartAt
bind:checked={videoStartAtChecked}
bind:timestamp={videoStartAtTimestamp}
{lengthSeconds}
/>
<div class="join join-vertical m-2">
<button class="btn join-item hover:btn-accent" on:click={playOnTv}>
Play on {tvName}
</button>
<button class="btn join-item hover:btn-accent" on:click={queueOnTv}>
Queue on {tvName}
</button>
<button class="btn join-item hover:btn-accent" on:click={openInvidious}>
Open in Invidious
</button>
<button class="btn join-item hover:btn-accent">Cancel</button>
</div>
</div>
</form>
<form method="dialog" class="modal-backdrop">
<button>Close</button>
</form>
</dialog>
@@ -0,0 +1,168 @@
<script lang="ts">
import { playletStateStore } from "lib/Stores";
import VideoCastDialog from "./VideoCastDialog.svelte";
import VideoThumbnail from "./VideoThumbnail.svelte";
export let title: string | undefined = undefined;
export let videoId: string | undefined = undefined;
export let author: string | undefined = undefined;
export let videoThumbnails: any[] | undefined = undefined;
export let viewCount: number | undefined = undefined;
export let publishedText: string | undefined = undefined;
export let isUpcoming: boolean = undefined;
export let premiereTimestamp: number | undefined = undefined;
export let lengthSeconds: number = undefined;
export let liveNow: boolean = undefined;
// svelte-ignore unused-export-let
export let type: string = undefined;
// svelte-ignore unused-export-let
export let authorId: string = undefined;
// svelte-ignore unused-export-let
export let authorUrl: string = undefined;
// svelte-ignore unused-export-let
export let authorVerified: boolean = undefined;
// svelte-ignore unused-export-let
export let description: string = undefined;
// svelte-ignore unused-export-let
export let descriptionHtml: string = undefined;
// svelte-ignore unused-export-let
export let viewCountText: string = undefined;
// svelte-ignore unused-export-let
export let published: number = undefined;
// svelte-ignore unused-export-let
export let premium: boolean = undefined;
// svelte-ignore unused-export-let
export let index: number = undefined;
// svelte-ignore unused-export-let
export let indexId: string = undefined;
let modal;
let invidiousInstance;
playletStateStore.subscribe((value) => {
invidiousInstance = value?.invidious?.current_instance;
});
function getViewCountDateText() {
if (isUpcoming) {
return `Premeres in ${getFormattedTimeLeft(premiereTimestamp)}`;
}
const pubText = publishedText || "";
const viewCountText = formatViewCount(viewCount);
if (pubText === "" && viewCountText === "") {
return "";
}
return `${pubText} • ${viewCountText}`;
}
function getFormattedTimeLeft(unixTimestamp) {
if (unixTimestamp === "invalid") {
return "N/A";
}
const currentTime = Math.floor(Date.now() / 1000);
let timeLeft = unixTimestamp - currentTime;
if (timeLeft <= 0) {
return "now";
}
if (timeLeft < 60) {
let result = `${timeLeft} second`;
if (timeLeft > 1) {
result += "s";
}
return result;
}
timeLeft = timeLeft / 60.0;
if (timeLeft < 60) {
timeLeft = Math.floor(timeLeft);
let result = `${timeLeft} minute`;
if (timeLeft > 1) {
result += "s";
}
return result;
}
timeLeft = timeLeft / 60.0;
if (timeLeft < 24) {
timeLeft = Math.floor(timeLeft);
let result = `${timeLeft} hour`;
if (timeLeft > 1) {
result += "s";
}
return result;
} else {
timeLeft = timeLeft / 24.0;
timeLeft = Math.floor(timeLeft);
let result = `${timeLeft} day`;
if (timeLeft > 1) {
result += "s";
}
return result;
}
}
function formatViewCount(viewCount) {
if (isNaN(viewCount)) {
return "";
}
if (viewCount < 1000) {
return `${formatFloat(viewCount)} views`;
}
viewCount = viewCount / 1000;
if (viewCount < 1000) {
return `${formatFloat(viewCount)}K views`;
}
viewCount = viewCount / 1000;
if (viewCount < 1000) {
return `${formatFloat(viewCount)}M views`;
}
viewCount = viewCount / 1000;
return `${formatFloat(viewCount)}B views`;
}
function formatFloat(X) {
X = X * 10;
X = X + 0.5;
X = Math.floor(X);
X = X / 10;
return X;
}
</script>
<button class="w-80 p-2" on:click={modal.show()}>
<div class="card card-compact bg-base-100 shadow-xl border border-neutral">
<VideoThumbnail
bind:title
bind:videoThumbnails
bind:liveNow
bind:lengthSeconds
bind:viewCount
bind:invidiousInstance
/>
<div class="card-body">
<h3 class="card-title text-base line-clamp-2 min-h-12">{title}</h3>
<div class="font-semibold">{author}</div>
<div>{getViewCountDateText()}</div>
</div>
</div>
</button>
<VideoCastDialog
bind:this={modal}
bind:videoId
bind:title
bind:author
bind:lengthSeconds
bind:videoThumbnails
bind:liveNow
bind:viewCount
videoStartAtChecked={false}
videoStartAtTimestamp={0}
/>
@@ -0,0 +1,263 @@
<script lang="ts">
import { InvidiousApi } from "lib/Api/InvidiousApi";
import { invidiousVideoApiStore, playletStateStore } from "lib/Stores";
import VideoCell from "./VideoCell.svelte";
import PlaylistCell from "./PlaylistCell.svelte";
import ChannelCell from "./ChannelCell.svelte";
// TODO:P1 figure out why some uncached feeds (e.g. channels/ucid/videos) get hit twice
export let feed: any = undefined;
export let videos = [];
enum FeedLoadState {
None,
Loading,
Loaded,
LoadedPage,
Error,
}
let feedLoadState = FeedLoadState.None;
let feedSourcesIndex = 0;
let itemWidths = [];
let carouselElement;
let carouselElementIntersecting = false;
let scrollStart = 0;
let scrollEnd = 0;
// w-80|w-60 p-2: 320px|240px + 16px padding on each side
const videoItemWidth = 320 + 16 * 2;
const channelItemWidth = 240 + 16 * 2;
const intersectionObserver = new IntersectionObserver(
function (entries) {
carouselElementIntersecting = entries[0].isIntersecting;
if (carouselElementIntersecting) {
loadRow();
}
},
{ threshold: [0] }
);
$: {
if (
carouselElement &&
carouselElementIntersecting &&
itemWidths &&
itemWidths.length
) {
recalculateVisibileCells();
}
}
$: {
if (videos && scrollEnd >= videos.length - 1) {
loadRow();
}
}
$: {
if (carouselElement) {
intersectionObserver.observe(carouselElement);
}
}
let invidiousApi = new InvidiousApi();
playletStateStore.subscribe((value) => {
invidiousApi.instance = value?.invidious?.current_instance;
invidiousApi.userCountryCode = value?.device?.user_country_code ?? "US";
invidiousApi.isLoggedIn = value.invidious?.logged_in ?? false;
loadRow();
});
invidiousVideoApiStore.subscribe((value) => {
invidiousApi.endpoints = value;
loadRow();
});
async function loadRow() {
if (!invidiousApi.canMakeRequest()) {
return;
}
if (!carouselElementIntersecting) {
return;
}
if (scrollEnd < videos.length - 3) {
return;
}
if (
feedLoadState === FeedLoadState.Loading ||
feedLoadState === FeedLoadState.Loaded
) {
return;
}
feedLoadState = FeedLoadState.Loading;
let totalFetchedItems = 0;
while (true) {
const feedSources = feed.feedSources;
if (feedSourcesIndex >= feedSources.length) {
break;
}
const feedSource = feedSources[feedSourcesIndex];
feedSource.state = feedSource.state || {};
if (
feedSource.state.loadState === FeedLoadState.Loaded ||
feedSource.state.loadState === FeedLoadState.Error
) {
feedSourcesIndex++;
continue;
}
invidiousApi.markFeedSourcePagination(feedSource);
try {
const result = await invidiousApi.makeRequest(feedSource);
if (!result) {
feedSource.state.loadState = FeedLoadState.Error;
feedSourcesIndex++;
if (feedSourcesIndex >= feedSources.length) {
feedLoadState = FeedLoadState.Loaded;
}
continue;
}
const hasContinuation = !!result.continuation;
if (hasContinuation) {
feedSource.state.continuation = result.continuation;
}
if (result.items.length > 0) {
const paginationType = feedSource.state.paginationType;
if (paginationType === "Continuation" && hasContinuation) {
feedSource.state.loadState = FeedLoadState.LoadedPage;
} else if (paginationType === "Pages") {
feedSource.state.loadState = FeedLoadState.LoadedPage;
} else {
feedSource.state.loadState = FeedLoadState.Loaded;
}
} else {
feedSource.state.loadState = FeedLoadState.Loaded;
}
if (
feedSource.state.loadState === FeedLoadState.Loaded &&
feedSourcesIndex === feedSources.length - 1
) {
feedLoadState = FeedLoadState.Loaded;
} else {
feedLoadState = FeedLoadState.LoadedPage;
}
if (result && result.items) {
const newVideos = result.items;
newVideos.forEach((item) => {
if (item.videoId) {
item.type = "video";
} else if (item.playlistId) {
item.type = "playlist";
}
});
const newItemWidths = newVideos.map((video) => {
if (video.type === "channel") {
return channelItemWidth;
} else {
return videoItemWidth;
}
});
itemWidths = [...itemWidths, ...newItemWidths];
videos = [...(videos || []), ...newVideos];
totalFetchedItems += result.items.length;
if (totalFetchedItems >= 3) {
break;
}
}
} catch (error) {
// TODO:P0 handle case of unauthenticated calls
// TODO:P0 handle case of popular feed disabled
// If we're done loading all the feed, but still have no videos, hide the whole component.
console.error(error);
feedSource.state.loadState = FeedLoadState.Error;
}
}
}
function recalculateVisibileCells() {
let scrollLeft = carouselElement.scrollLeft;
const clientWidth = carouselElement.clientWidth || window.innerWidth;
let _scrollStart = 0;
while (
_scrollStart < itemWidths.length &&
scrollLeft > itemWidths[_scrollStart]
) {
scrollLeft -= itemWidths[_scrollStart];
_scrollStart++;
}
let _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);
if (_scrollStart !== scrollStart) {
scrollStart = _scrollStart;
}
if (_scrollEnd !== scrollEnd) {
scrollEnd = _scrollEnd;
}
}
</script>
<!-- Hide rows that are loaded but have no videos. Typically a
disabled feed (like popular) or unauthenticated.
TODO:P0 show action nodes (action to login)
-->
{#if feedLoadState !== FeedLoadState.Loaded || videos.length !== 0}
<div class="text-lg font-semibold m-4">
{feed.title}
</div>
<div
class="carousel carousel-center rounded-box w-full space-x-4"
style="min-height: 16rem;"
bind:this={carouselElement}
on:scroll={recalculateVisibileCells}
>
{#each videos as video, i}
<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>
{/each}
</div>
{/if}
@@ -0,0 +1,102 @@
<script lang="ts">
export let checked = false;
export let lengthSeconds = undefined;
export let timestamp = 0;
let textInputElemet;
let timestampText = "";
let timestampRange = 0;
$: {
if (document.activeElement !== textInputElemet) {
timestampText = getFormattedTime(timestamp);
}
}
$: timestampRange = timestamp !== undefined ? timestamp : 0;
function getTimeStamp(timestampText) {
if (timestampText === "") {
return 0;
}
const match = /^((\d+):)?(\d{1,2}):(\d{1,2})$/.exec(timestampText);
if (match) {
const hours = match[2] ? parseInt(match[2]) : 0;
const minutes = parseInt(match[3]);
const seconds = parseInt(match[4]);
if (minutes < 60 && seconds < 60) {
return hours * 3600 + minutes * 60 + seconds;
}
}
}
function getFormattedTime(length) {
if (length === undefined) {
return undefined;
}
const hours = Math.floor(length / 3600);
const minutes = Math.floor((length / 60) % 60);
const seconds = length % 60;
const secondsString = seconds < 10 ? `0${seconds}` : seconds.toString();
const minutesString =
minutes < 10 && hours > 0 ? `0${minutes}` : minutes.toString();
let formattedTime = minutesString + ":" + secondsString;
if (hours > 0) {
formattedTime = hours.toString() + ":" + formattedTime;
}
return formattedTime;
}
</script>
<div class="form-control">
<div class="flex flex-row items-center">
<label class="label justify-start cursor-pointer">
<input type="checkbox" bind:checked class="checkbox" />
<span class="label-text m-2">Start at</span>
</label>
<input
bind:this={textInputElemet}
disabled={!checked}
class="input w-28"
placeholder="0:00"
bind:value={timestampText}
on:input={(e) => {
let newTimestamp = getTimeStamp(e.currentTarget.value);
if (newTimestamp !== undefined && lengthSeconds) {
newTimestamp = Math.min(newTimestamp, lengthSeconds);
}
timestamp = newTimestamp;
}}
on:change={(e) => {
timestampText = getFormattedTime(timestamp) ?? "0:00";
timestamp = getTimeStamp(timestampText);
}}
/>
{#if checked}
{#if timestamp !== undefined}
<span class="label-text m-2">({timestamp}s)</span>
{:else}
<span class="label-text m-2 text-error">(Invalid time)</span>
{/if}
{/if}
</div>
{#if lengthSeconds && checked}
<input
type="range"
min="0"
max={lengthSeconds}
bind:value={timestampRange}
on:input={(e) => {
// @ts-ignore
timestamp = e.currentTarget.value;
}}
class="range range-xs range-primary"
/>
{/if}
</div>
@@ -0,0 +1,73 @@
<script lang="ts">
export let title: string | undefined = undefined;
export let videoThumbnails: any[] | undefined = undefined;
export let liveNow: boolean = undefined;
export let lengthSeconds: number = undefined;
export let viewCount: number | undefined = undefined;
export let invidiousInstance;
let thumbnailUrl;
$: {
if (videoThumbnails && videoThumbnails.length > 0) {
const videoThumbnail =
videoThumbnails.find((thumbnail) => thumbnail.quality === "medium") ||
videoThumbnails[0];
let url = videoThumbnail.url;
if (url.startsWith("/") && invidiousInstance) {
url = invidiousInstance + url;
}
thumbnailUrl = url;
}
}
function getFormattedTime(length) {
const hours = Math.floor(length / 3600);
const minutes = Math.floor((length / 60) % 60);
const seconds = length % 60;
const secondsString = seconds < 10 ? `0${seconds}` : seconds.toString();
const minutesString =
minutes < 10 && hours > 0 ? `0${minutes}` : minutes.toString();
let formattedTime = minutesString + ":" + secondsString;
if (hours > 0) {
formattedTime = hours.toString() + ":" + formattedTime;
}
return formattedTime;
}
function isVideoLive() {
if (liveNow) {
return true;
}
return lengthSeconds === 0 && viewCount === 0;
}
</script>
<figure class="relative">
<img
class="w-full rounded-box aspect-video object-cover"
loading="lazy"
width="320"
height="180"
src={thumbnailUrl}
alt={title}
/>
{#if isVideoLive()}
<div
class="absolute bottom-2 right-0 bg-red-500 text-white text-sm rounded-sm font-bold pt-1 pb-1 pr-2 pl-2"
>
LIVE
</div>
{:else if lengthSeconds}
<div
class="absolute bottom-2 right-0 bg-black/70 text-white text-sm rounded-sm pt-1 pb-1 pr-2 pl-2"
>
{getFormattedTime(lengthSeconds)}
</div>
{/if}
</figure>