Implemented rename and delete operations for workspaces

This commit is contained in:
Thorsten Sommer 2024-07-12 12:44:41 +02:00
parent 50da92ee29
commit 7b5d2df873
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
4 changed files with 85 additions and 7 deletions

View File

@ -10,7 +10,7 @@ public class TreeItemData : ITreeItem
public string Icon { get; init; } = string.Empty; public string Icon { get; init; } = string.Empty;
public bool IsChat { get; init; } public TreeItemType Type { get; init; }
public string Path { get; init; } = string.Empty; public string Path { get; init; } = string.Empty;

View File

@ -0,0 +1,9 @@
namespace AIStudio.Components.Blocks;
public enum TreeItemType
{
NONE,
CHAT,
WORKSPACE,
}

View File

@ -9,9 +9,9 @@
break; break;
case TreeItemData treeItem: case TreeItemData treeItem:
@if (treeItem.IsChat) @if (treeItem.Type is TreeItemType.CHAT)
{ {
<MudTreeViewItem T="ITreeItem" Icon="@treeItem.Icon" Value="@item" LoadingIconColor="@Color.Info" CanExpand="@treeItem.Expandable" Items="@treeItem.Children" OnClick="() => this.LoadChat(treeItem.Path, true)"> <MudTreeViewItem T="ITreeItem" Icon="@treeItem.Icon" Value="@item" CanExpand="@treeItem.Expandable" Items="@treeItem.Children" OnClick="() => this.LoadChat(treeItem.Path, true)">
<BodyContent> <BodyContent>
<div style="display: grid; grid-template-columns: 1fr auto; align-items: center; width: 100%"> <div style="display: grid; grid-template-columns: 1fr auto; align-items: center; width: 100%">
<MudText Style="justify-self: start;">@treeItem.Text</MudText> <MudText Style="justify-self: start;">@treeItem.Text</MudText>
@ -28,9 +28,28 @@
</BodyContent> </BodyContent>
</MudTreeViewItem> </MudTreeViewItem>
} }
else if (treeItem.Type is TreeItemType.WORKSPACE)
{
<MudTreeViewItem T="ITreeItem" Icon="@treeItem.Icon" Value="@item" CanExpand="@treeItem.Expandable" Items="@treeItem.Children">
<BodyContent>
<div style="display: grid; grid-template-columns: 1fr auto; align-items: center; width: 100%">
<MudText Style="justify-self: start;">@treeItem.Text</MudText>
<div style="justify-self: end;">
<MudTooltip Text="Rename" Placement="Placement.Right">
<MudIconButton Icon="@Icons.Material.Filled.Edit" Size="Size.Medium" Color="Color.Inherit" OnClick="() => this.RenameWorkspace(treeItem.Path)"/>
</MudTooltip>
<MudTooltip Text="Delete" Placement="Placement.Right">
<MudIconButton Icon="@Icons.Material.Filled.Delete" Size="Size.Medium" Color="Color.Inherit" OnClick="() => this.DeleteWorkspace(treeItem.Path)"/>
</MudTooltip>
</div>
</div>
</BodyContent>
</MudTreeViewItem>
}
else else
{ {
<MudTreeViewItem T="ITreeItem" Icon="@treeItem.Icon" Value="@item" LoadingIconColor="@Color.Info" CanExpand="@treeItem.Expandable" Items="@treeItem.Children"> <MudTreeViewItem T="ITreeItem" Icon="@treeItem.Icon" Value="@item" CanExpand="@treeItem.Expandable" Items="@treeItem.Children">
<BodyContent> <BodyContent>
<div style="display: grid; grid-template-columns: 1fr auto; align-items: center; width: 100%"> <div style="display: grid; grid-template-columns: 1fr auto; align-items: center; width: 100%">
<MudText Style="justify-self: start;">@treeItem.Text</MudText> <MudText Style="justify-self: start;">@treeItem.Text</MudText>

View File

@ -108,7 +108,7 @@ public partial class Workspaces : ComponentBase
tempChildren.Add(new TreeItemData tempChildren.Add(new TreeItemData
{ {
IsChat = true, Type = TreeItemType.CHAT,
Depth = 1, Depth = 1,
Branch = WorkspaceBranch.TEMPORARY_CHATS, Branch = WorkspaceBranch.TEMPORARY_CHATS,
Text = chatName, Text = chatName,
@ -154,7 +154,7 @@ public partial class Workspaces : ComponentBase
workspaces.Add(new TreeItemData workspaces.Add(new TreeItemData
{ {
IsChat = false, Type = TreeItemType.WORKSPACE,
Depth = 1, Depth = 1,
Branch = WorkspaceBranch.WORKSPACES, Branch = WorkspaceBranch.WORKSPACES,
Text = workspaceName, Text = workspaceName,
@ -182,7 +182,7 @@ public partial class Workspaces : ComponentBase
workspaceChats.Add(new TreeItemData workspaceChats.Add(new TreeItemData
{ {
IsChat = true, Type = TreeItemType.CHAT,
Depth = 2, Depth = 2,
Branch = WorkspaceBranch.WORKSPACES, Branch = WorkspaceBranch.WORKSPACES,
Text = chatName, Text = chatName,
@ -308,6 +308,31 @@ public partial class Workspaces : ComponentBase
await this.StoreChat(chat); await this.StoreChat(chat);
await this.LoadTreeItems(); await this.LoadTreeItems();
} }
private async Task RenameWorkspace(string? workspacePath)
{
if(workspacePath is null)
return;
var workspaceId = Guid.Parse(Path.GetFileName(workspacePath));
var workspaceName = await this.LoadWorkspaceName(workspaceId);
var dialogParameters = new DialogParameters
{
{ "Message", $"Please enter a new or edit the name for your workspace '{workspaceName}':" },
{ "UserInput", workspaceName },
{ "ConfirmText", "Rename" },
};
var dialogReference = await this.DialogService.ShowAsync<SingleInputDialog>("Rename Workspace", dialogParameters, DialogOptions.FULLSCREEN);
var dialogResult = await dialogReference.Result;
if (dialogResult.Canceled)
return;
var alteredWorkspaceName = (dialogResult.Data as string)!;
var workspaceNamePath = Path.Join(workspacePath, "name");
await File.WriteAllTextAsync(workspaceNamePath, alteredWorkspaceName, Encoding.UTF8);
await this.LoadTreeItems();
}
private async Task AddWorkspace() private async Task AddWorkspace()
{ {
@ -333,6 +358,31 @@ public partial class Workspaces : ComponentBase
await this.LoadTreeItems(); await this.LoadTreeItems();
} }
private async Task DeleteWorkspace(string? workspacePath)
{
if(workspacePath is null)
return;
var workspaceId = Guid.Parse(Path.GetFileName(workspacePath));
var workspaceName = await this.LoadWorkspaceName(workspaceId);
// Determine how many chats are in the workspace:
var chatCount = Directory.EnumerateDirectories(workspacePath).Count();
var dialogParameters = new DialogParameters
{
{ "Message", $"Are you sure you want to delete the workspace '{workspaceName}'? This will also delete {chatCount} chat(s) in this workspace." },
};
var dialogReference = await this.DialogService.ShowAsync<ConfirmDialog>("Delete Workspace", dialogParameters, DialogOptions.FULLSCREEN);
var dialogResult = await dialogReference.Result;
if (dialogResult.Canceled)
return;
Directory.Delete(workspacePath, true);
await this.LoadTreeItems();
}
private async Task AddChat(string workspacePath) private async Task AddChat(string workspacePath)
{ {