Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dist/build/assets/cp-DPchgS2m.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#front-chat-iframe{z-index:1!important}
1 change: 0 additions & 1 deletion dist/build/assets/cp-b7ada135.css

This file was deleted.

File renamed without changes.
15 changes: 10 additions & 5 deletions dist/build/manifest.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
{
"resources/css/cp.css": {
"file": "assets/cp-b7ada135.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
}
}
8 changes: 2 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
11 changes: 1 addition & 10 deletions resources/css/cp.css
Original file line number Diff line number Diff line change
@@ -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;
}
20 changes: 20 additions & 0 deletions src/ChatConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace TransformStudios\Front;

use Statamic\Facades\User;

class ChatConfig
{
public static function forCurrentUser(): array
{
$email = User::current()?->email();

return [
'chatId' => config('front.chat.id'),
'email' => $email,
'hash' => $email ? hash_hmac('sha256', $email, config('front.secret_key')) : null,
'name' => User::current()?->name(),
];
}
}
17 changes: 2 additions & 15 deletions src/Http/Controllers/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
]);
}

Expand Down
35 changes: 4 additions & 31 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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,
]);
}
}
6 changes: 6 additions & 0 deletions tests/Http/Controllers/ConfigControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
34 changes: 34 additions & 0 deletions tests/ServiceProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Statamic\Facades\User;
use Statamic\Statamic;

beforeEach(function () {
config()->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();
});
Loading