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 1 commit
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
23 changes: 23 additions & 0 deletions src/Filament/Resources/TypeResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,29 @@ 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 {
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix array handling logic and optimize config access

The current implementation has several issues:

  1. Redundant config fetch in the loop
  2. Missing default return value for array case
  3. Potential null panel ID handling

Consider this improved implementation:

-        if (is_array($configItem) && !empty($configItem)) {
-            foreach (config('filament-types.panel_navigation', true) as $key => $val) {
-                if (Filament::getCurrentPanel()->getId() === $key) {
-                    return $val;
-                }
-            }
+        if (is_array($configItem)) {
+            $currentPanelId = Filament::getCurrentPanel()?->getId();
+            return $configItem[$currentPanelId] ?? false;
         }
📝 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
if (is_array($configItem) && !empty($configItem)) {
foreach (config('filament-types.panel_navigation', true) as $key => $val) {
if (Filament::getCurrentPanel()->getId() === $key) {
return $val;
}
}
} else {
if (is_array($configItem)) {
$currentPanelId = Filament::getCurrentPanel()?->getId();
return $configItem[$currentPanelId] ?? false;
} else {

return (empty($configItem)) ? FALSE : $configItem;
}
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add missing Filament class import

The Filament class is used but not imported.

Add this import at the top of the file:

namespace TomatoPHP\FilamentTypes\Filament\Resources;

+use Filament\Facades\Filament;
use Filament\Forms\Form;

Committable suggestion skipped: line range outside the PR's diff.


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