mirror of
https://github.com/moku-project/Moku.git
synced 2026-06-13 09:19:56 -05:00
Chore: Restructure Repository for SvelteKit
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
export function timeAgo(ts: number): string {
|
||||
const diff = Date.now() - ts, m = Math.floor(diff / 60000);
|
||||
if (m < 1) return "Just now";
|
||||
if (m < 60) return `${m}m ago`;
|
||||
const h = Math.floor(m / 60);
|
||||
if (h < 24) return `${h}h ago`;
|
||||
const d = Math.floor(h / 24);
|
||||
if (d < 7) return `${d}d ago`;
|
||||
return new Date(ts).toLocaleDateString("en-US", { month: "short", day: "numeric" });
|
||||
}
|
||||
|
||||
export function formatReadTime(mins: number): string {
|
||||
if (mins < 1) return `${Math.round(mins * 60)}s`;
|
||||
if (mins < 60) return `${Math.round(mins)}m`;
|
||||
const h = Math.floor(mins / 60), r = Math.round(mins % 60);
|
||||
if (h < 24) return r === 0 ? `${h}h` : `${h}h ${r}m`;
|
||||
const d = Math.floor(h / 24), rh = h % 24;
|
||||
return rh === 0 ? `${d}d` : `${d}d ${rh}h`;
|
||||
}
|
||||
|
||||
export function timeAgoRefresh(ts: number): string {
|
||||
if (!ts) return "";
|
||||
const diff = Date.now() - ts, m = Math.floor(diff / 60000);
|
||||
if (m < 1) return "just now";
|
||||
if (m < 60) return `${m}m ago`;
|
||||
const h = Math.floor(m / 60);
|
||||
if (h < 24) return `${h}h ago`;
|
||||
return `${Math.floor(h / 24)}d ago`;
|
||||
}
|
||||
|
||||
export function handleRowWheel(e: WheelEvent) {
|
||||
if (Math.abs(e.deltaY) <= Math.abs(e.deltaX)) return;
|
||||
(e.currentTarget as HTMLElement).scrollLeft += e.deltaY;
|
||||
e.stopPropagation();
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
import { gql } from "@api/client";
|
||||
import { MANGAS_BY_GENRE } from "@api/queries/manga";
|
||||
import { buildTagFilter } from "@features/discover/lib/searchFilter";
|
||||
import type { Manga } from "@types";
|
||||
import type { HistoryEntry } from "@store/state.svelte";
|
||||
|
||||
export interface RecommendedManga {
|
||||
manga: Manga;
|
||||
matchedGenres: string[];
|
||||
}
|
||||
|
||||
const TOP_GENRES = 6;
|
||||
const PAGE_SIZE = 100;
|
||||
const MAX_PAGES = 10;
|
||||
const TARGET_PER_GENRE = 20;
|
||||
const EXCLUDED_STATUSES = ["CANCELLED", "ABANDONED"];
|
||||
|
||||
export function topGenres(history: HistoryEntry[], libraryManga: Manga[]): string[] {
|
||||
const byId = new Map(libraryManga.map(m => [m.id, m]));
|
||||
const tally = new Map<string, { count: number; original: string }>();
|
||||
|
||||
for (const entry of history) {
|
||||
const manga = byId.get(entry.mangaId);
|
||||
if (!manga?.genre?.length) continue;
|
||||
for (const g of manga.genre) {
|
||||
const key = g.toLowerCase();
|
||||
const existing = tally.get(key);
|
||||
if (existing) { existing.count++; }
|
||||
else { tally.set(key, { count: 1, original: g }); }
|
||||
}
|
||||
}
|
||||
|
||||
return [...tally.values()]
|
||||
.sort((a, b) => b.count - a.count)
|
||||
.slice(0, TOP_GENRES)
|
||||
.map(e => e.original);
|
||||
}
|
||||
|
||||
type Result = { mangas: { nodes: Manga[] } };
|
||||
|
||||
async function fetchGenrePages(
|
||||
genre: string,
|
||||
globalSeen: Set<number>,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Manga[]> {
|
||||
const filter = {
|
||||
and: [
|
||||
buildTagFilter([genre], "OR", []),
|
||||
{ inLibrary: { equalTo: false } },
|
||||
],
|
||||
};
|
||||
|
||||
const localSeen = new Set<number>();
|
||||
const nodes: Manga[] = [];
|
||||
|
||||
for (let page = 0; page < MAX_PAGES; page++) {
|
||||
if (signal?.aborted) break;
|
||||
|
||||
let batch: Manga[];
|
||||
try {
|
||||
const d = await gql<Result>(MANGAS_BY_GENRE, { filter, first: PAGE_SIZE, offset: page * PAGE_SIZE }, signal);
|
||||
batch = d.mangas.nodes;
|
||||
} catch {
|
||||
break;
|
||||
}
|
||||
|
||||
if (!batch.length) break;
|
||||
|
||||
for (const m of batch) {
|
||||
if (localSeen.has(m.id) || globalSeen.has(m.id)) continue;
|
||||
if (EXCLUDED_STATUSES.includes(m.status ?? "")) continue;
|
||||
localSeen.add(m.id);
|
||||
nodes.push(m);
|
||||
}
|
||||
|
||||
if (nodes.length >= TARGET_PER_GENRE) break;
|
||||
if (batch.length < PAGE_SIZE) break;
|
||||
}
|
||||
|
||||
return nodes;
|
||||
}
|
||||
|
||||
export async function fetchRecommendations(
|
||||
history: HistoryEntry[],
|
||||
libraryManga: Manga[],
|
||||
signal?: AbortSignal,
|
||||
): Promise<RecommendedManga[]> {
|
||||
if (!history.length || !libraryManga.length) return [];
|
||||
|
||||
const genres = topGenres(history, libraryManga);
|
||||
if (!genres.length) return [];
|
||||
|
||||
const globalSeen = new Set<number>();
|
||||
const merged: Manga[] = [];
|
||||
|
||||
for (const genre of genres) {
|
||||
const results = await fetchGenrePages(genre, globalSeen, signal);
|
||||
for (const m of results) {
|
||||
globalSeen.add(m.id);
|
||||
merged.push(m);
|
||||
}
|
||||
}
|
||||
|
||||
return merged.map(m => ({
|
||||
manga: m,
|
||||
matchedGenres: (m.genre ?? []).filter(g =>
|
||||
genres.some(tg => tg.toLowerCase() === g.toLowerCase())
|
||||
),
|
||||
}));
|
||||
}
|
||||
Reference in New Issue
Block a user