using JobsJobsJobs.Shared; using Microsoft.JSInterop; using NodaTime; using System; using System.Collections.Generic; using System.Net.Http; using System.Reflection; using System.Threading.Tasks; namespace JobsJobsJobs.Client { /// /// Information about a user /// public record UserInfo(CitizenId Id, string Name); /// /// Client-side application state for Jobs, Jobs, Jobs /// public class AppState { /// /// The application version, as a nice display string /// public static Lazy Version => new Lazy(() => { var version = Assembly.GetExecutingAssembly().GetName().Version!; var display = $"v{version.Major}.{version.Minor}"; if (version.Build > 0) display += $".{version.Build}"; return display; }); public event Action OnChange = () => { }; private UserInfo? _user = null; /// /// The information of the currently logged-in user /// public UserInfo? User { get => _user; set { _user = value; NotifyChanged(); } } private string _jwt = ""; /// /// The JSON Web Token (JWT) for the currently logged-on user /// public string Jwt { get => _jwt; set { _jwt = value; NotifyChanged(); } } private IEnumerable? _continents = null; /// /// Get a list of continents (only retrieves once per application load) /// /// The HTTP client to use to obtain continents the first time /// The list of continents /// If the continents cannot be loaded public async Task> GetContinents(HttpClient http) { if (_continents == null) { ServerApi.SetJwt(http, this); var continentResult = await ServerApi.RetrieveMany(http, "continent/all"); if (continentResult.IsOk) { _continents = continentResult.Ok; } else { throw new ApplicationException($"Could not load continents - {continentResult.Error}"); } } return _continents; } private DateTimeZone? _tz = null; /// /// Get the time zone for the current user's browser /// /// The JS interop runtime for the application /// The time zone based on the user's browser public async Task GetTimeZone(IJSRuntime js) { if (_tz == null) { try { _tz = DateTimeZoneProviders.Tzdb.GetZoneOrNull(await js.InvokeAsync("getTimeZone")); } catch (Exception) { } } if (_tz == null) { // Either the zone wasn't found, or the user's browser denied us access to it; there's not much to do // here but set it to UTC and move on _tz = DateTimeZoneProviders.Tzdb.GetZoneOrNull("Etc/UTC")!; } return _tz; } public AppState() { } private void NotifyChanged() => OnChange.Invoke(); } }