Implemented delete chat operation

This commit is contained in:
Thorsten Sommer 2024-07-11 08:54:46 +02:00
parent 32af4211db
commit 73b7d81903
No known key found for this signature in database
GPG Key ID: B0B7E2FC074BF1F5
2 changed files with 69 additions and 17 deletions

View File

@ -17,7 +17,7 @@
<MudText Style="justify-self: start;">@treeItem.Text</MudText> <MudText Style="justify-self: start;">@treeItem.Text</MudText>
<div style="justify-self: end;"> <div style="justify-self: end;">
<MudIconButton Icon="@Icons.Material.Filled.Edit" Size="Size.Medium" Color="Color.Inherit"/> <MudIconButton Icon="@Icons.Material.Filled.Edit" Size="Size.Medium" Color="Color.Inherit"/>
<MudIconButton Icon="@Icons.Material.Filled.Delete" Size="Size.Medium" Color="Color.Inherit"/> <MudIconButton Icon="@Icons.Material.Filled.Delete" Size="Size.Medium" Color="Color.Inherit" OnClick="() => this.DeleteChat(treeItem.Path)"/>
</div> </div>
</div> </div>
</BodyContent> </BodyContent>

View File

@ -3,10 +3,13 @@ using System.Text.Json;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using AIStudio.Chat; using AIStudio.Chat;
using AIStudio.Components.CommonDialogs;
using AIStudio.Settings; using AIStudio.Settings;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using DialogOptions = AIStudio.Components.CommonDialogs.DialogOptions;
namespace AIStudio.Components.Blocks; namespace AIStudio.Components.Blocks;
public partial class Workspaces : ComponentBase public partial class Workspaces : ComponentBase
@ -14,6 +17,9 @@ public partial class Workspaces : ComponentBase
[Inject] [Inject]
private SettingsManager SettingsManager { get; set; } = null!; private SettingsManager SettingsManager { get; set; } = null!;
[Inject]
private IDialogService DialogService { get; set; } = null!;
[Parameter] [Parameter]
public ChatThread? CurrentChatThread { get; set; } public ChatThread? CurrentChatThread { get; set; }
@ -115,7 +121,17 @@ public partial class Workspaces : ComponentBase
return tempChildren; return tempChildren;
} }
private async Task<HashSet<ITreeItem<string>>> LoadWorkspaces() private async Task<string> LoadWorkspaceName(Guid workspaceId)
{
if(workspaceId == Guid.Empty)
return string.Empty;
var workspacePath = Path.Join(SettingsManager.DataDirectory, "workspaces", workspaceId.ToString());
var workspaceNamePath = Path.Join(workspacePath, "name");
return await File.ReadAllTextAsync(workspaceNamePath, Encoding.UTF8);
}
private async Task<HashSet<ITreeItem>> LoadWorkspaces()
{ {
var workspaces = new HashSet<ITreeItem>(); var workspaces = new HashSet<ITreeItem>();
@ -204,33 +220,69 @@ public partial class Workspaces : ComponentBase
this.StateHasChanged(); this.StateHasChanged();
} }
private async Task LoadChat(string? chatPath) private async Task<ChatThread?> LoadChat(string? chatPath, bool switchToChat)
{ {
if(string.IsNullOrWhiteSpace(chatPath)) if(string.IsNullOrWhiteSpace(chatPath))
{ return null;
Console.WriteLine("Error: chat path is empty.");
return;
}
if(!Directory.Exists(chatPath)) if(!Directory.Exists(chatPath))
{ return null;
Console.WriteLine($"Error: chat not found: '{chatPath}'");
return;
}
try try
{ {
var chatData = await File.ReadAllTextAsync(Path.Join(chatPath, "thread.json"), Encoding.UTF8); var chatData = await File.ReadAllTextAsync(Path.Join(chatPath, "thread.json"), Encoding.UTF8);
this.CurrentChatThread = JsonSerializer.Deserialize<ChatThread>(chatData, JSON_OPTIONS); var chat = JsonSerializer.Deserialize<ChatThread>(chatData, JSON_OPTIONS);
await this.CurrentChatThreadChanged.InvokeAsync(this.CurrentChatThread); if (switchToChat)
{
Console.WriteLine($"Loaded chat: {this.CurrentChatThread?.Name}"); this.CurrentChatThread = chat;
await this.CurrentChatThreadChanged.InvokeAsync(this.CurrentChatThread);
}
return chat;
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine(e); Console.WriteLine(e);
Console.WriteLine(e.StackTrace); }
throw;
return null;
}
private async Task DeleteChat(string? chatPath)
{
var chat = await this.LoadChat(chatPath, false);
if (chat is null)
return;
var workspaceName = await this.LoadWorkspaceName(chat.WorkspaceId);
var dialogParameters = new DialogParameters
{
{ "Message", (chat.WorkspaceId == Guid.Empty) switch
{
true => $"Are you sure you want to delete the temporary chat '{chat.Name}'?",
false => $"Are you sure you want to delete the chat '{chat.Name}' in the workspace '{workspaceName}'?",
}
},
};
var dialogReference = await this.DialogService.ShowAsync<ConfirmDialog>("Delete Chat", dialogParameters, DialogOptions.FULLSCREEN);
var dialogResult = await dialogReference.Result;
if (dialogResult.Canceled)
return;
string chatDirectory;
if (chat.WorkspaceId == Guid.Empty)
chatDirectory = Path.Join(SettingsManager.DataDirectory, "tempChats", chat.ChatId.ToString());
else
chatDirectory = Path.Join(SettingsManager.DataDirectory, "workspaces", chat.WorkspaceId.ToString(), chat.ChatId.ToString());
Directory.Delete(chatDirectory, true);
await this.LoadTreeItems();
if(this.CurrentChatThread?.ChatId == chat.ChatId)
{
this.CurrentChatThread = null;
await this.CurrentChatThreadChanged.InvokeAsync(this.CurrentChatThread);
} }
} }
} }