Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion resources/views/components/component.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
class="relative shrink-0">
<div x-ref="badgeAnchor">
@if ($component->incidents_count > 0)
<a href="{{ route('cachet.status-page.incident', [$component->incidents->first()]) }}" class="inline-flex">
<a href="{{ route('cachet.status-page.incident', [$component->latest_unresolved_incident]) }}" class="inline-flex">
<x-cachet::badge :status="$component->latest_status" />
</a>
@else
Expand Down
11 changes: 10 additions & 1 deletion src/Models/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* @property ?string $link
* @property ?ComponentStatusEnum $status
* @property ComponentStatusEnum $latest_status
* @property-read Incident|null $latest_unresolved_incident
* @property ?int $order
* @property ?int $component_group_id
* @property ?bool $checked
Expand Down Expand Up @@ -171,12 +172,20 @@ public function scopeOutage(Builder $query): void
$query->whereIn('status', ComponentStatusEnum::outage());
}

/**
* Get the latest unresolved incident for the component.
*/
public function latestUnresolvedIncident(): Attribute
{
return Attribute::get(fn () => $this->incidents()->unresolved()->latest()->first());
}

/**
* Get the latest status for the component.
*/
public function latestStatus(): Attribute
{
return Attribute::get(fn () => $this->incidents()->unresolved()->latest()->first()?->pivot->component_status ?? $this->status);
return Attribute::get(fn () => $this->latest_unresolved_incident?->pivot->component_status ?? $this->status);
}

/**
Expand Down
37 changes: 37 additions & 0 deletions tests/Unit/Views/ComponentTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Cachet\Enums\ComponentStatusEnum;
use Cachet\Enums\IncidentStatusEnum;
use Cachet\Models\Component;
use Cachet\Models\Incident;
use Illuminate\Foundation\Testing\Concerns\InteractsWithViews;
Expand Down Expand Up @@ -41,6 +42,42 @@
->assertDontSee(ComponentStatusEnum::operational->getLabel());
});

it('links the status pill to the latest unresolved incident, not an older resolved one', function () {
$component = Component::factory()->create([
'status' => ComponentStatusEnum::operational->value,
]);

$resolvedIncident = Incident::factory()->create([
'status' => IncidentStatusEnum::fixed->value,
]);
$ongoingIncident = Incident::factory()->create([
'status' => IncidentStatusEnum::investigating->value,
]);

$this->travelTo(now()->subSeconds(2), function () use ($component, $resolvedIncident) {
$component->incidents()->attach($resolvedIncident, [
'component_status' => ComponentStatusEnum::performance_issues->value,
]);
});

$component->incidents()->attach($ongoingIncident, [
'component_status' => ComponentStatusEnum::partial_outage->value,
]);

$component = Component::query()
->withCount(['incidents' => fn ($query) => $query->unresolved()])
->first();

$view = $this->view('cachet::components.component', [
'component' => $component,
'status' => $component->status,
]);

$view
->assertSee($ongoingIncident->guid)
->assertDontSee($resolvedIncident->guid);
});

it('shows the latest status for a component multiple linked incidents', function () {
$component = Component::factory()->create([
'status' => ComponentStatusEnum::operational->value,
Expand Down
Loading