Skip to content

A free open source IT asset/license management system

Made needs attention widget a LW component for lazy loading

snipe293d5aea8ffa
diff --git a/app/Livewire/NeedsAttention.php b/app/Livewire/NeedsAttention.php
new file mode 100644
index 0000000000..85dd6a1e2b
--- /dev/null
+++ b/app/Livewire/NeedsAttention.php
@@ -0,0 +1,151 @@
+<?php
+
+namespace App\Livewire;
+
+use App\Models\Accessory;
+use App\Models\Asset;
+use App\Models\AssetModel;
+use App\Models\CheckoutAcceptance;
+use App\Models\CheckoutRequest;
+// Aliased as SnipeComponent to sidestep the Livewire\Component
+// collision below. Project convention: reach for SnipeComponent when
+// a Livewire class needs the Snipe-IT Component model, never plain
+// `Component` in that context.
+use App\Models\Component as SnipeComponent;
+use App\Models\Consumable;
+use App\Models\License;
+use App\Models\LicenseSeat;
+use App\Models\Maintenance;
+use App\Models\User;
+use Illuminate\View\View;
+use Livewire\Attributes\Lazy;
+use Livewire\Component;
+
+/**
+ * Dashboard "Needs Attention" widget - eight action-required counts
+ * (audits overdue, checkins overdue, overdue maintenances, pending
+ * acceptances, open checkout requests, licenses expiring, assets
+ * past EOL, employees offboarding).
+ *
+ * Extracted from an inline block in DashboardController::index() +
+ * dashboard.blade.php so the eight count queries no longer sit on the
+ * critical render path. `#[Lazy]` makes the component render a
+ * placeholder on initial dashboard paint; Livewire fires a follow-up
+ * XHR to hydrate the real counts, matching the same pattern the
+ * top-nav AlertMenu already uses to keep its five subqueries off the
+ * first paint.
+ *
+ * boot() authorization: matches the DashboardController::index gate
+ * (hasAccess('admin')). Without this, a non-admin user with a
+ * captured snapshot could POST /livewire/update to hydrate the widget
+ * and read the counts even though the parent view would 302 them
+ * away - route-level middleware doesn't guard snapshot replays (see
+ * project memory: livewire_component_authorization).
+ */
+#[Lazy]
+class NeedsAttention extends Component
+{
+ // Public properties so tests can `assertSet('overdueAudits', N)`
+ // instead of parsing rendered HTML. Populated by mount() and read
+ // by render() through the view. Also lets Livewire's dehydrate /
+ // hydrate cycle re-use the computed counts across a component
+ // refresh rather than re-running the eight queries on every
+ // Livewire XHR (e.g. if the widget ever grows an action).
+ public int $overdueAudits = 0;
+
+ public int $overdueCheckins = 0;
+
+ public int $overdueMaintenances = 0;
+
+ public int $pendingAcceptancesCount = 0;
+
+ public int $pendingRequestsCount = 0;
+
+ public int $licensesExpiringSoon = 0;
+
+ public int $assetsPastEol = 0;
+
+ public int $usersOffboardingSoon = 0;
+
+ public function boot(): void
+ {
+ abort_unless(auth()->check() && auth()->user()->hasAccess('admin'), 403);
+ }
+
+ public function mount(): void
+ {
+ $todayStart = now()->startOfDay();
+ $inThirtyDays = now()->addDays(30)->endOfDay();
+
+ // Range comparisons (not whereDate) so the b-tree indexes on
+ // these date columns can actually be used - see the
+ // 2026_08_14_170000 migration that adds them for every
+ // column referenced below. whereDate's DATE() wrap would
+ // sidestep the index.
+ $this->overdueAudits = Asset::where('next_audit_date', '<', $todayStart)->count();
+ $this->overdueCheckins = Asset::where('expected_checkin', '<', $todayStart)
+ ->whereNotNull('assigned_to')
+ ->count();
+ $this->overdueMaintenances = Maintenance::where('expected_completion_date', '<', $todayStart)
+ ->whereNull('completed_at')
+ ->count();
+ $this->licensesExpiringSoon = License::whereBetween('expiration_date', [$todayStart, $inThirtyDays])->count();
+ $this->assetsPastEol = Asset::where('asset_eol_date', '<', $todayStart)->count();
+ $this->usersOffboardingSoon = User::whereBetween('end_date', [$todayStart, $inThirtyDays])->count();
+
+ // Pending checkout acceptances - matches ReportsController's
+ // pending-acceptance count on /reports/unaccepted_assets.
+ // whereHasMorph applies each checkoutable type's own
+ // CompanyableTrait global scope so FMCS scoping stays consistent.
+ $this->pendingAcceptancesCount = CheckoutAcceptance::pending()
+ ->whereHasMorph('checkoutable', [Asset::class, LicenseSeat::class, Accessory::class, SnipeComponent::class, Consumable::class])
+ ->count();
+
+ // CheckoutRequest is not itself Companyable, so the widget's
+ // count would otherwise leak cross-company requests to
+ // FMCS-scoped admins. whereHasMorph applies each requestable
+ // type's own CompanyableTrait global scope (Asset is scoped,
+ // AssetModel is intentionally not per its shared-catalog
+ // design) so the count matches what the requested-assets
+ // report page actually renders for the current viewer.
+ // Relationship method is requestedItem() (see CheckoutRequest
+ // model), even though the underlying columns are named
+ // requestable_id / requestable_type. whereHasMorph needs the
+ // method name, not the morph-column prefix.
+ $this->pendingRequestsCount = CheckoutRequest::whereNull('canceled_at')
+ ->whereNull('fulfilled_at')
+ ->whereHasMorph('requestedItem', [Asset::class, AssetModel::class])
+ ->count();
+ }
+
+ public function placeholder(): string
+ {
+ // Reserve the widget's visual footprint on initial paint so
+ // the dashboard row doesn't visibly shift when the hydrated
+ // list swaps in. Same height/box chrome as the rendered view.
+ $collapse = trans('general.collapse');
+ $title = trans('general.dashboard_attention');
+
+ return <<<HTML
+ <div class="box box-default" aria-busy="true">
+ <div class="box-header with-border">
+ <h2 class="box-title">{$title}</h2>
+ </div>
+ <div class="box-body">
+ <ul class="list-group list-group-unbordered" style="margin-bottom: 0;">
+ <li class="list-group-item"><span class="sr-only">{$collapse}</span>&nbsp;</li>
+ <li class="list-group-item">&nbsp;</li>
+ <li class="list-group-item">&nbsp;</li>
+ <li class="list-group-item">&nbsp;</li>
+ <li class="list-group-item">&nbsp;</li>
+ </ul>
+ </div>
+ </div>
+ HTML;
+ }
+
+ public function render(): View
+ {
+ return view('livewire.needs-attention');
+ }
+}
diff --git a/resources/views/livewire/needs-attention.blade.php b/resources/views/livewire/needs-attention.blade.php
new file mode 100644
index 0000000000..05bcc1c6b0
--- /dev/null
+++ b/resources/views/livewire/needs-attention.blade.php
@@ -0,0 +1,82 @@
+<div class="box box-default">
+ <div class="box-header with-border">
+ <h2 class="box-title">{{ trans('general.dashboard_attention') }}</h2>
+ <div class="box-tools pull-right">
+ <button type="button" class="btn btn-box-tool" data-widget="collapse" aria-hidden="true">
+ <x-icon type="minus"/>
+ <span class="sr-only">{{ trans('general.collapse') }}</span>
+ </button>
+ </div>
+ </div>
+ <div class="box-body">
+ {{-- Two-column layout inside each row: label flexes to fill,
+ badge column has a fixed min-width so "1" and "1,247" line
+ up in the same vertical spine instead of the label text
+ bouncing around with the badge width. See
+ .dashboard-attention-list in overrides.less. --}}
+ <ul class="list-group list-group-unbordered dashboard-attention-list" style="margin-bottom: 0;">
+ @can('audit', \App\Models\Asset::class)
+ <li class="list-group-item">
+ <a href="{{ route('assets.audit.due') }}">
+ <span class="dashboard-attention-label">{{ trans('general.dashboard_overdue_audits') }}</span>
+ <span class="badge dashboard-attention-count">{{ number_format($overdueAudits) }}</span>
+ </a>
+ </li>
+ @endcan
+ @can('checkin', \App\Models\Asset::class)
+ <li class="list-group-item">
+ <a href="{{ route('assets.checkins.due') }}">
+ <span class="dashboard-attention-label">{{ trans('general.dashboard_overdue_checkins') }}</span>
+ <span class="badge dashboard-attention-count">{{ number_format($overdueCheckins) }}</span>
+ </a>
+ </li>
+ @endcan
+ @can('view', \App\Models\Asset::class)
+ <li class="list-group-item">
+ <a href="{{ route('maintenances.index', ['upcoming_status' => 'overdue']) }}">
+ <span class="dashboard-attention-label">{{ trans('general.dashboard_overdue_maintenances') }}</span>
+ <span class="badge dashboard-attention-count">{{ number_format($overdueMaintenances) }}</span>
+ </a>
+ </li>
+ @endcan
+ <li class="list-group-item">
+ <a href="{{ route('reports/unaccepted_assets') }}">
+ <span class="dashboard-attention-label">{{ trans('general.dashboard_unaccepted_assets') }}</span>
+ <span class="badge dashboard-attention-count">{{ number_format($pendingAcceptancesCount) }}</span>
+ </a>
+ </li>
+ @canany(['checkout', 'checkin'], \App\Models\Asset::class)
+ <li class="list-group-item">
+ <a href="{{ route('assets.requested') }}">
+ <span class="dashboard-attention-label">{{ trans('general.dashboard_pending_requests') }}</span>
+ <span class="badge dashboard-attention-count">{{ number_format($pendingRequestsCount) }}</span>
+ </a>
+ </li>
+ @endcanany
+ @can('view', \App\Models\License::class)
+ <li class="list-group-item">
+ <a href="{{ route('licenses.index') }}">
+ <span class="dashboard-attention-label">{{ trans('general.dashboard_licenses_expiring_soon') }}</span>
+ <span class="badge dashboard-attention-count">{{ number_format($licensesExpiringSoon) }}</span>
+ </a>
+ </li>
+ @endcan
+ @can('view', \App\Models\Asset::class)
+ <li class="list-group-item">
+ <a href="{{ route('hardware.index') }}">
+ <span class="dashboard-attention-label">{{ trans('general.dashboard_assets_past_eol') }}</span>
+ <span class="badge dashboard-attention-count">{{ number_format($assetsPastEol) }}</span>
+ </a>
+ </li>
+ @endcan
+ @can('view', \App\Models\User::class)
+ <li class="list-group-item">
+ <a href="{{ route('users.index') }}">
+ <span class="dashboard-attention-label">{{ trans('general.dashboard_users_offboarding_soon') }}</span>
+ <span class="badge dashboard-attention-count">{{ number_format($usersOffboardingSoon) }}</span>
+ </a>
+ </li>
+ @endcan
+ </ul>
+ </div>
+</div>