102 lines
3.3 KiB
PHP
102 lines
3.3 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Middleware;
|
||
|
|
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use Illuminate\Support\Facades\Cache;
|
||
|
|
use Inertia\Inertia;
|
||
|
|
use Inertia\Middleware;
|
||
|
|
|
||
|
|
class HandleInertiaRequests extends Middleware
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* The root template that's loaded on the first page visit.
|
||
|
|
*
|
||
|
|
* @see https://inertiajs.com/server-side-setup#root-template
|
||
|
|
*
|
||
|
|
* @var string
|
||
|
|
*/
|
||
|
|
protected $rootView = 'app';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Determines the current asset version.
|
||
|
|
*
|
||
|
|
* @see https://inertiajs.com/asset-versioning
|
||
|
|
*/
|
||
|
|
public function version(Request $request): ?string
|
||
|
|
{
|
||
|
|
return parent::version($request);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Define the props that are shared by default.
|
||
|
|
*
|
||
|
|
* @see https://inertiajs.com/shared-data
|
||
|
|
*
|
||
|
|
* @return array<string, mixed>
|
||
|
|
*/
|
||
|
|
public function share(Request $request): array
|
||
|
|
{
|
||
|
|
$user = $request->user();
|
||
|
|
|
||
|
|
$workspaces = $user
|
||
|
|
? $user->workspaces()
|
||
|
|
->orderBy('name')
|
||
|
|
->get(['workspaces.id', 'workspaces.name', 'workspaces.slug'])
|
||
|
|
->map(fn ($w) => [
|
||
|
|
'id' => $w->id,
|
||
|
|
'name' => $w->name,
|
||
|
|
'slug' => $w->slug,
|
||
|
|
])
|
||
|
|
->values()
|
||
|
|
->all()
|
||
|
|
: [];
|
||
|
|
|
||
|
|
$currentWorkspaceId = $request->session()->get('current_workspace_id');
|
||
|
|
$currentWorkspace = collect($workspaces)->firstWhere('id', $currentWorkspaceId)
|
||
|
|
?? ($workspaces[0] ?? null);
|
||
|
|
|
||
|
|
if (! $currentWorkspaceId && count($workspaces) > 0) {
|
||
|
|
$request->session()->put('current_workspace_id', $currentWorkspace['id']);
|
||
|
|
}
|
||
|
|
|
||
|
|
return [
|
||
|
|
...parent::share($request),
|
||
|
|
'flash' => $request->session()->get('flash'),
|
||
|
|
'name' => config('app.name'),
|
||
|
|
'auth' => [
|
||
|
|
'user' => $user,
|
||
|
|
'workspaces' => $workspaces,
|
||
|
|
'currentWorkspace' => $currentWorkspace,
|
||
|
|
],
|
||
|
|
'sidebarOpen' => ! $request->hasCookie('sidebar_state') || $request->cookie('sidebar_state') === 'true',
|
||
|
|
'userNotifications' => [
|
||
|
|
'unread_count' => $user ? Cache::remember(
|
||
|
|
"user:{$user->id}:unread_notifications",
|
||
|
|
60,
|
||
|
|
fn () => $user->unreadNotifications()->count()
|
||
|
|
) : 0,
|
||
|
|
'readUrl' => fn () => $user ? route('notifications.read', ['id' => '__ID__']) : null,
|
||
|
|
'readAllUrl' => fn () => $user ? route('notifications.readAll') : null,
|
||
|
|
'items' => Inertia::defer(function () use ($user) {
|
||
|
|
if (! $user) {
|
||
|
|
return [];
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
return $user->notifications()->latest()->take(10)->get()->map(fn ($n) => [
|
||
|
|
'id' => $n->id,
|
||
|
|
'type' => class_basename($n->type),
|
||
|
|
'data' => $n->data,
|
||
|
|
'read_at' => $n->read_at?->toISOString(),
|
||
|
|
'created_at' => $n->created_at->diffForHumans(),
|
||
|
|
])->all();
|
||
|
|
} catch (\Throwable) {
|
||
|
|
return [];
|
||
|
|
}
|
||
|
|
}),
|
||
|
|
],
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|