Show a warning when the user leaves the chat page while there are unsaved changes

This commit is contained in:
Thorsten Sommer 2024-07-12 22:31:47 +02:00
parent 152eed6542
commit ba5b109bcf
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
3 changed files with 47 additions and 1 deletions

View File

@ -3,6 +3,7 @@ using AIStudio.Settings;
using AIStudio.Tools; using AIStudio.Tools;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Routing;
using DialogOptions = AIStudio.Components.CommonDialogs.DialogOptions; using DialogOptions = AIStudio.Components.CommonDialogs.DialogOptions;
@ -27,6 +28,9 @@ public partial class MainLayout : LayoutComponentBase, IMessageBusReceiver
[Inject] [Inject]
private ISnackbar Snackbar { get; init; } = null!; private ISnackbar Snackbar { get; init; } = null!;
[Inject]
private NavigationManager NavigationManager { get; init; } = null!;
public string AdditionalHeight { get; private set; } = "0em"; public string AdditionalHeight { get; private set; } = "0em";
@ -40,6 +44,8 @@ public partial class MainLayout : LayoutComponentBase, IMessageBusReceiver
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
this.NavigationManager.RegisterLocationChangingHandler(this.OnLocationChanging);
// //
// We use the Tauri API (Rust) to get the data and config directories // We use the Tauri API (Rust) to get the data and config directories
// for this app. // for this app.
@ -151,4 +157,26 @@ public partial class MainLayout : LayoutComponentBase, IMessageBusReceiver
this.StateHasChanged(); this.StateHasChanged();
await this.Rust.InstallUpdate(this.JsRuntime); await this.Rust.InstallUpdate(this.JsRuntime);
} }
private async ValueTask OnLocationChanging(LocationChangingContext context)
{
if (await MessageBus.INSTANCE.SendMessageUseFirstResult<bool, bool>(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<ConfirmDialog>("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<bool>(this, Event.RESET_CHAT_STATE);
}
}
} }

View File

@ -51,7 +51,7 @@ public partial class Chat : MSGComponentBase, IAsyncDisposable
protected override async Task OnInitializedAsync() 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: // Configure the spellchecking for the user input:
this.SettingsManager.InjectSpellchecking(USER_INPUT_ATTRIBUTES); this.SettingsManager.InjectSpellchecking(USER_INPUT_ATTRIBUTES);
@ -350,10 +350,27 @@ public partial class Chat : MSGComponentBase, IAsyncDisposable
await this.inputField.Clear(); 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 #region Overrides of MSGComponentBase
public override Task ProcessMessage<T>(ComponentBase? sendingComponent, Event triggeredEvent, T? data) where T : default public override Task ProcessMessage<T>(ComponentBase? sendingComponent, Event triggeredEvent, T? data) where T : default
{ {
switch (triggeredEvent)
{
case Event.RESET_CHAT_STATE:
this.ResetState();
break;
}
return Task.CompletedTask; return Task.CompletedTask;
} }

View File

@ -13,4 +13,5 @@ public enum Event
// Chat events: // Chat events:
HAS_CHAT_UNSAVED_CHANGES, HAS_CHAT_UNSAVED_CHANGES,
RESET_CHAT_STATE,
} }