Skip to content

Conversation

MACscr
Copy link

@MACscr MACscr commented Mar 22, 2025

User description

Add Search Configuration Options

This PR adds new features to customize the global search functionality in the Knowledge Base panel:

Changes

  • Added ability to customize searchable model attributes via globallySearchableAttributes()
  • Added ability to customize search result titles via globalSearchResultTitle()
  • Added ability to customize search result details via globalSearchResultDetails()
  • Updated DocumentationResource to use these configurations when available
  • Maintained backward compatibility for existing implementations

Example Usage

KnowledgeBasePanel::make()
    ->globallySearchableAttributes(['title', 'content', 'tags'])
    ->globalSearchResultTitle(function ($record) {
        return str($record->slug)
            ->replace('/', ' -> ')
            ->replace('-', ' ')
            ->title();
    })
    ->globalSearchResultDetails(function ($record) {
        return [
            'Summary' => Str::limit(strip_tags($record->content), 150),
            'Last Updated' => $record->updated_at?->diffForHumans(),
        ];
    })

These changes provide greater flexibility for customizing the search experience while following existing patterns in the codebase.


PR Type

Enhancement, Documentation


Description

  • Added customization options for global search attributes, titles, and details.

  • Updated KnowledgeBasePanel with new methods for search customization.

  • Enhanced DocumentationResource to utilize panel search configurations.

  • Expanded documentation to explain search customization features.


Changes walkthrough 📝

Relevant files
Enhancement
KnowledgeBasePanel.php
Add search customization methods and properties                   

src/Filament/Panels/KnowledgeBasePanel.php

  • Added properties for search attributes, title, and details callbacks.
  • Introduced methods to set and retrieve search customization options.
  • Enhanced flexibility for global search behavior.
  • +39/-3   
    DocumentationResource.php
    Integrate panel search customization in DocumentationResource

    src/Filament/Resources/DocumentationResource.php

  • Integrated panel's search customization methods for attributes,
    titles, and details.
  • Ensured fallback to default behavior if customization is unavailable.
  • Improved modularity and extensibility of search result handling.
  • +26/-1   
    Documentation
    README.md
    Document search customization options for KnowledgeBasePanel

    README.md

  • Added documentation for customizing search attributes, titles, and
    details.
  • Provided examples for implementing search customization in
    KnowledgeBasePanel.
  • Clarified default search behavior and customization options.
  • +50/-0   

    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • Copy link

    Qodo Merge was enabled for this repository. To continue using it, please link your Git account with your Qodo account here.

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Incomplete Implementation

    The getGlobalSearchResultTitle method doesn't complete the string transformation that was present in the original code. The original code likely had additional transformations after replace('/', ' -> ').

    return str($record->slug)
        ->replace('/', ' -> ')
        ;
    Missing Validation

    The globallySearchableAttributes method accepts any array without validating that the attributes actually exist on the model, which could lead to searching non-existent fields.

    public function globallySearchableAttributes(array $attributes): static
    {
        $this->globallySearchableAttributes = $attributes;
    
        return $this;
    }

    Copy link

    qodo-merge-pro bot commented Mar 22, 2025

    Qodo Merge was enabled for this repository. To continue using it, please link your Git account with your Qodo account here.

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    General
    Simplify callback retrieval logic

    Similar to the previous issue, the method_exists check is redundant.
    Additionally, the nested condition creates unnecessary complexity. Simplify by
    directly checking if the callback exists.

    src/Filament/Resources/DocumentationResource.php [54-57]

    -if (method_exists($panel, 'getGlobalSearchResultTitleCallback') &&
    -    ($callback = $panel->getGlobalSearchResultTitleCallback())) {
    +$callback = $panel->getGlobalSearchResultTitleCallback();
    +
    +if ($callback) {
         return $callback($record);
     }

    [Suggestion has been applied]

    Suggestion importance[1-10]: 4

    __

    Why: Separating the callback retrieval from the conditional check improves readability and maintainability. This is a worthwhile improvement to code structure, though not critical for functionality.

    Low
    Simplify callback validation pattern

    The same issue appears in the details callback handling. The method_exists check
    is unnecessary and the conditional assignment within the if statement makes the
    code harder to read and maintain.

    src/Filament/Resources/DocumentationResource.php [68-71]

    -if (method_exists($panel, 'getGlobalSearchResultDetailsCallback') &&
    -    ($callback = $panel->getGlobalSearchResultDetailsCallback())) {
    +$callback = $panel->getGlobalSearchResultDetailsCallback();
    +
    +if ($callback) {
         return $callback($record);
     }

    [Suggestion has been applied]

    Suggestion importance[1-10]: 4

    __

    Why: This suggestion follows the same pattern as the previous one, improving code readability by separating the callback retrieval from the conditional check. It's a good structural improvement but not critical.

    Low
    Remove unnecessary method check

    The method_exists check is unnecessary since the method is already defined in
    the KnowledgeBasePanel class. This could lead to unexpected behavior if the
    panel object is of a different type. Consider using a more robust type check or
    removing the conditional entirely.

    src/Filament/Resources/DocumentationResource.php [22-24]

    -if (method_exists($panel, 'getGloballySearchableAttributes')) {
    -    return $panel->getGloballySearchableAttributes();
    -}
    +return $panel->getGloballySearchableAttributes();

    [Suggestion has been applied]

    Suggestion importance[1-10]: 3

    __

    Why: The method_exists check is redundant since we're using a specific panel class. However, removing it could break backward compatibility if older panel versions are used, so the impact is moderate.

    Low
    • Update

    MACscr and others added 2 commits March 22, 2025 16:01
    Co-authored-by: qodo-merge-pro[bot] <151058649+qodo-merge-pro[bot]@users.noreply.github.com>
    Co-authored-by: qodo-merge-pro[bot] <151058649+qodo-merge-pro[bot]@users.noreply.github.com>
    Co-authored-by: qodo-merge-pro[bot] <151058649+qodo-merge-pro[bot]@users.noreply.github.com>
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    1 participant