mirror of
https://github.com/Ku6epXBOCTuK/easy-png-tools.git
synced 2026-11-13 17:08:20 +00:00
45 lines
1.1 KiB
TypeScript
45 lines
1.1 KiB
TypeScript
import { SvelteSet } from "svelte/reactivity";
|
|
import { safeSetItem, storage } from "./storage";
|
|
|
|
const KEY = "fav-tools";
|
|
|
|
const slugs = new SvelteSet<string>();
|
|
|
|
export function initFavorites(): void {
|
|
const raw = storage()?.getItem(KEY);
|
|
if (!raw) return;
|
|
try {
|
|
const list: unknown = JSON.parse(raw);
|
|
if (Array.isArray(list)) {
|
|
for (const s of list) if (typeof s === "string") slugs.add(s);
|
|
}
|
|
} catch {
|
|
// garbage in localStorage: fall back to an empty set
|
|
}
|
|
}
|
|
|
|
export function isFav(slug: string): boolean {
|
|
return slugs.has(slug);
|
|
}
|
|
|
|
export function toggleFav(slug: string): void {
|
|
if (slugs.has(slug)) {
|
|
slugs.delete(slug);
|
|
} else {
|
|
slugs.add(slug);
|
|
}
|
|
safeSetItem(KEY, JSON.stringify([...slugs]));
|
|
}
|
|
|
|
/** Favorite pages from the list (order preserved). */
|
|
export function favPages<T extends { slug: string }>(pages: readonly T[]): T[] {
|
|
return pages.filter((p) => slugs.has(p.slug));
|
|
}
|
|
|
|
/** Same pages, favorites first (a boost in search results). */
|
|
export function favFirst<T extends { slug: string }>(pages: readonly T[]): T[] {
|
|
return [...pages].sort(
|
|
(a, b) => Number(slugs.has(b.slug)) - Number(slugs.has(a.slug)),
|
|
);
|
|
}
|