2026-07-01 22:39:10 -04:00
|
|
|
import { useCallback } from 'react';
|
|
|
|
|
import { useAtomValue } from 'jotai';
|
|
|
|
|
import { MatrixClient } from 'matrix-js-sdk';
|
|
|
|
|
import { AccountDataEvent } from '../../types/matrix/accountData';
|
|
|
|
|
import { threadNotificationsAtom } from '../state/threadNotifications';
|
|
|
|
|
import {
|
|
|
|
|
getThreadNotificationMode,
|
|
|
|
|
pruneThreadNotifications,
|
|
|
|
|
ThreadNotificationEntry,
|
|
|
|
|
ThreadNotificationMode,
|
|
|
|
|
ThreadNotificationsContent,
|
|
|
|
|
} from '../utils/threadNotifications';
|
|
|
|
|
import { useMatrixClient } from './useMatrixClient';
|
|
|
|
|
import { AsyncState, useAsyncCallback } from './useAsyncCallback';
|
2026-07-07 23:14:35 -04:00
|
|
|
import { getAccountData, setAccountData } from '../utils/accountData';
|
2026-07-01 22:39:10 -04:00
|
|
|
|
|
|
|
|
/** Read the current notification mode for a thread from the bound atom. */
|
|
|
|
|
export function useThreadNotificationMode(
|
|
|
|
|
roomId: string,
|
|
|
|
|
threadRootId: string,
|
|
|
|
|
): ThreadNotificationMode {
|
|
|
|
|
const content = useAtomValue(threadNotificationsAtom);
|
|
|
|
|
return getThreadNotificationMode(content, roomId, threadRootId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const readContent = (mx: MatrixClient): ThreadNotificationsContent =>
|
2026-07-07 23:14:35 -04:00
|
|
|
getAccountData<ThreadNotificationsContent>(mx, AccountDataEvent.LotusThreadNotifications) ?? {};
|
2026-07-01 22:39:10 -04:00
|
|
|
|
|
|
|
|
const getJoinedRoomIds = (mx: MatrixClient): Set<string> => {
|
|
|
|
|
const joined = new Set<string>();
|
|
|
|
|
mx.getRooms().forEach((room) => {
|
|
|
|
|
if (room.getMyMembership() === 'join') {
|
|
|
|
|
joined.add(room.roomId);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return joined;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const writeThreadNotificationMode = async (
|
|
|
|
|
mx: MatrixClient,
|
|
|
|
|
roomId: string,
|
|
|
|
|
threadRootId: string,
|
|
|
|
|
mode: ThreadNotificationMode,
|
|
|
|
|
): Promise<void> => {
|
|
|
|
|
const current = readContent(mx);
|
|
|
|
|
const now = Date.now();
|
|
|
|
|
|
|
|
|
|
// Work on a mutable clone; prune produces a fresh object so the mutations
|
|
|
|
|
// below never touch the atom's/account-data's current content.
|
|
|
|
|
const next: ThreadNotificationsContent = {
|
|
|
|
|
...current,
|
|
|
|
|
rooms: Object.fromEntries(
|
|
|
|
|
Object.entries(current.rooms ?? {}).map(([rid, entries]) => [rid, { ...entries }]),
|
|
|
|
|
),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const rooms = next.rooms as Record<string, Record<string, ThreadNotificationEntry>>;
|
|
|
|
|
|
|
|
|
|
if (mode === ThreadNotificationMode.Default) {
|
|
|
|
|
if (rooms[roomId]) {
|
|
|
|
|
delete rooms[roomId][threadRootId];
|
|
|
|
|
if (Object.keys(rooms[roomId]).length === 0) {
|
|
|
|
|
delete rooms[roomId];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (!rooms[roomId]) {
|
|
|
|
|
rooms[roomId] = {};
|
|
|
|
|
}
|
|
|
|
|
rooms[roomId][threadRootId] = { mode, ts: now };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ALWAYS prune before persisting to keep account data bounded.
|
|
|
|
|
const finalContent = pruneThreadNotifications(next, getJoinedRoomIds(mx), now);
|
|
|
|
|
|
2026-07-07 23:14:35 -04:00
|
|
|
await setAccountData(mx, AccountDataEvent.LotusThreadNotifications, finalContent);
|
2026-07-01 22:39:10 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function useSetThreadNotificationMode(
|
|
|
|
|
roomId: string,
|
|
|
|
|
threadRootId: string,
|
|
|
|
|
): {
|
|
|
|
|
modeState: AsyncState<void, Error>;
|
|
|
|
|
setMode: (mode: ThreadNotificationMode) => Promise<void>;
|
|
|
|
|
} {
|
|
|
|
|
const mx = useMatrixClient();
|
|
|
|
|
|
|
|
|
|
const [modeState, setMode] = useAsyncCallback<void, Error, [ThreadNotificationMode]>(
|
|
|
|
|
useCallback(
|
|
|
|
|
(mode: ThreadNotificationMode) => writeThreadNotificationMode(mx, roomId, threadRootId, mode),
|
|
|
|
|
[mx, roomId, threadRootId],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return { modeState, setMode };
|
|
|
|
|
}
|