Files
w33b df6246e3c9 feat(playlist): enhance public playlist index with hero stats and loading optimizations
Add a redesigned hero section to the public playlist page displaying
community-generated totals for playlists and episodes, plus new string
translations (English, German, French). Eager-load user, limited episode
previews, and first gallery image to reduce N+1 queries. Cache aggregate
statistics for 10 minutes to improve performance on high-traffic pages.
2026-08-09 10:36:03 +02:00

85 lines
2.4 KiB
PHP

<?php
namespace App\Livewire;
use App\Models\Playlist;
use Illuminate\Support\Facades\Cache;
use Livewire\Attributes\Url;
use Livewire\Component;
use Livewire\WithPagination;
class Playlists extends Component
{
use WithPagination;
#[Url(history: true)]
public $search;
#[Url(history: true)]
public $order = 'episode-count';
public $pagination = 12;
public function render()
{
$orderby = 'episodes_count';
$orderdirection = 'desc';
switch ($this->order) {
case 'az':
$orderby = 'name';
$orderdirection = 'asc';
break;
case 'za':
$orderby = 'name';
$orderdirection = 'desc';
break;
case 'episode-count':
$orderby = 'episodes_count';
$orderdirection = 'desc';
break;
case 'newest':
$orderby = 'created_at';
$orderdirection = 'desc';
break;
case 'oldest':
$orderby = 'created_at';
$orderdirection = 'asc';
break;
default:
$orderby = 'episodes_count';
$orderdirection = 'desc';
}
$playlists = Playlist::where('is_private', 0)
->withCount('episodes')
->having('episodes_count', '>', 1)
->when($this->search != '', fn ($query) => $query->where('name', 'like', '%'.$this->search.'%'))
->with([
'user',
'episodes' => fn ($query) => $query->orderBy('position')->limit(4),
'episodes.episode.gallery' => fn ($query) => $query->limit(1),
])
->orderBy($orderby, $orderdirection)
->paginate($this->pagination);
$stats = Cache::remember('publicPlaylistStats', 600, function () {
$playlists = Playlist::where('is_private', 0)
->withCount('episodes')
->having('episodes_count', '>', 1)
->get();
return [
'playlists' => $playlists->count(),
'episodes' => $playlists->sum('episodes_count'),
];
});
return view('livewire.playlists', [
'playlists' => $playlists,
'totalPlaylists' => $stats['playlists'],
'totalEpisodes' => $stats['episodes'],
]);
}
}