Skip to content

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

Add DbStatus indicator to UI (#55)

Leendert de Borst4a586cf11761
diff --git a/src/AliasVault.WebApp/AliasVault.WebApp.csproj b/src/AliasVault.WebApp/AliasVault.WebApp.csproj
index 4411ae927..a9fd2d83f 100644
--- a/src/AliasVault.WebApp/AliasVault.WebApp.csproj
+++ b/src/AliasVault.WebApp/AliasVault.WebApp.csproj
@@ -60,6 +60,9 @@
</ItemGroup>
<ItemGroup>
+ <Content Update="Layout\ClientDbStatusIndicator.razor">
+ <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
+ </Content>
<Content Update="wwwroot\appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
diff --git a/src/AliasVault.WebApp/Auth/Pages/Unlock.razor b/src/AliasVault.WebApp/Auth/Pages/Unlock.razor
index 41f26bda9..84eb269fb 100644
--- a/src/AliasVault.WebApp/Auth/Pages/Unlock.razor
+++ b/src/AliasVault.WebApp/Auth/Pages/Unlock.razor
@@ -5,7 +5,7 @@
@inject NavigationManager NavigationManager
@inject AuthService AuthService
@inject GlobalNotificationService GlobalNotificationService
-@inject AliasClientDbService AliasClientDbService
+@inject DbService DbService
@inject ILocalStorageService LocalStorage
@using System.Text.Json
@using AliasVault.Shared.Models
@@ -145,7 +145,7 @@
AuthService.StoreEncryptionKey(passwordHash);
// Try to retrieve vault again from remote.
- await AliasClientDbService.GetDbContextAsync();
+ await DbService.GetDbContextAsync();
// Redirect to home page.
await AuthStateProvider.GetAuthenticationStateAsync();
diff --git a/src/AliasVault.WebApp/Components/Loading/SmallLoadingIndicator.razor b/src/AliasVault.WebApp/Components/Loading/SmallLoadingIndicator.razor
new file mode 100644
index 000000000..c6e69416e
--- /dev/null
+++ b/src/AliasVault.WebApp/Components/Loading/SmallLoadingIndicator.razor
@@ -0,0 +1,7 @@
+<div role="status" class="px-2 mt-2">
+ <svg aria-hidden="true" class="inline w-8 h-8 text-gray-200 animate-spin dark:text-gray-600 fill-primary-600" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
+ <path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor" />
+ <path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill" />
+ </svg>
+ <span class="sr-only">Loading...</span>
+</div>
diff --git a/src/AliasVault.WebApp/Layout/ClientDbStatusIndicator.razor b/src/AliasVault.WebApp/Layout/ClientDbStatusIndicator.razor
new file mode 100644
index 000000000..d4eb3ca6e
--- /dev/null
+++ b/src/AliasVault.WebApp/Layout/ClientDbStatusIndicator.razor
@@ -0,0 +1,57 @@
+@using AliasVault.WebApp.Services.Database
+@implements IDisposable
+@inject DbService DbService
+
+@if (Loading)
+{
+ <div class="flex items-center justify-center">
+ <SmallLoadingIndicator />
+ <p class="ml-2">@Message</p>
+ </div>
+}
+
+<!--
+<p>Message: @DbService.GetState().CurrentState.Message</p>
+<p>Last Updated: @DbService.GetState().CurrentState.LastUpdated</p>
+-->
+
+@code {
+ private bool Loading { get; set; } = false;
+ private string Message { get; set; } = "";
+ private bool _isSaving => DbService.GetState().CurrentState.Status == DbServiceState.DatabaseStatus.Saving;
+
+ protected override void OnInitialized()
+ {
+ DbService.GetState().StateChanged += OnDatabaseStateChanged;
+ }
+
+ private async void OnDatabaseStateChanged(object? sender, DbServiceState.DatabaseState newState)
+ {
+ await InvokeAsync(StateHasChanged);
+ if (newState.Status == DbServiceState.DatabaseStatus.Saving)
+ {
+ // Show loading indicator for at least 0.5 seconds even if the save operation is faster.
+ Message = "Saving...";
+ await ShowLoadingIndicatorAsync();
+ }
+ else if (newState.Status == DbServiceState.DatabaseStatus.Loading)
+ {
+ Message = "Loading...";
+ await ShowLoadingIndicatorAsync();
+ }
+ }
+
+ private async Task ShowLoadingIndicatorAsync()
+ {
+ Loading = true;
+ StateHasChanged();
+ await Task.Delay(500);
+ Loading = false;
+ StateHasChanged();
+ }
+
+ public void Dispose()
+ {
+ DbService.GetState().StateChanged -= OnDatabaseStateChanged;
+ }
+}
diff --git a/src/AliasVault.WebApp/Layout/ClientDbStatusIndicator.razor.css b/src/AliasVault.WebApp/Layout/ClientDbStatusIndicator.razor.css
new file mode 100644
index 000000000..881d128a5
--- /dev/null
+++ b/src/AliasVault.WebApp/Layout/ClientDbStatusIndicator.razor.css
@@ -0,0 +1,83 @@
+.navbar-toggler {
+ background-color: rgba(255, 255, 255, 0.1);
+}
+
+.top-row {
+ height: 3.5rem;
+ background-color: rgba(0,0,0,0.4);
+}
+
+.navbar-brand {
+ font-size: 1.1rem;
+}
+
+.bi {
+ display: inline-block;
+ position: relative;
+ width: 1.25rem;
+ height: 1.25rem;
+ margin-right: 0.75rem;
+ top: -1px;
+ background-size: cover;
+}
+
+.bi-house-door-fill-nav-menu {
+ background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='white' class='bi bi-house-door-fill' viewBox='0 0 16 16'%3E%3Cpath d='M6.5 14.5v-3.505c0-.245.25-.495.5-.495h2c.25 0 .5.25.5.5v3.5a.5.5 0 0 0 .5.5h4a.5.5 0 0 0 .5-.5v-7a.5.5 0 0 0-.146-.354L13 5.793V2.5a.5.5 0 0 0-.5-.5h-1a.5.5 0 0 0-.5.5v1.293L8.354 1.146a.5.5 0 0 0-.708 0l-6 6A.5.5 0 0 0 1.5 7.5v7a.5.5 0 0 0 .5.5h4a.5.5 0 0 0 .5-.5Z'/%3E%3C/svg%3E");
+}
+
+.bi-plus-square-fill-nav-menu {
+ background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='white' class='bi bi-plus-square-fill' viewBox='0 0 16 16'%3E%3Cpath d='M2 0a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2H2zm6.5 4.5v3h3a.5.5 0 0 1 0 1h-3v3a.5.5 0 0 1-1 0v-3h-3a.5.5 0 0 1 0-1h3v-3a.5.5 0 0 1 1 0z'/%3E%3C/svg%3E");
+}
+
+.bi-list-nested-nav-menu {
+ background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='white' class='bi bi-list-nested' viewBox='0 0 16 16'%3E%3Cpath fill-rule='evenodd' d='M4.5 11.5A.5.5 0 0 1 5 11h10a.5.5 0 0 1 0 1H5a.5.5 0 0 1-.5-.5zm-2-4A.5.5 0 0 1 3 7h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5zm-2-4A.5.5 0 0 1 1 3h10a.5.5 0 0 1 0 1H1a.5.5 0 0 1-.5-.5z'/%3E%3C/svg%3E");
+}
+
+.nav-item {
+ font-size: 0.9rem;
+ padding-bottom: 0.5rem;
+}
+
+ .nav-item:first-of-type {
+ padding-top: 1rem;
+ }
+
+ .nav-item:last-of-type {
+ padding-bottom: 1rem;
+ }
+
+ .nav-item ::deep a {
+ color: #d7d7d7;
+ border-radius: 4px;
+ height: 3rem;
+ display: flex;
+ align-items: center;
+ line-height: 3rem;
+ }
+
+.nav-item ::deep a.active {
+ background-color: rgba(255,255,255,0.37);
+ color: white;
+}
+
+.nav-item ::deep a:hover {
+ background-color: rgba(255,255,255,0.1);
+ color: white;
+}
+
+@media (min-width: 641px) {
+ .navbar-toggler {
+ display: none;
+ }
+
+ .collapse {
+ /* Never collapse the sidebar for wide screens */
+ display: block;
+ }
+
+ .nav-scrollable {
+ /* Allow sidebar to scroll for tall menus */
+ height: calc(100vh - 3.5rem);
+ overflow-y: auto;
+ }
+}
diff --git a/src/AliasVault.WebApp/Layout/TopMenu.razor b/src/AliasVault.WebApp/Layout/TopMenu.razor
index b459eeaf8..fa39d6468 100644
--- a/src/AliasVault.WebApp/Layout/TopMenu.razor
+++ b/src/AliasVault.WebApp/Layout/TopMenu.razor
@@ -24,7 +24,9 @@
</ul>
</div>
</div>
- <div class="flex justify-between items-center lg:order-2">
+ <div class="flex justify-end items-center lg:order-2">
+ <ClientDbStatusIndicator />
+
<div class="mr-3 -mb-1 hidden sm:block">
<span></span>
</div>
diff --git a/src/AliasVault.WebApp/Pages/Base/PageBase.cs b/src/AliasVault.WebApp/Pages/Base/PageBase.cs
index 293dcc2ab..90862f72a 100644
--- a/src/AliasVault.WebApp/Pages/Base/PageBase.cs
+++ b/src/AliasVault.WebApp/Pages/Base/PageBase.cs
@@ -10,6 +10,7 @@ namespace AliasVault.WebApp.Pages.Base;
using AliasVault.WebApp.Auth.Services;
using AliasVault.WebApp.Components.Models;
using AliasVault.WebApp.Services;
+using AliasVault.WebApp.Services.Database;
using Blazored.LocalStorage;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
@@ -49,10 +50,10 @@ public class PageBase : OwningComponentBase
public IJSRuntime Js { get; set; } = null!;
/// <summary>
- /// Gets or sets the AliasClientDbService.
+ /// Gets or sets the DbService.
/// </summary>
[Inject]
- public AliasClientDbService AliasClientDbService { get; set; } = null!;
+ public DbService DbService { get; set; } = null!;
/// <summary>
/// Gets or sets the AuthService.
diff --git a/src/AliasVault.WebApp/Pages/Dbtest.razor b/src/AliasVault.WebApp/Pages/Dbtest.razor
index 0ea294b66..ffdec555d 100644
--- a/src/AliasVault.WebApp/Pages/Dbtest.razor
+++ b/src/AliasVault.WebApp/Pages/Dbtest.razor
@@ -22,14 +22,12 @@
}
<button class="w-full px-5 py-3 text-base font-medium text-center text-white bg-primary-700 rounded-lg hover:bg-primary-800 focus:ring-4 focus:ring-primary-300 sm:w-auto dark:bg-primary-600 dark:hover:bg-primary-700 dark:focus:ring-primary-800" @onclick="AddRecord">Add new record</button>
-<button class="w-full px-5 py-3 text-base font-medium text-center text-white bg-primary-700 rounded-lg hover:bg-primary-800 focus:ring-4 focus:ring-primary-300 sm:w-auto dark:bg-primary-600 dark:hover:bg-primary-700 dark:focus:ring-primary-800" @onclick="LoadAliasFromDbAsync">Load from db again</button>
-<button class="w-full px-5 py-3 text-base font-medium text-center text-white bg-primary-700 rounded-lg hover:bg-primary-800 focus:ring-4 focus:ring-primary-300 sm:w-auto dark:bg-primary-600 dark:hover:bg-primary-700 dark:focus:ring-primary-800" @onclick="ExportDbToString">Save DB to remote</button>
-<button class="w-full px-5 py-3 text-base font-medium text-center text-white bg-primary-700 rounded-lg hover:bg-primary-800 focus:ring-4 focus:ring-primary-300 sm:w-auto dark:bg-primary-600 dark:hover:bg-primary-700 dark:focus:ring-primary-800" @onclick="SaveDb">SaveChanges</button>
<div class="grid gap-4 px-4 mb-4 md:grid-cols-4 xl:grid-cols-6">
@foreach (var password in Passwords)
{
<div>
+ <button @onclick="() => RemovePassword(password)">Remove</button>
<p>@password.CreatedAt</p>
<p>@password.Value</p>
</div>
@@ -40,6 +38,15 @@
@code {
private bool IsLoading { get; set; } = true;
private List<Password> Passwords { get; set; } = new();
+ private AliasClientDbContext dbContext = new();
+
+ private async Task RemovePassword(Password password)
+ {
+ var db = await DbService.GetDbContextAsync();
+ db.Passwords.Remove(password);
+ await DbService.SaveDatabaseAsync();
+ await RefreshAliasFromDbAsync();
+ }
/// <inheritdoc />
protected override async Task OnAfterRenderAsync(bool firstRender)
@@ -48,22 +55,20 @@
if (firstRender)
{
- await LoadAliasFromDbAsync();
+ await RefreshAliasFromDbAsync();
}
}
- private async Task LoadAliasFromDbAsync()
+ private async Task RefreshAliasFromDbAsync()
{
IsLoading = true;
StateHasChanged();
- var db = await AliasClientDbService.GetDbContextAsync();
+ var db = await DbService.GetDbContextAsync();
// Load all passwords from the database
var passwords = await db.Passwords.ToListAsync();
- // Show them on page
-
Passwords = passwords;
IsLoading = false;
StateHasChanged();
@@ -71,24 +76,13 @@
private async Task AddRecord()
{
- var db = await AliasClientDbService.GetDbContextAsync();
+ var db = await DbService.GetDbContextAsync();
// Insert row in database
db.Passwords.Add(new Password() { Id = Guid.NewGuid(), Value = "Test factory insert SQLite", CreatedAt = DateTime.Now });
- // Save changes
- await db.SaveChangesAsync();
-
- await LoadAliasFromDbAsync();
- }
-
- private async Task ExportDbToString()
- {
- await AliasClientDbService.SaveDatabaseAsync();
- }
-
- private async Task SaveDb()
- {
- await (await AliasClientDbService.GetDbContextAsync()).SaveChangesAsync();
+ // Save changes and upload db to remote.
+ await DbService.SaveDatabaseAsync();
+ await RefreshAliasFromDbAsync();
}
}
diff --git a/src/AliasVault.WebApp/Program.cs b/src/AliasVault.WebApp/Program.cs
index bf9d73270..05d87887d 100644
--- a/src/AliasVault.WebApp/Program.cs
+++ b/src/AliasVault.WebApp/Program.cs
@@ -9,6 +9,7 @@ using AliasVault.WebApp;
using AliasVault.WebApp.Auth.Providers;
using AliasVault.WebApp.Auth.Services;
using AliasVault.WebApp.Services;
+using AliasVault.WebApp.Services.Database;
using Blazored.LocalStorage;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Web;
@@ -36,7 +37,7 @@ builder.Services.AddTransient<AliasVaultApiHandlerService>();
builder.Services.AddScoped<AuthService>();
builder.Services.AddScoped<AuthenticationStateProvider, AuthStateProvider>();
builder.Services.AddScoped<AliasService>();
-builder.Services.AddScoped<AliasClientDbService>();
+builder.Services.AddScoped<DbService>();
builder.Services.AddScoped<GlobalNotificationService>();
builder.Services.AddSingleton<ClipboardCopyService>();
diff --git a/src/AliasVault.WebApp/Services/AliasClientDbService.cs b/src/AliasVault.WebApp/Services/Database/DbService.cs
similarity index 84%
rename from src/AliasVault.WebApp/Services/AliasClientDbService.cs
rename to src/AliasVault.WebApp/Services/Database/DbService.cs
index 8c8dd0f54..f12ffb0eb 100644
--- a/src/AliasVault.WebApp/Services/AliasClientDbService.cs
+++ b/src/AliasVault.WebApp/Services/Database/DbService.cs
@@ -1,18 +1,17 @@
//-----------------------------------------------------------------------
-// <copyright file="AliasClientDbService.cs" company="lanedirt">
+// <copyright file="DbService.cs" company="lanedirt">
// Copyright (c) lanedirt. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
// </copyright>
//-----------------------------------------------------------------------
-namespace AliasVault.WebApp.Services;
+namespace AliasVault.WebApp.Services.Database;
using System.Data;
using System.Net.Http.Json;
using AliasClientDb;
using AliasVault.Shared.Models.WebApi;
using AliasVault.WebApp.Auth.Services;
-using Microsoft.AspNetCore.Components;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.JSInterop;
@@ -22,32 +21,45 @@ using Microsoft.JSInterop;
/// with a AliasClientDb database instance that is only persisted in memory due to the encryption requirements of the
/// database itself. The database should not be persisted to disk when in un-encrypted form.
/// </summary>
-public class AliasClientDbService
+public class DbService
{
private readonly AuthService _authService;
private readonly IJSRuntime _jsRuntime;
private readonly HttpClient _httpClient;
+ private readonly DbServiceState _state = new();
private AliasClientDbContext? _dbContext;
private Task _initializationTask;
private bool _isSuccessfullyInitialized;
private int _retryCount;
/// <summary>
- /// Initializes a new instance of the <see cref="AliasClientDbService"/> class.
+ /// Initializes a new instance of the <see cref="DbService"/> class.
/// </summary>
/// <param name="authService">AuthService.</param>
/// <param name="jsRuntime">IJSRuntime.</param>
/// <param name="httpClient">HttpClient.</param>
- public AliasClientDbService(AuthService authService, IJSRuntime jsRuntime, HttpClient httpClient)
+ public DbService(AuthService authService, IJSRuntime jsRuntime, HttpClient httpClient)
{
_authService = authService;
_jsRuntime = jsRuntime;
_httpClient = httpClient;
+ // Set the initial state of the database service.
+ _state.UpdateState(DbServiceState.DatabaseStatus.Idle);
+
// Initialize the database asynchronously
_initializationTask = InitializeDatabaseAsync();
}
+ /// <summary>
+ /// Gets database service state object which can be subscribed to.
+ /// </summary>
+ /// <returns>DbServiceState instance.</returns>
+ public DbServiceState GetState()
+ {
+ return _state;
+ }
+
/// <summary>
/// Ensures that the service initialization is complete before proceeding.
/// </summary>
@@ -91,46 +103,58 @@ public class AliasClientDbService
{
await EnsureInitializedAsync();
- using var memoryStream = new MemoryStream();
- using var connection = new SqliteConnection(_dbContext!.Database.GetDbConnection().ConnectionString);
- await connection.OpenAsync();
- using var command = connection.CreateCommand();
- command.CommandText = "VACUUM main INTO @fileName";
+ // Set the initial state of the database service.
+ _state.UpdateState(DbServiceState.DatabaseStatus.Saving);
- var tempFileName = Path.GetRandomFileName();
- command.Parameters.Add(new SqliteParameter("@fileName", tempFileName));
- await command.ExecuteNonQueryAsync();
-
- var bytes = await File.ReadAllBytesAsync(tempFileName);
+ // Save the actual dbContext.
+ await _dbContext!.SaveChangesAsync();
- string base64String = Convert.ToBase64String(bytes);
- File.Delete(tempFileName);
+ string base64String = await ExportSqliteToBase64Async();
- // Encrypt base64 string.
- // Encrypt using IJSInterop
- Console.WriteLine("Encrypted using key: " + _authService.GetEncryptionKeyAsBase64Async());
+ // Encrypt base64 string using IJSInterop.
string encryptedBase64String = await _jsRuntime.InvokeAsync<string>("cryptoInterop.encrypt", base64String, _authService.GetEncryptionKeyAsBase64Async());
- // Decrypt it again
- string decryptedBase64String = await _jsRuntime.InvokeAsync<string>("cryptoInterop.decrypt", encryptedBase64String, _authService.GetEncryptionKeyAsBase64Async());
-
- // Print original, encrypted and decrypted sting to console
- Console.WriteLine("Original: " + base64String);
- Console.WriteLine("Encrypted: " + encryptedBase64String);
- Console.WriteLine("Decrypted: " + decryptedBase64String);
-
// Save to webapi.
var success = await SaveToServerAsync(encryptedBase64String);
if (success)
{
Console.WriteLine("Database succesfully saved to server.");
+ _state.UpdateState(DbServiceState.DatabaseStatus.Idle);
}
else
{
Console.WriteLine("Failed to save database to server.");
+ _state.UpdateState(DbServiceState.DatabaseStatus.Error);
}
}
+ /// <summary>
+ /// Export the in-memory SQLite database to a base64 string.
+ /// </summary>
+ /// <returns>Base64 encoded string that represents SQLite database.</returns>
+ public async Task<string> ExportSqliteToBase64Async()
+ {
+ var tempFileName = Path.GetRandomFileName();
+
+ // Export SQLite memory database to a temp file.
+ using var memoryStream = new MemoryStream();
+ using var connection = new SqliteConnection(_dbContext!.Database.GetDbConnection().ConnectionString);
+ await connection.OpenAsync();
+ using var command = connection.CreateCommand();
+ command.CommandText = "VACUUM main INTO @fileName";
+ command.Parameters.Add(new SqliteParameter("@fileName", tempFileName));
+ await command.ExecuteNonQueryAsync();
+
+ // Get bytes.
+ var bytes = await File.ReadAllBytesAsync(tempFileName);
+ string base64String = Convert.ToBase64String(bytes);
+
+ // Delete temp file.
+ File.Delete(tempFileName);
+
+ return base64String;
+ }
+
private static async Task ImportDbContextFromBase64Async(AliasClientDbContext dbContext, string base64String)
{
var bytes = Convert.FromBase64String(base64String);
@@ -202,6 +226,8 @@ public class AliasClientDbService
return;
}
+ _state.UpdateState(DbServiceState.DatabaseStatus.Loading);
+
// Create a new in-memory database.
string connectionString = "Data Source=AliasClientDb.sqlite";
using var connection = new SqliteConnection(connectionString);
@@ -221,10 +247,12 @@ public class AliasClientDbService
if (loaded)
{
_isSuccessfullyInitialized = true;
+ _state.UpdateState(DbServiceState.DatabaseStatus.Idle);
Console.WriteLine("Database succesfully loaded from server.");
}
else
{
+ _state.UpdateState(DbServiceState.DatabaseStatus.Error);
Console.WriteLine("Failed to load database from server.");
}
}
@@ -284,7 +312,7 @@ public class AliasClientDbService
try
{
- await _httpClient.PostAsJsonAsync<Vault>("api/v1/Vault", vaultObject);
+ await _httpClient.PostAsJsonAsync("api/v1/Vault", vaultObject);
return true;
}
catch
diff --git a/src/AliasVault.WebApp/Services/Database/DbServiceState.cs b/src/AliasVault.WebApp/Services/Database/DbServiceState.cs
new file mode 100644
index 000000000..8f35343ba
--- /dev/null
+++ b/src/AliasVault.WebApp/Services/Database/DbServiceState.cs
@@ -0,0 +1,122 @@
+//-----------------------------------------------------------------------
+// <copyright file="DbServiceState.cs" company="lanedirt">
+// Copyright (c) lanedirt. All rights reserved.
+// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace AliasVault.WebApp.Services.Database;
+
+/// <summary>
+/// Class to manage the state of the AliasClientDbService that others can subscribe to events for.
+/// </summary>
+public class DbServiceState
+{
+ private DatabaseState _currentState = new();
+
+ /// <summary>
+ /// Subscribe to this event to get notified when the state of the database changes.
+ /// </summary>
+ public event EventHandler<DatabaseState> StateChanged = (sender, e) => { };
+
+ /// <summary>
+ /// Database status enum.
+ /// </summary>
+ public enum DatabaseStatus
+ {
+ /// <summary>
+ /// No database operation is in progress.
+ /// </summary>
+ Idle,
+
+ /// <summary>
+ /// Database is loading from server.
+ /// </summary>
+ Loading,
+
+ /// <summary>
+ /// Database is saving to server.
+ /// </summary>
+ Saving,
+
+ /// <summary>
+ /// An error occurred during a database operation.
+ /// </summary>
+ Error,
+ }
+
+ /// <summary>
+ /// Gets the current state of the database.
+ /// </summary>
+ public DatabaseState CurrentState
+ {
+ get => _currentState;
+ private set
+ {
+ if (_currentState != value)
+ {
+ _currentState = value;
+ OnStateChanged(_currentState);
+ }
+ }
+ }
+
+ /// <summary>
+ /// Update the state of the database.
+ /// </summary>
+ /// <param name="status">New status.</param>
+ public void UpdateState(DatabaseStatus status)
+ {
+ CurrentState = new DatabaseState
+ {
+ Status = status,
+ Message = string.Empty,
+ LastUpdated = DateTime.Now,
+ };
+ }
+
+ /// <summary>
+ /// Update the state of the database with an additional message.
+ /// </summary>
+ /// <param name="status">New status.</param>
+ /// <param name="message">Status message.</param>
+ public void UpdateState(DatabaseStatus status, string message)
+ {
+ CurrentState = new DatabaseState
+ {
+ Status = status,
+ Message = message,
+ LastUpdated = DateTime.Now,
+ };
+ }
+
+ /// <summary>
+ /// OnStateChanged event handler.
+ /// </summary>
+ /// <param name="newState">The new state.</param>
+ protected virtual void OnStateChanged(DatabaseState newState)
+ {
+ StateChanged?.Invoke(this, newState);
+ }
+
+ /// <summary>
+ /// Database state class.
+ /// </summary>
+ public class DatabaseState
+ {
+ /// <summary>
+ /// Gets or sets the current status of the database.
+ /// </summary>
+ public DatabaseStatus Status { get; set; } = DatabaseStatus.Idle;
+
+ /// <summary>
+ /// Gets or sets the message associated with the current status.
+ /// </summary>
+ public string Message { get; set; } = string.Empty;
+
+ /// <summary>
+ /// Gets or sets the last time the state was updated.
+ /// </summary>
+ public DateTime LastUpdated { get; set; } = DateTime.Now;
+ }
+}
diff --git a/src/AliasVault.WebApp/_Imports.razor b/src/AliasVault.WebApp/_Imports.razor
index 3b0f46fa9..bae80ee01 100644
--- a/src/AliasVault.WebApp/_Imports.razor
+++ b/src/AliasVault.WebApp/_Imports.razor
@@ -17,6 +17,7 @@
@using AliasVault.WebApp.Components.Loading
@using AliasVault.WebApp.Pages.Base
@using AliasVault.WebApp.Services
+@using AliasVault.WebApp.Services.Database
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using Blazored.LocalStorage
diff --git a/src/Tests/AliasVault.E2ETests/Tests/UnlockTests.cs b/src/Tests/AliasVault.E2ETests/Tests/UnlockTests.cs
index 356608a14..f5cb928ee 100644
--- a/src/Tests/AliasVault.E2ETests/Tests/UnlockTests.cs
+++ b/src/Tests/AliasVault.E2ETests/Tests/UnlockTests.cs
@@ -14,8 +14,6 @@ namespace AliasVault.E2ETests.Tests;
[TestFixture]
public class UnlockTests : PlaywrightTest
{
- private static readonly Random Random = new();
-
/// <summary>
/// Test that the unlock page is displayed after hard refresh which should
/// clear the encryption key from memory.