Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions src/pages/plugins/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export default function PluginsInclude(updates) {
let $currList = $list.installed;
let currSection = "installed";
let hideSearchBar = () => {};
let currentPage = 1;
let isLoading = false;
let hasMore = true;
const LIMIT = 50;

Contextmenu({
toggler: $add,
Expand Down Expand Up @@ -142,6 +146,18 @@ export default function PluginsInclude(updates) {

$page.onclick = handleClick;

$list.all.addEventListener('scroll', (e) => {
if (isLoading || !hasMore) return;

const { scrollTop, scrollHeight, clientHeight } = $currList;
if (scrollTop + clientHeight >= scrollHeight - 100) {
if (currSection === "all") {
currentPage++;
getAllPlugins();
}
}
})

app.append($page);
helpers.showAd();

Expand Down Expand Up @@ -199,7 +215,6 @@ export default function PluginsInclude(updates) {
section = currSection;
}

// Hide search bar when changing tabs
if (document.getElementById("search-bar")) {
hideSearchBar();
}
Expand All @@ -210,6 +225,8 @@ export default function PluginsInclude(updates) {
$section.scrollTop = $section._scroll || 0;
$currList = $section;
currSection = section;
currentPage = 1;
hasMore = true;
$page.get(".options .active").classList.remove("active");
$page.get(`#${section}_plugins`).classList.add("active");
}
Expand Down Expand Up @@ -239,7 +256,7 @@ export default function PluginsInclude(updates) {
$list.all.setAttribute("empty-msg", strings["error"]);
window.log("error", "Failed to search remotely:");
window.log("error", error);
return []; // Return an empty array on error
return [];
}
}

Expand All @@ -263,13 +280,26 @@ export default function PluginsInclude(updates) {
}

async function getAllPlugins() {
if (isLoading || !hasMore) return;

try {
plugins.all = [];
isLoading = true;
if (currentPage === 1) {
plugins.all = [];
$list.all.replaceChildren();
}

$list.all.setAttribute("empty-msg", strings["loading..."]);

const installed = await fsOperation(PLUGIN_DIR).lsDir();
plugins.all = await fsOperation(constants.API_BASE, "plugins").readFile(
"json",
);
const response = await fetch(`${constants.API_BASE}/plugins?page=${currentPage}&limit=${LIMIT}`);
const newPlugins = await response.json();

if (newPlugins.length < LIMIT) {
hasMore = false;
}

plugins.all = [...plugins.all, ...newPlugins];

installed.forEach(({ url }) => {
const plugin = plugins.all.find(({ id }) => id === Url.basename(url));
Expand All @@ -286,6 +316,8 @@ export default function PluginsInclude(updates) {
$list.all.setAttribute("empty-msg", strings["no plugins found"]);
} catch (error) {
window.log("error", error);
} finally {
isLoading = false;
}
}

Expand Down
59 changes: 56 additions & 3 deletions src/sidebarApps/extensions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ let container = null;
/** @type {HTMLElement} */
let $searchResult = null;

const LIMIT = 50;
let currentPage = 1;
let hasMore = true;
let isLoading = false;

const $header = (
<div className="header">
<span className="title">
Expand Down Expand Up @@ -77,6 +82,7 @@ function initApp(el) {
if (!$explore) {
$explore = collapsableList(strings["explore"]);
$explore.ontoggle = loadExplore;
$explore.$ul.onscroll = handleScroll;
container.append($explore);
}

Expand All @@ -90,6 +96,44 @@ function initApp(el) {
Sidebar.on("show", onSelected);
}

async function handleScroll(e) {
if (isLoading || !hasMore) return;

const { scrollTop, scrollHeight, clientHeight } = e.target;

if (scrollTop + clientHeight >= scrollHeight - 50) {
await loadMorePlugins();
}
}

async function loadMorePlugins() {
try {
isLoading = true;
startLoading($explore);

const response = await fetch(
`${constants.API_BASE}/plugins?page=${currentPage}&limit=${LIMIT}`,
);
const newPlugins = await response.json();

if (newPlugins.length < LIMIT) {
hasMore = false;
}

installedPlugins = await listInstalledPlugins();
const pluginElements = newPlugins.map(ListItem);
$explore.$ul.append(...pluginElements);

currentPage++;
updateHeight($explore);
} catch (error) {
window.log("error", error);
} finally {
isLoading = false;
stopLoading($explore);
}
}

async function searchPlugin() {
clearTimeout(searchTimeout);
searchTimeout = setTimeout(async () => {
Expand Down Expand Up @@ -195,12 +239,21 @@ async function loadExplore() {

try {
startLoading($explore);
const plugins = await fsOperation(
Url.join(constants.API_BASE, "plugins?explore=random"),
).readFile("json");
currentPage = 1;
hasMore = true;

const response = await fetch(
`${constants.API_BASE}/plugins?page=${currentPage}&limit=${LIMIT}`,
);
const plugins = await response.json();

if (plugins.length < LIMIT) {
hasMore = false;
}

installedPlugins = await listInstalledPlugins();
$explore.$ul.content = plugins.map(ListItem);
currentPage++;
updateHeight($explore);
} catch (error) {
$explore.$ul.content = <span className="error">{strings["error"]}</span>;
Expand Down
Loading