mirror of
https://github.com/ivabus/gui
synced 2025-06-07 15:50:27 +03:00
refactor pkgs store (#581)
Co-authored-by: neil molina <neil@neils-MacBook-Pro.local>
This commit is contained in:
parent
fb854d85a1
commit
dcc9a34e2c
3 changed files with 298 additions and 285 deletions
26
modules/desktop/src/libs/search-index.ts
Normal file
26
modules/desktop/src/libs/search-index.ts
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
import type { GUIPackage, InstalledPackage, Packages } from "./types";
|
||||||
|
import Fuse from "fuse.js";
|
||||||
|
import log from "$libs/logger";
|
||||||
|
|
||||||
|
let packagesIndex: Fuse<GUIPackage>;
|
||||||
|
|
||||||
|
export function indexPackages(packages: GUIPackage[]) {
|
||||||
|
try {
|
||||||
|
packagesIndex = new Fuse(packages, {
|
||||||
|
keys: ["name", "full_name", "desc", "categories"],
|
||||||
|
minMatchCharLength: 3,
|
||||||
|
threshold: 0.3
|
||||||
|
});
|
||||||
|
log.info("refreshed packages fuse index");
|
||||||
|
} catch (error) {
|
||||||
|
log.error(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function searchPackages(term: string, limit = 5): GUIPackage[] {
|
||||||
|
if (!term || !packagesIndex) return [];
|
||||||
|
// TODO: if online, use algolia else use Fuse
|
||||||
|
const res = packagesIndex.search(term, { limit });
|
||||||
|
const matchingPackages: GUIPackage[] = res.map((v) => v.item);
|
||||||
|
return matchingPackages;
|
||||||
|
}
|
|
@ -7,14 +7,14 @@ import type { GUIPackage } from "$libs/types";
|
||||||
import { getFeaturedPackages, getPackageReviews } from "@native";
|
import { getFeaturedPackages, getPackageReviews } from "@native";
|
||||||
import initAuthStore from "./stores/auth";
|
import initAuthStore from "./stores/auth";
|
||||||
import initNavStore from "./stores/nav";
|
import initNavStore from "./stores/nav";
|
||||||
import initPackagesStore from "./stores/pkgs";
|
import pkgStore from "./stores/pkgs";
|
||||||
import initNotificationStore from "./stores/notifications";
|
import initNotificationStore from "./stores/notifications";
|
||||||
import initAppUpdateStore from "./stores/update";
|
import initAppUpdateStore from "./stores/update";
|
||||||
import { trackSearch } from "./analytics";
|
import { trackSearch } from "./analytics";
|
||||||
|
|
||||||
export const featuredPackages = writable<Package[]>([]);
|
export const featuredPackages = writable<Package[]>([]);
|
||||||
|
|
||||||
export const packagesStore = initPackagesStore();
|
export const packagesStore = pkgStore;
|
||||||
|
|
||||||
export const initializeFeaturedPackages = async () => {
|
export const initializeFeaturedPackages = async () => {
|
||||||
console.log("intialize featured packages");
|
console.log("intialize featured packages");
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import { derived, writable } from "svelte/store";
|
import { derived, writable } from "svelte/store";
|
||||||
import type { GUIPackage, InstalledPackage, Packages } from "../types";
|
import type { GUIPackage, InstalledPackage, Packages } from "../types";
|
||||||
import { PackageStates } from "../types";
|
import { PackageStates } from "../types";
|
||||||
import Fuse from "fuse.js";
|
|
||||||
import {
|
import {
|
||||||
getPackage,
|
getPackage,
|
||||||
getDistPackages,
|
getDistPackages,
|
||||||
|
@ -29,9 +28,10 @@ import log from "$libs/logger";
|
||||||
import { isPackageUpToDate } from "../packages/pkg-utils";
|
import { isPackageUpToDate } from "../packages/pkg-utils";
|
||||||
import withDelay from "$libs/utils/delay";
|
import withDelay from "$libs/utils/delay";
|
||||||
|
|
||||||
|
import { indexPackages, searchPackages } from "$libs/search-index";
|
||||||
|
|
||||||
const packageRefreshInterval = 1000 * 60 * 60; // 1 hour
|
const packageRefreshInterval = 1000 * 60 * 60; // 1 hour
|
||||||
|
|
||||||
export default function initPackagesStore() {
|
|
||||||
let initialized = false;
|
let initialized = false;
|
||||||
let isDestroyed = false;
|
let isDestroyed = false;
|
||||||
let refreshTimeoutId: ReturnType<typeof setTimeout> | null = null;
|
let refreshTimeoutId: ReturnType<typeof setTimeout> | null = null;
|
||||||
|
@ -46,8 +46,6 @@ export default function initPackagesStore() {
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
let packagesIndex: Fuse<GUIPackage>;
|
|
||||||
|
|
||||||
const updateAllPackages = (guiPkgs: GUIPackage[]) => {
|
const updateAllPackages = (guiPkgs: GUIPackage[]) => {
|
||||||
packageMap.update((pkgs) => {
|
packageMap.update((pkgs) => {
|
||||||
guiPkgs.forEach((pkg) => {
|
guiPkgs.forEach((pkg) => {
|
||||||
|
@ -174,12 +172,8 @@ To read more about this package go to [${guiPkg.homepage}](${guiPkg.homepage}).
|
||||||
log.info("initialized packages store with ", guiPkgs.length);
|
log.info("initialized packages store with ", guiPkgs.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
packagesIndex = new Fuse(guiPkgs, {
|
// initialize Fuse index for fuzzy search
|
||||||
keys: ["name", "full_name", "desc", "categories"],
|
indexPackages(guiPkgs);
|
||||||
minMatchCharLength: 3,
|
|
||||||
threshold: 0.3
|
|
||||||
});
|
|
||||||
log.info("refreshed packages fuse index");
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const installedPkgs: InstalledPackage[] = await getInstalledPackages();
|
const installedPkgs: InstalledPackage[] = await getInstalledPackages();
|
||||||
|
@ -312,25 +306,6 @@ To read more about this package go to [${guiPkg.homepage}](${guiPkg.homepage}).
|
||||||
updatePackage(full_name, {}, version);
|
updatePackage(full_name, {}, version);
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
|
||||||
packageList,
|
|
||||||
search: async (term: string, limit = 5): Promise<GUIPackage[]> => {
|
|
||||||
if (!term || !packagesIndex) return [];
|
|
||||||
// TODO: if online, use algolia else use Fuse
|
|
||||||
const res = packagesIndex.search(term, { limit });
|
|
||||||
const matchingPackages: GUIPackage[] = res.map((v) => v.item);
|
|
||||||
return matchingPackages;
|
|
||||||
},
|
|
||||||
init,
|
|
||||||
installPkg,
|
|
||||||
uninstallPkg,
|
|
||||||
syncPackageData,
|
|
||||||
deletePkg,
|
|
||||||
destroy,
|
|
||||||
cachePkgImage
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// This is only used for uninstall now
|
// This is only used for uninstall now
|
||||||
export const withFakeLoader = (
|
export const withFakeLoader = (
|
||||||
pkg: GUIPackage,
|
pkg: GUIPackage,
|
||||||
|
@ -364,3 +339,15 @@ const setBadgeCountFromPkgs = (pkgs: Packages) => {
|
||||||
log.error(error);
|
log.error(error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export default {
|
||||||
|
packageList,
|
||||||
|
search: searchPackages,
|
||||||
|
init,
|
||||||
|
installPkg,
|
||||||
|
uninstallPkg,
|
||||||
|
syncPackageData,
|
||||||
|
deletePkg,
|
||||||
|
destroy,
|
||||||
|
cachePkgImage
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in a new issue