992d2b83b3
threadSummary.ts (pure helpers) and ThreadSummary.tsx (chip component) lived in the same directory differing only by case. On the case-insensitive Windows release runner, RoomTimeline's extensionless import of ./thread/ThreadSummary resolved .ts BEFORE .tsx and matched the helper module → rolldown MISSING_EXPORT "ThreadSummary" — invisible on every Linux/macOS build (and the cause of the earlier masked pdf.worker failure). Helper module renamed to threadSummaryData.ts (+ test), 3 importers updated. Prevention: new caseCollision.test.ts walks src/ and fails on any same-directory names differing only by case (extensionless compare, so Foo.tsx vs foo.ts is caught) — verified it fails on the pre-rename tree. Runs in the hard CI gate. Gates: tsc clean, eslint/prettier clean, build OK, 658/659 tests (1 IDB skip). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
68 lines
2.3 KiB
TypeScript
68 lines
2.3 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
import { useAtomValue } from 'jotai';
|
|
import {
|
|
MatrixEvent,
|
|
NotificationCountType,
|
|
Room,
|
|
RoomEvent,
|
|
RoomEventHandlerMap,
|
|
ThreadEvent,
|
|
} from 'matrix-js-sdk';
|
|
import { getThreadSummary, ThreadSummaryData } from '../features/room/thread/threadSummaryData';
|
|
import { threadNotificationsAtom } from '../state/threadNotifications';
|
|
import { getThreadNotificationMode, ThreadNotificationMode } from '../utils/threadNotifications';
|
|
|
|
/**
|
|
* Reactive thread summary + unread count for a root event's "N replies" chip.
|
|
*
|
|
* Re-computes the summary on `ThreadEvent.Update` (the SDK re-emits this on the
|
|
* root MatrixEvent) and the unread count on `RoomEvent.UnreadNotifications`.
|
|
*/
|
|
export const useThreadSummary = (
|
|
rootEvent: MatrixEvent,
|
|
room: Room,
|
|
): { summary: ThreadSummaryData | undefined; unread: number; mode: ThreadNotificationMode } => {
|
|
const threadId = rootEvent.getId();
|
|
|
|
const threadNotifications = useAtomValue(threadNotificationsAtom);
|
|
const mode = threadId
|
|
? getThreadNotificationMode(threadNotifications, room.roomId, threadId)
|
|
: ThreadNotificationMode.Default;
|
|
|
|
const [summary, setSummary] = useState<ThreadSummaryData | undefined>(() =>
|
|
getThreadSummary(rootEvent),
|
|
);
|
|
const [unread, setUnread] = useState<number>(() =>
|
|
threadId
|
|
? (room.getThreadUnreadNotificationCount(threadId, NotificationCountType.Total) ?? 0)
|
|
: 0,
|
|
);
|
|
|
|
useEffect(() => {
|
|
const refreshSummary = () => setSummary(getThreadSummary(rootEvent));
|
|
const refreshUnread = () => {
|
|
if (!threadId) return;
|
|
setUnread(room.getThreadUnreadNotificationCount(threadId, NotificationCountType.Total) ?? 0);
|
|
};
|
|
|
|
refreshSummary();
|
|
refreshUnread();
|
|
|
|
const handleUnread: RoomEventHandlerMap[RoomEvent.UnreadNotifications] = (_counts, tId) => {
|
|
if (tId && tId !== threadId) return;
|
|
refreshUnread();
|
|
};
|
|
|
|
rootEvent.on(ThreadEvent.Update, refreshSummary);
|
|
room.on(RoomEvent.UnreadNotifications, handleUnread);
|
|
return () => {
|
|
rootEvent.removeListener(ThreadEvent.Update, refreshSummary);
|
|
room.removeListener(RoomEvent.UnreadNotifications, handleUnread);
|
|
};
|
|
}, [rootEvent, room, threadId]);
|
|
|
|
const muted = mode === ThreadNotificationMode.Mute;
|
|
|
|
return { summary, unread: muted ? 0 : unread, mode };
|
|
};
|