@page "/"
@attribute [Authorize]
@using Khaemhat.Application.Licensing
@using Khaemhat.Application.Modules
@using Khaemhat.Core.Security
@using Khaemhat.Application.Instance
@inject ShellSession Session
@inject DashboardService Dashboard
@inject ToastService Toasts
@inject TimeProvider Clock
@* The home page (SPEC §10.3, mockups 004 and 006): the widget dashboard, with the Personalizza panel to switch widgets
on and off, order and size them, choose the start page and, for whoever manages the company, the default of a
role. With the dashboard switched off, the areas the user can open. *@
@Greeting
@T.Format("Pages.Home.Today", Today)
@if (_loaded && ShowDashboard)
{
@T["Dashboard.Customize"]
}
@if (!_loaded)
{
@T["Dashboard.Feed.Loading"]
}
else if (ShowDashboard)
{
@if (_layout.Widgets.Count == 0)
{
@T["Dashboard.Empty"]
}
else
{
@foreach (var placement in _layout.Widgets)
{
if (WidgetCatalog.Find(placement.Id) is { } definition)
{
}
}
}
}
else
{
@T["Dashboard.Off"]
ChangeShowAsync(true)">@T["Dashboard.ShowAgain"]
@T["Pages.Home.LicenseTile"]
@(License.Kind == LicenseKind.Trial ? T["Pages.Home.LicenseTrial"] : T["Pages.Home.LicenseFull"])
@LicenseDetail
@T["Pages.Home.ModulesTile"]
@T.Format("Pages.Home.ModulesText", License.Modules.Count, ModuleRegistry.Default.Modules.Count)
@(License.Modules.Count == 0 ? T["Pages.Home.ModulesNone"] : string.Join(" · ", License.Modules.Order(StringComparer.Ordinal)))
@T["Pages.Home.CompanyTile"]
@(Session.Company?.Name ?? T["Pages.Home.CompanyNone"])
@Session.Company?.Code
@T["Pages.Home.AreasTitle"]
@T["Pages.Home.AreasHint"]
}
@T["Dashboard.Panel.Start"]
@T["Dashboard.Panel.StartPage"]
@T["Pages.Profile.Start.Dashboard"]
@T["Pages.Profile.Start.LastTab"]
@foreach (var item in StartChoices)
{
@T[item.Key]
}
@T["Dashboard.Panel.WidgetsHint"] @SourceText
@foreach (var definition in _available)
{
var on = _layout.Contains(definition.Id);
var title = T[definition.TitleKey];
@if (on)
{
ResizeAsync(definition.Id, e.Value as string)">
@foreach (var width in WidgetDefinition.Widths)
{
@T["Dashboard.Widths." + width]
}
MoveAsync(definition.Id, -1)">
MoveAsync(definition.Id, 1)">
}
}
@if (CanSetRoleDefaults)
{
@T["Dashboard.Panel.Role"]
@T["Dashboard.Panel.RoleHint"]
@if (_roles.Count == 0)
{
@T["Dashboard.Panel.RoleNone"]
}
else
{
@T["Dashboard.Panel.RolePick"]
_role = e.Value as string">
@foreach (var (role, layout) in _roles)
{
@(layout is null ? role.Name : T.Format("Dashboard.Panel.RoleHas", role.Name))
}
@T["Dashboard.Panel.RoleSave"]
@if (SelectedRole is { Layout: not null })
{
@T["Dashboard.Panel.RoleRemove"]
}
}
}
@T["Dashboard.Panel.Reset"]
@T["Dashboard.Panel.Done"]
@code {
private DashboardLayout _layout = WidgetCatalog.BuiltIn;
private DashboardView? _view;
private IReadOnlyList _available = [];
private IReadOnlyList<(Role Role, DashboardLayout? Layout)> _roles = [];
private string? _role;
private bool _loaded;
private bool _customizing;
private LicenseState License => Session.Licenses.Current;
private bool ShowDashboard => Session.Preferences.ShowDashboard;
private bool CanSetRoleDefaults => Session.Company is not null && Session.Permissions.Allows(CompanySettingsService.WritePermission);
private (Role Role, DashboardLayout? Layout)? SelectedRole =>
_roles.Where(r => r.Role.Code == _role).Select(r => ((Role, DashboardLayout?)?)r).FirstOrDefault();
private IEnumerable Areas =>
MenuCatalog.Sections.SelectMany(s => s.Items).Where(i => i.Href != "/" && Session.CanSee(i));
private IEnumerable StartChoices => MenuCatalog.All.Where(i => i.Href != "/" && i.Milestone is null && Session.CanSee(i));
private string StartValue => Session.Preferences.StartPage switch
{
StartPage.Page when Session.Preferences.StartPath is { } path => path,
StartPage.LastTab => nameof(StartPage.LastTab),
_ => nameof(StartPage.Dashboard),
};
private string SourceText => _view?.Source switch
{
DashboardLayoutSource.User => T["Dashboard.Panel.SourceUser"],
DashboardLayoutSource.Role => T.Format("Dashboard.Panel.SourceRole", _roles.FirstOrDefault(r => r.Role.Code == _view.RoleCode).Role?.Name ?? _view.RoleCode ?? string.Empty),
_ => T["Dashboard.Panel.SourceBuiltIn"],
};
private string Today => Clock.GetLocalNow().ToString("dddd d MMMM yyyy", T.Culture);
private string Greeting
{
get
{
var key = Clock.GetLocalNow().Hour switch
{
< 13 => "Pages.Home.Greeting",
< 18 => "Pages.Home.GreetingAfternoon",
_ => "Pages.Home.GreetingEvening",
};
return T.Format(key, FirstName);
}
}
private string FirstName => Session.DisplayName.Split(' ', StringSplitOptions.RemoveEmptyEntries).FirstOrDefault() ?? Session.DisplayName;
private string LicenseDetail
{
get
{
var parts = new List();
if (License.Licensee is { Length: > 0 } licensee)
{
parts.Add(T.Format("Pages.Home.LicenseLicensee", licensee));
}
parts.Add(License.DaysLeft(Clock.GetUtcNow().UtcDateTime) is { } days
? T.Format("Pages.Home.LicenseDaysLeft", days)
: T["Pages.Home.LicenseUnlimited"]);
return string.Join(" · ", parts);
}
}
///
protected override async Task OnInitializedAsync()
{
await Session.EnsureLoadedAsync();
if (Session.Actor is { } actor)
{
_available = await Dashboard.AvailableAsync(actor);
_view = await Dashboard.GetAsync(actor);
_layout = _view.Layout;
}
_loaded = true;
}
private string HintOf(WidgetDefinition definition) =>
definition.Milestone is { } milestone
? T[definition.DescriptionKey] + " " + T.Format("Dashboard.Panel.ComingFrom", milestone)
: T[definition.DescriptionKey];
private async Task ToggleCustomize()
{
if (_customizing)
{
CloseCustomize();
return;
}
if (CanSetRoleDefaults && Session.Company is { } company)
{
_roles = await Dashboard.RoleDefaultsAsync(company.Code);
_role ??= _roles.FirstOrDefault().Role?.Code;
}
_customizing = true;
}
private void CloseCustomize() => _customizing = false;
private async Task ChangeLayoutAsync(DashboardLayout next)
{
_layout = next;
if (Session.Actor is { } actor)
{
await Dashboard.SaveAsync(actor, next);
_view = new DashboardView(next, DashboardLayoutSource.User);
}
}
private Task ToggleAsync(string id, bool visible) => ChangeLayoutAsync(_layout.Toggle(id, visible));
private Task MoveAsync(string id, int offset) => ChangeLayoutAsync(_layout.Move(id, offset));
private Task ResizeAsync(string id, string? value) =>
int.TryParse(value, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out var width)
? ChangeLayoutAsync(_layout.Resize(id, width))
: Task.CompletedTask;
private async Task ResetAsync()
{
if (Session.Actor is not { } actor)
{
return;
}
await Dashboard.ResetAsync(actor);
_view = await Dashboard.GetAsync(actor);
_layout = _view.Layout;
Toasts.Show(ToastKind.Info, T["Dashboard.Panel.ResetDone"]);
}
private async Task ChangeShowAsync(bool show)
{
await Session.SavePreferencesAsync(Session.Preferences with { ShowDashboard = show });
if (!show)
{
_customizing = false;
}
}
private async Task ChangeStartAsync(ChangeEventArgs e)
{
var value = e.Value as string;
var next = value switch
{
nameof(StartPage.LastTab) => Session.Preferences with { StartPage = StartPage.LastTab },
_ when UserPreferences.IsLocalPath(value) && value != "/" => Session.Preferences with { StartPage = StartPage.Page, StartPath = value },
_ => Session.Preferences with { StartPage = StartPage.Dashboard },
};
await Session.SavePreferencesAsync(next);
}
private async Task SaveRoleAsync() => await SetRoleDefaultAsync(_layout, "Dashboard.Panel.RoleSaved");
private async Task RemoveRoleAsync() => await SetRoleDefaultAsync(null, "Dashboard.Panel.RoleRemoved");
private async Task SetRoleDefaultAsync(DashboardLayout? layout, string doneKey)
{
if (Session.Actor is not { } actor || Session.Company is not { } company || SelectedRole is not { } selected)
{
return;
}
await Dashboard.SaveRoleDefaultAsync(actor, company.Code, selected.Role.Code, layout);
_roles = await Dashboard.RoleDefaultsAsync(company.Code);
Toasts.Show(ToastKind.Success, T.Format(doneKey, selected.Role.Name));
}
}