| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- <?php
-
- namespace App\Http\Middleware;
-
- use App\Billing\UsageLimits;
- use App\Enums\OrderStatus;
- use App\Models\Store;
- use App\Models\User;
- use Illuminate\Http\Request;
- 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
- {
- return [
- ...parent::share($request),
- 'name' => config('app.name'),
- 'auth' => [
- 'user' => $request->user(),
- ],
- 'sidebarOpen' => ! $request->hasCookie('sidebar_state') || $request->cookie('sidebar_state') === 'true',
- // The store tree and the plan box in the sidebar of every dashboard page.
- 'sidebar' => fn () => $request->user() !== null ? $this->sidebar($request->user()) : null,
- ];
- }
-
- /**
- * @return array{stores: list<array<string, mixed>>, usage: array<string, mixed>}
- */
- private function sidebar(User $user): array
- {
- return [
- 'stores' => $user->stores()
- ->orderBy('created_at')
- ->withCount(['orders as to_ship' => fn ($query) => $query->where('status', OrderStatus::Paid)->whereNull('shipped_at')])
- ->get()
- ->map(fn (Store $store) => [
- 'name' => $store->name,
- 'slug' => $store->slug,
- 'is_published' => $store->isPublished(),
- 'to_ship' => (int) $store->getAttribute('to_ship'),
- ])
- ->all(),
- 'usage' => app(UsageLimits::class)->for($user),
- ];
- }
- }
|