-
-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathNodeClientConnectivity.php
More file actions
97 lines (80 loc) · 3.57 KB
/
Copy pathNodeClientConnectivity.php
File metadata and controls
97 lines (80 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
namespace App\Livewire;
use App\Enums\NodeJwtScope;
use App\Enums\TablerIcon;
use App\Models\Node;
use App\Services\Nodes\NodeJWTService;
use App\Services\Servers\GetUserPermissionsService;
use Filament\Support\Enums\IconSize;
use Filament\Tables\View\Components\Columns\IconColumnComponent\IconComponent;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\ComponentAttributeBag;
use Livewire\Attributes\Locked;
use Livewire\Component;
use function Filament\Support\generate_icon_html;
class NodeClientConnectivity extends Component
{
#[Locked]
public Node $node;
private GetUserPermissionsService $getUserPermissionsService;
private NodeJWTService $nodeJWTService;
public function boot(GetUserPermissionsService $getUserPermissionsService, NodeJWTService $nodeJWTService): void
{
$this->getUserPermissionsService = $getUserPermissionsService;
$this->nodeJWTService = $nodeJWTService;
}
public function render(): View
{
$httpUrl = $this->node->getConnectionAddress();
$wsUrl = null;
$wsToken = null;
$server = $this->node->servers()->first();
if ($server) {
$user = Auth::user();
$permissions = $this->getUserPermissionsService->handle($server, $user);
$wsToken = $this->nodeJWTService
->setExpiresAt(now()->addMinute()->toImmutable())
->setScopes(NodeJwtScope::Websocket)
->setUser($user)
->setClaims([
'server_uuid' => $server->uuid,
'permissions' => $permissions,
])
->handle($this->node, $user->id . $server->uuid)->toString();
$wsUrl = str_replace(['https://', 'http://'], ['wss://', 'ws://'], $this->node->getConnectionAddress());
$wsUrl .= sprintf('/api/servers/%s/ws', $server->uuid);
}
return view('livewire.node-client-connectivity', [
'httpUrl' => $httpUrl,
'wsUrl' => $wsUrl,
'wsToken' => $wsToken,
'loadingIcon' => $this->makeIcon(TablerIcon::WorldQuestion, 'warning', trans('admin/node.connectivity.checking')),
'offlineIcon' => $this->makeIcon(TablerIcon::WorldX, 'danger', trans('admin/node.connectivity.offline')),
'onlineIcon' => $this->makeIcon(TablerIcon::WorldCheck, 'success', trans('admin/node.connectivity.online')),
'warningIcon' => $this->makeIcon(TablerIcon::WorldExclamation, 'warning', trans('admin/node.connectivity.websocket_failed')),
'onlineNoWsIcon' => $this->makeIcon(TablerIcon::WorldCheck, 'success', trans('admin/node.connectivity.websocket_not_tested')),
]);
}
private function makeIcon(TablerIcon $icon, string $color, string $tooltip): string
{
return generate_icon_html($icon, attributes: (new ComponentAttributeBag())
->merge([
'x-tooltip' => '{
content: "' . $tooltip . '",
theme: $store.theme,
allowHTML: true,
placement: "bottom",
}',
'style' => 'color: var(--dark-text, var(--text))',
], escape: false)
->color(IconComponent::class, $color), size: IconSize::Large)
->toHtml();
}
public function placeholder(): string
{
return generate_icon_html(TablerIcon::WorldQuestion, attributes: (new ComponentAttributeBag())
->color(IconComponent::class, 'warning'), size: IconSize::Large)
->toHtml();
}
}