From ba5b109bcf2460217938f0bc3eeb5f674e88002e Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Fri, 12 Jul 2024 22:31:47 +0200 Subject: [PATCH] Show a warning when the user leaves the chat page while there are unsaved changes --- .../Components/Layout/MainLayout.razor.cs | 28 +++++++++++++++++++ .../Components/Pages/Chat.razor.cs | 19 ++++++++++++- app/MindWork AI Studio/Tools/Event.cs | 1 + 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/app/MindWork AI Studio/Components/Layout/MainLayout.razor.cs b/app/MindWork AI Studio/Components/Layout/MainLayout.razor.cs index 852d346a..e886937d 100644 --- a/app/MindWork AI Studio/Components/Layout/MainLayout.razor.cs +++ b/app/MindWork AI Studio/Components/Layout/MainLayout.razor.cs @@ -3,6 +3,7 @@ using AIStudio.Settings; using AIStudio.Tools; using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Routing; using DialogOptions = AIStudio.Components.CommonDialogs.DialogOptions; @@ -27,6 +28,9 @@ public partial class MainLayout : LayoutComponentBase, IMessageBusReceiver [Inject] private ISnackbar Snackbar { get; init; } = null!; + + [Inject] + private NavigationManager NavigationManager { get; init; } = null!; public string AdditionalHeight { get; private set; } = "0em"; @@ -40,6 +44,8 @@ public partial class MainLayout : LayoutComponentBase, IMessageBusReceiver protected override async Task OnInitializedAsync() { + this.NavigationManager.RegisterLocationChangingHandler(this.OnLocationChanging); + // // We use the Tauri API (Rust) to get the data and config directories // for this app. @@ -151,4 +157,26 @@ public partial class MainLayout : LayoutComponentBase, IMessageBusReceiver this.StateHasChanged(); await this.Rust.InstallUpdate(this.JsRuntime); } + + private async ValueTask OnLocationChanging(LocationChangingContext context) + { + if (await MessageBus.INSTANCE.SendMessageUseFirstResult(this, Event.HAS_CHAT_UNSAVED_CHANGES)) + { + var dialogParameters = new DialogParameters + { + { "Message", "Are you sure you want to leave the chat page? All unsaved changes will be lost." }, + }; + + var dialogReference = await this.DialogService.ShowAsync("Leave Chat Page", dialogParameters, DialogOptions.FULLSCREEN); + var dialogResult = await dialogReference.Result; + if (dialogResult.Canceled) + { + context.PreventNavigation(); + return; + } + + // User accepted to leave the chat page, reset the chat state: + await MessageBus.INSTANCE.SendMessage(this, Event.RESET_CHAT_STATE); + } + } } \ No newline at end of file diff --git a/app/MindWork AI Studio/Components/Pages/Chat.razor.cs b/app/MindWork AI Studio/Components/Pages/Chat.razor.cs index 0820b8de..67076370 100644 --- a/app/MindWork AI Studio/Components/Pages/Chat.razor.cs +++ b/app/MindWork AI Studio/Components/Pages/Chat.razor.cs @@ -51,7 +51,7 @@ public partial class Chat : MSGComponentBase, IAsyncDisposable protected override async Task OnInitializedAsync() { - this.ApplyFilters([], [ Event.HAS_CHAT_UNSAVED_CHANGES ]); + this.ApplyFilters([], [ Event.HAS_CHAT_UNSAVED_CHANGES, Event.RESET_CHAT_STATE ]); // Configure the spellchecking for the user input: this.SettingsManager.InjectSpellchecking(USER_INPUT_ATTRIBUTES); @@ -350,10 +350,27 @@ public partial class Chat : MSGComponentBase, IAsyncDisposable await this.inputField.Clear(); } + private void ResetState() + { + this.isStreaming = false; + this.hasUnsavedChanges = false; + this.userInput = string.Empty; + this.currentWorkspaceId = Guid.Empty; + this.currentWorkspaceName = string.Empty; + this.chatThread = null; + } + #region Overrides of MSGComponentBase public override Task ProcessMessage(ComponentBase? sendingComponent, Event triggeredEvent, T? data) where T : default { + switch (triggeredEvent) + { + case Event.RESET_CHAT_STATE: + this.ResetState(); + break; + } + return Task.CompletedTask; } diff --git a/app/MindWork AI Studio/Tools/Event.cs b/app/MindWork AI Studio/Tools/Event.cs index 9f55552e..62a6ae93 100644 --- a/app/MindWork AI Studio/Tools/Event.cs +++ b/app/MindWork AI Studio/Tools/Event.cs @@ -13,4 +13,5 @@ public enum Event // Chat events: HAS_CHAT_UNSAVED_CHANGES, + RESET_CHAT_STATE, } \ No newline at end of file