From 4ddd2c5cfb2c7c8d7f0bc2ad619db29da346fe0c Mon Sep 17 00:00:00 2001 From: Bezhan Salleh Date: Fri, 13 Sep 2024 01:26:12 +0200 Subject: [PATCH 1/4] pre-release check wip --- src/Concerns/HasPanelValidator.php | 24 +++++++++++++++++++ src/PanelSwitch.php | 38 +++++++++++++++++++++++++++--- 2 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 src/Concerns/HasPanelValidator.php diff --git a/src/Concerns/HasPanelValidator.php b/src/Concerns/HasPanelValidator.php new file mode 100644 index 0000000..1ec92f9 --- /dev/null +++ b/src/Concerns/HasPanelValidator.php @@ -0,0 +1,24 @@ +diff(collect(Filament::getPanels())->keys()->toArray()); + + if ($validated->isNotEmpty()) { + throw new InvalidArgumentException("Invalid panel IDs: {$validated->implode(', ')}"); + } + } +} diff --git a/src/PanelSwitch.php b/src/PanelSwitch.php index bedd6e5..372ca97 100644 --- a/src/PanelSwitch.php +++ b/src/PanelSwitch.php @@ -4,11 +4,17 @@ use Closure; use Filament\Panel; +use Illuminate\Support\Arr; +use Filament\Facades\Filament; use Filament\Support\Components\Component; use Filament\Support\Facades\FilamentView; +use function PHPUnit\Framework\callback; + class PanelSwitch extends Component { + use Concerns\HasPanelValidator; + protected array | Closure $excludes = []; protected bool | Closure | null $visible = null; @@ -27,6 +33,8 @@ class PanelSwitch extends Component protected array | Closure $labels = []; + protected array | Closure | null $panels = null; + protected bool $renderIconAsImage = false; protected string | Closure $modalHeading = 'Switch Panels'; @@ -89,6 +97,9 @@ public function canSwitchPanels(bool | Closure $condition): static return $this; } + /** + * @deprecated Use `panels()` instead. + */ public function excludes(array | Closure $panelIds): static { $this->excludes = $panelIds; @@ -141,6 +152,13 @@ public function modalWidth(string | Closure | null $width = null): static return $this; } + public function panels(array | Closure | null $panels = null): static + { + $this->panels = $panels; + + return $this; + } + public function renderHook(string $hook): static { $this->renderHook = $hook; @@ -232,14 +250,28 @@ public function isVisible(): bool */ public function getPanels(): array { - return collect(filament()->getPanels()) - ->reject(fn (Panel $panel) => in_array($panel->getId(), $this->getExcludes())) + $panelIds = (array) $this->evaluate($this->panels); + + return collect(Filament::getPanels()) + ->when( + value: filled($panelIds), + callback: function($panelCollection) use($panelIds) { + $this->areUserProvidedPanelsValid($panelIds); + + $withDefaultOrder = $panelCollection->only($panelIds); + + return collect($panelIds) + ->map(fn (string $id) => $withDefaultOrder[$id]) + ->filter(); + }, + default: fn ($panelCollection) => $panelCollection + ) ->toArray(); } public function getCurrentPanel(): Panel { - return filament()->getCurrentPanel(); + return Filament::getCurrentPanel(); } public function getRenderHook(): string From 1d90b1c744dfff8059f751d286c5680b8d5d06df Mon Sep 17 00:00:00 2001 From: bezhanSalleh Date: Thu, 10 Oct 2024 17:54:23 +0000 Subject: [PATCH 2/4] Fix styling --- src/PanelSwitch.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PanelSwitch.php b/src/PanelSwitch.php index b1ad13d..b317182 100644 --- a/src/PanelSwitch.php +++ b/src/PanelSwitch.php @@ -38,7 +38,7 @@ class PanelSwitch extends Component protected string $renderHook = 'panels::global-search.before'; - protected null | string $sortOrder = null; + protected ?string $sortOrder = null; public static function make(): static { From 146bcccd2e855e596098d667c9462dbe585dd7c5 Mon Sep 17 00:00:00 2001 From: Bezhan Salleh Date: Thu, 10 Oct 2024 20:01:30 +0200 Subject: [PATCH 3/4] readme updated --- README.md | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 90060c4..310ab99 100644 --- a/README.md +++ b/README.md @@ -52,9 +52,9 @@ Who Can Switch Panels?
  • -Panel [New 1.1.0] +Panel [New 1.0.7]
  • -Sort Order [New 1.1.0] +Sort Order [New 1.0.7]
  • Placement @@ -65,6 +65,8 @@
  • Theming
  • +Panel Exclusion [@deprecated] +
  • Testing
  • @@ -239,7 +241,7 @@ PanelSwitch::configureUsing(function (PanelSwitch $panelSwitch) { }); ``` -### Panels `New(1.1.0)` +### Panels `New(1.0.7)` By default all the panels available will be listed in the panel switch menu. But by providing an array of panel ids to the `panels()` method you can limit the panels that will be listed. ```php @@ -253,7 +255,7 @@ PanelSwitch::configureUsing(function (PanelSwitch $panelSwitch) { ``` Then `panels()` method also accepts a closure that returns an array of panel ids. This is useful when you want to dynamically determine the panels that will be listed. The plugin will also validate the panels to ensure that they are valid filament panels. If any of the panels provided are invalid, the plugin will throw an `InvalidArgumentException`. -### Sort Order `New(1.1.0)` +### Sort Order `New(1.0.7)` By default the panels will be listed in the order they were registered in `config/app.php`'s `providers` array or in the order they are provided through the `panels()` method. But you can opt-in to sort the panels either in `asc` or `desc` order via `sort()` method. ```php PanelSwitch::configureUsing(function (PanelSwitch $panelSwitch) { @@ -301,6 +303,7 @@ PanelSwitch::configureUsing(function (PanelSwitch $panelSwitch) { }); ``` + ### Theming By default the plugin uses the default filament theme, but you can customize it by adding the view path into the `content` array of your `panels'` `tailwind.config.js` file: @@ -314,6 +317,17 @@ export default { } ``` +### Panel Exclusion +**`@deprecated`** use **`panels()`** method instead. +By default all the panels available will be listed in the panel switch menu. But you can exclude some of them by using the excludes() method. +```php +PanelSwitch::configureUsing(function (PanelSwitch $panelSwitch) { + $panelSwitch->excludes([ + 'saas' + ]); +}); +``` + Optionally, you can publish the views using ```bash From 94afc1da4082b4b1a357e23afb08db01f65dc04b Mon Sep 17 00:00:00 2001 From: Bezhan Salleh Date: Thu, 10 Oct 2024 20:09:48 +0200 Subject: [PATCH 4/4] phpunit cache removed --- .phpunit.cache/test-results | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .phpunit.cache/test-results diff --git a/.phpunit.cache/test-results b/.phpunit.cache/test-results deleted file mode 100644 index 52d2564..0000000 --- a/.phpunit.cache/test-results +++ /dev/null @@ -1 +0,0 @@ -{"version":"pest_2.35.1","defects":[],"times":{"P\\Tests\\ArchTest::__pest_evaluable_it_will_not_use_debugging_functions":0.051,"P\\Tests\\ExampleTest::__pest_evaluable_it_can_test":0.001}} \ No newline at end of file