2023-05-03 05:23:45 +00:00
|
|
|
<?php
|
|
|
|
|
|
2023-12-07 18:06:32 +00:00
|
|
|
namespace App\Livewire;
|
2023-05-03 05:23:45 +00:00
|
|
|
|
2024-01-16 14:19:14 +00:00
|
|
|
use App\Models\User;
|
2023-05-03 05:23:45 +00:00
|
|
|
use Livewire\Component;
|
|
|
|
|
use Spatie\Activitylog\Models\Activity;
|
|
|
|
|
|
|
|
|
|
class ActivityMonitor extends Component
|
|
|
|
|
{
|
2023-12-08 12:55:55 +00:00
|
|
|
public ?string $header = null;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2025-11-26 09:43:07 +00:00
|
|
|
public $activityId = null;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-01-16 14:19:14 +00:00
|
|
|
public $eventToDispatch = 'activityFinished';
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2025-06-02 13:36:37 +00:00
|
|
|
public $eventData = null;
|
|
|
|
|
|
2023-05-03 05:23:45 +00:00
|
|
|
public $isPollingActive = false;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-04-16 10:41:13 +00:00
|
|
|
public bool $fullHeight = false;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2025-05-30 10:56:34 +00:00
|
|
|
public $activity;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2025-06-11 10:02:39 +00:00
|
|
|
public bool $showWaiting = true;
|
|
|
|
|
|
2025-05-15 20:21:46 +00:00
|
|
|
public static $eventDispatched = false;
|
|
|
|
|
|
2024-02-05 13:40:54 +00:00
|
|
|
protected $listeners = ['activityMonitor' => 'newMonitorActivity'];
|
2023-05-03 05:23:45 +00:00
|
|
|
|
2025-11-24 07:44:04 +00:00
|
|
|
public function newMonitorActivity($activityId, $eventToDispatch = 'activityFinished', $eventData = null, $header = null)
|
2023-05-03 05:23:45 +00:00
|
|
|
{
|
2025-11-24 07:44:04 +00:00
|
|
|
// Reset event dispatched flag for new activity
|
|
|
|
|
self::$eventDispatched = false;
|
|
|
|
|
|
2023-05-03 05:23:45 +00:00
|
|
|
$this->activityId = $activityId;
|
2024-01-16 14:19:14 +00:00
|
|
|
$this->eventToDispatch = $eventToDispatch;
|
2025-06-02 13:36:37 +00:00
|
|
|
$this->eventData = $eventData;
|
2023-05-03 05:23:45 +00:00
|
|
|
|
2025-11-24 07:44:04 +00:00
|
|
|
// Update header if provided
|
|
|
|
|
if ($header !== null) {
|
|
|
|
|
$this->header = $header;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-03 05:23:45 +00:00
|
|
|
$this->hydrateActivity();
|
|
|
|
|
|
|
|
|
|
$this->isPollingActive = true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-08 09:51:36 +00:00
|
|
|
public function hydrateActivity()
|
|
|
|
|
{
|
2025-11-26 09:43:07 +00:00
|
|
|
if ($this->activityId === null) {
|
|
|
|
|
$this->activity = null;
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-07 14:31:43 +00:00
|
|
|
$this->activity = Activity::find($this->activityId);
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-26 09:43:07 +00:00
|
|
|
public function updatedActivityId($value)
|
|
|
|
|
{
|
|
|
|
|
if ($value) {
|
|
|
|
|
$this->hydrateActivity();
|
|
|
|
|
$this->isPollingActive = true;
|
|
|
|
|
self::$eventDispatched = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-03 05:23:45 +00:00
|
|
|
public function polling()
|
|
|
|
|
{
|
|
|
|
|
$this->hydrateActivity();
|
2023-05-08 07:16:50 +00:00
|
|
|
$exit_code = data_get($this->activity, 'properties.exitCode');
|
|
|
|
|
if ($exit_code !== null) {
|
2024-01-16 14:19:14 +00:00
|
|
|
$this->isPollingActive = false;
|
2025-01-07 14:31:43 +00:00
|
|
|
if ($exit_code === 0) {
|
|
|
|
|
if ($this->eventToDispatch !== null) {
|
|
|
|
|
if (str($this->eventToDispatch)->startsWith('App\\Events\\')) {
|
|
|
|
|
$causer_id = data_get($this->activity, 'causer_id');
|
|
|
|
|
$user = User::find($causer_id);
|
|
|
|
|
if ($user) {
|
2025-12-28 12:26:52 +00:00
|
|
|
$teamId = data_get($this->activity, 'properties.team_id')
|
|
|
|
|
?? $user->currentTeam()?->id
|
|
|
|
|
?? $user->teams->first()?->id;
|
|
|
|
|
if ($teamId && ! self::$eventDispatched) {
|
2025-06-02 13:36:37 +00:00
|
|
|
if (filled($this->eventData)) {
|
|
|
|
|
$this->eventToDispatch::dispatch($teamId, $this->eventData);
|
|
|
|
|
} else {
|
|
|
|
|
$this->eventToDispatch::dispatch($teamId);
|
|
|
|
|
}
|
2025-05-15 20:21:46 +00:00
|
|
|
self::$eventDispatched = true;
|
2025-01-07 14:31:43 +00:00
|
|
|
}
|
2024-01-16 14:19:14 +00:00
|
|
|
}
|
2025-01-07 13:52:08 +00:00
|
|
|
|
2025-01-07 14:31:43 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2025-05-15 20:21:46 +00:00
|
|
|
if (! self::$eventDispatched) {
|
2025-06-02 13:36:37 +00:00
|
|
|
if (filled($this->eventData)) {
|
|
|
|
|
$this->dispatch($this->eventToDispatch, $this->eventData);
|
|
|
|
|
} else {
|
|
|
|
|
$this->dispatch($this->eventToDispatch);
|
|
|
|
|
}
|
2025-05-15 20:21:46 +00:00
|
|
|
self::$eventDispatched = true;
|
|
|
|
|
}
|
2024-01-16 14:19:14 +00:00
|
|
|
}
|
2023-05-08 07:16:50 +00:00
|
|
|
}
|
2023-05-03 05:23:45 +00:00
|
|
|
}
|
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|