using Khaemhat.Application.Modules; namespace Khaemhat.Application.Dashboard; /// /// A widget of the dashboard (SPEC §10.3, mockup 004): what it is, how wide it starts and who may see it. The /// 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. /// /// The id, kept in the layouts. /// The text key of the title. /// The icon. /// The starting width in columns of twelve. /// The licensed module it needs. /// The permission it needs, if any. /// Only for administrators of the instance. /// The milestone that brings its data, if still to come. public sealed record WidgetDefinition( string Id, string TitleKey, string Icon, int DefaultWidth, string Module, string? Permission = null, bool InstanceOnly = false, string? Milestone = null) { /// The widths a widget can take, in columns of twelve. public static readonly IReadOnlyList Widths = [3, 4, 6, 8, 12]; /// The text key of the description shown in the customize panel. public string DescriptionKey => TitleKey[..TitleKey.LastIndexOf('.')] + ".Description"; /// The nearest allowed width. /// Any number. /// One of . public static int Snap(int width) => Widths.OrderBy(w => Math.Abs(w - width)).ThenByDescending(w => w).First(); /// The next width up or down, staying at the ends. /// The current width. /// +1 wider, -1 narrower. /// The width. 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]; } } /// The ids of the widgets. public static class WidgetIds { /// Indicators chosen by the user. public const string Kpi = "kpi"; /// What to do today. public const string Todo = "todo"; /// What was done today. public const string Done = "done"; /// Deadlines of the next 30 days. public const string Deadlines = "deadlines"; /// Orders and delivery notes still to fulfil. public const string ToFulfil = "to-fulfil"; /// Revenue of the last 12 months. public const string Revenue = "revenue"; /// CRM activities. public const string CrmActivities = "crm-activities"; /// Personal reminders. public const string Notes = "notes"; /// Shortcut buttons. public const string Shortcuts = "shortcuts"; /// Items under the minimum stock. public const string Understock = "understock"; /// The CRM pipeline. public const string Pipeline = "pipeline"; /// The last documents. public const string RecentDocuments = "recent-documents"; /// State of the system (administrators). public const string SystemStatus = "system"; } /// Every widget, in the order of the customize panel (SPEC §10.3). public static class WidgetCatalog { /// The widgets. public static IReadOnlyList 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"), 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), ]; /// /// 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. /// 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), ]); /// Finds a widget. /// The id. /// The widget, or . public static WidgetDefinition? Find(string? id) => All.FirstOrDefault(w => string.Equals(w.Id, id, StringComparison.Ordinal)); }