fix: dock app badge shows incorrect number
Closes #122 We now only show number based on open streams
This commit is contained in:
@@ -1,24 +1,52 @@
|
||||
import { useEffect, useMemo } from "react";
|
||||
import { where } from "firebase/firestore";
|
||||
import { useLiveParticleChildren } from "@/hooks/use-particle";
|
||||
import { useAuthStore } from "@/stores/auth-store";
|
||||
import { particlePath } from "@/lib/particle-path";
|
||||
import type { Particle, StreamProperties } from "@/api/types";
|
||||
|
||||
type StreamParticle = Particle & { type: "stream"; properties: StreamProperties };
|
||||
|
||||
const openStatusFilter = where("status", "==", "open");
|
||||
|
||||
/**
|
||||
* Syncs the macOS dock badge with the count of unseen streams.
|
||||
* Both last_child_created_at and playback_markers store the child particle's
|
||||
* created_at, so they're directly comparable.
|
||||
* Self-contained hook that syncs the macOS dock badge with the count of
|
||||
* unseen open streams the current user is involved in.
|
||||
*
|
||||
* Sets up its own Firestore listener so it works independently of
|
||||
* whatever stream list is rendered on screen.
|
||||
*/
|
||||
export function useDockBadge(streams: StreamParticle[], userId: string | undefined) {
|
||||
export function useDockBadge(networkId: string | undefined) {
|
||||
const user = useAuthStore((s) => s.user);
|
||||
const userId = user?.id;
|
||||
|
||||
const visibilityScopes = useMemo(() => {
|
||||
const scopes: string[] = [];
|
||||
if (userId) scopes.push(`human:${userId}`);
|
||||
if (networkId) scopes.push(`network:${networkId}`);
|
||||
return scopes;
|
||||
}, [userId, networkId]);
|
||||
|
||||
const path = networkId ? particlePath(networkId, []) : undefined;
|
||||
|
||||
const { children } = useLiveParticleChildren(path, {
|
||||
orderByField: "last_child_created_at",
|
||||
orderDirection: "desc",
|
||||
visibilityScopes,
|
||||
whereFilter: openStatusFilter,
|
||||
});
|
||||
|
||||
const unseenCount = useMemo(() => {
|
||||
if (!userId) return 0;
|
||||
return streams.filter((s) => {
|
||||
const lastActivity = s.last_child_created_at?.getTime();
|
||||
return children.filter((c): c is StreamParticle => {
|
||||
if (c.type !== "stream") return false;
|
||||
const lastActivity = c.last_child_created_at?.getTime();
|
||||
if (!lastActivity) return false;
|
||||
const marker = s.playback_markers?.[userId]?.getTime();
|
||||
const marker = c.playback_markers?.[userId]?.getTime();
|
||||
if (marker === undefined) return false;
|
||||
return lastActivity > marker;
|
||||
}).length;
|
||||
}, [streams, userId]);
|
||||
}, [children, userId]);
|
||||
|
||||
useEffect(() => {
|
||||
window.electronApp.setDockBadge(unseenCount);
|
||||
|
||||
@@ -66,7 +66,7 @@ interface UseLiveParticleChildrenParams {
|
||||
}
|
||||
|
||||
export function useLiveParticleChildren(
|
||||
path: ParticlePath,
|
||||
path: ParticlePath | undefined,
|
||||
{
|
||||
orderByField = "created_at",
|
||||
orderDirection = "desc",
|
||||
@@ -81,6 +81,12 @@ export function useLiveParticleChildren(
|
||||
const [error, setError] = useState<Error | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!path) {
|
||||
setChildren([]);
|
||||
setIsLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
setChildren([]);
|
||||
|
||||
Reference in New Issue
Block a user