Skip to content
Open
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
74 changes: 62 additions & 12 deletions app/Http/Controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
use App\Models\User;
use App\Support\Dashboard\CompoundLibraryRankedStudiesQuery;
use App\Support\Dashboard\WorkspaceMoleculeAggregates;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Inertia\Inertia;

class DashboardController extends Controller
Expand Down Expand Up @@ -58,6 +60,7 @@ public function dashboard(DashboardIndexRequest $request)

if ($workspace !== 'default') {
[$workspaceProjects, $workspaceStudies] = $this->workspaceProjectAndStudyLists($user, $workspace);
$workspaceProjects = $this->decorateProjectsForDashboard($workspaceProjects, $user);

$hasProjects = $this->scopedProjectsQuery($user, $team)->exists();
$hasSamples = $this->scopedSamplesQuery($user, $team)->exists();
Expand All @@ -77,18 +80,21 @@ public function dashboard(DashboardIndexRequest $request)
}

$projectsQuery = $this->scopedProjectsQuery($user, $team)
->with(['users', 'owner', 'tags', 'draft'])
->with($this->dashboardProjectEagerLoads())
->orderByDesc('updated_at');

$this->applyStatusFilter($projectsQuery, $filters['projects_status']);
$this->applySearchToProjects($projectsQuery, $filters['projects_q']);

$projects = $projectsQuery->paginate(
$filters['projects_per_page'],
['*'],
'projects_page',
$filters['projects_page']
)->withQueryString();
$projects = $this->decorateProjectsForDashboard(
$projectsQuery->paginate(
$filters['projects_per_page'],
['*'],
'projects_page',
$filters['projects_page']
)->withQueryString(),
$user
);

$samplesQuery = $this->scopedSamplesQuery($user, $team)
->with([
Expand Down Expand Up @@ -134,7 +140,7 @@ protected function workspaceProjectAndStudyLists(User $user, string $workspace):
{
return match ($workspace) {
'shared' => [
$user->sharedProjects()->with(['owner', 'tags', 'draft'])->get()->all(),
$user->sharedProjects()->with($this->dashboardProjectEagerLoads())->get()->all(),
$user->sharedStudies()->with(['owner', 'sample.molecules'])->get()->all(),
],
'recent' => [
Expand All @@ -145,7 +151,7 @@ protected function workspaceProjectAndStudyLists(User $user, string $workspace):
Project::query()
->where('is_deleted', false)
->whereHasBookmark($user)
->with(['owner', 'tags', 'draft'])
->with($this->dashboardProjectEagerLoads())
->get()
->all(),
Study::query()
Expand All @@ -159,7 +165,7 @@ protected function workspaceProjectAndStudyLists(User $user, string $workspace):
Project::query()
->where('owner_id', $user->id)
->where('is_deleted', true)
->with(['owner', 'tags', 'draft'])
->with($this->dashboardProjectEagerLoads())
->get()
->all(),
[],
Expand All @@ -173,11 +179,12 @@ protected function workspaceProjectAndStudyLists(User $user, string $workspace):
*/
protected function recentProjectsForUser(User $user): array
{
$projects = $user->activeProjects()->with(['owner', 'tags', 'draft'])->get();
$eagerLoads = $this->dashboardProjectEagerLoads();
$projects = $user->activeProjects()->with($eagerLoads)->get();

foreach ($user->allTeams() as $teamModel) {
$projects = $projects->concat(
$teamModel->activeProjects()->with(['owner', 'tags', 'draft'])->get()
$teamModel->activeProjects()->with($eagerLoads)->get()
);
}

Expand Down Expand Up @@ -218,6 +225,49 @@ function (QueryBuilder $query) use ($user, $team): void {
);
}

/**
* @return array<int, string>
*/
protected function dashboardProjectEagerLoads(): array
{
return [
'users',
'owner',
'tags',
'draft',
'projectInvitations',
'team.owner',
'team.users',
];
}

/**
* @param LengthAwarePaginator<Project>|array<int, Project>|Collection<int, Project> $projects
* @return LengthAwarePaginator<Project>|array<int, Project>|Collection<int, Project>
*/
protected function decorateProjectsForDashboard(LengthAwarePaginator|array|Collection $projects, User $user): LengthAwarePaginator|array|Collection
{
$decorate = function (Project $project) use ($user): Project {
$project->setAttribute('viewer_role', $project->userProjectRole($user->email));

return $project;
};

if ($projects instanceof LengthAwarePaginator) {
$projects->setCollection(
$projects->getCollection()->map($decorate)
);

return $projects;
}

if ($projects instanceof Collection) {
return $projects->map($decorate);
}

return array_map($decorate, $projects);
}

protected function applyStatusFilter(Builder $query, string $status): void
{
if ($status !== 'all') {
Expand Down
61 changes: 35 additions & 26 deletions app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,40 +158,49 @@ public function review(Request $request, $obfuscationCode, GetLicense $getLicens
{
$project = Project::where([['is_archived', false], ['obfuscationcode', $obfuscationCode]])->firstOrFail();
$project->load('projectInvitations', 'tags', 'authors', 'citations', 'owner');
if (! $project->is_public) {
$license = null;
if ($project->license_id) {
$license = $getLicense->getLicensebyId($project->license_id);
}

return Inertia::render('Project/Show', [
'project' => $project,
'team' => null,
'members' => $project->allUsers(),
'availableRoles' => array_values(Jetstream::$roles),
'role' => 'reviewer',
'teamRole' => null,
'license' => $license,
'projectPermissions' => [
'canDeleteProject' => false,
'canUpdateProject' => false,
],
'preview' => true,
]);
} else {
$identifier = explode(':', $project->identifier)[1];
if ($project->is_public) {
$rawIdentifier = $project->getRawOriginal('identifier');
if ($rawIdentifier !== null && $rawIdentifier !== '') {
$targetUrl = route('public.project.id', ['id' => 'P'.$rawIdentifier]);
$query = $request->query();
if ($query !== []) {
$targetUrl .= '?'.http_build_query($query);
}

return redirect()->route('public', $identifier);
return redirect()->to($targetUrl);
}
}

$license = null;
if ($project->license_id) {
$license = $getLicense->getLicensebyId($project->license_id);
}

return Inertia::render('Project/Show', [
'project' => $project,
'team' => null,
'members' => $project->allUsers(),
'availableRoles' => array_values(Jetstream::$roles),
'role' => 'reviewer',
'teamRole' => null,
'license' => $license,
'projectPermissions' => [
'canDeleteProject' => false,
'canUpdateProject' => false,
],
'preview' => true,
]);
}

public function reviewerStudies(Request $request, $obfuscationCode)
{
$project = Project::where([['is_archived', false], ['obfuscationcode', $obfuscationCode]])->firstOrFail();
if ($project) {
return StudyResource::collection(Study::where('project_id', $project->id)->filter($request->only('search', 'sort', 'mode'))->paginate(9)->withQueryString());
}
$project = Project::where([
['is_archived', false],
['obfuscationcode', $obfuscationCode],
])->firstOrFail();

return $this->projectStudiesResponse($request, $project, publicOnly: false);
}

public function studies(Request $request, Project $project)
Expand Down
2 changes: 1 addition & 1 deletion resources/js/Layouts/AppLayout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ const navigationSections = [
{
auth: false,
name: "Spectra Library",
href: "/compounds",
href: "/search?scope=compounds",
icon: SwatchIcon,
},
],
Expand Down
6 changes: 4 additions & 2 deletions resources/js/Mixins/Global.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,18 @@ export default {
}).format(date);
},
/**
* Unified date+time display for record metadata (Published / Created / Updated rows).
* Unified date display for record metadata (Published / Created / Updated rows).
*/
formatRecordTimestamp(timestamp) {
if (!timestamp) {
return "";
}
const date = new Date(timestamp);
if (Number.isNaN(date.getTime())) {
return "";
}
return new Intl.DateTimeFormat(undefined, {
dateStyle: "medium",
timeStyle: "short",
}).format(date);
},
md(data) {
Expand Down
Loading
Loading