LANCommander/LANCommander.UI/Components/Chat/ChatThread.razor
2025-09-06 13:09:47 -05:00

57 lines
No EOL
1.7 KiB
Text

@using System.Collections.Specialized
@using LANCommander.SDK
@using Microsoft.AspNetCore.Components.Web.Virtualization
@inject Client Client
@namespace LANCommander.UI.Components
<Flex Direction="FlexDirection.Vertical">
<div style="flex-grow: 1;">
<Virtualize Items="_messageGroups" Context="messageGroup">
<CascadingValue Name="Messages" Value="@messageGroup.Messages">
<ChatMessageGroup
@key="messageGroup.Id"
UserId="@messageGroup.UserId"
UserName="@messageGroup.UserName"
StartedOn="@messageGroup.StartedOn.LocalDateTime" />
</CascadingValue>
</Virtualize>
</div>
<TextArea MinRows="1" AutoSize="true" OnPressEnter="SendMessage"></TextArea>
</Flex>
@code {
[Parameter] public Guid Id { get; set; }
ICollection<SDK.Models.ChatMessageGroup> _messageGroups;
ICollection<SDK.Models.ChatMessage> _messages = new List<SDK.Models.ChatMessage>();
SDK.Models.ChatThread _thread;
bool _sendingMessage = false;
string _inputContents = String.Empty;
protected override async Task OnParametersSetAsync()
{
if (Id != Guid.Empty)
{
_thread = Client.Chat.GetThread(Id);
_thread.MessageGroups.CollectionChanged += MessageGroupsOnCollectionChanged;
}
}
void MessageGroupsOnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
StateHasChanged();
}
async Task SendMessage(PressEnterEventArgs args)
{
if (!args.ShiftKey)
{
_sendingMessage = true;
await Client.Chat.SendMessageAsync(Id, _inputContents);
}
}
}