*/ public const SORTS = [ 'newest' => ['created_at', 'desc'], 'oldest' => ['created_at', 'asc'], 'expiring' => ['expires_at', 'asc'], 'largest' => ['total_size', 'desc'], 'most-downloaded' => ['download_count', 'desc'], 'most-files' => ['files_count', 'desc'], ]; public string $sort = 'newest'; /** The share the delete dialog is asking about, while it is open. */ public ?int $deletingShareId = null; public function deleteShare(int $shareId, ShareService $shareService): void { $share = Share::query()->findOrFail($shareId); $shareService->deleteShare($share); $this->deletingShareId = null; } /** * A new order starts again from the first page. */ public function updatedSort(): void { $this->resetPage(); } public function render(): mixed { $shareService = app(ShareService::class); // The sort comes from the browser: only a known order reaches the query. [$column, $direction] = self::SORTS[$this->sort] ?? self::SORTS['newest']; // Shares whose files are still being uploaded are not shares yet; their bytes do count as used space. $shares = Share::query() ->whereNotNull('completed_at') ->withCount('files') // Shares that never expire come after every share that does, whichever way expiry is sorted. ->when($column === 'expires_at', fn ($query) => $query->orderByRaw('expires_at is null')) ->orderBy($column, $direction) ->orderByDesc('id') ->paginate(15); return view('livewire.admin.admin-dashboard', [ 'shares' => $shares, 'totalShares' => Share::query()->whereNotNull('completed_at')->count(), 'activeShares' => Share::query()->whereNotNull('completed_at')->where(function ($q) { $q->whereNull('expires_at')->orWhere('expires_at', '>', now()); })->count(), 'totalFiles' => ShareFile::query()->whereHas('share', fn ($query) => $query->whereNotNull('completed_at'))->count(), 'usedSpace' => $shareService->getTotalUsedSpace(), 'maxQuota' => $shareService->getMaxStorageQuota(), ]); } }