mailwolt/app/Livewire/System/TaskBanner.php

201 lines
5.7 KiB
PHP

<?php
namespace App\Livewire\System;
use App\Models\SystemTask;
use Illuminate\Support\Facades\Cache;
use Livewire\Attributes\On;
use Livewire\Component;
class TaskBanner extends Component
{
// public ?array $task = null;
// public string $taskKey;
// public bool $finished = false;
// public string $position;
//
// public function mount(string $taskKey, string $position = 'top-right')
// {
// $this->taskKey = $taskKey;
// $this->position = $position;
// }
//
// public function loadTask()
// {
// $this->task = Cache::store('redis')->get($this->taskKey);
//
// if ($this->task && in_array($this->task['status'], ['done','failed'])) {
// $this->finished = true;
// }
// }
//
public string $taskKey;
public ?array $task = null;
// damit wir nur bei echtem Statuswechsel einen Toast schicken
public ?string $lastStatus = null;
public function mount(string $taskKey)
{
$this->taskKey = $taskKey;
}
public function loadTask()
{
$t = Cache::store('redis')->get($this->taskKey);
$this->task = $t;
if (!$t) return;
$status = $t['status'] ?? 'queued';
if ($status !== $this->lastStatus) {
$this->lastStatus = $status;
$type = match ($status) {
'done' => 'success',
'failed' => 'error',
'running' => 'update', // „spinner“
'queued' => 'info',
default => 'info',
};
$this->dispatch('notify', [
'id' => $this->taskKey, // <- gleiches ID = replace
'title' => strtoupper($t['type'] ?? 'TASK').' · '.($t['payload']['domain'] ?? ''),
'badge' => 'CERTBOT',
'message' => $t['message'] ?? '',
'type' => $type,
'duration' => in_array($status, ['done','failed']) ? 6000 : -1, // running = sticky
'close' => true,
// Phosphor Icon (optional):
'icon' => match ($status) {
'done' => 'ph-check-circle',
'failed' => 'ph-x-circle',
'running'=> 'ph-arrow-clockwise',
default => 'ph-info',
},
]);
}
// Pollen stoppen, sobald final
if (in_array($status, ['done','failed'])) {
$this->dispatch('stop-poll'); // falls du den Poll im Frontend beenden willst
}
}
public function render()
{
return view('livewire.system.task-banner');
}
// public string $taskKey;
//
// public ?SystemTask $task = null;
//
// public function mount(string $taskKey)
// {
// $this->taskKey = $taskKey;
// $this->loadTask();
// }
//
// public function loadTask()
// {
// $this->task = SystemTask::where('key', $this->taskKey)->first();
// }
//
// public function getFinishedProperty(): bool
// {
// return in_array(optional($this->task)->status, ['done','failed'], true);
// }
// public function render()
// {
// $status = $this->taskKey
// ? cache()->get("tasks:{$this->taskKey}", 'unbekannt')
// : null;
//
// return view('livewire.system.task-banner', [
// 'status' => $status,
// ]);
//
//// return view('livewire.system.task-banner');
// }
// public ?string $taskKey = null;
//
// /** @var array|null */
// public $task = null;
//
// public bool $finished = false;
//
// public function mount(?string $taskKey = null): void
// {
// // aus Session übernehmen, falls nicht als Prop übergeben
// $this->taskKey = $taskKey ?? session('task_key');
// $this->loadTask();
// }
//
// #[On('refresh-task-banner')]
// public function loadTask(): void
// {
// if (!$this->taskKey) {
// $this->task = null;
// return;
// }
//
// // Erwartete Cache-Struktur: array mit keys: type,status,payload,message
// $data = Cache::get("tasks:{$this->taskKey}");
//
// // Nichts gefunden -> Banner ausblenden
// if (!$data) {
// $this->task = null;
// return;
// }
//
// // Sicherstellen, dass wir array haben (kein Model nötig)
// $this->task = is_array($data) ? $data : (array) $data;
//
// // Fertig/Failed -> einmal anzeigen, dann Session-Key löschen
// if (in_array($this->task['status'] ?? '', ['done','failed'], true)) {
// $this->finished = true;
// session()->forget('task_key');
// }
// }
// public ?string $taskKey = null;
// public ?array $task = null;
//
// // Position: top-right|top-left|bottom-right|bottom-left
// public string $position = 'top-right';
//
// public bool $finished = false;
//
// public function mount(?string $taskKey = null, string $position = 'top-right'): void
// {
// $this->taskKey = $taskKey ?? session('task_key');
// $this->position = $position;
// $this->loadTask();
// }
//
// #[On('refresh-task-banner')]
// public function loadTask(): void
// {
// if (!$this->taskKey) { $this->task = null; return; }
//
// $data = Cache::get("tasks:{$this->taskKey}");
// $this->task = $data ? (array) $data : null;
//
// if ($this->task && in_array($this->task['status'] ?? '', ['done','failed'], true)) {
// $this->finished = true;
// session()->forget('task_key');
// }
// }
//
// public function dismiss(): void
// {
// $this->task = null;
// }
}