mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-07-07 13:08:54 -07:00
feat(stats): speed up session maintenance and improve stats UI (#111)
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
import { useEffect, useMemo, useState } from 'react';
|
||||
import {
|
||||
buildCoverImageRequestKey,
|
||||
collectSessionCoverRequests,
|
||||
getCoverImageKey,
|
||||
mergeCoverImageData,
|
||||
type CoverImageMap,
|
||||
} from '../lib/cover-images';
|
||||
import { getCoverRetryDelayMs } from '../lib/cover-retry';
|
||||
import type { SessionSummary } from '../types/stats';
|
||||
import { getStatsClient } from './useStatsApi';
|
||||
|
||||
interface UseCoverImagesOptions {
|
||||
enabled?: boolean;
|
||||
}
|
||||
|
||||
export function useCoverImages(
|
||||
sessions: SessionSummary[],
|
||||
options: UseCoverImagesOptions = {},
|
||||
): CoverImageMap {
|
||||
const enabled = options.enabled ?? true;
|
||||
const requests = useMemo(() => collectSessionCoverRequests(sessions), [sessions]);
|
||||
const requestKey = useMemo(
|
||||
() => buildCoverImageRequestKey(requests.animeIds, requests.videoIds, enabled ? 1 : 0),
|
||||
[requests, enabled],
|
||||
);
|
||||
const [images, setImages] = useState<CoverImageMap>({});
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
let timer: ReturnType<typeof setTimeout> | null = null;
|
||||
let cachedImages: CoverImageMap = {};
|
||||
const client = getStatsClient();
|
||||
|
||||
async function load(animeIds: number[], videoIds: number[], attempt: number): Promise<void> {
|
||||
if (animeIds.length === 0 && videoIds.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const data = await client.getCoverImages({ animeIds, videoIds });
|
||||
if (cancelled) return;
|
||||
cachedImages = mergeCoverImageData(cachedImages, data);
|
||||
setImages(cachedImages);
|
||||
} catch {
|
||||
if (cancelled) return;
|
||||
}
|
||||
|
||||
const missingAnimeIds = animeIds.filter((id) => !cachedImages[getCoverImageKey('anime', id)]);
|
||||
const missingVideoIds = videoIds.filter((id) => !cachedImages[getCoverImageKey('media', id)]);
|
||||
if (missingAnimeIds.length === 0 && missingVideoIds.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
timer = setTimeout(() => {
|
||||
void load(missingAnimeIds, missingVideoIds, attempt + 1);
|
||||
}, getCoverRetryDelayMs(attempt));
|
||||
}
|
||||
|
||||
if (!enabled) {
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}
|
||||
|
||||
if (requests.animeIds.length === 0 && requests.videoIds.length === 0) {
|
||||
setImages({});
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}
|
||||
|
||||
void load(requests.animeIds, requests.videoIds, 0);
|
||||
|
||||
return () => {
|
||||
cancelled = true;
|
||||
if (timer) clearTimeout(timer);
|
||||
};
|
||||
}, [requestKey]);
|
||||
|
||||
return images;
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import {
|
||||
isExcludedWord,
|
||||
getExcludedWordsSnapshot,
|
||||
initializeExcludedWordsStore,
|
||||
resetExcludedWordsStoreForTests,
|
||||
@@ -100,6 +101,46 @@ test('setExcludedWords updates the database-backed exclusion list', async () =>
|
||||
}
|
||||
});
|
||||
|
||||
test('setExcludedWords persists one row per excluded token', async () => {
|
||||
resetExcludedWordsStoreForTests();
|
||||
const { values: storage, restore } = installLocalStorage();
|
||||
const originalFetch = globalThis.fetch;
|
||||
let seenBody = '';
|
||||
globalThis.fetch = (async (_input: RequestInfo | URL, init?: RequestInit) => {
|
||||
seenBody = String(init?.body ?? '');
|
||||
return new Response(JSON.stringify({ ok: true }), { status: 200 });
|
||||
}) as typeof globalThis.fetch;
|
||||
|
||||
try {
|
||||
const rows = [
|
||||
{ headword: 'ない', word: 'ない', reading: 'ない' },
|
||||
{ headword: 'ない', word: '無い', reading: 'ない' },
|
||||
];
|
||||
const expected = [{ headword: 'ない', word: 'ない', reading: 'ない' }];
|
||||
|
||||
await setExcludedWords(rows);
|
||||
|
||||
assert.deepEqual(getExcludedWordsSnapshot(), expected);
|
||||
assert.equal(seenBody, JSON.stringify({ words: expected }));
|
||||
assert.equal(storage.get(STORAGE_KEY), JSON.stringify(expected));
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
restore();
|
||||
resetExcludedWordsStoreForTests();
|
||||
}
|
||||
});
|
||||
|
||||
test('exclusion matching covers vocabulary rows with the same visible token', () => {
|
||||
const excluded = [{ headword: 'ない', word: 'ない', reading: 'ない' }];
|
||||
|
||||
assert.equal(isExcludedWord(excluded, { headword: 'ない', word: '無い', reading: 'ない' }), true);
|
||||
assert.equal(isExcludedWord(excluded, { headword: '無い', word: 'ない', reading: 'ない' }), true);
|
||||
assert.equal(
|
||||
isExcludedWord(excluded, { headword: 'なる', word: 'なる', reading: 'なる' }),
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
test('setExcludedWords rolls back local state when persistence fails', async () => {
|
||||
resetExcludedWordsStoreForTests();
|
||||
const previousRows = [{ headword: '猫', word: '猫', reading: 'ねこ' }];
|
||||
|
||||
@@ -6,8 +6,37 @@ export type ExcludedWord = StatsExcludedWord;
|
||||
|
||||
const STORAGE_KEY = 'subminer-excluded-words';
|
||||
|
||||
function toKey(w: ExcludedWord): string {
|
||||
return `${w.headword}\0${w.word}\0${w.reading}`;
|
||||
type ExclusionCandidate = { headword: string; word: string; reading: string };
|
||||
|
||||
function normalizedTokenText(value: string): string {
|
||||
return value.trim();
|
||||
}
|
||||
|
||||
export function getExcludedWordTokenKey(w: ExclusionCandidate): string {
|
||||
return (
|
||||
normalizedTokenText(w.headword) || normalizedTokenText(w.word) || normalizedTokenText(w.reading)
|
||||
);
|
||||
}
|
||||
|
||||
function getExcludedWordAliasKeys(w: ExclusionCandidate): string[] {
|
||||
const aliases = [normalizedTokenText(w.headword), normalizedTokenText(w.word)].filter(Boolean);
|
||||
const unique = new Set(aliases);
|
||||
if (unique.size === 0) unique.add(getExcludedWordTokenKey(w));
|
||||
return [...unique];
|
||||
}
|
||||
|
||||
export function dedupeExcludedWords(words: ExcludedWord[]): ExcludedWord[] {
|
||||
const byToken = new Map<string, ExcludedWord>();
|
||||
for (const word of words) {
|
||||
const key = getExcludedWordTokenKey(word);
|
||||
if (!byToken.has(key)) byToken.set(key, word);
|
||||
}
|
||||
return [...byToken.values()];
|
||||
}
|
||||
|
||||
export function isExcludedWord(excluded: ExcludedWord[], w: ExclusionCandidate): boolean {
|
||||
const excludedKeys = new Set(excluded.flatMap(getExcludedWordAliasKeys));
|
||||
return getExcludedWordAliasKeys(w).some((key) => excludedKeys.has(key));
|
||||
}
|
||||
|
||||
let cached: ExcludedWord[] | null = null;
|
||||
@@ -22,13 +51,15 @@ function readLocalStorage(): ExcludedWord[] {
|
||||
const raw = localStorage.getItem(STORAGE_KEY);
|
||||
const parsed: unknown = raw ? JSON.parse(raw) : [];
|
||||
if (!Array.isArray(parsed)) return [];
|
||||
return parsed.filter(
|
||||
(row): row is ExcludedWord =>
|
||||
row !== null &&
|
||||
typeof row === 'object' &&
|
||||
typeof (row as ExcludedWord).headword === 'string' &&
|
||||
typeof (row as ExcludedWord).word === 'string' &&
|
||||
typeof (row as ExcludedWord).reading === 'string',
|
||||
return dedupeExcludedWords(
|
||||
parsed.filter(
|
||||
(row): row is ExcludedWord =>
|
||||
row !== null &&
|
||||
typeof row === 'object' &&
|
||||
typeof (row as ExcludedWord).headword === 'string' &&
|
||||
typeof (row as ExcludedWord).word === 'string' &&
|
||||
typeof (row as ExcludedWord).reading === 'string',
|
||||
),
|
||||
);
|
||||
} catch {
|
||||
return [];
|
||||
@@ -48,14 +79,15 @@ function load(): ExcludedWord[] {
|
||||
|
||||
function getKeySet(): Set<string> {
|
||||
if (cachedKeys) return cachedKeys;
|
||||
cachedKeys = new Set(load().map(toKey));
|
||||
cachedKeys = new Set(load().flatMap(getExcludedWordAliasKeys));
|
||||
return cachedKeys;
|
||||
}
|
||||
|
||||
function applyWords(words: ExcludedWord[]): void {
|
||||
cached = words;
|
||||
cachedKeys = new Set(words.map(toKey));
|
||||
writeLocalStorage(words);
|
||||
const normalized = dedupeExcludedWords(words);
|
||||
cached = normalized;
|
||||
cachedKeys = new Set(normalized.flatMap(getExcludedWordAliasKeys));
|
||||
writeLocalStorage(normalized);
|
||||
for (const fn of listeners) fn();
|
||||
}
|
||||
|
||||
@@ -67,10 +99,11 @@ export async function setExcludedWords(words: ExcludedWord[]): Promise<void> {
|
||||
const previousWords = [...load()];
|
||||
const previousRevision = revision;
|
||||
const writeRevision = previousRevision + 1;
|
||||
const normalized = dedupeExcludedWords(words);
|
||||
revision = writeRevision;
|
||||
applyWords(words);
|
||||
applyWords(normalized);
|
||||
try {
|
||||
await apiClient.setExcludedWords(words);
|
||||
await apiClient.setExcludedWords(normalized);
|
||||
} catch (error) {
|
||||
if (revision === writeRevision) {
|
||||
revision = previousRevision;
|
||||
@@ -137,22 +170,27 @@ export function useExcludedWords() {
|
||||
}, []);
|
||||
|
||||
const isExcluded = useCallback(
|
||||
(w: { headword: string; word: string; reading: string }) => getKeySet().has(toKey(w)),
|
||||
(w: ExclusionCandidate) => getExcludedWordAliasKeys(w).some((key) => getKeySet().has(key)),
|
||||
[excluded],
|
||||
);
|
||||
|
||||
const toggleExclusion = useCallback((w: ExcludedWord) => {
|
||||
const key = toKey(w);
|
||||
const current = load();
|
||||
if (getKeySet().has(key)) {
|
||||
void setExcludedWords(current.filter((e) => toKey(e) !== key));
|
||||
const candidateKeys = new Set(getExcludedWordAliasKeys(w));
|
||||
const existing = current.find((e) =>
|
||||
getExcludedWordAliasKeys(e).some((key) => candidateKeys.has(key)),
|
||||
);
|
||||
if (existing) {
|
||||
const key = getExcludedWordTokenKey(existing);
|
||||
void setExcludedWords(current.filter((e) => getExcludedWordTokenKey(e) !== key));
|
||||
} else {
|
||||
void setExcludedWords([...current, w]);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const removeExclusion = useCallback((w: ExcludedWord) => {
|
||||
void setExcludedWords(load().filter((e) => toKey(e) !== toKey(w)));
|
||||
const key = getExcludedWordTokenKey(w);
|
||||
void setExcludedWords(load().filter((e) => getExcludedWordTokenKey(e) !== key));
|
||||
}, []);
|
||||
|
||||
const clearAll = useCallback(() => {
|
||||
|
||||
Reference in New Issue
Block a user