Skip to content

Commit 8542493

Browse files
authored
Merge pull request #13 from awcodes/feat/shorthand-counts
Feature: support shorthand counts and tooltips with actual count
2 parents cb7c5b7 + ad061ba commit 8542493

File tree

5 files changed

+63
-8
lines changed

5 files changed

+63
-8
lines changed

README.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,19 @@ Then add the widget to your dashboard class or the Filament config file.
2828
],
2929
```
3030

31-
## Usage
31+
## Configuration
3232

3333
By default, Overlook will display any resource registered with Filament, while still honoring the `canViewAny` policy. This can be undesired and also slow down the dashboard. To prevent this behavior publish the config file with:
3434

3535
```bash
3636
php artisan vendor:publish --tag="overlook-config"
3737
```
3838

39-
Inside the config you will have options to either "include" or "exclude" resources from being displayed.
39+
Inside the config you will have options to either "include" or "exclude" resources from being displayed. These are not meant to work together, you should use one of the other.
40+
41+
You can also choose to convert the count to a human-readable format. For example, 1000 will be converted to 1k. This is the default behavior.
42+
43+
Converted counts will also have a tooltip that displays the full count. This can be disabled by setting `enable_convert_tooltip` to false.
4044

4145
```php
4246
return [
@@ -48,9 +52,28 @@ return [
4852
'excludes' => [
4953
// App\Filament\Resources\Blog\AuthorResource::class,
5054
],
55+
'should_convert_count' => true,
56+
'enable_convert_tooltip' => true,
5157
];
5258
```
5359

60+
## Reordering & Polling
61+
62+
Should you need to reorder the location of the widget or want to enable polling, you can make your own version of the Overlook widget and register it instead.
63+
64+
```php
65+
namespace App\Filament\Widgets;
66+
67+
use Awcodes\Overlook\Overlook;
68+
69+
class CustomOverlookWidget extends Overlook
70+
{
71+
protected static ?int $sort = 10;
72+
73+
protected static ?string $pollingInterval = '10s';
74+
}
75+
```
76+
5477
## Changelog
5578

5679
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

config/overlook.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
'excludes' => [
88
// App\Filament\Resources\Blog\AuthorResource::class,
99
],
10+
'should_convert_count' => true,
11+
'enable_convert_tooltip' => true,
1012
];

resources/dist/overlook.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/views/widget.blade.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22
@if($data)
33
<ul class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5 gap-6">
44
@foreach($data as $resource)
5-
<li class="rounded-xl border border-gray-200 dark:border-gray-800 relative h-24 bg-gradient-to-tr from-gray-100 via-white to-white dark:from-gray-900 dark:via-gray-800 dark:to-gray-800" wire:key="{{ $resource['name'] }}">
5+
<li
6+
class="rounded-xl border border-gray-200 dark:border-gray-800 relative h-24 bg-gradient-to-tr from-gray-100 via-white to-white dark:from-gray-900 dark:via-gray-800 dark:to-gray-800"
7+
wire:key="{{ $resource['name'] }}"
8+
@if($this->shouldShowTooltip($resource['raw_count']))
9+
x-data x-tooltip="'{{ $resource['raw_count'] }}'"
10+
@endif
11+
>
612
<a href="{{ $resource['url'] }}" class="overflow-hidden absolute inset-0 py-2 px-3 text-gray-600 font-medium rounded-xl ring-primary-500 dark:text-gray-400 group hover:ring-2 focus:ring-2">
713
@if ($resource['icon'])
814
<x-dynamic-component :component="$resource['icon']" class="w-auto h-24 absolute left-0 top-8 text-primary-500 opacity-20 dark:opacity-20 transition group-hover:scale-110 group-hover:-rotate-12 group-hover:opacity-40 dark:group-hover:opacity-80" />
915
@endif
1016
{{ $resource['name'] }}
11-
<span class="text-gray-600 dark:text-gray-300 absolute bottom-4 right-4 text-3xl font-bold">{{ $resource['count'] }}</span>
17+
<span class="text-gray-600 dark:text-gray-300 absolute leading-none bottom-3 right-4 text-3xl font-bold">{{ $resource['count'] }}</span>
1218
</a>
1319
</li>
1420
@endforeach

src/Overlook.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
namespace Awcodes\Overlook;
44

55
use Filament\Facades\Filament;
6-
use Filament\Resources\Resource;
76
use Filament\Widgets\Widget;
8-
use Illuminate\Support\Str;
97

108
class Overlook extends Widget
119
{
@@ -44,11 +42,13 @@ public function getData(): array
4442
return ! in_array($resource, $this->getExcludes());
4543
})->transform(function ($resource) {
4644
$res = app($resource);
45+
$rawCount = $res::getEloquentQuery()->count();
4746

4847
if ($res->canViewAny()) {
4948
return [
5049
'name' => ucfirst($res->getPluralModelLabel()),
51-
'count' => $res::getEloquentQuery()->count(),
50+
'raw_count' => $this->formatRawCount($rawCount),
51+
'count' => $this->convertCount($rawCount),
5252
'icon' => invade($res)->getNavigationIcon(),
5353
'url' => $res->getUrl('index'),
5454
];
@@ -61,4 +61,28 @@ public function getData(): array
6161
->values()
6262
->toArray();
6363
}
64+
65+
public function formatRawCount($number): string
66+
{
67+
return number_format($number);
68+
}
69+
70+
public function convertCount($number): string
71+
{
72+
if (config('overlook.should_convert_count')) {
73+
return match(true) {
74+
strlen($number) >= 10 => substr($number, 0, -9) . 'B',
75+
strlen($number) >= 7 => substr($number, 0, -6) . 'M',
76+
strlen($number) >= 4 => substr($number, 0, -3) . 'K',
77+
default => $number
78+
};
79+
}
80+
81+
return $number;
82+
}
83+
84+
public function shouldShowTooltip($number): bool
85+
{
86+
return strlen($number) >= 4 && config('overlook.should_convert_count') && config('overlook.enable_convert_tooltip');
87+
}
6488
}

0 commit comments

Comments
 (0)