Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Fix AliasVault.Client issue when creating new item when already on the item AddEdit page

Leendert de Borstcb90dc11999f
diff --git a/apps/server/AliasVault.Client/Main/Components/Widgets/CreateNewIdentityWidget.razor b/apps/server/AliasVault.Client/Main/Components/Widgets/CreateNewIdentityWidget.razor
index 5bf07af17..807c0edcc 100644
--- a/apps/server/AliasVault.Client/Main/Components/Widgets/CreateNewIdentityWidget.razor
+++ b/apps/server/AliasVault.Client/Main/Components/Widgets/CreateNewIdentityWidget.razor
@@ -282,6 +282,12 @@
QuickCreateStateService.FolderId = GetCurrentFolderIdFromUrl();
NavigationManager.NavigateTo("/items/create");
+
+ // Notify subscribers (e.g. AddEdit page) that the state has changed. This handles
+ // the case where the user is already on the AddEdit page and the navigation above
+ // does not trigger a re-initialization.
+ QuickCreateStateService.NotifyStateChanged();
+
ClosePopup();
}
diff --git a/apps/server/AliasVault.Client/Main/Pages/Items/AddEdit.razor b/apps/server/AliasVault.Client/Main/Pages/Items/AddEdit.razor
index d157b8d71..b5d0a250b 100644
--- a/apps/server/AliasVault.Client/Main/Pages/Items/AddEdit.razor
+++ b/apps/server/AliasVault.Client/Main/Pages/Items/AddEdit.razor
@@ -457,6 +457,7 @@ else
/// <inheritdoc />
async ValueTask IAsyncDisposable.DisposeAsync()
{
+ QuickCreateStateService.OnChange -= OnQuickCreateStateChanged;
await KeyboardShortcutService.UnregisterShortcutAsync("gc");
if (Module is not null)
{
@@ -502,34 +503,17 @@ else
{
Module = await JSRuntime.InvokeAsync<IJSObjectReference>("import", "./js/modules/newIdentityWidget.js");
+ // Subscribe to quick create state changes so we can reinitialize
+ // when the user triggers the widget while already on this page.
+ QuickCreateStateService.OnChange += OnQuickCreateStateChanged;
+
if (EditMode)
{
await LoadExistingCredential();
}
else
{
- CreateNewCredential();
-
- // Use the state service to pre-fill form data
- if (!string.IsNullOrEmpty(QuickCreateStateService.ServiceName))
- {
- Obj.ServiceName = QuickCreateStateService.ServiceName;
- }
- if (!string.IsNullOrEmpty(QuickCreateStateService.ServiceUrl))
- {
- Obj.SetFieldValue(FieldKey.LoginUrl, QuickCreateStateService.ServiceUrl);
- }
- if (!string.IsNullOrEmpty(QuickCreateStateService.ItemType))
- {
- await HandleItemTypeChange(QuickCreateStateService.ItemType);
- }
- if (QuickCreateStateService.FolderId.HasValue)
- {
- Obj.FolderId = QuickCreateStateService.FolderId;
- }
-
- // Clear the state after using it
- QuickCreateStateService.ClearState();
+ await ApplyQuickCreateState();
}
Loading = false;
@@ -542,6 +526,60 @@ else
}
}
+ /// <summary>
+ /// Handles the quick create state change event when the user triggers the
+ /// CreateNewIdentityWidget while already on the AddEdit page.
+ /// </summary>
+ private async void OnQuickCreateStateChanged()
+ {
+ await InvokeAsync(async () =>
+ {
+ // Ensure we are in create mode since quick create always creates new items.
+ EditMode = false;
+
+ await ApplyQuickCreateState();
+ Loading = false;
+ StateHasChanged();
+ await JsInteropService.FocusElementById("service-name");
+ });
+ }
+
+ /// <summary>
+ /// Creates a new credential and applies any prefilled data from the QuickCreateStateService.
+ /// </summary>
+ private async Task ApplyQuickCreateState()
+ {
+ CreateNewCredential();
+
+ // Reset manually added fields tracking for the fresh form.
+ ManuallyAddedFields = [];
+ InitiallyVisibleFields = [];
+
+ // Use the state service to pre-fill form data
+ if (!string.IsNullOrEmpty(QuickCreateStateService.ServiceName))
+ {
+ Obj.ServiceName = QuickCreateStateService.ServiceName;
+ }
+
+ if (!string.IsNullOrEmpty(QuickCreateStateService.ServiceUrl))
+ {
+ Obj.SetFieldValue(FieldKey.LoginUrl, QuickCreateStateService.ServiceUrl);
+ }
+
+ if (!string.IsNullOrEmpty(QuickCreateStateService.ItemType))
+ {
+ await HandleItemTypeChange(QuickCreateStateService.ItemType);
+ }
+
+ if (QuickCreateStateService.FolderId.HasValue)
+ {
+ Obj.FolderId = QuickCreateStateService.FolderId;
+ }
+
+ // Clear the state after using it
+ QuickCreateStateService.ClearState();
+ }
+
/// <summary>
/// Checks if the current item type has login-related fields.
/// </summary>
diff --git a/apps/server/AliasVault.Client/Services/QuickCreateStateService.cs b/apps/server/AliasVault.Client/Services/QuickCreateStateService.cs
index 776da521f..fa20c6a8d 100644
--- a/apps/server/AliasVault.Client/Services/QuickCreateStateService.cs
+++ b/apps/server/AliasVault.Client/Services/QuickCreateStateService.cs
@@ -12,6 +12,11 @@ namespace AliasVault.Client.Services;
/// </summary>
public class QuickCreateStateService
{
+ /// <summary>
+ /// Event that is raised when the state has been updated and the consumer should reinitialize.
+ /// </summary>
+ public event Action? OnChange;
+
/// <summary>
/// Gets or sets the service name from quick create.
/// </summary>
@@ -32,6 +37,11 @@ public class QuickCreateStateService
/// </summary>
public Guid? FolderId { get; set; }
+ /// <summary>
+ /// Notifies subscribers that the state has changed.
+ /// </summary>
+ public void NotifyStateChanged() => OnChange?.Invoke();
+
/// <summary>
/// Clears the stored state.
/// </summary>