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
10 changes: 10 additions & 0 deletions packages/cypress/src/integration/admin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ describe('[Admin]', () => {
cy.contains('Overview');
});

it('[Admin can reach the projects overview from the admin navigation]', () => {
cy.signIn(admin.email, admin.password);

cy.visit('/admin');
cy.contains('button', 'Library').click();
cy.contains('a', 'Projects').click();
cy.url().should('include', '/admin/projects');
cy.contains('h1', 'Projects');
});

it('[Admin can reach the remakes overview from the admin navigation]', () => {
cy.signIn(admin.email, admin.password);

Expand Down
62 changes: 62 additions & 0 deletions shared/models/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,65 @@ export type ProjectStepDTO = {
images: DBMedia[] | null;
videoUrl: string | null;
};

export type DBAdminProject = Pick<
DBProject,
| 'id'
| 'created_at'
| 'title'
| 'slug'
| 'difficulty_level'
| 'moderation'
| 'is_draft'
| 'published_at'
| 'comment_count'
| 'total_views'
| 'deleted'
> & {
profile: Pick<DBAuthor, 'username' | 'display_name'> | null;
category: Pick<DBCategory, 'name'> | null;
steps: { count: number }[];
remakes: { count: number }[];
};

export class AdminProject {
id: number;
title: string;
slug: string;
author: { username: string | null; displayName: string } | null;
category: string | null;
difficultyLevel: DifficultyLevel | null;
moderation: Moderation | null;
isDraft: boolean;
publishedAt: Date | null;
commentCount: number;
totalViews: number;
stepCount: number;
remakeCount: number;
deleted: boolean;

constructor(obj: AdminProject) {
Object.assign(this, obj);
}

static fromDB(obj: DBAdminProject) {
return new AdminProject({
id: obj.id,
title: obj.title,
slug: obj.slug,
author: obj.profile
? { username: obj.profile.username, displayName: obj.profile.display_name }
: null,
category: obj.category?.name ?? null,
difficultyLevel: obj.difficulty_level,
moderation: obj.moderation,
isDraft: obj.is_draft || false,
publishedAt: obj.published_at ? new Date(obj.published_at) : null,
commentCount: obj.comment_count ?? 0,
totalViews: obj.total_views ?? 0,
stepCount: obj.steps[0]?.count ?? 0,
remakeCount: obj.remakes[0]?.count ?? 0,
deleted: obj.deleted || false,
});
}
}
5 changes: 2 additions & 3 deletions src/pages/Admin/AdminSidebar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ describe('AdminSidebar', () => {
renderAt('/admin/remakes');

expect(screen.getByRole('link', { name: 'Remakes' })).toBeInTheDocument();
expect(screen.queryByRole('link', { name: /Projects/ })).not.toBeInTheDocument();
expect(screen.getByText('Projects').parentElement).toHaveAttribute('aria-disabled', 'true');
expect(screen.getAllByText('Coming soon')).toHaveLength(1);
expect(screen.getByRole('link', { name: 'Projects' })).toHaveAttribute('href', '/admin/projects');
expect(screen.queryByText('Coming soon')).not.toBeInTheDocument();
});

it('links Research to its admin page', () => {
Expand Down
5 changes: 4 additions & 1 deletion src/pages/Admin/AdminSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ const ADMIN_NAV_ITEMS = [
{
label: 'Library',
icon: LibraryIcon,
items: [{ label: 'Projects' }, { label: 'Remakes', href: '/admin/remakes' }],
items: [
{ label: 'Projects', href: '/admin/projects' },
{ label: 'Remakes', href: '/admin/remakes' },
],
},
{ label: 'Research', href: '/admin/research', icon: FlaskConicalIcon },
{ label: 'Questions', href: '/admin/questions', icon: MessageCircleQuestionMarkIcon },
Expand Down
15 changes: 1 addition & 14 deletions src/pages/Admin/MapPins/MapPinsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ExternalLinkIcon } from 'lucide-react';
import { Link } from 'react-router';
import { AdminPagination } from 'src/pages/Admin/AdminPagination';
import { Badge } from '@/components/ui/badge';
import { ModerationBadge } from 'src/pages/Admin/ModerationBadge';
import {
Table,
TableBody,
Expand Down Expand Up @@ -40,19 +40,6 @@ export interface MapPinsPageProps {
totalCount: number;
}

function ModerationBadge({ status }: { status: string }) {
const variant =
status === 'accepted'
? 'success'
: status === 'awaiting-moderation'
? 'warning'
: status === 'improvements-needed'
? 'info'
: 'destructive';

return <Badge variant={variant}>{status}</Badge>;
}

export function MapPinsPage({ mapPins, page, totalPages, totalCount }: MapPinsPageProps) {
return (
<div className="flex flex-col gap-4">
Expand Down
14 changes: 14 additions & 0 deletions src/pages/Admin/ModerationBadge.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Badge } from '@/components/ui/badge';

export function ModerationBadge({ status }: { status: string }) {
const variant =
status === 'accepted'
? 'success'
: status === 'awaiting-moderation'
? 'warning'
: status === 'improvements-needed'
? 'info'
: 'destructive';

return <Badge variant={variant}>{status}</Badge>;
}
132 changes: 132 additions & 0 deletions src/pages/Admin/Projects/ProjectsPage.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import '@testing-library/jest-dom/vitest';
import { render, screen, within } from '@testing-library/react';
import type { AdminProject } from 'oa-shared';
import { MemoryRouter } from 'react-router';
import { FactoryAdminProject } from 'src/test/factories/Library';
import { describe, expect, it } from 'vitest';
import { ProjectsPage } from './ProjectsPage';

const project = FactoryAdminProject({
title: 'Plastic bench',
slug: 'plastic-bench',
author: { username: 'maker', displayName: 'Maker One' },
category: 'Furniture',
difficultyLevel: 'very-hard',
moderation: 'awaiting-moderation',
publishedAt: new Date('2026-08-20T10:00:00Z'),
stepCount: 7,
commentCount: 12,
totalViews: 345,
remakeCount: 2,
});

const renderPage = (projects: AdminProject[], page = 1, totalPages = 1) =>
render(
<MemoryRouter>
<ProjectsPage
projects={projects}
page={page}
totalPages={totalPages}
totalCount={projects.length}
/>
</MemoryRouter>,
);

const cellsOf = (title: string) =>
within(screen.getByText(title).closest('tr')!).getAllByRole('cell');

describe('ProjectsPage', () => {
it('lists each project with its details', () => {
renderPage([project]);

expect(screen.getByRole('heading', { name: 'Projects' })).toBeInTheDocument();
expect(screen.getByRole('link', { name: 'Maker One' })).toHaveAttribute('href', '/u/maker');
expect(cellsOf('Plastic bench').map((cell) => cell.textContent)).toEqual([
'Plastic bench',
'Maker One',
'Furniture',
'Very Hard',
'awaiting-moderation',
'20 Aug 2026',
'7',
'12',
'345',
'2',
]);
});

it('links each project to its live page', () => {
renderPage([project]);

expect(screen.getByRole('link', { name: 'Plastic bench' })).toHaveAttribute(
'href',
'/library/plastic-bench',
);
});

it('shows drafts as Draft instead of a published date', () => {
renderPage([FactoryAdminProject({ ...project, isDraft: true, publishedAt: null })]);

expect(cellsOf('Plastic bench')[5]).toHaveTextContent('Draft');
});

it('greys out a deleted project and does not link to it', () => {
renderPage([FactoryAdminProject({ ...project, deleted: true })]);

expect(screen.getByText('Plastic bench (marked for deletion)').closest('tr')).toHaveClass(
'bg-muted',
);
expect(screen.queryByRole('link', { name: 'Plastic bench' })).not.toBeInTheDocument();
});

it('leaves optional fields empty when a project has none', () => {
renderPage([
FactoryAdminProject({
...project,
category: null,
difficultyLevel: null,
moderation: null,
publishedAt: null,
}),
]);

const cells = cellsOf('Plastic bench');
for (const index of [2, 3, 4, 5]) {
expect(cells[index]).toBeEmptyDOMElement();
}
});

it('keeps an unavailable author visible without a profile link', () => {
renderPage([FactoryAdminProject({ ...project, author: null })]);

expect(screen.getByText('Author unavailable')).toBeInTheDocument();
expect(screen.getAllByRole('link')).toHaveLength(1);
});

it('renders an author without a username as plain text', () => {
renderPage([FactoryAdminProject({ ...project, author: { displayName: 'Maker One', username: null } })]);

expect(screen.getByText('Maker One')).toBeInTheDocument();
expect(screen.queryByRole('link', { name: 'Maker One' })).not.toBeInTheDocument();
});

it('shows an empty state when there are no projects', () => {
renderPage([]);

expect(screen.getByText('No projects yet.')).toBeInTheDocument();
});

it('has no create, edit or delete controls', () => {
renderPage([project]);

expect(screen.queryByRole('button')).not.toBeInTheDocument();
});

it('pages through the projects', () => {
renderPage([project], 2, 3);

expect(screen.getByText('Page 2 of 3')).toBeInTheDocument();
expect(screen.getByText('Previous').closest('a')).toHaveAttribute('href', '/?page=1');
expect(screen.getByText('Next').closest('a')).toHaveAttribute('href', '/?page=3');
});
});
105 changes: 105 additions & 0 deletions src/pages/Admin/Projects/ProjectsPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import type { AdminProject } from 'oa-shared';
import { DifficultyLevelRecord } from 'oa-shared';
import { Link } from 'react-router';
import { AdminPagination } from 'src/pages/Admin/AdminPagination';
import { ModerationBadge } from 'src/pages/Admin/ModerationBadge';
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '@/components/ui/table';

const PUBLISHED_AT_FORMAT = new Intl.DateTimeFormat('en-GB', {
dateStyle: 'medium',
timeZone: 'UTC',
});

interface IProps {
projects: AdminProject[];
page: number;
totalPages: number;
totalCount: number;
}

export function ProjectsPage({ projects, page, totalPages, totalCount }: IProps) {
return (
<div className="flex flex-col gap-4">
<div className="flex items-center justify-between">
<h1 className="text-lg font-semibold">Projects</h1>
<span className="text-sm text-muted-foreground">Total: {totalCount}</span>
</div>

<Table>
<TableHeader>
<TableRow>
<TableHead>Title</TableHead>
<TableHead>Author</TableHead>
<TableHead>Category</TableHead>
<TableHead>Difficulty</TableHead>
<TableHead>Moderation</TableHead>
<TableHead>Published</TableHead>
<TableHead>Steps</TableHead>
<TableHead>Comments</TableHead>
<TableHead>Views</TableHead>
<TableHead>Remakes</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{projects.length === 0 ? (
<TableRow>
<TableCell colSpan={10} variant="muted" className="h-24 text-center">
No projects yet.
</TableCell>
</TableRow>
) : (
projects.map((project) => (
<TableRow key={project.id} variant={project.deleted ? 'muted' : 'default'}>
<TableCell>
{project.deleted ? (
`${project.title} (marked for deletion)`
) : (
<Link to={`/library/${project.slug}`} className="font-medium hover:underline">
{project.title}
</Link>
)}
</TableCell>
<TableCell>
{!project.author ? (
<span className="text-muted-foreground">Author unavailable</span>
) : project.author.username ? (
<Link to={`/u/${project.author.username}`} className="hover:underline">
{project.author.displayName}
</Link>
) : (
project.author.displayName
)}
</TableCell>
<TableCell>{project.category}</TableCell>
<TableCell>
{project.difficultyLevel && DifficultyLevelRecord[project.difficultyLevel]}
</TableCell>
<TableCell>
{project.moderation && <ModerationBadge status={project.moderation} />}
</TableCell>
<TableCell>
{project.isDraft
? 'Draft'
: project.publishedAt && PUBLISHED_AT_FORMAT.format(project.publishedAt)}
</TableCell>
<TableCell>{project.stepCount}</TableCell>
<TableCell>{project.commentCount}</TableCell>
<TableCell>{project.totalViews}</TableCell>
<TableCell>{project.remakeCount}</TableCell>
</TableRow>
))
)}
</TableBody>
</Table>

<AdminPagination page={page} totalPages={totalPages} />
</div>
);
}
Loading
Loading