Skip to content
This repository has been archived by the owner on Nov 7, 2024. It is now read-only.

Commit

Permalink
Added notificationsByTypeAndGroup
Browse files Browse the repository at this point in the history
  • Loading branch information
guillemcordoba committed May 2, 2024
1 parent 6e71347 commit e81e3a6
Showing 1 changed file with 59 additions and 1 deletion.
60 changes: 59 additions & 1 deletion ui/src/notifications-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@ import {
deletesForEntrySignal,
immutableEntrySignal,
joinAsync,
joinAsyncMap,
liveLinksSignal,
uniquify,
} from '@holochain-open-dev/signals';
import { EntryRecord, LazyHoloHashMap, slice } from '@holochain-open-dev/utils';
import {
EntryRecord,
LazyHoloHashMap,
LazyMap,
mapValues,
pickBy,
slice,
} from '@holochain-open-dev/utils';
import { ActionHash, encodeHashToBase64 } from '@holochain/client';
import { decode } from '@msgpack/msgpack';

Expand Down Expand Up @@ -169,4 +177,54 @@ export class NotificationsStore {
value,
};
});

/** Helpers for consuming UIs */

notificationsByTypeAndGroup = new LazyMap(
(notificationType: string) =>
new LazyMap((notificationGroup: string) => ({
read$: new AsyncComputed(() => {
const notifications = this.readNotifications$.get();
if (notifications.status !== 'completed') return notifications;

const entries = joinAsyncMap(
mapValues(notifications.value, n => n.entry$.get()),
);
if (entries.status !== 'completed') return entries;

const value = pickBy(
entries.value,
n =>
n.entry.notification_type === notificationType &&
n.entry.notification_group === notificationGroup,
);

return {
status: 'completed',
value,
};
}),
unread$: new AsyncComputed(() => {
const notifications = this.unreadNotifications$.get();
if (notifications.status !== 'completed') return notifications;

const entries = joinAsyncMap(
mapValues(notifications.value, n => n.entry$.get()),
);
if (entries.status !== 'completed') return entries;

const value = pickBy(
entries.value,
n =>
n.entry.notification_type === notificationType &&
n.entry.notification_group === notificationGroup,
);

return {
status: 'completed',
value,
};
}),
})),
);
}

0 comments on commit e81e3a6

Please sign in to comment.