mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-05-04 00:41:33 -07:00
Persist stats exclusions in DB and fix word metrics filtering (#60)
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
import { useCallback, useSyncExternalStore } from 'react';
|
||||
import { useCallback, useEffect, useSyncExternalStore } from 'react';
|
||||
import { apiClient } from '../lib/api-client';
|
||||
import type { StatsExcludedWord } from '../types/stats';
|
||||
|
||||
export interface ExcludedWord {
|
||||
headword: string;
|
||||
word: string;
|
||||
reading: string;
|
||||
}
|
||||
export type ExcludedWord = StatsExcludedWord;
|
||||
|
||||
const STORAGE_KEY = 'subminer-excluded-words';
|
||||
|
||||
@@ -14,16 +12,37 @@ function toKey(w: ExcludedWord): string {
|
||||
|
||||
let cached: ExcludedWord[] | null = null;
|
||||
let cachedKeys: Set<string> | null = null;
|
||||
let initialized: Promise<void> | null = null;
|
||||
let revision = 0;
|
||||
const listeners = new Set<() => void>();
|
||||
|
||||
function readLocalStorage(): ExcludedWord[] {
|
||||
if (typeof localStorage === 'undefined') return [];
|
||||
try {
|
||||
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',
|
||||
);
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
function writeLocalStorage(words: ExcludedWord[]): void {
|
||||
if (typeof localStorage === 'undefined') return;
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(words));
|
||||
}
|
||||
|
||||
function load(): ExcludedWord[] {
|
||||
if (cached) return cached;
|
||||
try {
|
||||
const raw = localStorage.getItem(STORAGE_KEY);
|
||||
cached = raw ? JSON.parse(raw) : [];
|
||||
} catch {
|
||||
cached = [];
|
||||
}
|
||||
cached = readLocalStorage();
|
||||
return cached!;
|
||||
}
|
||||
|
||||
@@ -33,24 +52,89 @@ function getKeySet(): Set<string> {
|
||||
return cachedKeys;
|
||||
}
|
||||
|
||||
function persist(words: ExcludedWord[]) {
|
||||
function applyWords(words: ExcludedWord[]): void {
|
||||
cached = words;
|
||||
cachedKeys = new Set(words.map(toKey));
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(words));
|
||||
writeLocalStorage(words);
|
||||
for (const fn of listeners) fn();
|
||||
}
|
||||
|
||||
function getSnapshot(): ExcludedWord[] {
|
||||
export function getExcludedWordsSnapshot(): ExcludedWord[] {
|
||||
return load();
|
||||
}
|
||||
|
||||
export async function setExcludedWords(words: ExcludedWord[]): Promise<void> {
|
||||
const previousWords = [...load()];
|
||||
const previousRevision = revision;
|
||||
const writeRevision = previousRevision + 1;
|
||||
revision = writeRevision;
|
||||
applyWords(words);
|
||||
try {
|
||||
await apiClient.setExcludedWords(words);
|
||||
} catch (error) {
|
||||
if (revision === writeRevision) {
|
||||
revision = previousRevision;
|
||||
applyWords(previousWords);
|
||||
}
|
||||
console.error('Failed to persist excluded words to stats database', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export function initializeExcludedWordsStore(): Promise<void> {
|
||||
if (initialized) return initialized;
|
||||
const startRevision = revision;
|
||||
initialized = (async () => {
|
||||
const localWords = load();
|
||||
let dbWords: ExcludedWord[];
|
||||
try {
|
||||
dbWords = await apiClient.getExcludedWords();
|
||||
} catch (error) {
|
||||
initialized = null;
|
||||
console.error('Failed to load excluded words from stats database', error);
|
||||
return;
|
||||
}
|
||||
|
||||
if (revision !== startRevision) {
|
||||
initialized = null;
|
||||
return;
|
||||
}
|
||||
if (dbWords.length > 0) {
|
||||
applyWords(dbWords);
|
||||
return;
|
||||
}
|
||||
if (localWords.length > 0) {
|
||||
try {
|
||||
await setExcludedWords(localWords);
|
||||
} catch {
|
||||
initialized = null;
|
||||
}
|
||||
return;
|
||||
}
|
||||
applyWords([]);
|
||||
})();
|
||||
return initialized;
|
||||
}
|
||||
|
||||
export function resetExcludedWordsStoreForTests(): void {
|
||||
cached = null;
|
||||
cachedKeys = null;
|
||||
initialized = null;
|
||||
revision = 0;
|
||||
listeners.clear();
|
||||
}
|
||||
|
||||
function subscribe(fn: () => void): () => void {
|
||||
listeners.add(fn);
|
||||
return () => listeners.delete(fn);
|
||||
}
|
||||
|
||||
export function useExcludedWords() {
|
||||
const excluded = useSyncExternalStore(subscribe, getSnapshot);
|
||||
const excluded = useSyncExternalStore(subscribe, getExcludedWordsSnapshot);
|
||||
|
||||
useEffect(() => {
|
||||
void initializeExcludedWordsStore();
|
||||
}, []);
|
||||
|
||||
const isExcluded = useCallback(
|
||||
(w: { headword: string; word: string; reading: string }) => getKeySet().has(toKey(w)),
|
||||
@@ -61,17 +145,19 @@ export function useExcludedWords() {
|
||||
const key = toKey(w);
|
||||
const current = load();
|
||||
if (getKeySet().has(key)) {
|
||||
persist(current.filter((e) => toKey(e) !== key));
|
||||
void setExcludedWords(current.filter((e) => toKey(e) !== key));
|
||||
} else {
|
||||
persist([...current, w]);
|
||||
void setExcludedWords([...current, w]);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const removeExclusion = useCallback((w: ExcludedWord) => {
|
||||
persist(load().filter((e) => toKey(e) !== toKey(w)));
|
||||
void setExcludedWords(load().filter((e) => toKey(e) !== toKey(w)));
|
||||
}, []);
|
||||
|
||||
const clearAll = useCallback(() => persist([]), []);
|
||||
const clearAll = useCallback(() => {
|
||||
void setExcludedWords([]);
|
||||
}, []);
|
||||
|
||||
return { excluded, isExcluded, toggleExclusion, removeExclusion, clearAll };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user