Skip to content

Commit 66799a6

Browse files
authored
Merge pull request #1354 from moonshine-software/fragment-metrics
feat: Fragment metrics
2 parents bdb4b61 + 18ccac8 commit 66799a6

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

src/Laravel/src/Pages/Crud/IndexPage.php

+12-4
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,18 @@ protected function getMetrics(): ?ComponentContract
116116

117117
$metrics = $this->getResource()->getMetrics();
118118

119-
return $metrics
120-
? Div::make($metrics)
121-
->class('layout-metrics')
122-
: null;
119+
if ($metrics === []) {
120+
return null;
121+
}
122+
123+
$components = Div::make($metrics)->class('layout-metrics');
124+
125+
126+
if (! \is_null($fragment = $this->getResource()->getFragmentMetrics())) {
127+
return $fragment([$components]);
128+
}
129+
130+
return $components;
123131
}
124132

125133
/**

src/Laravel/src/Resources/CrudResource.php

+21-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace MoonShine\Laravel\Resources;
66

7+
use Closure;
8+
use Illuminate\Support\Collection;
79
use MoonShine\Contracts\Core\CrudPageContract;
810
use MoonShine\Contracts\Core\CrudResourceContract;
911
use MoonShine\Contracts\Core\DependencyInjection\FieldsContract;
@@ -12,6 +14,7 @@
1214
use MoonShine\Contracts\Core\TypeCasts\DataWrapperContract;
1315
use MoonShine\Core\Resources\Resource;
1416
use MoonShine\Core\TypeCasts\MixedDataCaster;
17+
use MoonShine\Laravel\Components\Fragment;
1518
use MoonShine\Laravel\Pages\Crud\DetailPage;
1619
use MoonShine\Laravel\Pages\Crud\FormPage;
1720
use MoonShine\Laravel\Pages\Crud\IndexPage;
@@ -250,7 +253,22 @@ protected function metrics(): array
250253
*/
251254
public function getMetrics(): array
252255
{
253-
return $this->metrics();
256+
return Collection::make($this->metrics())
257+
->ensure(Metric::class)
258+
->toArray();
259+
}
260+
261+
/**
262+
* @return ?Closure(array $components): Fragment
263+
*/
264+
protected function fragmentMetrics(): ?Closure
265+
{
266+
return null;
267+
}
268+
269+
public function getFragmentMetrics(): ?Closure
270+
{
271+
return $this->fragmentMetrics();
254272
}
255273

256274
/**
@@ -279,7 +297,7 @@ public function getListComponentName(): string
279297
return rescue(
280298
fn (): string => $this->getIndexPage()?->getListComponentName(),
281299
"index-table-{$this->getUriKey()}",
282-
false
300+
false,
283301
);
284302
}
285303

@@ -295,7 +313,7 @@ public function getListEventName(?string $name = null, array $params = []): stri
295313
return rescue(
296314
fn (): string => AlpineJs::event($this->getIndexPage()?->getListEventName() ?? '', $name, $params),
297315
AlpineJs::event(JsEvent::TABLE_UPDATED, $name, $params),
298-
false
316+
false,
299317
);
300318
}
301319

0 commit comments

Comments
 (0)