Skip to content

Commit e0ec0ba

Browse files
committed
Fix some issues and set locale based on chosen tab
1 parent 5b70493 commit e0ec0ba

File tree

4 files changed

+70
-22
lines changed

4 files changed

+70
-22
lines changed

composer.json

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@
99
"homepage": "https://github.com/wotz/filament-live-preview",
1010
"license": "MIT",
1111
"require": {
12-
"php": "^8.2",
13-
"illuminate/contracts": "^10.0 || ^11.0 || ^12.0",
14-
"livewire/livewire": "^3.6 || ^4.0",
15-
"pboivin/filament-peek": "dev-filament-v5",
12+
"php": "^8.4",
13+
"illuminate/contracts": "^12.0",
14+
"livewire/livewire": "^4.0",
15+
"pboivin/filament-peek": "dev-filament-5 as 4.0",
1616
"spatie/laravel-package-tools": "^1.12"
1717
},
1818
"require-dev": {
19-
"larastan/larastan": "^2.0",
19+
"larastan/larastan": "^3.0",
2020
"laravel/pint": "^1.0",
21-
"nunomaduro/collision": "^7.0 || ^8.0",
22-
"orchestra/testbench": "^8.0 || ^9.0",
23-
"pestphp/pest": "^2.0",
24-
"pestphp/pest-plugin-laravel": "^2.0",
21+
"nunomaduro/collision": "^8.0",
22+
"orchestra/testbench": "^10.0",
23+
"pestphp/pest": "^4.0",
24+
"pestphp/pest-plugin-laravel": "^4.0",
2525
"phpstan/extension-installer": "^1.1",
26-
"phpstan/phpstan-deprecation-rules": "^1.0",
27-
"phpstan/phpstan-phpunit": "^1.0"
26+
"phpstan/phpstan-deprecation-rules": "^2.0",
27+
"phpstan/phpstan-phpunit": "^2.0"
2828
},
2929
"autoload": {
3030
"psr-4": {
@@ -55,11 +55,5 @@
5555
}
5656
},
5757
"minimum-stability": "dev",
58-
"prefer-stable": true,
59-
"repositories": [
60-
{
61-
"type": "vcs",
62-
"url": "git@github.com:thibautdeg/filament-peek.git"
63-
}
64-
]
58+
"prefer-stable": true
6559
}

src/CachedPreview.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Wotz\FilamentLivePreview;
4+
5+
use Illuminate\Support\Facades\Cache;
6+
7+
class CachedPreview
8+
{
9+
public static ?string $cacheStore = null;
10+
11+
public static int $cacheDuration = 60;
12+
13+
public function __construct(
14+
public string $pageClass,
15+
public string $view,
16+
public array $data,
17+
public ?string $locale = null,
18+
) {}
19+
20+
public static function make(
21+
string $pageClass,
22+
string $view,
23+
array $data,
24+
?string $locale = null,
25+
): CachedPreview {
26+
return new CachedPreview($pageClass, $view, $data, $locale);
27+
}
28+
29+
public function put(string $token, ?int $ttl = null): bool
30+
{
31+
$ttl ??= self::$cacheDuration;
32+
33+
return Cache::store(static::$cacheStore)->put("filament-peek-preview-{$token}", $this, $ttl);
34+
}
35+
36+
public static function get(string $token): ?CachedPreview
37+
{
38+
return Cache::store(static::$cacheStore)->get("filament-peek-preview-{$token}");
39+
}
40+
}

src/Filament/Traits/HasLivePreviewComponent.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
use Filament\Support\Exceptions\Halt;
66
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Http\Request;
78
use InvalidArgumentException;
89
use Livewire\Attributes\On;
9-
use Pboivin\FilamentPeek\CachedPreview;
1010
use Pboivin\FilamentPeek\Facades\Peek;
1111
use Pboivin\FilamentPeek\Support\Cache;
12+
use Wotz\FilamentLivePreview\CachedPreview;
1213
use Wotz\FilamentLivePreview\Events\RefreshLivePreview;
1314

1415
trait HasLivePreviewComponent
@@ -114,7 +115,11 @@ public function openPreview(): void
114115
} elseif (($view = $this->getPreviewModalView()) && config('filament-peek.internalPreviewUrl.enabled', false)) {
115116
$this->token = app(Cache::class)->createPreviewToken();
116117

117-
CachedPreview::make(static::class, $view, $this->previewModalData)
118+
$request = Request::create(request()->header('referer'));
119+
120+
$locale = $request->query('locale');
121+
122+
CachedPreview::make(static::class, $view, $this->previewModalData, $locale)
118123
->put($this->token, config('filament-peek.internalPreviewUrl.cacheDuration', 60));
119124

120125
$previewModalUrl = route('live-preview-frame', [
@@ -180,7 +185,11 @@ public function refreshPreview(): void
180185
$this->previewModalData = $this->mutatePreviewModalData($this->preparePreviewModalData());
181186

182187
if (($view = $this->getPreviewModalView()) && config('filament-peek.internalPreviewUrl.enabled', false)) {
183-
CachedPreview::make(static::class, $view, $this->previewModalData)
188+
$request = Request::create(request()->header('referer'));
189+
190+
$locale = $request->query('locale');
191+
192+
CachedPreview::make(static::class, $view, $this->previewModalData, $locale)
184193
->put($this->token, config('filament-peek.internalPreviewUrl.cacheDuration', 60));
185194
} else {
186195
throw new InvalidArgumentException('Missing preview modal URL or Blade view.');

src/Livewire/LivePreviewScreen.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
use Livewire\Attributes\Layout;
66
use Livewire\Attributes\On;
77
use Livewire\Component;
8-
use Pboivin\FilamentPeek\CachedPreview;
8+
use Wotz\FilamentLivePreview\CachedPreview;
9+
use Wotz\LocaleCollection\Facades\LocaleCollection;
910

1011
class LivePreviewScreen extends Component
1112
{
@@ -39,6 +40,10 @@ private function refreshPreview()
3940
{
4041
abort_unless((bool) $preview = CachedPreview::get($this->token), 404);
4142

43+
if ($preview->locale) {
44+
app()->setLocale($preview->locale);
45+
}
46+
4247
$this->view = $preview->view;
4348
$this->data = $preview->data;
4449

0 commit comments

Comments
 (0)