mirror of
https://github.com/multica-ai/multica.git
synced 2026-07-27 04:56:20 +02:00
75 lines
2.2 KiB
TypeScript
75 lines
2.2 KiB
TypeScript
import { queryOptions, useQuery } from "@tanstack/react-query";
|
|
import { api } from "../api";
|
|
import type { InboxItem } from "../types";
|
|
|
|
export const inboxKeys = {
|
|
all: (wsId: string) => ["inbox", wsId] as const,
|
|
list: (wsId: string) => [...inboxKeys.all(wsId), "list"] as const,
|
|
};
|
|
|
|
export function inboxListOptions(wsId: string) {
|
|
return queryOptions({
|
|
queryKey: inboxKeys.list(wsId),
|
|
queryFn: () => api.listInbox(),
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Unread inbox count for the given workspace, aligned with what the inbox
|
|
* list UI renders: archived items excluded, then deduplicated by issue so a
|
|
* single issue with three unread notifications counts once.
|
|
*/
|
|
export function useInboxUnreadCount(wsId: string | null | undefined): number {
|
|
const { data } = useQuery({
|
|
queryKey: inboxKeys.list(wsId ?? ""),
|
|
queryFn: () => api.listInbox(),
|
|
enabled: !!wsId,
|
|
select: (items: InboxItem[]) =>
|
|
deduplicateInboxItems(items).filter((i) => !i.read).length,
|
|
});
|
|
return data ?? 0;
|
|
}
|
|
|
|
/**
|
|
* Deduplicate inbox items by issue_id (one entry per issue, Linear-style).
|
|
* Exported for consumers to use in useMemo — not in queryOptions select
|
|
* (to avoid new array references on every cache update).
|
|
*/
|
|
export function deduplicateInboxItems(items: InboxItem[]): InboxItem[] {
|
|
const active = items.filter((i) => !i.archived);
|
|
const groups = new Map<string, InboxItem[]>();
|
|
for (const item of active) {
|
|
const key = item.issue_id ?? item.id;
|
|
const group = groups.get(key) ?? [];
|
|
group.push(item);
|
|
groups.set(key, group);
|
|
}
|
|
const merged: InboxItem[] = [];
|
|
for (const group of groups.values()) {
|
|
group.sort(
|
|
(a, b) =>
|
|
new Date(b.created_at).getTime() - new Date(a.created_at).getTime(),
|
|
);
|
|
const newest = group[0];
|
|
if (!newest) continue;
|
|
|
|
const commentId =
|
|
newest.details?.comment_id ??
|
|
group.find((item) => item.details?.comment_id)?.details?.comment_id;
|
|
|
|
if (commentId && newest.details?.comment_id !== commentId) {
|
|
merged.push({
|
|
...newest,
|
|
details: { ...(newest.details ?? {}), comment_id: commentId },
|
|
});
|
|
continue;
|
|
}
|
|
|
|
merged.push(newest);
|
|
}
|
|
return merged.sort(
|
|
(a, b) =>
|
|
new Date(b.created_at).getTime() - new Date(a.created_at).getTime(),
|
|
);
|
|
}
|