caelum0x d56639a594
Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

57 lines
1.8 KiB
TypeScript

/**
* Small hook that owns the bell-badge unread count. Fetches
* GET /me/notifications/unread_count and refreshes whenever the screen it's
* mounted on regains focus (e.g. after the user reads notifications and returns
* to the Me tab). Failures are swallowed to `0` — a badge must never surface an
* error or block the surface it decorates.
*
* The auth token comes from the session and is sent as `x-api-key` by the shared
* api client. `refresh` is exposed for callers that want to re-poll manually.
*/
import { useCallback, useState } from "react";
import { useFocusEffect } from "expo-router";
import { getUnreadCount, isAbortError } from "@/lib/api";
import { useSession } from "@/src/shared/auth";
export interface UnreadCount {
/** Current unread count (0 while loading or on any error). */
count: number;
/** Re-fetch the count immediately. */
refresh: () => void;
}
export function useUnreadCount(): UnreadCount {
const { apiKey } = useSession();
const token = apiKey ?? undefined;
const [count, setCount] = useState(0);
const load = useCallback(
(signal?: AbortSignal) => {
getUnreadCount({ token, signal })
.then((value) => setCount(value))
.catch((err: unknown) => {
// Never surface a badge error. Abort during unmount is expected; on
// any other transient failure keep the last-known count in place.
if (isAbortError(err)) return;
});
},
[token],
);
// Re-poll on every focus so the badge is fresh when the surface reappears.
useFocusEffect(
useCallback(() => {
const controller = new AbortController();
load(controller.signal);
return () => controller.abort();
}, [load]),
);
const refresh = useCallback(() => load(), [load]);
return { count, refresh };
}