Initial commit
This commit is contained in:
55
app/Http/Livewire/UsersOnline.php
Normal file
55
app/Http/Livewire/UsersOnline.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use App\Services\PresenceService;
|
||||
use Livewire\Attributes\On;
|
||||
use Livewire\Component;
|
||||
|
||||
class UsersOnline extends Component
|
||||
{
|
||||
public $onlineUsers = [];
|
||||
public $guard = 'web';
|
||||
public $showCount = true;
|
||||
public $showAvatars = true;
|
||||
public $maxDisplay = 10;
|
||||
public $refreshInterval = 10;
|
||||
|
||||
public function mount($guard = 'web', $showCount = true, $showAvatars = true, $maxDisplay = 100)
|
||||
{
|
||||
$this->guard = $guard;
|
||||
$this->showCount = $showCount;
|
||||
$this->showAvatars = $showAvatars;
|
||||
$this->maxDisplay = $maxDisplay;
|
||||
$this->loadOnlineUsers();
|
||||
}
|
||||
|
||||
public function loadOnlineUsers()
|
||||
{
|
||||
try {
|
||||
$presenceService = app(PresenceService::class);
|
||||
$users = $presenceService->getOnlineUsers($this->guard);
|
||||
|
||||
// Limit the display count
|
||||
$this->onlineUsers = $users->take($this->maxDisplay)->toArray();
|
||||
|
||||
$this->dispatch('users-online-updated', [
|
||||
'count' => $users->count(),
|
||||
'users' => $this->onlineUsers
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
$this->onlineUsers = [];
|
||||
}
|
||||
}
|
||||
|
||||
#[On('presence-updated')]
|
||||
public function refreshUsers()
|
||||
{
|
||||
$this->loadOnlineUsers();
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.users-online');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user