GsDChat/src/GsDChat.Client/Screens/InboxScreen.cs

208 lines
9.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using GsDChat.Client.Network;
using GsDChat.Client.Rendering;
using GsDChat.Client.State;
using GsDChat.Core.Models;
using GsDChat.Core.Protocol;
namespace GsDChat.Client.Screens;
public class InboxScreen : IScreen
{
private readonly ClientState _state;
private readonly ChatClientConnection _connection;
private int _selectedIndex = 0;
public string ScreenId => "F5_Inbox";
public string Title => "Inbox";
public string ShortcutKey => "F5";
public InboxScreen(ClientState state, ChatClientConnection connection)
{
_state = state;
_connection = connection;
}
public void Render(int width, int height, StringBuilder sb)
{
var items = _state.GetInboxItems();
var prefs = _state.InboxPreferences;
string unreadTag = _state.UnreadInboxCount > 0
? $"{Ansi.BrightYellow}({_state.UnreadInboxCount} unread){Ansi.Reset}"
: $"{Ansi.Gray}(All caught up){Ansi.Reset}";
sb.AppendLine($" {Ansi.Bold}{Ansi.Yellow}User Inbox{Ansi.Reset} {unreadTag}");
string mTag = prefs.MentionsEnabled ? $"{Ansi.BrightGreen}ON{Ansi.Reset}" : $"{Ansi.BrightRed}OFF{Ansi.Reset}";
string aTag = prefs.AnnouncementsEnabled ? $"{Ansi.BrightGreen}ON{Ansi.Reset}" : $"{Ansi.BrightRed}OFF{Ansi.Reset}";
string dTag = prefs.DirectMessagesEnabled ? $"{Ansi.BrightGreen}ON{Ansi.Reset}" : $"{Ansi.BrightRed}OFF{Ansi.Reset}";
string fTag = prefs.FollowedChannelsEnabled ? $"{Ansi.BrightGreen}ON{Ansi.Reset}" : $"{Ansi.BrightRed}OFF{Ansi.Reset}";
sb.AppendLine($" {Ansi.Gray}Settings:{Ansi.Reset} [1] Mentions: {mTag} │ [2] Announcements: {aTag} │ [3] DMs/Groups: {dTag} │ [4] Followed Channels: {fTag}");
sb.AppendLine(new string('─', Math.Max(0, width - 1)));
int maxVisible = Math.Max(4, height - 10);
if (items.Count == 0)
{
sb.AppendLine($" {Ansi.Gray}Your Inbox is empty.{Ansi.Reset}");
sb.AppendLine($" {Ansi.Gray}Mentions (@username), announcements, DMs, and followed channels will appear here.{Ansi.Reset}");
sb.AppendLine($" {Ansi.Gray}Tip: Press F2 and hit [F] on any channel or DM to follow/mute it.{Ansi.Reset}");
}
else
{
if (_selectedIndex >= items.Count) _selectedIndex = items.Count - 1;
if (_selectedIndex < 0) _selectedIndex = 0;
int startIdx = 0;
if (_selectedIndex >= maxVisible)
{
startIdx = _selectedIndex - maxVisible + 1;
}
int endIdx = Math.Min(items.Count, startIdx + maxVisible);
for (int i = startIdx; i < endIdx; i++)
{
var item = items[i];
bool isSelected = i == _selectedIndex;
string cursor = isSelected ? $"{Ansi.BrightYellow}► " : " ";
string readDot = item.IsRead
? $"{Ansi.Gray}○{Ansi.Reset}"
: $"{Ansi.BrightYellow}●{Ansi.Reset}";
string catBadge = item.Category switch
{
InboxCategories.Mention => $"{Ansi.BrightCyan}[@Mention]{Ansi.Reset}",
InboxCategories.Announcement => $"{Ansi.BrightMagenta}[📢 Announce]{Ansi.Reset}",
InboxCategories.DirectMessage => $"{Ansi.BrightGreen}[DM]{Ansi.Reset}",
InboxCategories.GroupMessage => $"{Ansi.BrightMagenta}[Group]{Ansi.Reset}",
InboxCategories.GroupInvite => $"{Ansi.BrightYellow}[Invite]{Ansi.Reset}",
_ => $"{Ansi.Cyan}[#Followed]{Ansi.Reset}"
};
string convLabel = item.ChannelType switch
{
ChannelTypes.DirectMessage => $"@{item.ChannelName.TrimStart('@')}",
ChannelTypes.Group => $"[Group] {item.ChannelName}",
_ => $"#{item.ChannelName}"
};
string time = item.CreatedAt.ToLocalTime().ToString("MM-dd HH:mm");
string preview = item.ContentPreview ?? string.Empty;
if (preview.StartsWith("E2EE:v1:", StringComparison.Ordinal))
{
preview = !_state.HasUnlockedE2eeKey
? "[🔒 Encrypted Message — Unlock E2EE PIN]"
: "[🔒 Encrypted Message]";
}
preview = preview.Replace("\r", " ").Replace("\n", " ");
if (preview.Length > 60)
{
preview = preview[..57] + "...";
}
if (isSelected)
{
sb.AppendLine($"{cursor}{readDot} {catBadge} {Ansi.Inverse} {convLabel} {Ansi.Reset} {Ansi.Gray}[{time}]{Ansi.Reset} <@{item.SenderUsername}> {preview}");
}
else
{
string titleStyle = item.IsRead ? Ansi.Gray : Ansi.Bold;
sb.AppendLine($"{cursor}{readDot} {catBadge} {titleStyle}{convLabel}{Ansi.Reset} {Ansi.Gray}[{time}]{Ansi.Reset} <@{item.SenderUsername}> {preview}");
}
}
}
sb.AppendLine(new string('─', Math.Max(0, width - 1)));
sb.AppendLine($" {Ansi.Gray}[↑/↓] Select [Enter] Open Chat [R] Mark Read [A] Mark All Read [D] Dismiss [C] Clear Read [1-4] Toggle Prefs{Ansi.Reset}");
}
public async Task HandleKeyAsync(ConsoleKeyInfo key)
{
var items = _state.GetInboxItems();
var prefs = _state.InboxPreferences;
if (key.Key == ConsoleKey.UpArrow)
{
_selectedIndex = Math.Max(0, _selectedIndex - 1);
_state.NeedsRedraw = true;
}
else if (key.Key == ConsoleKey.DownArrow)
{
_selectedIndex = Math.Min(Math.Max(0, items.Count - 1), _selectedIndex + 1);
_state.NeedsRedraw = true;
}
else if (key.Key == ConsoleKey.Enter)
{
if (_selectedIndex >= 0 && _selectedIndex < items.Count)
{
var selected = items[_selectedIndex];
await _connection.SendInboxActionAsync("mark_read", itemId: selected.Id, channelId: selected.ChannelId);
if (!string.IsNullOrWhiteSpace(selected.ChannelId))
{
await _connection.JoinChannelAsync(selected.ChannelId);
_state.ActiveScreenId = "F1_Chat";
_state.SetStatus($"Opened {selected.ChannelName} from Inbox.");
}
_state.NeedsRedraw = true;
}
}
else if (key.Key == ConsoleKey.R)
{
if (_selectedIndex >= 0 && _selectedIndex < items.Count)
{
var selected = items[_selectedIndex];
await _connection.SendInboxActionAsync("mark_read", itemId: selected.Id);
_state.SetStatus("Marked item as read.");
}
}
else if (key.Key == ConsoleKey.A)
{
await _connection.SendInboxActionAsync("mark_all_read");
_state.SetStatus("Marked all Inbox items as read.");
}
else if (key.Key == ConsoleKey.D || key.Key == ConsoleKey.Delete)
{
if (_selectedIndex >= 0 && _selectedIndex < items.Count)
{
var selected = items[_selectedIndex];
await _connection.SendInboxActionAsync("dismiss", itemId: selected.Id);
_state.SetStatus("Dismissed Inbox item.");
}
}
else if (key.Key == ConsoleKey.C)
{
bool clearAll = (key.Modifiers & ConsoleModifiers.Shift) != 0;
await _connection.SendInboxActionAsync(clearAll ? "clear_all" : "clear_read");
_state.SetStatus(clearAll ? "Cleared all Inbox items." : "Cleared read Inbox items.");
}
else if (key.Key == ConsoleKey.D1 || key.Key == ConsoleKey.NumPad1)
{
bool next = !prefs.MentionsEnabled;
await _connection.UpdateInboxPreferencesAsync(new UpdateInboxPreferencesPacket { MentionsEnabled = next });
_state.SetStatus($"Mention notifications {(next ? "enabled" : "disabled")}.");
}
else if (key.Key == ConsoleKey.D2 || key.Key == ConsoleKey.NumPad2)
{
bool next = !prefs.AnnouncementsEnabled;
await _connection.UpdateInboxPreferencesAsync(new UpdateInboxPreferencesPacket { AnnouncementsEnabled = next });
_state.SetStatus($"Announcement notifications {(next ? "enabled" : "disabled")}.");
}
else if (key.Key == ConsoleKey.D3 || key.Key == ConsoleKey.NumPad3)
{
bool next = !prefs.DirectMessagesEnabled;
await _connection.UpdateInboxPreferencesAsync(new UpdateInboxPreferencesPacket { DirectMessagesEnabled = next });
_state.SetStatus($"DM & Group notifications {(next ? "enabled" : "disabled")}.");
}
else if (key.Key == ConsoleKey.D4 || key.Key == ConsoleKey.NumPad4)
{
bool next = !prefs.FollowedChannelsEnabled;
await _connection.UpdateInboxPreferencesAsync(new UpdateInboxPreferencesPacket { FollowedChannelsEnabled = next });
_state.SetStatus($"Followed channel notifications {(next ? "enabled" : "disabled")}.");
}
}
}