129 lines
4.3 KiB
PHP
129 lines
4.3 KiB
PHP
<?php
|
|
|
|
// app/Http/Livewire/UsersOnline.php
|
|
namespace App\Http\Livewire;
|
|
|
|
use App\Services\PresenceService;
|
|
use Livewire\Component;
|
|
use Livewire\Attributes\On;
|
|
|
|
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 = 10)
|
|
{
|
|
$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');
|
|
}
|
|
}
|
|
|
|
/*
|
|
Blade Template: resources/views/livewire/users-online.blade.php
|
|
*/
|
|
?>
|
|
|
|
{{-- resources/views/livewire/users-online.blade.php --}}
|
|
<div class="users-online" wire:poll.{{ $refreshInterval }}s="loadOnlineUsers">
|
|
@if($showCount)
|
|
<div class="flex items-center space-x-2 text-sm text-theme-secondary dark:text-theme-muted mb-3">
|
|
<div class="w-2 h-2 bg-green-500 rounded-full animate-pulse"></div>
|
|
<span class="font-medium">{{ count($onlineUsers) }} online</span>
|
|
</div>
|
|
@endif
|
|
|
|
<div class="space-y-2">
|
|
@forelse($onlineUsers as $user)
|
|
<div class="flex items-center space-x-3 p-2 rounded-lg hover:bg-gray-50 dark:hover:bg-theme-secondary transition-colors">
|
|
@if($showAvatars)
|
|
<div class="relative flex-shrink-0">
|
|
@if(isset($user['avatar']) && $user['avatar'])
|
|
<img src="{{ $user['avatar'] }}" alt="{{ $user['name'] }}"
|
|
class="w-8 h-8 rounded-full object-cover">
|
|
@else
|
|
<div class="w-8 h-8 bg-gradient-to-br from-blue-500 to-purple-600 rounded-full flex items-center justify-center text-white text-sm font-medium">
|
|
{{ substr($user['name'], 0, 1) }}
|
|
</div>
|
|
@endif
|
|
<div class="absolute -bottom-1 -right-1 w-3 h-3 bg-green-500 border-2 border-theme-background dark:border-theme-secondary rounded-full"></div>
|
|
</div>
|
|
@endif
|
|
|
|
<div class="flex-1 min-w-0">
|
|
<p class="text-sm font-medium text-theme-primary dark:text-white truncate">
|
|
{{ $user['name'] }}
|
|
</p>
|
|
<p class="text-xs text-theme-muted dark:text-theme-muted">
|
|
{{ isset($user['last_seen']) ? \Carbon\Carbon::parse($user['last_seen'])->diffForHumans() : 'Now' }}
|
|
</p>
|
|
</div>
|
|
</div>
|
|
@empty
|
|
<div class="text-center py-4 text-theme-muted dark:text-theme-muted">
|
|
<div class="w-8 h-8 mx-auto mb-2 text-theme-muted">
|
|
<svg fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
|
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-2.776M13 7a4 4 0 11-8 0 4 4 0 018 0z" />
|
|
</svg>
|
|
</div>
|
|
<p class="text-sm">No users online</p>
|
|
</div>
|
|
@endforelse
|
|
</div>
|
|
</div>
|
|
|
|
<?php
|
|
/*
|
|
Usage Examples:
|
|
|
|
<!-- Basic usage -->
|
|
<livewire:users-online />
|
|
|
|
<!-- Customized -->
|
|
<livewire:users-online
|
|
:guard="'web'"
|
|
:show-count="true"
|
|
:show-avatars="true"
|
|
:max-display="5"
|
|
/>
|
|
|
|
<!-- For admin guard -->
|
|
<livewire:users-online :guard="'admin'" :max-display="20" />
|
|
*/
|
|
?>
|