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 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<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()
{
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<T>(ComponentBase? sendingComponent, Event triggeredEvent, T? data) where T : default
{
switch (triggeredEvent)
{
case Event.RESET_CHAT_STATE:
this.ResetState();
break;
}
return Task.CompletedTask;
}

View File

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