chatMessagesProvider top-level property

dynamic chatMessagesProvider
final

Implementation

final chatMessagesProvider =
    StateProvider.autoDispose.family<List<Message>, String>((ref, roomId) {
  final moreMessages = [];
  if (ref.watch(chatStateProvider(roomId).select((value) => !value.hasMore))) {
    moreMessages.add(
      const types.SystemMessage(
        id: 'chat-invite',
        text: 'invite',
        metadata: {
          'type': '_invite',
        },
      ),
    );

    // we have reached the end, show topic
    final topic = ref.watch(chatTopic(roomId)).valueOrNull;
    if (topic != null) {
      moreMessages.add(
        types.SystemMessage(
          id: 'chat-topic',
          text: topic,
          metadata: const {
            'type': '_topic',
          },
        ),
      );
    }

    // and encryption information block
    if (ref.watch(isRoomEncryptedProvider(roomId)).valueOrNull == true) {
      moreMessages.add(
        const types.SystemMessage(
          id: 'encrypted-information',
          text: '',
          metadata: {
            'type': '_encryptedInfo',
          },
        ),
      );
    }
  }
  final messages = ref.watch(renderableChatMessagesProvider(roomId));
  if (moreMessages.isEmpty) {
    return messages;
  }
  // return as a new list to ensure the provider is properly resetting
  return [...messages, ...moreMessages];
});