Display in a chip the number of unmatched inbox entries

This commit is contained in:
2025-05-12 18:46:40 +02:00
parent 0b586039c3
commit 1e8064946a
5 changed files with 138 additions and 41 deletions

View File

@ -0,0 +1,67 @@
import React from "react";
import { InboxApi } from "../api/InboxApi";
import { AsyncWidget } from "../widgets/AsyncWidget";
interface UnmatchedInboxEntriesCountContext {
count: number;
reload: () => Promise<void>;
}
const UnmatchedInboxEntriesCountContextK =
React.createContext<UnmatchedInboxEntriesCountContext | null>(null);
export function UnmatchedInboxEntriesCountProvider(
p: React.PropsWithChildren
): React.ReactElement {
const [count, setCount] = React.useState<number | null>(null);
const loadKey = React.useRef(1);
const loadPromise = React.useRef<(() => void) | null>(null);
const load = async () => {
setCount(await InboxApi.CountUnmatched());
};
const onReload = async () => {
loadKey.current += 1;
load();
return new Promise<void>((res) => {
loadPromise.current = () => {
res();
};
});
};
return (
<AsyncWidget
ready={true}
loadKey={loadKey.current}
load={load}
errMsg="Failed to get the number of unread inbox entries!"
build={() => {
if (loadPromise.current != null) {
loadPromise.current();
loadPromise.current = null;
}
return (
<UnmatchedInboxEntriesCountContextK
value={{
count: count ?? 0,
reload: onReload,
}}
>
{p.children}
</UnmatchedInboxEntriesCountContextK>
);
}}
/>
);
}
export function useUnmatchedInboxEntriesCount(): UnmatchedInboxEntriesCountContext {
return React.use(UnmatchedInboxEntriesCountContextK)!;
}