Check unsaved changes from the workspace component

This commit is contained in:
Thorsten Sommer 2024-07-12 21:44:46 +02:00
parent 562276ed3f
commit 4f8a6533c3
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
3 changed files with 58 additions and 1 deletions

View File

@ -5,6 +5,7 @@ using System.Text.Json.Serialization;
using AIStudio.Chat; using AIStudio.Chat;
using AIStudio.Components.CommonDialogs; using AIStudio.Components.CommonDialogs;
using AIStudio.Settings; using AIStudio.Settings;
using AIStudio.Tools;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
@ -236,6 +237,20 @@ public partial class Workspaces : ComponentBase
if(!Directory.Exists(chatPath)) if(!Directory.Exists(chatPath))
return null; return null;
// Check if the chat has unsaved changes:
if (switchToChat && await MessageBus.INSTANCE.SendMessageUseFirstResult<bool, bool>(this, Event.HAS_CHAT_UNSAVED_CHANGES))
{
var dialogParameters = new DialogParameters
{
{ "Message", "Are you sure you want to load another chat? All unsaved changes will be lost." },
};
var dialogReference = await this.DialogService.ShowAsync<ConfirmDialog>("Load Chat", dialogParameters, DialogOptions.FULLSCREEN);
var dialogResult = await dialogReference.Result;
if (dialogResult.Canceled)
return null;
}
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);
@ -450,6 +465,20 @@ public partial class Workspaces : ComponentBase
private async Task AddChat(string workspacePath) private async Task AddChat(string workspacePath)
{ {
// Check if the chat has unsaved changes:
if (await MessageBus.INSTANCE.SendMessageUseFirstResult<bool, bool>(this, Event.HAS_CHAT_UNSAVED_CHANGES))
{
var dialogParameters = new DialogParameters
{
{ "Message", "Are you sure you want to create a another chat? All unsaved changes will be lost." },
};
var dialogReference = await this.DialogService.ShowAsync<ConfirmDialog>("Create Chat", dialogParameters, DialogOptions.FULLSCREEN);
var dialogResult = await dialogReference.Result;
if (dialogResult.Canceled)
return;
}
var workspaceId = Guid.Parse(Path.GetFileName(workspacePath)); var workspaceId = Guid.Parse(Path.GetFileName(workspacePath));
var chat = new ChatThread var chat = new ChatThread
{ {

View File

@ -2,6 +2,8 @@
@using AIStudio.Chat @using AIStudio.Chat
@using AIStudio.Settings @using AIStudio.Settings
@inherits AIStudio.Tools.MSGComponentBase
<MudText Typo="Typo.h3" Class="mb-2 mr-3"> <MudText Typo="Typo.h3" Class="mb-2 mr-3">
@if (this.chatThread is not null && this.chatThread.WorkspaceId != Guid.Empty) @if (this.chatThread is not null && this.chatThread.WorkspaceId != Guid.Empty)
{ {

View File

@ -3,6 +3,7 @@ using AIStudio.Components.Blocks;
using AIStudio.Components.CommonDialogs; using AIStudio.Components.CommonDialogs;
using AIStudio.Provider; using AIStudio.Provider;
using AIStudio.Settings; using AIStudio.Settings;
using AIStudio.Tools;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Components.Web;
@ -14,7 +15,7 @@ namespace AIStudio.Components.Pages;
/// <summary> /// <summary>
/// The chat page. /// The chat page.
/// </summary> /// </summary>
public partial class Chat : ComponentBase, IAsyncDisposable public partial class Chat : MSGComponentBase, IAsyncDisposable
{ {
[Inject] [Inject]
private SettingsManager SettingsManager { get; set; } = null!; private SettingsManager SettingsManager { get; set; } = null!;
@ -50,6 +51,8 @@ public partial class Chat : ComponentBase, IAsyncDisposable
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
this.ApplyFilters([], [ Event.HAS_CHAT_UNSAVED_CHANGES ]);
// Configure the spellchecking for the user input: // Configure the spellchecking for the user input:
this.SettingsManager.InjectSpellchecking(USER_INPUT_ATTRIBUTES); this.SettingsManager.InjectSpellchecking(USER_INPUT_ATTRIBUTES);
@ -332,6 +335,29 @@ public partial class Chat : ComponentBase, IAsyncDisposable
await this.inputField.Clear(); await this.inputField.Clear();
} }
#region Overrides of MSGComponentBase
public override Task ProcessMessage<T>(ComponentBase? sendingComponent, Event triggeredEvent, T? data) where T : default
{
return Task.CompletedTask;
}
public override Task<TResult?> ProcessMessageWithResult<TPayload, TResult>(ComponentBase? sendingComponent, Event triggeredEvent, TPayload? data) where TResult : default where TPayload : default
{
switch (triggeredEvent)
{
case Event.HAS_CHAT_UNSAVED_CHANGES:
if(this.SettingsManager.ConfigurationData.WorkspaceStorageBehavior is WorkspaceStorageBehavior.STORE_CHATS_AUTOMATICALLY)
return Task.FromResult((TResult?) (object) false);
return Task.FromResult((TResult?)(object)this.hasUnsavedChanges);
}
return Task.FromResult(default(TResult));
}
#endregion
#region Implementation of IAsyncDisposable #region Implementation of IAsyncDisposable
public async ValueTask DisposeAsync() public async ValueTask DisposeAsync()