wire-elements / spotlight

Livewire component that brings Spotlight/Alfred-like functionality to your Laravel application.
MIT License
911 stars 71 forks source link

Shortcut arguments #10

Closed ryangjchandler closed 3 years ago

ryangjchandler commented 3 years ago

I think it could be cool for shortcuts to accept arguments.

An example might be a shortcut to visit a particular user profile, where the shortcut would be Visit user - hitting enter would allow you to enter some sort of identifier, I.e. username or email and then that would be made available to the shortcut handler.

PhiloNL commented 3 years ago

Hey @ryangjchandler

Thanks for the suggestion! I'm not sure I completely understand. Commands support dependencies, so your example would be something like:

class VisitUserCommand extends SpotlightCommand
{
    protected string $name = 'Visit user';

    public function dependencies(): ?SpotlightCommandDependencies
    {
        return SpotlightCommandDependencies::collection()
            ->add(
                SpotlightCommandDependency::make('user')
                    ->setPlaceholder('Which use do you want to visit?')
            );
    }

    public function searchUser(Request $request, $query)
    {
        return User::where('name', 'like', "%$query%")
            ->get()
            ->map(function($user) {
                return new SpotlightSearchResult(
                    $user->id,
                    $user->name,
                    sprintf('Visit %s', $user->name)
                );
            });
    }

    public function execute(Spotlight $spotlight, User $user)
    {
        $spotlight->redirectRoute('users.show', $user);
    }
}

Is this what you are looking for, or did you mean something else? 😄

ryangjchandler commented 3 years ago

Yeah that covers it. The word dependency kind of threw me off...