2024-06-02 22:03:59 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Livewire;
|
|
|
|
|
|
|
|
use App\Models\Mod;
|
2024-07-03 17:47:02 -04:00
|
|
|
use App\Models\User;
|
2024-09-12 13:19:52 -04:00
|
|
|
use Illuminate\Support\Collection;
|
2024-07-03 17:47:02 -04:00
|
|
|
use Illuminate\Support\Str;
|
2024-06-02 22:03:59 -04:00
|
|
|
use Illuminate\View\View;
|
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
class GlobalSearch extends Component
|
|
|
|
{
|
2024-07-03 17:47:02 -04:00
|
|
|
/**
|
|
|
|
* The search query.
|
|
|
|
*/
|
2024-06-02 22:03:59 -04:00
|
|
|
public string $query = '';
|
|
|
|
|
2024-07-03 17:47:02 -04:00
|
|
|
/**
|
|
|
|
* Whether to show the search result dropdown.
|
|
|
|
*/
|
|
|
|
public bool $showDropdown = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether to show the "no results found" message.
|
|
|
|
*/
|
|
|
|
public bool $noResults = false;
|
|
|
|
|
2024-06-02 22:03:59 -04:00
|
|
|
public function render(): View
|
|
|
|
{
|
|
|
|
return view('livewire.global-search', [
|
2024-07-03 17:47:02 -04:00
|
|
|
'results' => $this->executeSearch($this->query),
|
2024-06-02 22:03:59 -04:00
|
|
|
]);
|
|
|
|
}
|
2024-07-03 17:47:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the search against each of the searchable models.
|
|
|
|
*/
|
|
|
|
protected function executeSearch(string $query): array
|
|
|
|
{
|
|
|
|
$query = Str::trim($query);
|
|
|
|
$results = ['data' => [], 'total' => 0];
|
|
|
|
|
2024-09-12 13:19:52 -04:00
|
|
|
if (Str::length($query) > 0) {
|
2024-07-03 17:47:02 -04:00
|
|
|
$results['data'] = [
|
2024-09-12 13:19:52 -04:00
|
|
|
'user' => $this->fetchUserResults($query),
|
|
|
|
'mod' => $this->fetchModResults($query),
|
2024-07-03 17:47:02 -04:00
|
|
|
];
|
|
|
|
$results['total'] = $this->countTotalResults($results['data']);
|
|
|
|
}
|
|
|
|
|
2024-09-17 11:59:28 -04:00
|
|
|
$this->noResults = $results['total'] === 0;
|
2024-07-03 17:47:02 -04:00
|
|
|
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
2024-09-12 13:19:52 -04:00
|
|
|
/**
|
|
|
|
* Fetch the user search results.
|
|
|
|
*/
|
|
|
|
protected function fetchUserResults(string $query): Collection
|
|
|
|
{
|
|
|
|
/** @var array<int, array<string, mixed>> $userHits */
|
|
|
|
$userHits = User::search($query)->raw()['hits'];
|
|
|
|
|
|
|
|
return collect($userHits);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch the mod search results.
|
|
|
|
*/
|
|
|
|
protected function fetchModResults(string $query): Collection
|
|
|
|
{
|
|
|
|
/** @var array<int, array<string, mixed>> $modHits */
|
|
|
|
$modHits = Mod::search($query)->raw()['hits'];
|
|
|
|
|
|
|
|
return collect($modHits);
|
|
|
|
}
|
|
|
|
|
2024-07-03 17:47:02 -04:00
|
|
|
/**
|
|
|
|
* Count the total number of results across all models.
|
|
|
|
*/
|
2024-09-12 13:19:52 -04:00
|
|
|
protected function countTotalResults(array $results): int
|
2024-07-03 17:47:02 -04:00
|
|
|
{
|
2024-09-12 13:19:52 -04:00
|
|
|
return collect($results)->reduce(function (int $carry, Collection $result) {
|
2024-07-03 17:47:02 -04:00
|
|
|
return $carry + $result->count();
|
|
|
|
}, 0);
|
|
|
|
}
|
2024-06-02 22:03:59 -04:00
|
|
|
}
|