The home page becomes the widget dashboard of mockups 004-006. Each user switches widgets on and off, orders them (drag and drop or buttons that also work from the keyboard), sizes them on the 12-column grid and picks the start page; every change is saved at once. Whoever may change the company settings sets a default layout per role, used by people who never customized; Reset goes back to it. Todo, Done and Deadlines collect lines from sources each module registers, so later milestones fill them without touching the widgets; a failing source is logged and skipped. M3 provides licence, unread warnings and scheduled task runs. Notes, Shortcuts and System status work now; widgets of modules still to come say which milestone brings their data instead of showing fake numbers. Role defaults live in the new company_settings table, written through CompanySettingsService with company.settings.write (ADR 0022). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
133 lines
6.1 KiB
C#
133 lines
6.1 KiB
C#
using Khaemhat.Application.Modules;
|
|
|
|
namespace Khaemhat.Application.Dashboard;
|
|
|
|
/// <summary>
|
|
/// A widget of the dashboard (SPEC §10.3, mockup 004): what it is, how wide it starts and who may see it. The
|
|
/// <see cref="Milestone"/> of a widget whose data comes from a later module says where its data will come from; until
|
|
/// then the widget shows that instead of invented numbers.
|
|
/// </summary>
|
|
/// <param name="Id">The id, kept in the layouts.</param>
|
|
/// <param name="TitleKey">The text key of the title.</param>
|
|
/// <param name="Icon">The icon.</param>
|
|
/// <param name="DefaultWidth">The starting width in columns of twelve.</param>
|
|
/// <param name="Module">The licensed module it needs.</param>
|
|
/// <param name="Permission">The permission it needs, if any.</param>
|
|
/// <param name="InstanceOnly">Only for administrators of the instance.</param>
|
|
/// <param name="Milestone">The milestone that brings its data, if still to come.</param>
|
|
public sealed record WidgetDefinition(
|
|
string Id,
|
|
string TitleKey,
|
|
string Icon,
|
|
int DefaultWidth,
|
|
string Module,
|
|
string? Permission = null,
|
|
bool InstanceOnly = false,
|
|
string? Milestone = null)
|
|
{
|
|
/// <summary>The widths a widget can take, in columns of twelve.</summary>
|
|
public static readonly IReadOnlyList<int> Widths = [3, 4, 6, 8, 12];
|
|
|
|
/// <summary>The text key of the description shown in the customize panel.</summary>
|
|
public string DescriptionKey => TitleKey[..TitleKey.LastIndexOf('.')] + ".Description";
|
|
|
|
/// <summary>The nearest allowed width.</summary>
|
|
/// <param name="width">Any number.</param>
|
|
/// <returns>One of <see cref="Widths"/>.</returns>
|
|
public static int Snap(int width) => Widths.OrderBy(w => Math.Abs(w - width)).ThenByDescending(w => w).First();
|
|
|
|
/// <summary>The next width up or down, staying at the ends.</summary>
|
|
/// <param name="width">The current width.</param>
|
|
/// <param name="step">+1 wider, -1 narrower.</param>
|
|
/// <returns>The width.</returns>
|
|
public static int Step(int width, int step)
|
|
{
|
|
var index = Math.Clamp(Widths.ToList().IndexOf(Snap(width)) + Math.Sign(step), 0, Widths.Count - 1);
|
|
return Widths[index];
|
|
}
|
|
}
|
|
|
|
/// <summary>The ids of the widgets.</summary>
|
|
public static class WidgetIds
|
|
{
|
|
/// <summary>Indicators chosen by the user.</summary>
|
|
public const string Kpi = "kpi";
|
|
|
|
/// <summary>What to do today.</summary>
|
|
public const string Todo = "todo";
|
|
|
|
/// <summary>What was done today.</summary>
|
|
public const string Done = "done";
|
|
|
|
/// <summary>Deadlines of the next 30 days.</summary>
|
|
public const string Deadlines = "deadlines";
|
|
|
|
/// <summary>Orders and delivery notes still to fulfil.</summary>
|
|
public const string ToFulfil = "to-fulfil";
|
|
|
|
/// <summary>Revenue of the last 12 months.</summary>
|
|
public const string Revenue = "revenue";
|
|
|
|
/// <summary>CRM activities.</summary>
|
|
public const string CrmActivities = "crm-activities";
|
|
|
|
/// <summary>Personal reminders.</summary>
|
|
public const string Notes = "notes";
|
|
|
|
/// <summary>Shortcut buttons.</summary>
|
|
public const string Shortcuts = "shortcuts";
|
|
|
|
/// <summary>Items under the minimum stock.</summary>
|
|
public const string Understock = "understock";
|
|
|
|
/// <summary>The CRM pipeline.</summary>
|
|
public const string Pipeline = "pipeline";
|
|
|
|
/// <summary>The last documents.</summary>
|
|
public const string RecentDocuments = "recent-documents";
|
|
|
|
/// <summary>State of the system (administrators).</summary>
|
|
public const string SystemStatus = "system";
|
|
}
|
|
|
|
/// <summary>Every widget, in the order of the customize panel (SPEC §10.3).</summary>
|
|
public static class WidgetCatalog
|
|
{
|
|
/// <summary>The widgets.</summary>
|
|
public static IReadOnlyList<WidgetDefinition> All { get; } =
|
|
[
|
|
new(WidgetIds.Kpi, "Dashboard.Widgets.Kpi.Title", "chart", 12, ModuleCodes.Core, "sales.invoice.read", Milestone: "M5"),
|
|
new(WidgetIds.Todo, "Dashboard.Widgets.Todo.Title", "check-circle", 4, ModuleCodes.Core),
|
|
new(WidgetIds.Done, "Dashboard.Widgets.Done.Title", "check", 4, ModuleCodes.Core),
|
|
new(WidgetIds.Deadlines, "Dashboard.Widgets.Deadlines.Title", "calendar", 4, ModuleCodes.Core),
|
|
new(WidgetIds.ToFulfil, "Dashboard.Widgets.ToFulfil.Title", "truck", 8, ModuleCodes.Core, "sales.order.read", Milestone: "M5"),
|
|
new(WidgetIds.Revenue, "Dashboard.Widgets.Revenue.Title", "chart", 4, ModuleCodes.Core, "sales.invoice.read", Milestone: "M5"),
|
|
new(WidgetIds.CrmActivities, "Dashboard.Widgets.CrmActivities.Title", "target", 6, ModuleCodes.Crm, "crm.lead.read", Milestone: "M11"),
|
|
new(WidgetIds.Notes, "Dashboard.Widgets.Notes.Title", "edit", 4, ModuleCodes.Core),
|
|
new(WidgetIds.Shortcuts, "Dashboard.Widgets.Shortcuts.Title", "zap", 4, ModuleCodes.Core),
|
|
new(WidgetIds.Understock, "Dashboard.Widgets.Understock.Title", "box", 6, ModuleCodes.Core, "inventory.stock.read", Milestone: "M6"),
|
|
new(WidgetIds.Pipeline, "Dashboard.Widgets.Pipeline.Title", "target", 6, ModuleCodes.Crm, "crm.lead.read", Milestone: "M11"),
|
|
new(WidgetIds.RecentDocuments, "Dashboard.Widgets.RecentDocuments.Title", "file", 6, ModuleCodes.Core, "sales.invoice.read", Milestone: "M5"),
|
|
new(WidgetIds.SystemStatus, "Dashboard.Widgets.System.Title", "shield", 4, ModuleCodes.Core, InstanceOnly: true),
|
|
];
|
|
|
|
/// <summary>
|
|
/// The layout of whoever never customized it and has no default from a role: the widgets that already have data.
|
|
/// Those that wait for a later module stay in the panel, ready to be switched on.
|
|
/// </summary>
|
|
public static DashboardLayout BuiltIn { get; } = new(
|
|
[
|
|
new(WidgetIds.Todo, 4),
|
|
new(WidgetIds.Done, 4),
|
|
new(WidgetIds.Deadlines, 4),
|
|
new(WidgetIds.Shortcuts, 4),
|
|
new(WidgetIds.Notes, 4),
|
|
new(WidgetIds.SystemStatus, 4),
|
|
]);
|
|
|
|
/// <summary>Finds a widget.</summary>
|
|
/// <param name="id">The id.</param>
|
|
/// <returns>The widget, or <see langword="null"/>.</returns>
|
|
public static WidgetDefinition? Find(string? id) => All.FirstOrDefault(w => string.Equals(w.Id, id, StringComparison.Ordinal));
|
|
}
|