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

Panel Navigation #12

merged 2 commits into from
Nov 28, 2024

Conversation

pschilly
Copy link
Contributor

@pschilly pschilly commented Nov 25, 2024

The current config item of "show_navigation" does not currently function (at least not that I could tell) - further, it does not take into consideration multiple panels.

This PR will add the functionaility to allow to control whether or not the navigation item is visible for multiple panels based on the panel ID.

Summary by CodeRabbit

  • New Features

    • Introduced a new navigation configuration key, panel_navigation, allowing for more flexible navigation control.
    • Added a method to determine navigation registration based on the new configuration.
  • Documentation

    • Updated documentation comments to clarify the usage of the panel_navigation key with examples.

Copy link

coderabbitai bot commented Nov 25, 2024

Walkthrough

The changes involve modifications to the configuration file config/filament-types.php, where the key 'show_navigation' has been renamed to 'panel_navigation', and its value structure has been updated. Additionally, a new method shouldRegisterNavigation() has been added to the TypeResource class in src/Filament/Resources/TypeResource.php, which determines navigation registration based on the new configuration. Documentation comments have also been updated to clarify the new configuration usage.

Changes

File Change Summary
config/filament-types.php Renamed key 'show_navigation' to 'panel_navigation'. Updated value structure to accept boolean or array of panel IDs. Documentation updated.
src/Filament/Resources/TypeResource.php Added method public static function shouldRegisterNavigation(): bool to handle navigation registration logic based on panel_navigation.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant TypeResource
    participant Config

    User->>Config: Request panel_navigation configuration
    Config-->>User: Returns panel_navigation value (boolean or array)
    User->>TypeResource: Calls shouldRegisterNavigation()
    TypeResource->>Config: Checks panel_navigation value
    alt If array
        TypeResource->>TypeResource: Check current panel ID against array keys
        TypeResource-->>User: Returns corresponding boolean value
    else If boolean
        TypeResource-->>User: Returns boolean value
    end
Loading

🐇 "In the land of code where changes bloom,
The navigation's path finds new room.
From 'show' to 'panel', a clearer view,
With methods that guide, our journey's anew!
Hops of joy in every line,
As we embrace this change divine!" 🐇


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (4)
config/filament-types.php (1)

19-24: Documentation looks good, but could be more explicit

The documentation clearly explains the new configuration format. However, consider adding an example that shows both boolean and multiple panel configurations for completeness.

      * 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];
+     * Single panel: 'panel_navigation' => true
+     * Multiple panels: 'panel_navigation' => [
+     *     'admin' => true,
+     *     'customer' => false,
+     * ];
src/Filament/Resources/TypeResource.php (3)

46-53: Enhance documentation clarity with PHPDoc standards and examples

While the documentation provides basic information, it could be more helpful with proper PHPDoc formatting and examples.

Consider updating the documentation like this:

     /**
-     * Config Item: `panel_navigation`
-     * Returns: bool
-     * 
-     * Accepts: array OR bool
-     * 
-     * Compares against current panel ID based on what is in the array (if provided).
+     * Determines if navigation should be registered based on panel configuration.
+     *
+     * @return bool Whether navigation should be registered
+     *
+     * Configuration can be either:
+     * - boolean: Directly controls navigation visibility
+     * - array: Maps panel IDs to visibility boolean values
+     *
+     * Example configuration:
+     * ```php
+     * // Simple boolean
+     * 'panel_navigation' => true
+     *
+     * // Panel-specific visibility
+     * 'panel_navigation' => [
+     *     'admin' => true,
+     *     'customer' => false,
+     * ]
+     * ```
      */

65-65: Simplify boolean handling

The empty check is unnecessary for boolean values as FALSE is already a valid boolean.

-            return (empty($configItem)) ? FALSE : $configItem;
+            return (bool) $configItem;

56-56: Use consistent boolean notation

The codebase mixes TRUE and true notations. Stick to lowercase for consistency with PHP conventions.

-        $configItem = config('filament-types.panel_navigation', TRUE);
+        $configItem = config('filament-types.panel_navigation', true);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between a3901ff and 7d4f506.

📒 Files selected for processing (2)
  • config/filament-types.php (1 hunks)
  • src/Filament/Resources/TypeResource.php (1 hunks)
🔇 Additional comments (2)
config/filament-types.php (1)

26-26: Verify the configuration usage across the codebase

The default value of true maintains backward compatibility. Let's verify how this configuration is being used in the codebase.

✅ Verification successful

Let me gather more context about how this configuration is being used in the TypeResource file.


Configuration implementation is properly handled and flexible

The implementation in TypeResource.php shows that the panel_navigation configuration is well-designed and properly handled:

  • It supports both boolean and array values for fine-grained control
  • It maintains backward compatibility with a default value of true
  • It includes proper panel ID comparison when array configuration is provided
  • The configuration is used to determine navigation visibility through shouldRegisterNavigation()
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check how the new panel_navigation config is being used

# Look for references to the old and new config keys
echo "Checking for old config key usage..."
rg -l "show_navigation" 

echo "Checking for new config key usage..."
rg -l "panel_navigation"

# Look for Filament panel registrations to ensure all panels are covered
echo "Checking panel registrations..."
ast-grep --pattern 'Filament::registerPanel($_)'

Length of output: 404


Script:

#!/bin/bash
# Check the usage context of panel_navigation in TypeResource
rg -A 5 -B 5 "panel_navigation" src/Filament/Resources/TypeResource.php

# Look for any navigation-related methods or configurations
ast-grep --pattern 'function configureNavigation() { $$$ }'

# Check if there are any other navigation-related configurations
rg -l "navigation" --type php

Length of output: 970

src/Filament/Resources/TypeResource.php (1)

54-67: Verify configuration updates across the application

The change from show_navigation to panel_navigation might affect existing installations.

Let's check for any remaining references to the old configuration key:

Comment on lines 58 to 64
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 {

Comment on lines 54 to 67
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 (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.

@pschilly
Copy link
Contributor Author

Fixed:

Use Filament\Facades\Filament;

As well as a small bug where it would not default to FALSE if the current panel ID was not in the provided array.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (1)
src/Filament/Resources/TypeResource.php (1)

47-54: Improve method documentation

The current documentation could be enhanced with better PHPDoc formatting and more detailed information.

Here's an improved version:

     /**
-     * Config Item: `panel_navigation`
-     * Returns: bool
-     * 
-     * Accepts: array OR bool
-     * 
-     * Compares against current panel ID based on what is in the array (if provided).
+     * Determines if navigation should be registered for the current panel.
+     *
+     * This method checks the 'panel_navigation' configuration to determine if
+     * navigation should be shown. The configuration can be either:
+     * - boolean: Applies to all panels
+     * - array: Panel-specific settings where keys are panel IDs and values are booleans
+     *
+     * @example
+     * // config/filament-types.php
+     * 'panel_navigation' => [
+     *     'admin' => true,
+     *     'customer' => false,
+     * ]
+     *
+     * @return bool True if navigation should be registered for the current panel
      */
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 7d4f506 and f152c85.

📒 Files selected for processing (1)
  • src/Filament/Resources/TypeResource.php (2 hunks)
🔇 Additional comments (2)
src/Filament/Resources/TypeResource.php (2)

12-12: LGTM!

The Filament facade import has been added as suggested in the past review.


55-70: Verify configuration usage across panels

Let's ensure the configuration is properly structured in all panel configurations.

✅ Verification successful

Implementation correctly handles panel-specific navigation configuration

The implementation is correctly structured to handle both boolean and panel-specific array configurations as defined in the config file. The code matches the documented configuration format:

  • The default configuration is set to true (show navigation for all panels)
  • The code properly handles array configurations for panel-specific visibility (e.g., ['admin' => true])
  • The empty array case is handled correctly to hide navigation
  • The implementation correctly uses Filament::getCurrentPanel()->getId() to match against panel IDs

The configuration file's documentation and the implementation are in sync, providing a flexible way to control navigation visibility per panel.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check panel configuration structure across the codebase

# Search for panel configuration files
echo "Searching for panel configuration files..."
rg -l "panel_navigation|show_navigation" config/

# Check for panel registration to understand available panel IDs
echo "Checking panel registration..."
rg -A 5 "Panel::make|::configurePanel" 

Length of output: 283


Script:

#!/bin/bash
# Let's examine the configuration file and panel setup more thoroughly

# Check the content of the configuration file
echo "Content of config/filament-types.php:"
cat config/filament-types.php

# Look for panel-related code in the codebase
echo -e "\nSearching for panel-related code:"
rg -A 3 "getCurrentPanel|Panel::make|PanelProvider" 

# Look for any test cases related to panel navigation
echo -e "\nSearching for related test cases:"
fd -e php -e test.php --exec grep -l "panel_navigation\|shouldRegisterNavigation\|TypeResource" {}

Length of output: 5987

Comment on lines +55 to +70
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;
}
}
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;
}

@3x1io 3x1io merged commit bbe8862 into tomatophp:master Nov 28, 2024
4 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants