1. Bugs fixed:
- Backup schema now accepts theme: "aero" so backups exported under
Frutiger Aero pass parseBackup on import, and applyImport in "replace"
mode replaces SRS schedules, answer history, and chats instead of
merging into existing SRS or skipping empty arrays.
- Typed FillBlank cards bind the input value to chosen ?? value instead
of chosen ?? "", and normalise() preserves Unicode letters (\p{L}\p{N})
when grading non-ASCII answers.
- OpenAI-compatible vision calls (contentFor in lib/openai-compat.ts)
prefix data:image/jpeg;base64, when routes pass stripped base64.
- Streaming calls (ollamaChatStream, providerChatStream) keep their
AbortController active across the entire body stream and API routes
forward request.signal so stopping a chat or cancelling a run aborts
the upstream request immediately.
- HomeworkCard uses homeworkModeLabelKey so "check" records display the
Checked label instead of Step-by-step guide.
- Desktop dropzones on /create and /homework accept dropped documents as
well as images.
- LibraryScreen filters reported questions out of its due and mistake
counts to match ReviewClient.
- Clearing a provider key resets generation if it pointed at that
provider; chat conversations persist and restore provider; ModelPicker
keys items by provider:name.
- RouteTransition no longer mutates module-level lastPath during render.
- Service worker asset handler falls back to Response.error() when an
uncached asset fetch fails offline.
- Confetti and .ice-orb / .ice-bubble respect the in-app reduce-motion
toggle.
2. Performance & cleanup:
- Deduplicate window storage listeners across all six localStorage
stores (one listener while subscribed instead of N^2 notifications).
- Use todayAnswerCount in LibraryScreen and pass sorted folders to
DeckCard.
- Memoize MessageRow with stable callbacks in ChatScreen and compute
last message IDs in a single pass.
- Close ImageBitmap after drawing in lib/image.ts and destroy pdf.js
documents in finally in lib/documents.ts.
- Avoid re-parsing serialized sync payloads in POST /api/sync.
- Deduplicate copyText (lib/share.ts -> lib/clipboard.ts), chatMessageId,
describeExtraction, ALL_FOLDERS, useIsPhone, and remove unused helpers
and boilerplate public/*.svg files.
Version 0.5.8, SW -> freezmo-v80.
83 lines
2.7 KiB
TypeScript
83 lines
2.7 KiB
TypeScript
"use client";
|
|
|
|
import { t } from "./i18n";
|
|
import { useCallback, useEffect, useState, useSyncExternalStore } from "react";
|
|
|
|
import { fetchHealth, type Health } from "./client-api";
|
|
import { settingsStore } from "./settings";
|
|
|
|
/**
|
|
* Health status for the current browser (re-checks whenever the API key
|
|
* changes). Used by the settings dialog and the API-key gate.
|
|
*
|
|
* Every page mounts its own gate, and one probe can sit for seconds while
|
|
* Ollama answers — so probes are deduplicated per key and cached briefly
|
|
* instead of firing one per mount. The manual re-check always forces.
|
|
*/
|
|
const HEALTH_TTL_MS = 30_000;
|
|
|
|
let cache: { key: string; at: number; value: Health } | null = null;
|
|
let inflight: { key: string; promise: Promise<Health> } | null = null;
|
|
|
|
function loadHealth(key: string, force: boolean): Promise<Health> {
|
|
if (!force && cache && cache.key === key && Date.now() - cache.at < HEALTH_TTL_MS) {
|
|
return Promise.resolve(cache.value);
|
|
}
|
|
if (!force && inflight && inflight.key === key) return inflight.promise;
|
|
|
|
const promise = fetchHealth()
|
|
.then((value) => {
|
|
cache = { key, at: Date.now(), value };
|
|
return value;
|
|
})
|
|
.finally(() => {
|
|
if (inflight?.promise === promise) inflight = null;
|
|
});
|
|
inflight = { key, promise };
|
|
return promise;
|
|
}
|
|
|
|
export function useHealth() {
|
|
const apiKey = useSyncExternalStore(
|
|
settingsStore.subscribe,
|
|
() => settingsStore.getSnapshot().apiKey,
|
|
() => "",
|
|
);
|
|
const [health, setHealth] = useState<Health | null>(() =>
|
|
cache && cache.key === apiKey ? cache.value : null,
|
|
);
|
|
const [loading, setLoading] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [checkedAt, setCheckedAt] = useState<number | null>(() =>
|
|
cache && cache.key === apiKey ? cache.at : null,
|
|
);
|
|
|
|
const run = useCallback(async (force: boolean) => {
|
|
setLoading(true);
|
|
setError(null);
|
|
const key = settingsStore.getSnapshot().apiKey;
|
|
try {
|
|
const result = await loadHealth(key, force);
|
|
if (settingsStore.getSnapshot().apiKey !== key) return;
|
|
setHealth(result);
|
|
} catch (loadError) {
|
|
if (settingsStore.getSnapshot().apiKey !== key) return;
|
|
setError(loadError instanceof Error ? loadError.message : t("api.unreachableShort"));
|
|
} finally {
|
|
if (settingsStore.getSnapshot().apiKey === key) {
|
|
// Even a failed check is a check: the panel can say when it last ran.
|
|
setCheckedAt(Date.now());
|
|
setLoading(false);
|
|
}
|
|
}
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
void run(false);
|
|
}, [run, apiKey]);
|
|
|
|
/** The settings dialog's re-check button: always a fresh probe. */
|
|
const refresh = useCallback(() => run(true), [run]);
|
|
|
|
return { health, loading, error, refresh, apiKey, checkedAt };
|
|
}
|