Bookmarks in web app (#177)
* Bookmarks api * Lint fix * first pass * Lint fix * comment * bookmarks, loading feedSources, and pagination * comment * null check --------- Co-authored-by: github-action linter <githubaction@githubaction.com>
This commit is contained in:
1 parent
58ce2d6396
commit
57fa1c5c3f
23 files changed
+510
-192
No files matched your search
@@ -147,12 +147,12 @@ function CreateChannelFeed(title as string, endpoint as string, ucid as string,
|
||||
return {
|
||||
title: title,
|
||||
feedSources: [{
|
||||
id: `inv_${endpoint}_${ucid}`,
|
||||
title: `${author} - ${title}`,
|
||||
apiType: "Invidious",
|
||||
endpoint: endpoint,
|
||||
pathParams: {
|
||||
ucid: ucid
|
||||
"id": `inv_${endpoint}_${ucid}`,
|
||||
"title": `${author} - ${title}`,
|
||||
"apiType": "Invidious",
|
||||
"endpoint": endpoint,
|
||||
"pathParams": {
|
||||
"ucid": ucid
|
||||
}
|
||||
}]
|
||||
}
|
||||
|
||||
@@ -63,7 +63,8 @@
|
||||
applicationInfo="bind:../ApplicationInfo"
|
||||
invidious="bind:../Invidious"
|
||||
preferences="bind:../Preferences"
|
||||
playQueue="bind:../PlayQueue" />
|
||||
playQueue="bind:../PlayQueue"
|
||||
bookmarks="bind:../Bookmarks" />
|
||||
</AppController>
|
||||
</children>
|
||||
</component>
|
||||
@@ -24,7 +24,8 @@ function InitializeBindings()
|
||||
"applicationInfo": "../ApplicationInfo",
|
||||
"invidious": "../Invidious",
|
||||
"preferences": "../Preferences",
|
||||
"playQueue": "../PlayQueue"
|
||||
"playQueue": "../PlayQueue",
|
||||
"bookmarks": "../Bookmarks"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,16 +73,15 @@ function SetRowListContent(bookmarksContent as object)
|
||||
feeds = []
|
||||
for each bookmarkGroupNode in bookmarkGroupNodes
|
||||
bookmarkNodes = bookmarkGroupNode.getChildren(-1, 0)
|
||||
feed = {
|
||||
title: bookmarkGroupNode.title,
|
||||
feedSources: []
|
||||
}
|
||||
|
||||
feedSources = []
|
||||
for each bookmarkNode in bookmarkNodes
|
||||
feed.feedSources.push(bookmarkNode.feedSource)
|
||||
feedSources.push(bookmarkNode.feedSource)
|
||||
end for
|
||||
|
||||
feeds.push(feed)
|
||||
feeds.push({
|
||||
"title": bookmarkGroupNode.title,
|
||||
"feedSources": feedSources
|
||||
})
|
||||
end for
|
||||
|
||||
m.rowList.feeds = feeds
|
||||
|
||||
@@ -204,12 +204,12 @@ function Search(text as string)
|
||||
ShowLoadingScreen()
|
||||
|
||||
feedSource = {
|
||||
id: `inv_search_${CryptoUtils.GetMd5(text)}`,
|
||||
title: `Search - ${text}`,
|
||||
apiType: "Invidious",
|
||||
endpoint: "search",
|
||||
queryParams: {
|
||||
q: text
|
||||
"id": `inv_search_${CryptoUtils.GetMd5(text)}`,
|
||||
"title": `Search - ${text}`,
|
||||
"apiType": "Invidious",
|
||||
"endpoint": "search",
|
||||
"queryParams": {
|
||||
"q": text
|
||||
}
|
||||
}
|
||||
|
||||
@@ -229,8 +229,8 @@ function Search(text as string)
|
||||
end for
|
||||
|
||||
m.rowList.feeds = [{
|
||||
title: `Search - ${text}`,
|
||||
feedSources: [feedSource]
|
||||
"title": `Search - ${text}`,
|
||||
"feedSources": [feedSource]
|
||||
}]
|
||||
end function
|
||||
|
||||
|
||||
@@ -53,19 +53,19 @@ function Save() as void
|
||||
bookmarkNodes = bookmarkGroupNode.getChildren(-1, 0)
|
||||
for each bookmarkNode in bookmarkNodes
|
||||
bookmarks.push({
|
||||
id: bookmarkNode.id,
|
||||
feedSource: bookmarkNode.feedSource
|
||||
"id": bookmarkNode.id,
|
||||
"feedSource": bookmarkNode.feedSource
|
||||
})
|
||||
end for
|
||||
groups.push({
|
||||
title: bookmarkGroupNode.title,
|
||||
bookmarks: bookmarks
|
||||
"title": bookmarkGroupNode.title,
|
||||
"bookmarks": bookmarks
|
||||
})
|
||||
end for
|
||||
|
||||
bookmarksString = FormatJson({
|
||||
__version: m.top.__version,
|
||||
groups: groups
|
||||
"__version": m.top.__version,
|
||||
"groups": groups
|
||||
})
|
||||
|
||||
if m.bookmarksString = bookmarksString
|
||||
@@ -110,10 +110,10 @@ function AddVideoBookmark(id as string, groupName as string)
|
||||
bookmarkGroupNode = GetOrCreateBookmarkGroup(groupName)
|
||||
|
||||
feedSource = {
|
||||
apiType: "Invidious",
|
||||
endpoint: "video_info",
|
||||
pathParams: {
|
||||
id: id
|
||||
"apiType": "Invidious",
|
||||
"endpoint": "video_info",
|
||||
"pathParams": {
|
||||
"id": id
|
||||
}
|
||||
}
|
||||
AddFeedSourceBookmark(feedSource, id, bookmarkGroupNode)
|
||||
@@ -123,10 +123,10 @@ function AddPlaylistBookmark(id as string, groupName as string)
|
||||
bookmarkGroupNode = GetOrCreateBookmarkGroup(groupName)
|
||||
|
||||
feedSource = {
|
||||
apiType: "Invidious",
|
||||
endpoint: "playlist_info",
|
||||
pathParams: {
|
||||
plid: id
|
||||
"apiType": "Invidious",
|
||||
"endpoint": "playlist_info",
|
||||
"pathParams": {
|
||||
"plid": id
|
||||
}
|
||||
}
|
||||
AddFeedSourceBookmark(feedSource, id, bookmarkGroupNode)
|
||||
@@ -136,21 +136,21 @@ function AddPlaylistBookmarkWithSpread(id as string, groupName as string)
|
||||
bookmarkGroupNode = GetOrCreateBookmarkGroup(groupName)
|
||||
|
||||
feedSource = {
|
||||
id: `inv_playlist_${id}`,
|
||||
title: `${groupName} videos`,
|
||||
apiType: "Invidious",
|
||||
endpoint: "playlist",
|
||||
pathParams: {
|
||||
plid: id
|
||||
"id": `inv_playlist_${id}`,
|
||||
"title": `${groupName} videos`,
|
||||
"apiType": "Invidious",
|
||||
"endpoint": "playlist",
|
||||
"pathParams": {
|
||||
"plid": id
|
||||
}
|
||||
}
|
||||
AddFeedSourceBookmark(feedSource, `inv_playlist_${id}`, bookmarkGroupNode)
|
||||
|
||||
feedSource = {
|
||||
apiType: "Invidious",
|
||||
endpoint: "playlist_info",
|
||||
pathParams: {
|
||||
plid: id
|
||||
"apiType": "Invidious",
|
||||
"endpoint": "playlist_info",
|
||||
"pathParams": {
|
||||
"plid": id
|
||||
}
|
||||
}
|
||||
AddFeedSourceBookmark(feedSource, id, bookmarkGroupNode)
|
||||
@@ -160,10 +160,10 @@ function AddChannelBookmark(id as string, groupName as string)
|
||||
bookmarkGroupNode = GetOrCreateBookmarkGroup(groupName)
|
||||
|
||||
feedSource = {
|
||||
apiType: "Invidious",
|
||||
endpoint: "channel_info",
|
||||
pathParams: {
|
||||
ucid: id
|
||||
"apiType": "Invidious",
|
||||
"endpoint": "channel_info",
|
||||
"pathParams": {
|
||||
"ucid": id
|
||||
}
|
||||
}
|
||||
AddFeedSourceBookmark(feedSource, id, bookmarkGroupNode)
|
||||
@@ -173,21 +173,21 @@ function AddChannelBookmarkWithSpread(id as string, groupName as string)
|
||||
bookmarkGroupNode = GetOrCreateBookmarkGroup(groupName)
|
||||
|
||||
feedSource = {
|
||||
id: `inv_channel_videos_${id}`,
|
||||
title: `${groupName} videos`,
|
||||
apiType: "Invidious",
|
||||
endpoint: "channel_videos",
|
||||
pathParams: {
|
||||
ucid: id
|
||||
"id": `inv_channel_videos_${id}`,
|
||||
"title": `${groupName} videos`,
|
||||
"apiType": "Invidious",
|
||||
"endpoint": "channel_videos",
|
||||
"pathParams": {
|
||||
"ucid": id
|
||||
}
|
||||
}
|
||||
AddFeedSourceBookmark(feedSource, `inv_channel_videos_${id}`, bookmarkGroupNode)
|
||||
|
||||
feedSource = {
|
||||
apiType: "Invidious",
|
||||
endpoint: "channel_info",
|
||||
pathParams: {
|
||||
ucid: id
|
||||
"apiType": "Invidious",
|
||||
"endpoint": "channel_info",
|
||||
"pathParams": {
|
||||
"ucid": id
|
||||
}
|
||||
}
|
||||
AddFeedSourceBookmark(feedSource, id, bookmarkGroupNode)
|
||||
|
||||
@@ -169,7 +169,7 @@ namespace Invidious
|
||||
|
||||
feedSourceState = feedSource.state
|
||||
if not feedSourceState.DoesExist("queryParams")
|
||||
feedSourceState.queryParams = {}
|
||||
feedSourceState["queryParams"] = {}
|
||||
end if
|
||||
|
||||
feedSourceState.paginationType = endpoint.paginationType
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
import "pkg:/source/utils/RegistryUtils.bs"
|
||||
|
||||
namespace Http
|
||||
|
||||
class BookmarksRouter extends HttpRouter
|
||||
|
||||
function new()
|
||||
super()
|
||||
|
||||
m.Get("/api/bookmarks", function(context as object) as boolean
|
||||
response = context.response
|
||||
|
||||
bookmarksString = RegistryUtils.Read(RegistryUtils.BOOKMARKS)
|
||||
if bookmarksString = invalid
|
||||
response.Json({
|
||||
groups: []
|
||||
})
|
||||
return true
|
||||
end if
|
||||
|
||||
response.SetBodyDataString(bookmarksString)
|
||||
response.ContentType("application/json")
|
||||
response.http_code = 200
|
||||
|
||||
return true
|
||||
end function)
|
||||
|
||||
m.Get("/api/bookmarks/feeds", function(context as object) as boolean
|
||||
response = context.response
|
||||
router = context.router
|
||||
bookmarks = context.server.task.bookmarks
|
||||
|
||||
feeds = router.BookmarksContentToFeeds(bookmarks.content)
|
||||
|
||||
response.Json(feeds)
|
||||
|
||||
return true
|
||||
end function)
|
||||
end function
|
||||
|
||||
' TODO:P1 refactor to share code with Bookmarks screen
|
||||
function BookmarksContentToFeeds(bookmarksContent as object) as object
|
||||
bookmarkGroupNodes = bookmarksContent.getChildren(-1, 0)
|
||||
|
||||
feeds = []
|
||||
for each bookmarkGroupNode in bookmarkGroupNodes
|
||||
bookmarkNodes = bookmarkGroupNode.getChildren(-1, 0)
|
||||
|
||||
feedSources = []
|
||||
for each bookmarkNode in bookmarkNodes
|
||||
feedSources.push(bookmarkNode.feedSource)
|
||||
end for
|
||||
|
||||
feeds.push({
|
||||
"title": bookmarkGroupNode.title,
|
||||
"feedSources": feedSources
|
||||
})
|
||||
end for
|
||||
|
||||
return feeds
|
||||
end function
|
||||
|
||||
end class
|
||||
|
||||
end namespace
|
||||
@@ -47,14 +47,15 @@ namespace Http
|
||||
|
||||
' This is an endpoint allowing the web app to make an authenticated request
|
||||
' without needing the Invidious token
|
||||
' TODO:P2 this should be a POST request
|
||||
m.Get("/invidious/authenticated-request", function(context as object) as boolean
|
||||
request = context.request
|
||||
response = context.response
|
||||
invidiousNode = context.server.task.invidious
|
||||
|
||||
requestData = request.query["request-data"]
|
||||
if requestData = invalid
|
||||
response.Default(400, "Expected request-data")
|
||||
feedSource = request.query["feed-source"]
|
||||
if feedSource = invalid
|
||||
response.Default(400, "Expected feed-source")
|
||||
return true
|
||||
end if
|
||||
|
||||
@@ -64,12 +65,10 @@ namespace Http
|
||||
return true
|
||||
end if
|
||||
|
||||
requestData = ParseJson(requestData)
|
||||
feedSource = ParseJson(feedSource)
|
||||
|
||||
' TODO:P0 handle pagination
|
||||
' Perhaps the page arg can be added to queryParam of requestData directly
|
||||
invService = new Invidious.InvidiousService(invidiousNode)
|
||||
invResponse = invService.MakeRequest(requestData)
|
||||
invResponse = invService.MakeRequest(feedSource, feedSource.state?.queryParams)
|
||||
if not invResponse.success
|
||||
response.Default(500, `Failed to make request: ${invResponse.error}`)
|
||||
return true
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import "pkg:/components/Web/PlayletWebServer/Middleware/BookmarksRouter.bs"
|
||||
import "pkg:/components/Web/PlayletWebServer/Middleware/DashRouter.bs"
|
||||
import "pkg:/components/Web/PlayletWebServer/Middleware/InvidiousRouter.bs"
|
||||
import "pkg:/components/Web/PlayletWebServer/Middleware/PlayletLibUrlsRouter.bs"
|
||||
@@ -38,6 +39,7 @@ function SetupRoutes(server as object)
|
||||
server.UseRouter(new Http.PreferencesRouter())
|
||||
server.UseRouter(new Http.InvidiousRouter())
|
||||
server.UseRouter(new Http.PlayQueueRouter())
|
||||
server.UseRouter(new Http.BookmarksRouter())
|
||||
server.UseRouter(new Http.SearchHistoryRouter())
|
||||
server.UseRouter(new Http.PlayletLibUrlsRouter())
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
<field id="invidious" type="node" />
|
||||
<field id="applicationInfo" type="node" />
|
||||
<field id="playQueue" type="node" />
|
||||
<field id="bookmarks" type="node" />
|
||||
<function name="GetServerAddress" />
|
||||
</interface>
|
||||
</component>
|
||||
@@ -4,6 +4,7 @@
|
||||
import { PlayletApi } from "lib/Api/PlayletApi";
|
||||
import {
|
||||
appStateStore,
|
||||
bookmarksStore,
|
||||
homeLayoutFileStore,
|
||||
invidiousVideoApiStore,
|
||||
playletStateStore,
|
||||
@@ -18,6 +19,7 @@
|
||||
import SettingsScreen from "lib/Screens/SettingsScreen.svelte";
|
||||
import InfoScreen from "lib/Screens/InfoScreen.svelte";
|
||||
import LinkDragDrop from "lib/LinkDragDrop.svelte";
|
||||
import BookmarksScreen from "lib/Screens/BookmarksScreen.svelte";
|
||||
|
||||
onMount(async () => {
|
||||
PlayletApi.getState().then((value) => {
|
||||
@@ -43,6 +45,10 @@
|
||||
PlayletApi.getSearchHistory().then((value) => {
|
||||
searchHistoryStore.set(value);
|
||||
});
|
||||
|
||||
PlayletApi.getBookmarkFeeds().then((value) => {
|
||||
bookmarksStore.set(value);
|
||||
});
|
||||
});
|
||||
|
||||
let currentScreen: AppState["screen"];
|
||||
@@ -61,6 +67,8 @@
|
||||
|
||||
<HomeScreen visibility={currentScreen == "home"} />
|
||||
|
||||
<BookmarksScreen visibility={currentScreen == "bookmarks"} />
|
||||
|
||||
<SettingsScreen visibility={currentScreen == "settings"} />
|
||||
|
||||
<InfoScreen visibility={currentScreen == "info"} />
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
<svg
|
||||
version="1.1"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
xmlns:xlink="http://www.w3.org/1999/xlink"
|
||||
x="0px"
|
||||
y="0px"
|
||||
viewBox="0 0 24 24"
|
||||
preserveAspectRatio="xMidYMid meet"
|
||||
focusable="false"
|
||||
style="pointer-events: none; display: block; width: 100%; height: 100%;"
|
||||
>
|
||||
<g class="fill-current">
|
||||
<path
|
||||
d="M22,10.1c0.1-0.5-0.3-1.1-0.8-1.1l-5.7-0.8L12.9,3c-0.1-0.2-0.2-0.3-0.4-0.4C12,2.3,11.4,2.5,11.1,3L8.6,8.2L2.9,9
|
||||
C2.6,9,2.4,9.1,2.3,9.3c-0.4,0.4-0.4,1,0,1.4l4.1,4l-1,5.7c0,0.2,0,0.4,0.1,0.6c0.3,0.5,0.9,0.7,1.4,0.4l5.1-2.7l5.1,2.7
|
||||
c0.1,0.1,0.3,0.1,0.5,0.1l0,0c0.1,0,0.1,0,0.2,0c0.5-0.1,0.9-0.6,0.8-1.2l-1-5.7l4.1-4C21.9,10.5,22,10.3,22,10.1z"
|
||||
/>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 691 B |
@@ -12,6 +12,13 @@ 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),
|
||||
"VideoInfoHandler": (requestData, response) => this.VideoInfoHandler(requestData, response),
|
||||
"PlaylistInfoHandler": (requestData, response) => this.PlaylistInfoHandler(requestData, response),
|
||||
"ChannelInfoHandler": (requestData, response) => this.ChannelInfoHandler(requestData, response),
|
||||
"ChannelVideosHandler": (requestData, response) => this.ChannelVideosHandler(requestData, response),
|
||||
"ChannelPlaylistsHandler": (requestData, response) => this.ChannelPlaylistsHandler(requestData, response),
|
||||
"ChannelRelatedChannelsHandler": (requestData, response) => this.ChannelRelatedChannelsHandler(requestData, response),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,10 +51,39 @@ export class InvidiousApi {
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
public async makeRequest(feed: any) {
|
||||
// TODO:P0 handle multiple feed sources
|
||||
const feedSource = feed.feedSources[0]
|
||||
public async markFeedSourcePagination(feedSource: any) {
|
||||
const endpoint = this.endpoints[feedSource.endpoint]
|
||||
if (!endpoint || !endpoint.paginationType) {
|
||||
return;
|
||||
}
|
||||
|
||||
const feedSourceState = feedSource.state
|
||||
feedSourceState.queryParams = feedSourceState.queryParams || {}
|
||||
|
||||
feedSourceState.paginationType = endpoint.paginationType
|
||||
|
||||
if (feedSourceState.paginationType === "Pages") {
|
||||
if (!Number.isInteger(feedSourceState.page)) {
|
||||
feedSourceState.page = 0;
|
||||
}
|
||||
feedSourceState.page += 1;
|
||||
feedSourceState.queryParams.page = feedSourceState.page;
|
||||
} else if (feedSourceState.paginationType === "Continuation") {
|
||||
const continuation = feedSourceState.continuation;
|
||||
if (continuation) {
|
||||
feedSourceState.queryParams.continuation = continuation;
|
||||
} else {
|
||||
delete feedSourceState.queryParams.continuation;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public canMakeRequest() {
|
||||
return !!(this.instance && this.endpoints && Object.keys(this.endpoints).length);
|
||||
}
|
||||
|
||||
public async makeRequest(feedSource: any) {
|
||||
// TODO:P0 implement localStorage caching
|
||||
if (!feedSource || !this.instance || !this.endpoints) {
|
||||
return null;
|
||||
}
|
||||
@@ -90,6 +126,10 @@ export class InvidiousApi {
|
||||
queryParams = { ...queryParams, ...feedSource.queryParams };
|
||||
}
|
||||
|
||||
if (feedSource.state.queryParams !== undefined) {
|
||||
queryParams = { ...queryParams, ...feedSource.state.queryParams };
|
||||
}
|
||||
|
||||
if (feedSource.pathParams !== undefined) {
|
||||
for (let param in feedSource.pathParams) {
|
||||
url = url.replace(`{${param}}`, feedSource.pathParams[param]);
|
||||
@@ -111,6 +151,55 @@ export class InvidiousApi {
|
||||
return { items };
|
||||
}
|
||||
|
||||
private async PlaylistHandler(feedSource, response) {
|
||||
const json = await response.json();
|
||||
return {
|
||||
items: json.videos,
|
||||
};
|
||||
}
|
||||
|
||||
private async VideoInfoHandler(feedSource, response) {
|
||||
const info = await response.json();
|
||||
info.type = "video";
|
||||
return { items: [info] };
|
||||
}
|
||||
|
||||
private async ChannelInfoHandler(feedSource, response) {
|
||||
const info = await response.json();
|
||||
info.type = "channel";
|
||||
return { items: [info] };
|
||||
}
|
||||
|
||||
private async PlaylistInfoHandler(feedSource, response) {
|
||||
const info = await response.json();
|
||||
info.type = "playlist";
|
||||
return { items: [info] };
|
||||
}
|
||||
|
||||
private async ChannelVideosHandler(feedSource, response) {
|
||||
const json = await response.json();
|
||||
return {
|
||||
items: json.videos,
|
||||
continuation: json.continuation
|
||||
};
|
||||
}
|
||||
|
||||
private async ChannelPlaylistsHandler(feedSource, response) {
|
||||
const json = await response.json();
|
||||
return {
|
||||
items: json.playlists,
|
||||
continuation: json.continuation
|
||||
};
|
||||
}
|
||||
|
||||
private async ChannelRelatedChannelsHandler(feedSource, response) {
|
||||
const json = await response.json();
|
||||
return {
|
||||
items: json.relatedChannels,
|
||||
continuation: json.continuation
|
||||
};
|
||||
}
|
||||
|
||||
private makeUrl(url: string, params: any) {
|
||||
const encodedUrl = new URL(url);
|
||||
const existingParams = new URLSearchParams(encodedUrl.search);
|
||||
|
||||
@@ -23,8 +23,8 @@ export class PlayletApi {
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
static async invidiousAuthenticatedRequest(requestData) {
|
||||
const url = PlayletApi.host() + "/invidious/authenticated-request?request-data=" + encodeURIComponent(JSON.stringify(requestData));
|
||||
static async invidiousAuthenticatedRequest(feedSource) {
|
||||
const url = PlayletApi.host() + "/invidious/authenticated-request?feed-source=" + encodeURIComponent(JSON.stringify(feedSource));
|
||||
const response = await fetch(url);
|
||||
return await response.json();
|
||||
}
|
||||
@@ -128,6 +128,11 @@ export class PlayletApi {
|
||||
return await fetch(`${PlayletApi.host()}/api/search-history`, { method: "DELETE" });
|
||||
}
|
||||
|
||||
static async getBookmarkFeeds() {
|
||||
const response = await fetch(`${PlayletApi.host()}/api/bookmarks/feeds`);
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
static async updateInstance(instance) {
|
||||
return await PlayletApi.putJson(`${PlayletApi.host()}/api/preferences`, { "invidious.instance": instance });
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
import InfoIcon from "../assets/info-icon.svg.svelte";
|
||||
import SearchIcon from "../assets/search-icon.svg.svelte";
|
||||
import SettingsIcon from "../assets/settings-icon.svg.svelte";
|
||||
import BookmarksIcon from "../assets/star-icon.svg.svelte";
|
||||
|
||||
import { appStateStore } from "lib/Stores";
|
||||
import type { AppState } from "lib/Types";
|
||||
|
||||
@@ -37,6 +39,14 @@
|
||||
<HomeIcon />
|
||||
</div>
|
||||
</button>
|
||||
<button
|
||||
on:click={() => setScreen("bookmarks")}
|
||||
class={$appStateStore.screen === "bookmarks" ? "active" : ""}
|
||||
>
|
||||
<div class="h-6">
|
||||
<BookmarksIcon />
|
||||
</div>
|
||||
</button>
|
||||
<button
|
||||
on:click={() => setScreen("settings")}
|
||||
class={$appStateStore.screen === "settings" ? "active" : ""}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
<script lang="ts">
|
||||
import VideoListRow from "./Home/VideoListRow.svelte";
|
||||
import { bookmarksStore } from "lib/Stores";
|
||||
|
||||
export let visibility: boolean;
|
||||
</script>
|
||||
|
||||
<div class={visibility ? "" : "hidden"}>
|
||||
{#each $bookmarksStore as feed}
|
||||
<VideoListRow {feed} />
|
||||
{/each}
|
||||
</div>
|
||||
@@ -1,115 +0,0 @@
|
||||
<script lang="ts">
|
||||
import { InvidiousApi } from "lib/Api/InvidiousApi";
|
||||
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 feed: any = undefined;
|
||||
export let videos = undefined;
|
||||
|
||||
let itemWidths = [];
|
||||
|
||||
let carouselElement;
|
||||
|
||||
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;
|
||||
|
||||
$: {
|
||||
if (carouselElement && itemWidths && itemWidths.length) {
|
||||
recalculateVisibileCells();
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
updateRow();
|
||||
});
|
||||
|
||||
invidiousVideoApiStore.subscribe((value) => {
|
||||
invidiousApi.endpoints = value;
|
||||
updateRow();
|
||||
});
|
||||
|
||||
async function updateRow() {
|
||||
const result = await invidiousApi.makeRequest(feed);
|
||||
if (result && result.items) {
|
||||
result.items.forEach((item) => {
|
||||
if (item.videoId) {
|
||||
item.type = "video";
|
||||
} else if (item.playlistId) {
|
||||
item.type = "playlist";
|
||||
}
|
||||
});
|
||||
videos = result.items;
|
||||
itemWidths = videos.map((video) => {
|
||||
if (video.type === "channel") {
|
||||
return channelItemWidth;
|
||||
} else {
|
||||
return videoItemWidth;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function recalculateVisibileCells() {
|
||||
let scrollLeft = carouselElement.scrollLeft;
|
||||
const clientWidth = carouselElement.clientWidth || window.innerWidth;
|
||||
|
||||
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>
|
||||
|
||||
{#if videos}
|
||||
<div class="text-lg font-semibold m-4">
|
||||
{feed.title}
|
||||
</div>
|
||||
<div
|
||||
class="carousel carousel-center rounded-box w-full space-x-4"
|
||||
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,219 @@
|
||||
<script lang="ts">
|
||||
import { InvidiousApi } from "lib/Api/InvidiousApi";
|
||||
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 feed: any = undefined;
|
||||
export let videos = undefined;
|
||||
|
||||
enum FeedLoadState {
|
||||
None,
|
||||
Loading,
|
||||
Loaded,
|
||||
LoadedPage,
|
||||
Error,
|
||||
}
|
||||
|
||||
let feedLoadState = FeedLoadState.None;
|
||||
let feedSourcesIndex = 0;
|
||||
|
||||
let itemWidths = [];
|
||||
|
||||
let carouselElement;
|
||||
|
||||
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;
|
||||
|
||||
$: {
|
||||
if (carouselElement && itemWidths && itemWidths.length) {
|
||||
recalculateVisibileCells();
|
||||
}
|
||||
}
|
||||
|
||||
$: {
|
||||
if (videos && scrollEnd >= videos.length - 1) {
|
||||
loadRow();
|
||||
}
|
||||
}
|
||||
|
||||
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 (
|
||||
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) {
|
||||
return;
|
||||
}
|
||||
|
||||
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) {
|
||||
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>
|
||||
|
||||
{#if videos}
|
||||
<div class="text-lg font-semibold m-4">
|
||||
{feed.title}
|
||||
</div>
|
||||
<div
|
||||
class="carousel carousel-center rounded-box w-full space-x-4"
|
||||
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}
|
||||
@@ -1,5 +1,5 @@
|
||||
<script lang="ts">
|
||||
import ScreenHomeRow from "lib/Screens/Home/ScreenHomeRow.svelte";
|
||||
import VideoListRow from "lib/Screens/Home/VideoListRow.svelte";
|
||||
import { homeLayoutFileStore } from "lib/Stores";
|
||||
|
||||
export let visibility: boolean;
|
||||
@@ -7,6 +7,6 @@
|
||||
|
||||
<div class={visibility ? "" : "hidden"}>
|
||||
{#each $homeLayoutFileStore as feed}
|
||||
<ScreenHomeRow {feed} />
|
||||
<VideoListRow {feed} />
|
||||
{/each}
|
||||
</div>
|
||||
@@ -87,6 +87,7 @@
|
||||
|
||||
try {
|
||||
isLoading = true;
|
||||
// TODO:P1: search pagination - load more results as we scroll
|
||||
videos = await invidiousApi.search(searchBoxText, searchFilters);
|
||||
} finally {
|
||||
isLoading = false;
|
||||
|
||||
@@ -17,4 +17,6 @@ export const invidiousVideoApiStore = writable({} as any);
|
||||
|
||||
export const homeLayoutFileStore = writable([] as any);
|
||||
|
||||
export const bookmarksStore = writable([] as any);
|
||||
|
||||
export const searchHistoryStore = writable([] as any);
|
||||
@@ -1,5 +1,5 @@
|
||||
export type AppState = {
|
||||
screen: "search" | "home" | "settings" | "info"
|
||||
screen: "search" | "home" | "bookmarks" | "settings" | "info"
|
||||
}
|
||||
|
||||
export type PlayletState = {
|
||||
|
||||
Reference in new issue
Block a user