@trycourier/courier-ui-inbox
Version:
Inbox components for the Courier web UI
111 lines (110 loc) • 5.09 kB
TypeScript
import { InboxMessage } from '@trycourier/courier-js';
import { CourierInboxDatasetFilter, InboxDataSet } from '../types/inbox-data-set';
import { CourierGetInboxMessagesQueryFilter } from '@trycourier/courier-js/dist/types/inbox';
import { CourierInboxDataStoreListener } from './datastore-listener';
export declare class CourierInboxDataset {
/** The unique ID for this dataset, provided by the consumer to later identify this set of messages. */
private _id;
/** The set of messages in this dataset. */
private _messages;
/**
* True if the first fetch of messages has completed successfully.
*
* This marker is used to distinguish if _messages can be returned when cached messages
* are acceptable, since an empty array of messages could indicate they weren't
* ever fetched or that they were fetched but there are currently none in the dataset.
*/
private _firstFetchComplete;
/** True if the fetched dataset sets hasNextPage to true. */
private _hasNextPage;
/**
* The pagination cursor to pass to subsequent fetch requests
* or null if this is the first request or a response has indicated
* there is no next page.
*/
private _lastPaginationCursor?;
private readonly _filter;
private readonly _datastoreListeners;
/**
* The total unread count loaded before messages are fetched.
* Used to show unread badge counts on tabs before the user clicks into them.
*
* Total unread count is maintained manually (rather than derived from _messages) because:
*
* 1. We load unread counts for all tabs in view before their messages are loaded.
* 2. The set of loaded messages may not fully reflect the unread count for a tab.
* Messages are paginated, so unread messages may be present on the server but
* but not on the client.
*/
private _totalUnreadCount;
constructor(id: string, filter: CourierInboxDatasetFilter);
/** Get the current total unread count. */
get totalUnreadCount(): number;
/** Private setter for unread count. */
private set totalUnreadCount(value);
/**
* Set the unread count explicitly.
* Used for batch loading unread counts for all datasets before messages are fetched.
*/
setUnreadCount(count: number): void;
/**
* Get the filter configuration for this dataset.
* Used for batch loading unread counts.
*/
getFilter(): CourierGetInboxMessagesQueryFilter;
/**
* Add a message to the dataset if it qualifies based on the dataset's filters.
*
* @param message the message to add
* @returns true if the message was added, otherwise false
*/
addMessage(message: InboxMessage, insertIndex?: number): boolean;
/**
* Update the messages and unread count for the dataset based on a change in a message.
*
* Based on a message's change (unread -> read, archived -> unarchived, etc) this method
* inserts, updates, removes, or excludes it from the dataset. Given the before/existing
* and after states, it updates the unread count.
*
* The before state identifies messages that would qualify for the dataset
* before the dataset (or a particular message in the dataset) has been loaded.
* These messages may not be explicitly removed from the dataset since they aren't
* yet present, but may have an effect on the unread count.
*
* @param beforeMessage the message before the change
* @param afterMessage the message after the change
* @returns true if afterMessage qualifies for the dataset and was inserted or updated, false if the message was removed
*/
updateWithMessageChange(beforeMessage: InboxMessage, afterMessage: InboxMessage): boolean;
private calculateUnreadChange;
/**
* Remove the specified message from this dataset, if it's present.
*
* @param message the message to remove from this dataset
* @returns true if the message was removed, else false
*/
removeMessage(message: InboxMessage): boolean;
getMessage(messageId: string): InboxMessage | undefined;
loadDataset(canUseCache: boolean): Promise<void>;
fetchNextPageOfMessages(): Promise<InboxDataSet | null>;
addDatastoreListener(listener: CourierInboxDataStoreListener): void;
removeDatastoreListener(listener: CourierInboxDataStoreListener): void;
toInboxDataset(): InboxDataSet;
private fetchMessages;
private indexOfMessage;
/**
* Find the insert index for a new message in a data set
* @param newMessage - The new message to insert
* @param dataSet - The data set to insert the message into
* @returns The index to insert the message at
*/
private findInsertIndex;
private messageQualifiesForDataset;
/**
* Restore this dataset from a snapshot.
*
* Note: _firstFetchComplete does not need to be restored
* as it indicates specific lifecycle stages for the dataset.
*/
restoreFromSnapshot(snapshot: InboxDataSet): void;
}