using System.Text.Json; using System.Text.Json.Serialization; using Microsoft.JSInterop; namespace MesaFabApproval.Client.Services; public interface ILocalStorageService { Task AddItem(string key, string value); Task AddItem(string key, T value); Task RemoveItem(string key); Task GetItem(string key); Task GetItem(string key); } public class LocalStorageService : ILocalStorageService { private readonly IJSRuntime _jsRuntime; public LocalStorageService(IJSRuntime jsRuntime) { _jsRuntime = jsRuntime; } public async Task AddItem(string key, string value) { await _jsRuntime.InvokeVoidAsync("localStorage.setItem", key, value); } public async Task AddItem(string key, T value) { string item = JsonSerializer.Serialize(value); await _jsRuntime.InvokeVoidAsync("localStorage.setItem", key, item); } public async Task RemoveItem(string key) { await _jsRuntime.InvokeVoidAsync("localStorage.removeItem", key); } public async Task GetItem(string key) { return await _jsRuntime.InvokeAsync("localStorage.getItem", key); } public async Task GetItem(string key) { string item = await _jsRuntime.InvokeAsync("localStorage.getItem", key); JsonSerializerOptions jsonSerializerOptions = new() { PropertyNameCaseInsensitive = true }; return JsonSerializer.Deserialize(item, jsonSerializerOptions); } }