using System.Text.Json; using Microsoft.JSInterop; namespace EnvelopeGenerator.Server.Client.Services; public sealed class CreateEnvelopeBootstrapStorageService(IJSRuntime jsRuntime) { private const string StorageKeyPrefix = "sender.createEnvelope.bootstrap."; private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web); public async Task StoreAsync(CreateEnvelopeBootstrapPayload payload) { var token = Guid.NewGuid().ToString("N"); var key = BuildStorageKey(token); var json = JsonSerializer.Serialize(payload, JsonOptions); await jsRuntime.InvokeVoidAsync("sessionStorage.setItem", key, json); return token; } public async Task ConsumeAsync(string? token) { if (string.IsNullOrWhiteSpace(token)) return null; var key = BuildStorageKey(token); var raw = await jsRuntime.InvokeAsync("sessionStorage.getItem", key); await jsRuntime.InvokeVoidAsync("sessionStorage.removeItem", key); if (string.IsNullOrWhiteSpace(raw)) return null; try { return JsonSerializer.Deserialize(raw, JsonOptions); } catch { return null; } } private static string BuildStorageKey(string token) => $"{StorageKeyPrefix}{token}"; } public sealed record CreateEnvelopeBootstrapPayload( string Title, string Message, int? EnvelopeTypeId, string Language, IReadOnlyList Receivers, byte[]? DocumentBytes, string? DocumentFileName); public sealed record CreateEnvelopeBootstrapReceiver(string FullName, string EmailAddress);