awcodes / recently

Easily track and access recently viewed records in your filament panels.
MIT License
33 stars 10 forks source link

[Bug]: When using RelationManagers with Tabs, multiple History Entires are added, inlcuding the Livewire Update Route #12

Closed schaper1337 closed 3 weeks ago

schaper1337 commented 1 month ago

What happened?

I am using a RelationManagers with Tabs (hasCombinedRelationManagerTabsWithContent: true) on an Edit Page of a Resource. When using the HasRecentHistoryRecorder Trait on that Edit Page and clicking a Tab of a Relationmanager, the Livewire Update Route is added as a URL (obviously not wanted, since we cannot visit it), as well as the active RelationManager (which would be fine, if adjusted a little bit - see below).

Each Relationmanager has its own queryparameter and is added to the history, when activated, which would be fine, if the Title of the Relationmanager would be used. Instead the page's Title is used, which results in multiple Entries with the same Label (eg. "Edit Record X" that point to various RelationManagers.

Lastly the URLs ".../edit" (entry point for edit)

and

".../edit?activeRelationManager=" (link to the form in the tabs)

point to the same Page and shouldn't be recorded as two different Entries.

How to reproduce the bug

See above. All out-of-the-box functionality. Nothing customized.

Package Version

1.0.3

PHP Version

8.2.0

Laravel Version

11.26

Which operating systems does with happen with?

No response

Notes

No response

schaper1337 commented 1 month ago

I use a custom Trait for now, to filter out livewire requests and record the urls without their query parameters

trait HasRecentHistoryRecorder
{
    public function renderedHasRecentHistoryRecorder(): void
    {
        $panel = Filament::getCurrentPanel();

        if($this->isLivewireRequest())
            return;

        match (true) {
            Str::contains(request()->url(), $panel->getPath()) => $this->recordHistory(),
            $panel->isDefault() && blank($panel->getPath()) => $this->recordHistory(),
            default => null,
        };
    }

    protected function recordHistory(): void
    {
        $resource = static::getResource();
        $record = $this->getRecord();
        $title = $this->getTitle($record);

        if (! $resource::getRecordTitleAttribute()) {
            $title .= ' ' . $record->id;
        }

        Recently::add(
            url: request()->url(),
            icon: $resource::getNavigationIcon(),
            title: strip_tags($resource::getModelLabel().' '.$title),
        );
    }

    protected function isLivewireRequest(): bool
    {
        return class_exists(LivewireManager::class) && app(LivewireManager::class)->isLivewireRequest();
    }

}