diff --git a/src/web/Jordnaer/Features/Chat/ChatComponent.razor b/src/web/Jordnaer/Features/Chat/ChatComponent.razor
deleted file mode 100644
index f5f4ba6a..00000000
--- a/src/web/Jordnaer/Features/Chat/ChatComponent.razor
+++ /dev/null
@@ -1,342 +0,0 @@
-@inject IChatService ChatService
-@inject IChatMessageCache ChatMessageCache
-@inject IProfileCache ProfileCache
-@inject ISnackbar Snackbar
-@inject IJSRuntime JsRuntime
-@inject IBrowserViewportService BrowserViewportService
-@inject ChatSignalRClient ChatSignalRClient
-@inject NavigationManager Navigation
-@implements IAsyncDisposable
-
-@rendermode @(new InteractiveServerRenderMode(false))
-
-@attribute [Authorize]
-
-
-
-
-
-
- @if ((_activeChat is null && _isMobile) || _isMobile is false)
- {
-
- }
-
- @if (_isMobile is false)
- {
-
-
-
- }
-
- @if (_activeChat is not null)
- {
-
- @if (_isMobile)
- {
-
-
- @_activeChat.GetDisplayName(_currentUser.Id)
-
- }
-
-
-
-
-
-
- @if (LastMessageWasSentSuccessfullyByCurrentUser)
- {
-
-
-
- }
-
-
-
-
-
- }
-
-
-
-@code {
- [Parameter]
- public Guid? ChatId { get; set; }
-
- private UserProfile _currentUser = null!;
- private List _chats = [];
- private ChatDto? _activeChat;
-
- private string _userText = string.Empty;
-
- private bool _isActiveChatPublished = true;
- private bool _isLoading = true;
- private bool _isMobile = true;
- private Guid _breakpointObserverId = Guid.NewGuid();
-
- private bool LastMessageWasSentSuccessfullyByCurrentUser;
-
- protected override async Task OnAfterRenderAsync(bool firstRender)
- {
- await ScrollToBottom();
- await FocusMessageInput();
- await HideFooter();
- await BrowserViewportService.SubscribeAsync(
- observerId: _breakpointObserverId,
- lambda: args => _isMobile = args.Breakpoint <= Breakpoint.Sm,
- fireImmediately: true);
- }
-
- protected override async Task OnInitializedAsync()
- {
- var currentUser = await ProfileCache.GetProfileAsync();
- if (currentUser is null)
- {
- Snackbar.Add(ErrorMessages.Something_Went_Wrong_Refresh, Severity.Warning);
- return;
- }
-
- _currentUser = currentUser;
-
- _chats = await ChatService.GetChatsAsync(_currentUser.Id);
- if (ChatId is not null)
- {
- var chatFromRoute = _chats.FirstOrDefault(chat => chat.Id == ChatId);
- if (chatFromRoute is not null)
- {
- await SelectChat(chatFromRoute);
- }
- }
-
- await StartSignalR();
-
- _isLoading = false;
- }
-
- private async Task StartSignalR()
- {
- ChatSignalRClient.OnChatStarted(async chat =>
- {
- var lastChatMessage = chat.Messages.LastOrDefault();
- if (lastChatMessage is not null && lastChatMessage.SenderId == _currentUser.Id)
- {
- return;
- }
-
- var chatDto = chat.ToChatDto();
- chatDto.UnreadMessageCount++;
- _chats.Insert(0, chatDto);
-
- await InvokeAsync(StateHasChanged);
- });
-
- ChatSignalRClient.OnMessageReceived(async message =>
- {
- var chat = _chats.FirstOrDefault(chat => chat.Id == message.ChatId);
- if (chat is null)
- {
- return;
- }
-
- if (message.SenderId == _currentUser.Id)
- {
- LastMessageWasSentSuccessfullyByCurrentUser = true;
- await InvokeAsync(StateHasChanged);
- return;
- }
-
- chat.Messages.Add(message.ToChatMessageDto());
-
- if (_activeChat?.Id == chat.Id)
- {
- LastMessageWasSentSuccessfullyByCurrentUser = false;
- await ChatService.MarkMessagesAsReadAsync(_currentUser.Id, message.ChatId);
- await InvokeAsync(StateHasChanged);
- }
- else
- {
- await InvokeAsync(() =>
- {
- chat.UnreadMessageCount++;
- _chats = _chats.OrderByDescending(c => c.UnreadMessageCount).ToList();
- StateHasChanged();
- });
- }
- });
-
- await ChatSignalRClient.StartAsync();
- }
-
- public async Task LoadMessages()
- {
- if (_activeChat is null)
- {
- return;
- }
-
- _activeChat.Messages = await ChatMessageCache.GetChatMessagesAsync(_currentUser.Id, _activeChat.Id);
-
- StateHasChanged();
- }
-
- private async Task SelectChat(ChatDto chat)
- {
- _activeChat = chat;
- if (_isActiveChatPublished)
- {
- await LoadMessages();
-
- if (chat.HasUnreadMessages)
- {
- await ChatService.MarkMessagesAsReadAsync(_currentUser.Id, _activeChat.Id);
- chat.UnreadMessageCount = 0;
- }
- }
-
- MarkMessageIfSuccessfullySentByCurrentUser();
-
- if (_activeChat is not null && !Navigation.Uri.EndsWith($"/chat/{_activeChat.Id}"))
- {
- Navigation.NavigateTo($"/chat/{_activeChat.Id}");
- }
- }
-
- private void MarkMessageIfSuccessfullySentByCurrentUser()
- {
- if (_activeChat is null)
- {
- return;
- }
-
- var lastMessage = _activeChat.Messages.LastOrDefault();
- if (lastMessage is null)
- {
- LastMessageWasSentSuccessfullyByCurrentUser = false;
- return;
- }
-
- LastMessageWasSentSuccessfullyByCurrentUser = lastMessage.SenderId == _currentUser.Id;
- }
-
- private async Task HideFooter() => await JsRuntime.InvokeVoidAsync("utilities.hideElement", ".footer");
- private async Task FocusMessageInput() => await JsRuntime.InvokeVoidAsync("utilities.focusElement", "#chat-message-input");
- private async Task ScrollToBottom() => await JsRuntime.InvokeVoidAsync("scrollFunctions.scrollToBottomOfElement", ".chat-message-window");
-
- private void BackToList()
- {
- _activeChat = null;
- Navigation.NavigateTo($"{Navigation.BaseUri}chat");
- }
-
- private async Task SendMessage()
- {
- if (_activeChat is null || string.IsNullOrWhiteSpace(_userText))
- {
- return;
- }
-
- var message = new ChatMessageDto
- {
- ChatId = _activeChat.Id,
- Id = NewId.NextGuid(),
- SentUtc = DateTime.UtcNow,
- SenderId = _currentUser.Id,
- Text = _userText
- };
-
- _activeChat.Messages.Add(message);
- _userText = string.Empty;
-
- if (_isActiveChatPublished)
- {
- await ChatService.SendMessageAsync(message);
- }
- else
- {
- await ChatService.StartChatAsync(_activeChat.ToStartChatCommand(_currentUser.Id));
- _isActiveChatPublished = true;
- }
-
- await ScrollToBottom();
- }
-
- private async Task SendMessageOnEnter(KeyboardEventArgs keyboardEventArgs)
- {
- if (keyboardEventArgs is { Key: "Enter", ShiftKey: false })
- {
- await SendMessage();
-
- _userText = "";
- }
- }
-
- private async Task StartNewChat(IEnumerable users)
- {
- var userList = users.ToList();
- if (userList.Count == 1)
- {
- var userIdsToFind = new[] { userList.First().Id, _currentUser.Id };
- var existingChat = _chats.FirstOrDefault(chat => chat.Recipients.Count == 2 &&
- userIdsToFind.All(id => chat.Recipients.Any(recipient => recipient.Id == id)));
- if (existingChat is not null)
- {
- await SelectChat(existingChat);
- await ScrollToBottom();
- await FocusMessageInput();
- return;
- }
- }
-
- var newChat = new ChatDto
- {
- Id = NewId.NextGuid(),
- Recipients = [_currentUser.ToUserSlim()],
- LastMessageSentUtc = DateTime.UtcNow,
- StartedUtc = DateTime.UtcNow
- };
-
- foreach (var user in userList.Where(u => u.Id != _currentUser.Id))
- {
- newChat.Recipients.Add(new UserSlim
- {
- DisplayName = user.DisplayName,
- Id = user.Id,
- ProfilePictureUrl = user.ProfilePictureUrl,
- UserName = user.UserName
- });
- }
-
- _chats.Insert(0, newChat);
-
- _isActiveChatPublished = false;
-
- await SelectChat(newChat);
- await ScrollToBottom();
- await FocusMessageInput();
- }
-
- public async ValueTask DisposeAsync()
- {
- await BrowserViewportService.UnsubscribeAsync(_breakpointObserverId);
- await ChatSignalRClient.StopAsync();
- }
-}
diff --git a/src/web/Jordnaer/Features/Chat/ChatMessageList.razor b/src/web/Jordnaer/Features/Chat/ChatMessageList.razor
index 1caef283..2321ba3e 100644
--- a/src/web/Jordnaer/Features/Chat/ChatMessageList.razor
+++ b/src/web/Jordnaer/Features/Chat/ChatMessageList.razor
@@ -1,3 +1,5 @@
+@inject CurrentUser CurrentUser
+
@if (IsMessageFromSelf(message))
@@ -40,8 +42,6 @@
@code
{
- [Parameter]
- public required UserProfile CurrentUser { get; set; } = null!;
[Parameter]
public required ChatDto? ActiveChat { get; set; }
diff --git a/src/web/Jordnaer/Features/Chat/ChatSelector.razor b/src/web/Jordnaer/Features/Chat/ChatSelector.razor
index 942e3f21..ba7a14e9 100644
--- a/src/web/Jordnaer/Features/Chat/ChatSelector.razor
+++ b/src/web/Jordnaer/Features/Chat/ChatSelector.razor
@@ -1,7 +1,11 @@
@using Jordnaer.Extensions
@using Jordnaer.Shared
-@if (CurrentUser is null)
+@inject CurrentUser CurrentUser
+
+@attribute [Authorize]
+
+@if (CurrentUser.Id is null)
{
return;
}
@@ -36,8 +40,6 @@
@code{
- [Parameter]
- public UserProfile? CurrentUser { get; set; }
[Parameter]
public required List Chats { get; set; } =[];
diff --git a/src/web/Jordnaer/Pages/Chat/ChatPage.razor b/src/web/Jordnaer/Pages/Chat/ChatPage.razor
index 1e19129d..f3ea4629 100644
--- a/src/web/Jordnaer/Pages/Chat/ChatPage.razor
+++ b/src/web/Jordnaer/Pages/Chat/ChatPage.razor
@@ -25,7 +25,7 @@
@if ((_activeChat is null && _isMobile) || _isMobile is false)
{
-
+
}
@if (_isMobile is false)
@@ -52,7 +52,7 @@
-
+
@if (LastMessageWasSentSuccessfullyByCurrentUser)
{
@@ -62,20 +62,20 @@
}
-
-
@@ -92,7 +92,7 @@
private List _chats = [];
private ChatDto? _activeChat;
- private string _userText = string.Empty;
+ private MudTextField _messageInput = new();
private bool _isActiveChatPublished = true;
private bool _isLoading = true;
@@ -252,22 +252,24 @@
private async Task SendMessage()
{
- if (_activeChat is null || string.IsNullOrWhiteSpace(_userText))
+ if (_activeChat is null || string.IsNullOrWhiteSpace(_messageInput.Value))
{
return;
}
var message = new ChatMessageDto
- {
- ChatId = _activeChat.Id,
- Id = NewId.NextGuid(),
- SentUtc = DateTime.UtcNow,
- SenderId = _currentUser.Id,
- Text = _userText
- };
+ {
+ ChatId = _activeChat.Id,
+ Id = NewId.NextGuid(),
+ SentUtc = DateTime.UtcNow,
+ SenderId = _currentUser.Id,
+ Text = _messageInput.Value
+ };
+
+ await _messageInput.BlurAsync();
+ await _messageInput.Clear();
_activeChat.Messages.Add(message);
- _userText = string.Empty;
if (_isActiveChatPublished)
{
@@ -287,8 +289,6 @@
if (keyboardEventArgs is { Key: "Enter", ShiftKey: false })
{
await SendMessage();
-
- _userText = "";
}
}