feat(stats): add Hide Kana filter and fix vocabulary exclusion matching

- Remove overlay mining image toast; OSD card notifications no longer flash a frame screenshot
- Add Hide Kana toggle to frequency rank table to filter kana-only headwords
- Fix vocab exclusions to deduplicate and match token variants (e.g. ない / 無い) under one exclusion entry
- Skip cover image fetching when the overview tab is inactive
This commit is contained in:
2026-06-06 00:08:49 -07:00
parent 10c99b526b
commit e18ccfe288
25 changed files with 304 additions and 241 deletions
+1
View File
@@ -188,6 +188,7 @@ export function App() {
<OverviewTab
onNavigateToMediaDetail={navigateToOverviewMediaDetail}
onNavigateToSession={navigateToSession}
isActive={activeTab === 'overview'}
/>
</section>
) : null}
@@ -20,9 +20,14 @@ import type { SessionSummary } from '../../types/stats';
interface OverviewTabProps {
onNavigateToMediaDetail: (videoId: number, sessionId?: number | null) => void;
onNavigateToSession: (sessionId: number) => void;
isActive?: boolean;
}
export function OverviewTab({ onNavigateToMediaDetail, onNavigateToSession }: OverviewTabProps) {
export function OverviewTab({
onNavigateToMediaDetail,
onNavigateToSession,
isActive = true,
}: OverviewTabProps) {
const { data, sessions, setSessions, loading, error } = useOverview();
const { calendar, loading: calLoading } = useStreakCalendar(90);
const [deleteError, setDeleteError] = useState<string | null>(null);
@@ -153,6 +158,7 @@ export function OverviewTab({ onNavigateToMediaDetail, onNavigateToSession }: Ov
onDeleteDayGroup={handleDeleteDayGroup}
onDeleteAnimeGroup={handleDeleteAnimeGroup}
deletingIds={deletingIds}
isActive={isActive}
/>
<DeleteProgressToast count={deletingIds.size} />
@@ -20,6 +20,7 @@ interface RecentSessionsProps {
onDeleteDayGroup: (dayLabel: string, daySessions: SessionSummary[]) => void;
onDeleteAnimeGroup: (sessions: SessionSummary[]) => void;
deletingIds: Set<number>;
isActive?: boolean;
}
interface AnimeGroup {
@@ -352,8 +353,9 @@ export function RecentSessions({
onDeleteDayGroup,
onDeleteAnimeGroup,
deletingIds,
isActive = true,
}: RecentSessionsProps) {
const coverImages = useCoverImages(sessions);
const coverImages = useCoverImages(sessions, { enabled: isActive });
if (sessions.length === 0) {
return (
@@ -1,7 +1,11 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { renderToStaticMarkup } from 'react-dom/server';
import { FrequencyRankTable } from './FrequencyRankTable';
import {
buildFrequencyRankRows,
FrequencyRankTable,
isKanaOnlyTokenText,
} from './FrequencyRankTable';
import type { VocabularyEntry } from '../../types/stats';
function makeEntry(over: Partial<VocabularyEntry>): VocabularyEntry {
@@ -41,3 +45,42 @@ test('omits reading when reading equals headword', () => {
'should not render any bracketed reading when equal to headword',
);
});
test('identifies kana-only token text without hiding mixed kanji words', () => {
assert.equal(isKanaOnlyTokenText('さらに'), true);
assert.equal(isKanaOnlyTokenText('バカ'), true);
assert.equal(isKanaOnlyTokenText('カレー'), true);
assert.equal(isKanaOnlyTokenText('前に'), false);
assert.equal(isKanaOnlyTokenText('間違いない'), false);
});
test('frequency rows can hide kana-only headwords', () => {
const rows = buildFrequencyRankRows(
[
makeEntry({ wordId: 1, headword: 'さらに', word: 'さらに', frequencyRank: 10 }),
makeEntry({
wordId: 2,
headword: '前に',
word: '前に',
reading: 'まえに',
frequencyRank: 20,
}),
makeEntry({ wordId: 3, headword: 'バカ', word: 'バカ', reading: 'バカ', frequencyRank: 30 }),
],
new Set(),
{ hideKnown: false, hideKanaOnly: true },
);
assert.deepEqual(
rows.map((row) => row.headword),
['前に'],
);
});
test('renders a Hide Kana filter button', () => {
const entry = makeEntry({ headword: 'さらに', word: 'さらに', reading: 'さらに' });
const markup = renderToStaticMarkup(
<FrequencyRankTable words={[entry]} knownWords={new Set()} />,
);
assert.match(markup, /Hide Kana/);
});
@@ -11,45 +11,74 @@ interface FrequencyRankTableProps {
const PAGE_SIZE = 25;
interface FrequencyRankOptions {
hideKnown: boolean;
hideKanaOnly: boolean;
}
const KANA_ONLY_TEXT = /^[\p{Script=Hiragana}\p{Script=Katakana}\u30fc\u309d\u309e\u30fd\u30fe]+$/u;
export function isKanaOnlyTokenText(text: string): boolean {
const trimmed = text.trim();
return trimmed.length > 0 && KANA_ONLY_TEXT.test(trimmed);
}
function isWordKnown(w: VocabularyEntry, knownWords: Set<string>): boolean {
return knownWords.has(w.headword) || knownWords.has(w.word);
}
function isKanaOnlyWord(w: VocabularyEntry): boolean {
return isKanaOnlyTokenText(w.headword || w.word);
}
export function buildFrequencyRankRows(
words: VocabularyEntry[],
knownWords: Set<string>,
options: FrequencyRankOptions,
): VocabularyEntry[] {
const hasKnownData = knownWords.size > 0;
let filtered = words.filter((w) => w.frequencyRank != null && w.frequencyRank > 0);
if (options.hideKnown && hasKnownData) {
filtered = filtered.filter((w) => !isWordKnown(w, knownWords));
}
if (options.hideKanaOnly) {
filtered = filtered.filter((w) => !isKanaOnlyWord(w));
}
const byHeadword = new Map<string, VocabularyEntry>();
for (const w of filtered) {
const existing = byHeadword.get(w.headword);
if (!existing) {
byHeadword.set(w.headword, { ...w });
} else {
existing.frequency += w.frequency;
existing.animeCount = Math.max(existing.animeCount, w.animeCount);
if (w.frequencyRank! < existing.frequencyRank!) {
existing.frequencyRank = w.frequencyRank;
}
if (!existing.reading && w.reading) {
existing.reading = w.reading;
}
if (!existing.partOfSpeech && w.partOfSpeech) {
existing.partOfSpeech = w.partOfSpeech;
}
}
}
return [...byHeadword.values()].sort((a, b) => a.frequencyRank! - b.frequencyRank!);
}
export function FrequencyRankTable({ words, knownWords, onSelectWord }: FrequencyRankTableProps) {
const [page, setPage] = useState(0);
const [hideKnown, setHideKnown] = useState(true);
const [hideKanaOnly, setHideKanaOnly] = useState(false);
const [collapsed, setCollapsed] = useState(false);
const hasKnownData = knownWords.size > 0;
const isWordKnown = (w: VocabularyEntry): boolean => {
return knownWords.has(w.headword) || knownWords.has(w.word);
};
const ranked = useMemo(() => {
let filtered = words.filter((w) => w.frequencyRank != null && w.frequencyRank > 0);
if (hideKnown && hasKnownData) {
filtered = filtered.filter((w) => !isWordKnown(w));
}
const byHeadword = new Map<string, VocabularyEntry>();
for (const w of filtered) {
const existing = byHeadword.get(w.headword);
if (!existing) {
byHeadword.set(w.headword, { ...w });
} else {
existing.frequency += w.frequency;
existing.animeCount = Math.max(existing.animeCount, w.animeCount);
if (w.frequencyRank! < existing.frequencyRank!) {
existing.frequencyRank = w.frequencyRank;
}
if (!existing.reading && w.reading) {
existing.reading = w.reading;
}
if (!existing.partOfSpeech && w.partOfSpeech) {
existing.partOfSpeech = w.partOfSpeech;
}
}
}
return [...byHeadword.values()].sort((a, b) => a.frequencyRank! - b.frequencyRank!);
}, [words, knownWords, hideKnown, hasKnownData]);
return buildFrequencyRankRows(words, knownWords, { hideKnown, hideKanaOnly });
}, [words, knownWords, hideKnown, hideKanaOnly]);
if (words.every((w) => w.frequencyRank == null)) {
return (
@@ -81,10 +110,11 @@ export function FrequencyRankTable({ words, knownWords, onSelectWord }: Frequenc
</span>
{hideKnown && hasKnownData ? 'Common Words Not Yet Mined' : 'Most Common Words Seen'}
</button>
<div className="flex items-center gap-3">
<div className="flex flex-wrap items-center justify-end gap-2">
{hasKnownData && (
<button
type="button"
aria-pressed={hideKnown}
onClick={() => {
setHideKnown(!hideKnown);
setPage(0);
@@ -98,12 +128,31 @@ export function FrequencyRankTable({ words, knownWords, onSelectWord }: Frequenc
Hide Known
</button>
)}
<button
type="button"
aria-pressed={hideKanaOnly}
onClick={() => {
setHideKanaOnly(!hideKanaOnly);
setPage(0);
}}
className={`px-2.5 py-1 rounded-lg text-xs transition-colors border ${
hideKanaOnly
? 'bg-ctp-surface2 text-ctp-text border-ctp-blue/50'
: 'bg-ctp-surface0 text-ctp-overlay2 border-ctp-surface1 hover:text-ctp-subtext0'
}`}
>
Hide Kana
</button>
<span className="text-xs text-ctp-overlay2">{ranked.length} words</span>
</div>
</div>
{collapsed ? null : ranked.length === 0 ? (
<div className="text-xs text-ctp-overlay2 mt-3">
{hideKnown ? 'All ranked words are already in Anki!' : 'No words with frequency data.'}
{hideKnown && hasKnownData && !hideKanaOnly
? 'All ranked words are already in Anki!'
: hideKnown || hideKanaOnly
? 'No ranked words match the active filters.'
: 'No words with frequency data.'}
</div>
) : (
<>
+16 -5
View File
@@ -1,5 +1,6 @@
import { useEffect, useMemo, useState } from 'react';
import {
buildCoverImageRequestKey,
collectSessionCoverRequests,
getCoverImageKey,
mergeCoverImageData,
@@ -9,15 +10,19 @@ import { getCoverRetryDelayMs } from '../lib/cover-retry';
import type { SessionSummary } from '../types/stats';
import { getStatsClient } from './useStatsApi';
function buildRequestKey(animeIds: number[], videoIds: number[]): string {
return `a:${animeIds.join(',')}|m:${videoIds.join(',')}`;
interface UseCoverImagesOptions {
enabled?: boolean;
}
export function useCoverImages(sessions: SessionSummary[]): CoverImageMap {
export function useCoverImages(
sessions: SessionSummary[],
options: UseCoverImagesOptions = {},
): CoverImageMap {
const enabled = options.enabled ?? true;
const requests = useMemo(() => collectSessionCoverRequests(sessions), [sessions]);
const requestKey = useMemo(
() => buildRequestKey(requests.animeIds, requests.videoIds),
[requests],
() => buildCoverImageRequestKey(requests.animeIds, requests.videoIds, enabled ? 1 : 0),
[requests, enabled],
);
const [images, setImages] = useState<CoverImageMap>({});
@@ -52,6 +57,12 @@ export function useCoverImages(sessions: SessionSummary[]): CoverImageMap {
}, getCoverRetryDelayMs(attempt));
}
if (!enabled) {
return () => {
cancelled = true;
};
}
if (requests.animeIds.length === 0 && requests.videoIds.length === 0) {
setImages({});
return () => {
+41
View File
@@ -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: 'ねこ' }];
+58 -20
View File
@@ -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(() => {
+9 -1
View File
@@ -1,6 +1,10 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { collectSessionCoverRequests, getCoverImageKey } from './cover-images';
import {
buildCoverImageRequestKey,
collectSessionCoverRequests,
getCoverImageKey,
} from './cover-images';
import type { SessionSummary } from '../types/stats';
function makeSession(overrides: Partial<SessionSummary> & { sessionId: number }): SessionSummary {
@@ -42,3 +46,7 @@ test('getCoverImageKey separates anime and media ids', () => {
assert.equal(getCoverImageKey('anime', 1), 'anime:1');
assert.equal(getCoverImageKey('media', 1), 'media:1');
});
test('buildCoverImageRequestKey changes when callers force a cover refresh', () => {
assert.notEqual(buildCoverImageRequestKey([10], [], 0), buildCoverImageRequestKey([10], [], 1));
});
+8
View File
@@ -22,6 +22,14 @@ export function getCoverImageKey(kind: CoverImageKind, id: number): string {
return `${kind}:${id}`;
}
export function buildCoverImageRequestKey(
animeIds: number[],
videoIds: number[],
refreshToken = 0,
): string {
return `a:${animeIds.join(',')}|m:${videoIds.join(',')}|r:${refreshToken}`;
}
export function collectSessionCoverRequests(
sessions: Pick<SessionSummary, 'animeId' | 'videoId'>[],
): CoverImageRequest {