refactor: moved platform detection to helper function

Signed-off-by: Lukas Frey <mail@lukasfrey.cz>
This commit is contained in:
Lukas Frey
2024-05-02 16:01:47 +02:00
parent 5cb4a043cd
commit 5d53ceffbc
4 changed files with 24 additions and 14 deletions
@@ -193,13 +193,14 @@ For example, to provide a custom suffix for each platform on your own, you could
```php
use Filament\Panel;
use Filament\Enums\Platform;
use Filament\Support\Enums\Platform;
use function Filament\Support\detect_platform;
public function panel(Panel $panel): Panel
{
return $panel
// ...
->globalSearchSuffix(fn(Platform $platform) => match($platform) {
->globalSearchFieldSuffix(fn() => match(detect_platform()) {
Platform::Windows, Platform::Linux => 'CTRL+K',
Platform::Mac => '⌘K'
default => null,
@@ -4,9 +4,9 @@ namespace Filament\Panel\Concerns;
use Closure;
use Exception;
use Filament\Enums\Platform;
use Filament\GlobalSearch\Contracts\GlobalSearchProvider;
use Filament\GlobalSearch\DefaultGlobalSearchProvider;
use Filament\Support\Enums\Platform;
use Illuminate\Support\Arr;
use Illuminate\Support\Stringable;
@@ -105,16 +105,7 @@ trait HasGlobalSearch
public function getGlobalSearchFieldSuffix(): ?string
{
$userAgent = request()->userAgent();
return $this->evaluate($this->globalSearchFieldSuffix, [
'platform' => match (true) {
str_contains($userAgent, 'Windows') => Platform::Windows,
str_contains($userAgent, 'Mac') => Platform::Mac,
str_contains($userAgent, 'Linux') => Platform::Linux,
default => Platform::Other,
},
]);
return $this->evaluate($this->globalSearchFieldSuffix);
}
public function getGlobalSearchProvider(): ?GlobalSearchProvider
@@ -1,6 +1,6 @@
<?php
namespace Filament\Enums;
namespace Filament\Support\Enums;
enum Platform: string
{
+18
View File
@@ -2,6 +2,7 @@
namespace Filament\Support;
use Filament\Support\Enums\Platform;
use Filament\Support\Facades\FilamentColor;
use Filament\Support\Facades\FilamentView;
use Illuminate\Contracts\Support\Htmlable;
@@ -219,3 +220,20 @@ if (! function_exists('Filament\Support\generate_search_term_expression')) {
return Str::lower($search);
}
}
if (! function_exists('Filament\Support\detect_platform')) {
/**
* Attempts to detect the platform from the user-agent.
*/
function detect_platform(): Platform
{
$userAgent = request()->userAgent();
return match (true) {
str_contains($userAgent, 'Windows') => Platform::Windows,
str_contains($userAgent, 'Mac') => Platform::Mac,
str_contains($userAgent, 'Linux') => Platform::Linux,
default => Platform::Other,
};
}
}