Refactor real-time sync from per-event precise mutations to WS-as-invalidation-signal + debounced refetch. Backend: - Add SubscribeAll to Event Bus — auto-broadcasts ALL events, eliminates manual 25-item allEvents list - Add skill event constants to protocol, fix skill handler string literals - Add title_changed activity tracking Frontend: - WSClient: add onAny() method for wildcard event subscription - useRealtimeSync: rewrite to refreshMap + prefix routing + 100ms debounce - Precise handlers only for side effects: workspace:deleted, member:removed, member:added (self-check) - Reconnect now refetches all stores (fixes missing members/skills/workspace refresh) - Stale-while-revalidate: fetch() only shows loading spinner on initial load, not on refetch - Remove redundant useWSEvent in agents/page.tsx and skills-page.tsx - WSClient.disconnect() now clears all handler registrations Inbox bugfixes: - Unify sidebar badge count with page count via dedupedItems + unreadCount in store - Sort by time DESC (removed severity-first ordering) - Ellipsis on truncated detail labels UI: - Status/Priority pickers: replace RadioGroup with MenuItem for auto-close on selection Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
117 lines
3.4 KiB
TypeScript
117 lines
3.4 KiB
TypeScript
"use client";
|
|
|
|
import { create } from "zustand";
|
|
import type { InboxItem, IssueStatus } from "@/shared/types";
|
|
import { api } from "@/shared/api";
|
|
import { createLogger } from "@/shared/logger";
|
|
|
|
const logger = createLogger("inbox-store");
|
|
|
|
/**
|
|
* Deduplicate inbox items by issue_id (one entry per issue, Linear-style),
|
|
* keep latest, sort by time DESC.
|
|
* Memoized by reference — returns the same array if `items` hasn't changed.
|
|
*/
|
|
let _prevItems: InboxItem[] = [];
|
|
let _prevDeduped: InboxItem[] = [];
|
|
|
|
function deduplicateInboxItems(items: InboxItem[]): InboxItem[] {
|
|
if (items === _prevItems) return _prevDeduped;
|
|
_prevItems = items;
|
|
|
|
const active = items.filter((i) => !i.archived);
|
|
const groups = new Map<string, InboxItem[]>();
|
|
active.forEach((item) => {
|
|
const key = item.issue_id ?? item.id;
|
|
const group = groups.get(key) ?? [];
|
|
group.push(item);
|
|
groups.set(key, group);
|
|
});
|
|
const merged: InboxItem[] = [];
|
|
groups.forEach((group) => {
|
|
const sorted = group.sort(
|
|
(a, b) =>
|
|
new Date(b.created_at).getTime() - new Date(a.created_at).getTime(),
|
|
);
|
|
if (sorted[0]) merged.push(sorted[0]);
|
|
});
|
|
_prevDeduped = merged.sort(
|
|
(a, b) =>
|
|
new Date(b.created_at).getTime() - new Date(a.created_at).getTime(),
|
|
);
|
|
return _prevDeduped;
|
|
}
|
|
|
|
interface InboxState {
|
|
items: InboxItem[];
|
|
loading: boolean;
|
|
fetch: () => Promise<void>;
|
|
setItems: (items: InboxItem[]) => void;
|
|
addItem: (item: InboxItem) => void;
|
|
markRead: (id: string) => void;
|
|
archive: (id: string) => void;
|
|
markAllRead: () => void;
|
|
archiveAll: () => void;
|
|
archiveAllRead: () => void;
|
|
updateIssueStatus: (issueId: string, status: IssueStatus) => void;
|
|
dedupedItems: () => InboxItem[];
|
|
unreadCount: () => number;
|
|
}
|
|
|
|
export const useInboxStore = create<InboxState>((set, get) => ({
|
|
items: [],
|
|
loading: true,
|
|
|
|
fetch: async () => {
|
|
logger.debug("fetch start");
|
|
const isInitialLoad = get().items.length === 0;
|
|
if (isInitialLoad) set({ loading: true });
|
|
try {
|
|
const data = await api.listInbox();
|
|
logger.info("fetched", data.length, "items");
|
|
set({ items: data, loading: false });
|
|
} catch (err) {
|
|
logger.error("fetch failed", err);
|
|
if (isInitialLoad) set({ loading: false });
|
|
}
|
|
},
|
|
|
|
setItems: (items) => set({ items }),
|
|
addItem: (item) =>
|
|
set((s) => ({
|
|
items: s.items.some((i) => i.id === item.id)
|
|
? s.items
|
|
: [item, ...s.items],
|
|
})),
|
|
markRead: (id) =>
|
|
set((s) => ({
|
|
items: s.items.map((i) => (i.id === id ? { ...i, read: true } : i)),
|
|
})),
|
|
archive: (id) =>
|
|
set((s) => ({
|
|
items: s.items.map((i) => (i.id === id ? { ...i, archived: true } : i)),
|
|
})),
|
|
markAllRead: () =>
|
|
set((s) => ({
|
|
items: s.items.map((i) => (!i.archived ? { ...i, read: true } : i)),
|
|
})),
|
|
archiveAll: () =>
|
|
set((s) => ({
|
|
items: s.items.map((i) => (!i.archived ? { ...i, archived: true } : i)),
|
|
})),
|
|
archiveAllRead: () =>
|
|
set((s) => ({
|
|
items: s.items.map((i) =>
|
|
i.read && !i.archived ? { ...i, archived: true } : i
|
|
),
|
|
})),
|
|
updateIssueStatus: (issueId, status) =>
|
|
set((s) => ({
|
|
items: s.items.map((i) =>
|
|
i.issue_id === issueId ? { ...i, issue_status: status } : i
|
|
),
|
|
})),
|
|
dedupedItems: () => deduplicateInboxItems(get().items),
|
|
unreadCount: () =>
|
|
get().dedupedItems().filter((i) => !i.read).length,
|
|
}));
|