Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panel Navigation #12

Merged
merged 2 commits into from
Nov 28, 2024
Merged
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
11 changes: 7 additions & 4 deletions config/filament-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
'types_resource' => null,

/**
* Show Navigation Menu
*
* If you need to show the navigation menu for the types
* Panel Navigation
* Accepts: boolean OR array of panel ID with boolean
* If array is empty, assumes to not display navigation item.
*
* Panel Example:
* 'panel_navigation' => ['admin' => TRUE];
*/
'show_navigation' => true,
'panel_navigation' => true,

/**
* Empty State
Expand Down
26 changes: 26 additions & 0 deletions src/Filament/Resources/TypeResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use TomatoPHP\FilamentTypes\Filament\Resources\TypeResource\Form\TypeForm;
use TomatoPHP\FilamentTypes\Filament\Resources\TypeResource\Table\TypeTable;
use TomatoPHP\FilamentTypes\Models\Type;
use Filament\Facades\Filament;

class TypeResource extends Resource
{
Expand Down Expand Up @@ -43,6 +44,31 @@ public static function getNavigationGroup(): ?string
return trans('filament-types::messages.group');
}

/**
* Config Item: `panel_navigation`
* Returns: bool
*
* Accepts: array OR bool
*
* Compares against current panel ID based on what is in the array (if provided).
*/
public static function shouldRegisterNavigation(): bool
{
$configItem = config('filament-types.panel_navigation', TRUE);

if (is_array($configItem) && !empty($configItem)) {
foreach (config('filament-types.panel_navigation', true) as $key => $val) {
if (Filament::getCurrentPanel()->getId() === $key) {
return $val;
} else {
return FALSE;
}
}
} else {
return (empty($configItem)) ? FALSE : $configItem;
}
}
Comment on lines +55 to +70
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Critical: Fix logical issues in navigation registration method

The current implementation has several issues that need to be addressed:

  1. The foreach loop's else clause causes early return, making it only process the first item
  2. Redundant config fetch inside the loop
  3. Missing null safety check for getCurrentPanel()
  4. Inconsistent boolean casing
  5. Unnecessary combination of is_array and empty checks

Here's the corrected implementation:

     public static function shouldRegisterNavigation(): bool
     {
-        $configItem = config('filament-types.panel_navigation', TRUE);
+        $configItem = config('filament-types.panel_navigation', true);
 
-        if (is_array($configItem) && !empty($configItem)) {
-            foreach (config('filament-types.panel_navigation', true) as $key => $val) {
-                if (Filament::getCurrentPanel()->getId() === $key) {
-                    return $val;
-                } else {
-                    return FALSE;
-                }
-            }
-        } else {
-            return (empty($configItem)) ? FALSE : $configItem;
-        }
+        if (is_array($configItem)) {
+            $currentPanel = Filament::getCurrentPanel();
+            if (!$currentPanel) {
+                return false;
+            }
+            return $configItem[$currentPanel->getId()] ?? false;
+        }
+
+        return (bool) $configItem;
     }

This implementation:

  1. Removes redundant config fetch
  2. Adds null safety check for getCurrentPanel()
  3. Uses array key lookup instead of loop
  4. Consistently uses lowercase booleans
  5. Properly handles array and boolean cases
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public static function shouldRegisterNavigation(): bool
{
$configItem = config('filament-types.panel_navigation', TRUE);
if (is_array($configItem) && !empty($configItem)) {
foreach (config('filament-types.panel_navigation', true) as $key => $val) {
if (Filament::getCurrentPanel()->getId() === $key) {
return $val;
} else {
return FALSE;
}
}
} else {
return (empty($configItem)) ? FALSE : $configItem;
}
}
public static function shouldRegisterNavigation(): bool
{
$configItem = config('filament-types.panel_navigation', true);
if (is_array($configItem)) {
$currentPanel = Filament::getCurrentPanel();
if (!$currentPanel) {
return false;
}
return $configItem[$currentPanel->getId()] ?? false;
}
return (bool) $configItem;
}


public static function form(Form $form): Form
{
return TypeForm::make($form);
Expand Down
Loading