From 1e56ac2f8fdce02dbb8e0efd85878efd81cec1e9 Mon Sep 17 00:00:00 2001 From: edalzell Date: Tue, 13 Oct 2026 18:45:58 -0700 Subject: [PATCH 1/3] Share chat config between the CP script and the config endpoint --- src/ChatConfig.php | 20 +++++++++++ src/Http/Controllers/ConfigController.php | 17 ++------- src/ServiceProvider.php | 35 +++---------------- .../Http/Controllers/ConfigControllerTest.php | 6 ++++ tests/ServiceProviderTest.php | 34 ++++++++++++++++++ 5 files changed, 66 insertions(+), 46 deletions(-) create mode 100644 src/ChatConfig.php create mode 100644 tests/ServiceProviderTest.php diff --git a/src/ChatConfig.php b/src/ChatConfig.php new file mode 100644 index 0000000..268044f --- /dev/null +++ b/src/ChatConfig.php @@ -0,0 +1,20 @@ +email(); + + return [ + 'chatId' => config('front.chat.id'), + 'email' => $email, + 'hash' => $email ? hash_hmac('sha256', $email, config('front.secret_key')) : null, + 'name' => User::current()?->name(), + ]; + } +} diff --git a/src/Http/Controllers/ConfigController.php b/src/Http/Controllers/ConfigController.php index 67c778e..cf9da9d 100644 --- a/src/Http/Controllers/ConfigController.php +++ b/src/Http/Controllers/ConfigController.php @@ -3,28 +3,15 @@ namespace TransformStudios\Front\Http\Controllers; use Illuminate\Http\JsonResponse; -use Statamic\Facades\User; +use TransformStudios\Front\ChatConfig; class ConfigController { public function __invoke(): JsonResponse { - $email = null; - $hash = null; - $name = null; - - if ($user = User::current()) { - $email = $user->email(); - $hash = hash_hmac('sha256', $email, config('front.secret_key')); - $name = $user->get('name'); - } - return response()->json([ 'configured' => $this->isConfigured(), - 'chatId' => config('front.chat.id'), - 'email' => $email, - 'name' => $name, - 'hash' => $hash, + ...ChatConfig::forCurrentUser(), ]); } diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 33dd752..452ae68 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -2,11 +2,9 @@ namespace TransformStudios\Front; -use Illuminate\Support\Facades\View; -use Statamic\Facades\User as UserFacade; +use Statamic\Facades\User; use Statamic\Providers\AddonServiceProvider; use Statamic\Statamic; -use Statamic\Support\Arr; class ServiceProvider extends AddonServiceProvider { @@ -20,41 +18,16 @@ class ServiceProvider extends AddonServiceProvider 'resources/js/cp.js', ], 'publicDirectory' => 'dist', - 'hotFile' => __DIR__.'/../dist/hot', ]; public function bootAddon() - { - $this->bootScript(); - - // needed for testing but not production - // $this->loadViewsFrom( - // __DIR__.'/../resources/views', - // 'front' - // ); - } - - private function bootScript() { if (! config('front.chat.show_on_control_panel')) { return; } - View::composer('statamic::layout', function ($view) { - if (! $user = UserFacade::current()) { - return; - } - - Statamic::provideToScript( - [ - 'front' => Arr::removeNullValues([ - 'chatId' => config('front.chat.id'), - 'email' => $user->email(), - 'hash' => hash_hmac('sha256', $user->email(), config('front.secret_key')), - 'name' => $user->name ?? $user->first_name.' '.$user->last_name, - ]), - ] - ); - }); + Statamic::provideToScript([ + 'front' => fn () => User::current() ? ChatConfig::forCurrentUser() : null, + ]); } } diff --git a/tests/Http/Controllers/ConfigControllerTest.php b/tests/Http/Controllers/ConfigControllerTest.php index 918c00e..6b9f542 100644 --- a/tests/Http/Controllers/ConfigControllerTest.php +++ b/tests/Http/Controllers/ConfigControllerTest.php @@ -23,6 +23,12 @@ ]); }); +it('falls back to first and last name', function () { + $user = tap(User::make()->email('erin@transformstudios.com')->set('first_name', 'Erin')->set('last_name', 'Dalzell'))->save(); + + $this->actingAs($user)->getJson('/!/front/config')->assertJson(['name' => 'Erin Dalzell']); +}); + it('returns nulls for a guest', function () { $this->getJson('/!/front/config') ->assertOk() diff --git a/tests/ServiceProviderTest.php b/tests/ServiceProviderTest.php new file mode 100644 index 0000000..cef6948 --- /dev/null +++ b/tests/ServiceProviderTest.php @@ -0,0 +1,34 @@ +set('front.secret_key', 'secret'); + config()->set('front.chat.id', 'chat-id'); +}); + +it('provides chat config to the control panel script for the current user', function () { + $user = tap(User::make()->email('erin@transformstudios.com')->set('name', 'Erin'))->save(); + + $this->actingAs($user); + + expect(Statamic::jsonVariables(request())['front'])->toBe([ + 'chatId' => 'chat-id', + 'email' => 'erin@transformstudios.com', + 'hash' => hash_hmac('sha256', 'erin@transformstudios.com', 'secret'), + 'name' => 'Erin', + ]); +}); + +it('falls back to first and last name', function () { + $user = tap(User::make()->email('erin@transformstudios.com')->set('first_name', 'Erin')->set('last_name', 'Dalzell'))->save(); + + $this->actingAs($user); + + expect(Statamic::jsonVariables(request())['front']['name'])->toBe('Erin Dalzell'); +}); + +it('provides nothing for a guest', function () { + expect(Statamic::jsonVariables(request())['front'])->toBeNull(); +}); From e71f5c54dd7b11ed053be2e465e95c72784f1bd4 Mon Sep 17 00:00:00 2001 From: edalzell Date: Tue, 13 Oct 2026 18:46:05 -0700 Subject: [PATCH 2/3] Drop CP chat CSS that never matched --- dist/build/assets/cp-aa7cd9af.css | 1 + dist/build/assets/cp-b7ada135.css | 1 - dist/build/manifest.json | 2 +- resources/css/cp.css | 11 +---------- 4 files changed, 3 insertions(+), 12 deletions(-) create mode 100644 dist/build/assets/cp-aa7cd9af.css delete mode 100644 dist/build/assets/cp-b7ada135.css diff --git a/dist/build/assets/cp-aa7cd9af.css b/dist/build/assets/cp-aa7cd9af.css new file mode 100644 index 0000000..75f9119 --- /dev/null +++ b/dist/build/assets/cp-aa7cd9af.css @@ -0,0 +1 @@ +#front-chat-iframe{z-index:1!important} diff --git a/dist/build/assets/cp-b7ada135.css b/dist/build/assets/cp-b7ada135.css deleted file mode 100644 index 4636458..0000000 --- a/dist/build/assets/cp-b7ada135.css +++ /dev/null @@ -1 +0,0 @@ -#front-chat-container{z-index:100!important}#front-chat-container .fc-1_tjF{display:none!important}#front-chat-iframe{z-index:1!important} diff --git a/dist/build/manifest.json b/dist/build/manifest.json index 09220fd..c59b6c2 100644 --- a/dist/build/manifest.json +++ b/dist/build/manifest.json @@ -1,6 +1,6 @@ { "resources/css/cp.css": { - "file": "assets/cp-b7ada135.css", + "file": "assets/cp-aa7cd9af.css", "isEntry": true, "src": "resources/css/cp.css" }, diff --git a/resources/css/cp.css b/resources/css/cp.css index c73ea20..582dfce 100644 --- a/resources/css/cp.css +++ b/resources/css/cp.css @@ -1,12 +1,3 @@ -#front-chat-container { - z-index: 100 !important -} - -/* !Front Chat */ -#front-chat-container .fc-1_tjF { - display: none!important; -} - #front-chat-iframe { - z-index: 1!important; + z-index: 1 !important; } From acf4ce18f0676047bdc0d17e009563cb14c027b6 Mon Sep 17 00:00:00 2001 From: edalzell Date: Tue, 13 Oct 2026 18:46:19 -0700 Subject: [PATCH 3/3] Drop unused Vue deps and update Vite --- .../assets/{cp-aa7cd9af.css => cp-DPchgS2m.css} | 0 .../assets/{cp-4834b1d9.js => cp-peOtejVL.js} | 0 dist/build/manifest.json | 15 ++++++++++----- package.json | 8 ++------ 4 files changed, 12 insertions(+), 11 deletions(-) rename dist/build/assets/{cp-aa7cd9af.css => cp-DPchgS2m.css} (100%) rename dist/build/assets/{cp-4834b1d9.js => cp-peOtejVL.js} (100%) diff --git a/dist/build/assets/cp-aa7cd9af.css b/dist/build/assets/cp-DPchgS2m.css similarity index 100% rename from dist/build/assets/cp-aa7cd9af.css rename to dist/build/assets/cp-DPchgS2m.css diff --git a/dist/build/assets/cp-4834b1d9.js b/dist/build/assets/cp-peOtejVL.js similarity index 100% rename from dist/build/assets/cp-4834b1d9.js rename to dist/build/assets/cp-peOtejVL.js diff --git a/dist/build/manifest.json b/dist/build/manifest.json index c59b6c2..7d10e5e 100644 --- a/dist/build/manifest.json +++ b/dist/build/manifest.json @@ -1,12 +1,17 @@ { "resources/css/cp.css": { - "file": "assets/cp-aa7cd9af.css", + "file": "assets/cp-DPchgS2m.css", + "src": "resources/css/cp.css", "isEntry": true, - "src": "resources/css/cp.css" + "name": "cp", + "names": [ + "cp.css" + ] }, "resources/js/cp.js": { - "file": "assets/cp-4834b1d9.js", - "isEntry": true, - "src": "resources/js/cp.js" + "file": "assets/cp-peOtejVL.js", + "name": "cp", + "src": "resources/js/cp.js", + "isEntry": true } } \ No newline at end of file diff --git a/package.json b/package.json index 1f0b1ca..a0399e1 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,11 @@ { - "dependencies": { - "vue": "^2.6.11" - }, "scripts": { "dev": "vite", "build": "vite build", "production": "vite build" }, "devDependencies": { - "@vitejs/plugin-vue2": "^2.2.0", - "laravel-vite-plugin": "^0.7.2", - "vite": "^4.0.0" + "laravel-vite-plugin": "^2.0.0", + "vite": "^7.0.0" } }