Skip to content

Commit 49a91c2

Browse files
authored
Merge pull request #5 from niladam/feature/allow-custom-position-and-use-badge
2 parents 3609cb5 + ed69b07 commit 49a91c2

File tree

7 files changed

+105
-90
lines changed

7 files changed

+105
-90
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ This is the contents of the published config file:
2626

2727
```php
2828
use Carbon\Carbon;
29-
use Niladam\FilamentAutoLogout\Enums\AutoLogoutPosition;
29+
use Filament\View\PanelsRenderHook;
3030

3131
return [
3232
/**
@@ -64,31 +64,31 @@ return [
6464
'time_left_text' => env('FILAMENT_AUTO_LOGOUT_TIME_LEFT_TEXT', 'Time left:'),
6565

6666
/**
67-
* The position of the time left box.
67+
* Where should the badge be rendered?
6868
*
69-
* Defaults to right.
70-
*
71-
* Currently only 'right' and 'left' (bottom) are supported
69+
* @see https://filamentphp.com/docs/3.x/support/render-hooks#available-render-hooks for a list of supported hooks.
7270
*/
73-
'time_left_position' => env('FILAMENT_AUTO_LOGOUT_TIME_LEFT_POSITION', AutoLogoutPosition::BOTTOM_RIGHT),
71+
'location' => env('FILAMENT_AUTO_LOGOUT_LOCATION', PanelsRenderHook::GLOBAL_SEARCH_BEFORE),
7472
];
7573
```
7674

7775
## Usage
7876

7977
```php
8078
use Carbon\Carbon;
79+
use Filament\Support\Colors\Color;
8180
use Niladam\FilamentAutoLogout\AutoLogoutPlugin;
8281

8382
$panel
8483
->plugins([
8584
AutoLogoutPlugin::make()
86-
->positionLeft() // Align the time left box to the left. Defaults to right.
85+
->color(Color::Emerald) // Set the color. Defaults to Zinc
8786
->disableIf(fn () => auth()->id() === 1) // Disable the user with ID 1
8887
->logoutAfter(Carbon::SECONDS_PER_MINUTE * 5) // Logout the user after 5 minutes
89-
->withoutWarning() // Disable the warning
88+
->withoutWarning() // Disable the warning before logging out
9089
->withoutTimeLeft() // Disable the time left
9190
->timeLeftText('Oh no. Kicking you in...') // Change the time left text
91+
->timeLeftText('') // Remove the time left text (displays only countdown)
9292
]);
9393
```
9494

config/filament-auto-logout.php

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
use Carbon\Carbon;
4-
use Niladam\FilamentAutoLogout\Enums\AutoLogoutPosition;
4+
use Filament\View\PanelsRenderHook;
55

66
return [
77
/**
@@ -39,22 +39,9 @@
3939
'time_left_text' => env('FILAMENT_AUTO_LOGOUT_TIME_LEFT_TEXT', 'Time left:'),
4040

4141
/**
42-
* The position of the time left box.
42+
* Where should the badge be rendered?
4343
*
44-
* Defaults to right.
45-
*
46-
* Currently only 'right' and 'left' (bottom) are supported
47-
*/
48-
'time_left_position' => env('FILAMENT_AUTO_LOGOUT_TIME_LEFT_POSITION', AutoLogoutPosition::BOTTOM_RIGHT),
49-
50-
/**
51-
* The route name where the logout will be posted.
52-
*
53-
* Defaults to filament's logout route & controller, but uses a custom route name
54-
* to ensure it doesn't conflict with any other logout routes
55-
*
56-
* You can overwrite this here but ensure that the route accepts a POST request
57-
* and handles the logout process entirely.
44+
* @see https://filamentphp.com/docs/3.x/support/render-hooks#available-render-hooks for a list of supported hooks.
5845
*/
59-
'auto_logout_route_name' => env('FILAMENT_AUTO_LOGOUT_ROUTE_NAME', 'filament-auto-logout-plugin-form'),
46+
'location' => env('FILAMENT_AUTO_LOGOUT_LOCATION', PanelsRenderHook::GLOBAL_SEARCH_BEFORE),
6047
];

resources/dist/filament-auto-logout.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/js/index.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
},
5454
showWarning() {
5555
if (!this.warningDisplayed) {
56-
5756
const totalSeconds = this.warningBefore;
5857
const minutes = Math.floor(totalSeconds / 60);
5958
const seconds = totalSeconds % 60;
@@ -150,32 +149,38 @@
150149
}
151150
});
152151

153-
if (!document.getElementById('idle-timeout-element')) {
154-
if (form.dataset.showTimeleft === '1') {
155-
const el = document.createElement('div');
156-
el.setAttribute('x-data', '{}');
152+
if (form.dataset.showTimeleft === '1') {
153+
let el = document.getElementById('idle-timeout-element');
154+
155+
if (!el) {
156+
el = document.createElement('div');
157157
el.id = 'idle-timeout-element';
158+
document.body.appendChild(el);
159+
}
160+
161+
const timeLeftText = form.dataset.timeLeftText || null;
158162

159-
const timeLeftText = form.dataset.timeLeftText || 'Time left: '; // Fallback to default text
163+
el.setAttribute(
164+
'x-data',
165+
JSON.stringify({
166+
timeLeftText: timeLeftText,
167+
})
168+
);
160169

161-
el.setAttribute(
162-
'x-data',
163-
JSON.stringify({
164-
timeLeftText: timeLeftText, // Pass it into the Alpine scope
165-
})
166-
);
170+
let timerDisplay = el.querySelector('#timer-display');
167171

168-
const timerDisplay = document.createElement('div');
172+
if (!timerDisplay) {
173+
timerDisplay = document.createElement('div');
169174
timerDisplay.id = 'timer-display';
170-
timerDisplay.setAttribute(
171-
'x-text',
172-
'timeLeftText + Math.floor($store.idleTimeoutStore.timeLeftSecs / 60) + "m " + ($store.idleTimeoutStore.timeLeftSecs % 60) + "s"'
173-
);
174175
el.appendChild(timerDisplay);
175-
176-
document.body.appendChild(el);
177-
Alpine.initTree(el);
178176
}
177+
178+
timerDisplay.setAttribute(
179+
'x-text',
180+
'timeLeftText + Math.floor($store.idleTimeoutStore.timeLeftSecs / 60) + "m " + ($store.idleTimeoutStore.timeLeftSecs % 60) + "s"'
181+
);
182+
183+
Alpine.initTree(el);
179184
}
180185
}
181186
});

resources/views/main.blade.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<script src="{{ \Filament\Support\Facades\FilamentAsset::getAlpineComponentSrc('filament-auto-logout', 'niladam/filament-auto-logout') }}"></script>
33
<form id="auto-logout-form"
44
data-auto-logout-enabled="{{ $enabled }}"
5-
action="{{ route($route_name) }}"
5+
action="{{ $logout_url }}"
66
data-duration="{{ $duration }}"
77
data-warn-before="{{ $warn_before }}"
88
data-show-timeleft="{{ $show_time_left }}"
@@ -11,16 +11,22 @@
1111
@csrf
1212
</form>
1313

14-
<style>
15-
#idle-timeout-element {
16-
position: fixed;
17-
bottom: 50px;
18-
padding: 6px;
19-
background: #333;
20-
color: #fff;
21-
z-index: 20;
22-
{{ $position }}: 10px;
23-
border-radius: 5px;
24-
}
25-
</style>
14+
@if($show_time_left)
15+
<div id="idle-timeout-element"
16+
class="
17+
idle-timeout-element hidden sm:flex items-center h-9 px-3 text-sm font-medium
18+
rounded-lg shadow-sm ring-1
19+
ring-custom-600/20 bg-custom-50 text-custom-600
20+
dark:ring-custom-400/30 dark:bg-custom-400/10 dark:text-custom-400
21+
text-center
22+
"
23+
style="
24+
--c-50: {{ $color[50] }};
25+
--c-300: {{ $color[300] }};
26+
--c-400: {{ $color[400] }};
27+
--c-600: {{ $color[600] }};
28+
"
29+
>
30+
</div>
31+
@endif
2632
@endauth

src/AutoLogoutPlugin.php

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
use Closure;
66
use Filament\Contracts\Plugin;
77
use Filament\Panel;
8+
use Filament\Support\Colors\Color;
89
use Filament\Support\Concerns\EvaluatesClosures;
910
use Filament\View\PanelsRenderHook;
1011
use Illuminate\Support\Facades\View;
11-
use Niladam\FilamentAutoLogout\Enums\AutoLogoutPosition;
12+
use ReflectionClass;
1213

1314
class AutoLogoutPlugin implements Plugin
1415
{
@@ -24,10 +25,12 @@ class AutoLogoutPlugin implements Plugin
2425

2526
public int | Closure $warnBeforeSeconds = 30;
2627

27-
public AutoLogoutPosition | Closure $position = AutoLogoutPosition::BOTTOM_RIGHT;
28+
public array | Closure $color = Color::Zinc;
2829

2930
public ?string $timeleftText = null;
3031

32+
private string $location = PanelsRenderHook::GLOBAL_SEARCH_BEFORE;
33+
3134
public function getId(): string
3235
{
3336
return 'filament-auto-logout';
@@ -36,8 +39,8 @@ public function getId(): string
3639
public function register(Panel $panel): void
3740
{
3841
$panel->renderHook(
39-
PanelsRenderHook::BODY_END,
40-
fn () => $this->renderView()
42+
name: $this->getLocation(),
43+
hook: fn () => $this->renderView($panel->getLogoutUrl())
4144
);
4245
}
4346

@@ -47,17 +50,17 @@ public function boot(Panel $panel): void
4750

4851
}
4952

50-
protected function renderView(): string
53+
protected function renderView(string $logoutUrl): string
5154
{
5255
return View::make('filament-auto-logout::main', [
5356
'enabled' => $this->evaluate($this->enabled),
5457
'has_warning' => $this->evaluate($this->hasWarning),
5558
'show_time_left' => $this->evaluate($this->showTimeLeft),
5659
'duration' => $this->evaluate($this->duration),
5760
'warn_before' => $this->evaluate($this->hasWarning) ? $this->evaluate($this->warnBeforeSeconds) : 0,
58-
'position' => $this->position->getPosition(),
5961
'time_left_text' => $this->timeleftText,
60-
'route_name' => config('filament-auto-logout.auto_logout_route_name'),
62+
'color' => $this->getColor(),
63+
'logout_url' => $logoutUrl,
6164
])->render();
6265
}
6366

@@ -111,13 +114,6 @@ public function warnBefore(int | Closure $warnBefore): static
111114
return $this;
112115
}
113116

114-
public function positionLeft(AutoLogoutPosition $position = AutoLogoutPosition::BOTTOM_LEFT): static
115-
{
116-
$this->position = $position;
117-
118-
return $this;
119-
}
120-
121117
public function logoutAfter(int | Closure $duration): static
122118
{
123119
$this->duration = $duration instanceof Closure
@@ -133,4 +129,42 @@ public function timeLeftText(?string $timeLeftText): static
133129

134130
return $this;
135131
}
132+
133+
public function color(array | Closure $color = Color::Zinc): static
134+
{
135+
$this->color = $color;
136+
137+
return $this;
138+
}
139+
140+
protected function getColor(): array
141+
{
142+
return $this->evaluate($this->color);
143+
}
144+
145+
public function location(string $location = PanelsRenderHook::GLOBAL_SEARCH_BEFORE): static
146+
{
147+
$this->location = $this->isValidPanelHook($location)
148+
? $location
149+
: PanelsRenderHook::GLOBAL_SEARCH_BEFORE;
150+
151+
return $this;
152+
}
153+
154+
protected function isValidPanelHook(string $location): bool
155+
{
156+
static $validLocations = null;
157+
158+
if ($validLocations === null) {
159+
$reflection = new ReflectionClass(PanelsRenderHook::class);
160+
$validLocations = array_values($reflection->getConstants());
161+
}
162+
163+
return in_array($location, $validLocations, true);
164+
}
165+
166+
protected function getLocation(): string
167+
{
168+
return $this->evaluate($this->location);
169+
}
136170
}

src/Enums/AutoLogoutPosition.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)