diff --git a/README.md b/README.md index 9968b6e..37b5dd8 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,17 @@ Content lives in JSON files under [`src/data/`](src/data/): To change content, edit the relevant file and open a pull request. +In `funding.json`, `category` is the funding programme, `ELIXIR` or +`Google Summer of Code`: it titles the grant's card, sets its logo and is +its filter. `funder` is the detailed grant type, e.g. `ELIXIR Strategic +Implementation Study`, shown under the title unless it equals the +programme; it starts with the programme's name. Dates are `YYYY-MM-DD`; +grants are listed by end date (newest first), then start date (oldest +first), then name. `formattedDuration` is +the period as shown on the card; keep it in step with the dates. A new +programme needs its logo in `public/` and an entry in `PROGRAMMES` in +[`src/lib/funding.ts`](src/lib/funding.ts). + The listings (Products, Funding, Contributors) share one filter behaviour ([`src/lib/filters.ts`](src/lib/filters.ts)): several filters can be selected, and an item is listed if it matches any of them. The selection is kept in the diff --git a/public/gsoc.svg b/public/gsoc.svg new file mode 100644 index 0000000..d394054 --- /dev/null +++ b/public/gsoc.svg @@ -0,0 +1 @@ + diff --git a/src/app/funding/page.test.tsx b/src/app/funding/page.test.tsx new file mode 100644 index 0000000..4257369 --- /dev/null +++ b/src/app/funding/page.test.tsx @@ -0,0 +1,64 @@ +import { render, screen, within } from '@testing-library/react'; +import { describe, expect, it } from 'vitest'; +import funding from '@/data/funding.json'; +import { grantType, sortGrants } from '@/lib/funding'; +import FundingPage from './page'; + +// The grant cards, in display order +const cards = () => screen.getAllByRole('article'); + +describe('Funding page', () => { + it('shows the stats in order, capitalized like the rest of the site', () => { + render(); + const labels = screen + .getAllByTestId('stat') + .map((stat) => stat.lastChild?.textContent); + expect(labels).toEqual(['Total grants', 'ELIXIR grants', 'GSoC projects']); + const count = (category: string) => + funding.filter((grant) => grant.category === category).length; + const values = screen + .getAllByTestId('stat') + .map((stat) => stat.firstChild?.textContent); + expect(values).toEqual( + [funding.length, count('ELIXIR'), count('Google Summer of Code')].map( + String, + ), + ); + }); + + it('offers one filter per programme', () => { + render(); + const filters = within(screen.getByRole('group', { name: 'Filter' })) + .getAllByRole('button') + .map((button) => button.firstChild?.textContent); + expect(filters).toEqual(['ELIXIR', 'Google Summer of Code']); + }); + + it('sorts grants by end date, start date and name', () => { + render(); + const titles = cards().map( + (card) => within(card).getByRole('heading', { level: 3 }).textContent, + ); + expect(titles).toEqual(sortGrants(funding).map((grant) => grant.project)); + }); + + it('titles each card with its programme, logo and grant type', () => { + render(); + const sorted = sortGrants(funding); + cards().forEach((card, index) => { + const grant = sorted[index]; + const header = within(card); + expect( + header.getByRole('heading', { level: 4 }).textContent, + grant.project, + ).toBe(grant.category); + expect(header.getByRole('img').getAttribute('alt')).toBe( + `${grant.category} logo`, + ); + const type = card.querySelector('[data-testid="grant-type"]'); + expect(type?.textContent ?? null).toBe(grantType(grant)); + // The programme is in the title; no badge repeats it + expect(header.queryByText(/^(Grant|GSoC)$/)).toBeNull(); + }); + }); +}); diff --git a/src/app/funding/page.tsx b/src/app/funding/page.tsx index 4efa2c6..1a7b87a 100644 --- a/src/app/funding/page.tsx +++ b/src/app/funding/page.tsx @@ -1,13 +1,12 @@ 'use client'; import { useMemo, useState } from 'react'; +import Image from 'next/image'; import { Award, - Building2, Calendar, HeartHandshake, Search, - Sparkles, Users, X, } from 'lucide-react'; @@ -19,33 +18,29 @@ import { Card } from '@/components/ui/card'; import { Input } from '@/components/ui/input'; import { AnimatedGridPattern } from '@/components/ui/animated-grid-pattern'; import { checkFilterIds, filterId, matchesAny } from '@/lib/filters'; +import { + ELIXIR, + type Grant, + grants, + grantType, + GSOC, + PROGRAMMES, +} from '@/lib/funding'; import { useUrlFilters } from '@/lib/use-url-filters'; import { cn } from '@/lib/utils'; -import fundingData from '@/data/funding.json'; - -type FundingItem = { - id: string; - funder: string; - project: string; - recipients: string[]; - startDate: string; - endDate: string; - formattedDuration: string; - category: string; -}; - -const grants = fundingData as FundingItem[]; +// The logo of each programme (`category`) +const LOGOS = Object.fromEntries( + PROGRAMMES.map(({ name, logo }) => [name, logo]), +); -// One filter per category, in the order the categories first appear +// One filter per programme, in the order of PROGRAMMES const categoryOptions = checkFilterIds( - Array.from(new Set(grants.map((grant) => grant.category))).map( - (category) => ({ - value: filterId(category), - label: category, - count: grants.filter((grant) => grant.category === category).length, - }), - ), + PROGRAMMES.map(({ name }) => ({ + value: filterId(name), + label: name, + count: grants.filter((grant) => grant.category === name).length, + })), ); const categoryIds = categoryOptions.map((option) => option.value); @@ -69,10 +64,8 @@ export default function FundingPage() { }, [query, selected]); // Stat metrics - const gsocCount = grants.filter((g) => - g.funder.includes('Google Summer of Code'), - ).length; - const elixirCount = grants.filter((g) => g.funder.includes('ELIXIR')).length; + const gsocCount = grants.filter((g) => g.category === GSOC).length; + const elixirCount = grants.filter((g) => g.category === ELIXIR).length; return ( @@ -116,9 +109,9 @@ export default function FundingPage() { {/* Stats Bar */} - - - + + + @@ -205,42 +198,46 @@ export default function FundingPage() { /* Helper Components */ -function FundingCard({ item }: { item: FundingItem }) { - const isGSoC = item.funder.includes('Google Summer of Code'); +function FundingCard({ item }: { item: Grant }) { + const logo = LOGOS[item.category]; + const type = grantType(item); return ( - + {/* Background Hover Effect */} - {/* Funder Header Row */} - - - - {isGSoC ? ( - - ) : ( - - )} - - - - {item.funder} - - + {/* Programme: logo, title and detailed grant type. The logo tile + stays white, as the logos are made for light backgrounds. */} + + + {logo && ( + + )} + + + + {item.category} + + {type && ( + + {type} + + )} - - - {isGSoC ? 'GSoC' : 'Grant'} - {/* Project Title */} @@ -273,7 +270,7 @@ function FundingCard({ item }: { item: FundingItem }) { function Stat({ value, label }: { value: number; label: string }) { return ( - + {value} diff --git a/src/app/listing-filters.test.tsx b/src/app/listing-filters.test.tsx index 7d03aad..dd38daf 100644 --- a/src/app/listing-filters.test.tsx +++ b/src/app/listing-filters.test.tsx @@ -3,6 +3,7 @@ import userEvent from '@testing-library/user-event'; import { afterEach, describe, expect, it, vi } from 'vitest'; import contributors from '@/data/contributor.json'; import funding from '@/data/funding.json'; +import { sortGrants } from '@/lib/funding'; import ContributorsPage from './contributors/page'; import FundingPage from './funding/page'; @@ -28,8 +29,10 @@ const removeFilters = () => screen.queryByRole('button', { name: 'Remove filters' }); describe('Funding page filters', () => { + // Grants in display order + const sorted = sortGrants(funding); const inCategories = (...categories: string[]) => - funding + sorted .filter((grant) => categories.includes(grant.category)) .map((grant) => grant.project); const count = (...categories: string[]) => inCategories(...categories).length; @@ -37,23 +40,23 @@ describe('Funding page filters', () => { const shown = () => screen.getByText(/^Showing \d+ of \d+ project grants\.$/).textContent; - it('lists grants of any selected category', async () => { + it('lists grants of any selected programme', async () => { const user = userEvent.setup(); render(); expect(removeFilters()).toBeNull(); - await user.click(filterButton(/^ELIXIR Services/)); + await user.click(filterButton(/^ELIXIR/)); expect(shown()).toBe( - `Showing ${count('ELIXIR Services')} of ${funding.length} project grants.`, + `Showing ${count('ELIXIR')} of ${funding.length} project grants.`, ); - expect(listedGrants()).toEqual(inCategories('ELIXIR Services')); + expect(listedGrants()).toEqual(inCategories('ELIXIR')); - await user.click(filterButton(/^ELIXIR Platform Tasks/)); + await user.click(filterButton(/^Google Summer of Code/)); expect(shown()).toBe( - `Showing ${count('ELIXIR Services', 'ELIXIR Platform Tasks')} of ${funding.length} project grants.`, + `Showing ${funding.length} of ${funding.length} project grants.`, ); expect(listedGrants()).toEqual( - inCategories('ELIXIR Services', 'ELIXIR Platform Tasks'), + inCategories('ELIXIR', 'Google Summer of Code'), ); await user.click(removeFilters()!); @@ -63,16 +66,13 @@ describe('Funding page filters', () => { }); it('starts with the filters given in the URL', () => { - window.history.replaceState(null, '', '/funding?filter=elixir-services'); + window.history.replaceState(null, '', '/funding?filter=elixir'); render(); expect(shown()).toBe( - `Showing ${count('ELIXIR Services')} of ${funding.length} project grants.`, - ); - expect(listedGrants()).toEqual(inCategories('ELIXIR Services')); - expect(filterButton(/^ELIXIR Services/)).toHaveAttribute( - 'aria-pressed', - 'true', + `Showing ${count('ELIXIR')} of ${funding.length} project grants.`, ); + expect(listedGrants()).toEqual(inCategories('ELIXIR')); + expect(filterButton(/^ELIXIR/)).toHaveAttribute('aria-pressed', 'true'); }); }); diff --git a/src/data/data.test.ts b/src/data/data.test.ts index 6629061..45b563c 100644 --- a/src/data/data.test.ts +++ b/src/data/data.test.ts @@ -8,6 +8,8 @@ import type { Service, } from '@/components/catalog/types'; import contributors from './contributor.json'; +import { PROGRAMMES } from '@/lib/funding'; +import funding from './funding.json'; import nodesData from './nodes.json'; import productsData from './product.json'; import { GA4GH_DRIVER_PROJECT_URL } from '@/lib/site'; @@ -116,6 +118,39 @@ describe('nodes.json', () => { ); }); +describe('funding.json', () => { + it.each(PROGRAMMES.map(({ name, logo }) => [name, logo]))( + 'has the logo of %s', + (_, logo) => { + expect(publicFile(logo), logo).toBe(true); + }, + ); + + it.each(PROGRAMMES.map(({ name }) => [name]))('has grants of %s', (name) => { + expect(funding.some((grant) => grant.category === name)).toBe(true); + }); + + it('has unique ids', () => { + expect(duplicates(funding.map((grant) => grant.id))).toEqual([]); + }); + + it.each(funding.map((grant) => [grant.project, grant] as const))( + '%s names its programme and dates', + (_, grant) => { + // The programme is the card's title, filter and logo + expect(PROGRAMMES.map(({ name }) => name)).toContain(grant.category); + expect(grant.funder.startsWith(grant.category)).toBe(true); + // ISO dates, as the listing sorts them as text + expect(grant.startDate).toMatch(/^\d{4}-\d{2}-\d{2}$/); + expect(grant.endDate).toMatch(/^\d{4}-\d{2}-\d{2}$/); + expect(grant.startDate <= grant.endDate).toBe(true); + expect(grant.project).toBeTruthy(); + expect(grant.recipients.length).toBeGreaterThan(0); + expect(grant.formattedDuration).toBeTruthy(); + }, + ); +}); + describe('contributor.json', () => { it('only references images that exist', () => { const missing = contributors diff --git a/src/data/funding.json b/src/data/funding.json index 216fbbc..65bb718 100644 --- a/src/data/funding.json +++ b/src/data/funding.json @@ -1,13 +1,13 @@ [ { "id": "1", - "funder": "ELIXIR Commissioned Services: Technology", + "funder": "ELIXIR Commissioned Service: Technology", "project": "24-TECH-Compute", "recipients": ["ELIXIR Compute Platform"], "startDate": "2024-01-01", "endDate": "2026-12-31", "formattedDuration": "1 January 2024 - 31 December 2026", - "category": "ELIXIR Services" + "category": "ELIXIR" }, { "id": "2", @@ -77,7 +77,7 @@ "startDate": "2022-01-01", "endDate": "2023-12-31", "formattedDuration": "1 January 2022 - 31 December 2023", - "category": "ELIXIR Platform Tasks" + "category": "ELIXIR" }, { "id": "9", @@ -117,7 +117,7 @@ "startDate": "2021-06-01", "endDate": "2023-05-31", "formattedDuration": "1 June 2021 - 31 May 2023", - "category": "ELIXIR Implementation Studies" + "category": "ELIXIR" }, { "id": "13", @@ -187,7 +187,7 @@ "startDate": "2019-06-01", "endDate": "2021-05-31", "formattedDuration": "1 June 2019 - 31 May 2021", - "category": "ELIXIR Implementation Studies" + "category": "ELIXIR" }, { "id": "20", @@ -197,7 +197,7 @@ "startDate": "2019-06-01", "endDate": "2021-05-31", "formattedDuration": "1 June 2019 - 31 May 2021", - "category": "ELIXIR Platform Tasks" + "category": "ELIXIR" }, { "id": "21", diff --git a/src/lib/funding.test.ts b/src/lib/funding.test.ts new file mode 100644 index 0000000..e558f79 --- /dev/null +++ b/src/lib/funding.test.ts @@ -0,0 +1,106 @@ +import { describe, expect, it } from 'vitest'; +import { + checkGrants, + type Grant, + grantType, + type Programme, + sortGrants, +} from './funding'; + +const grant = ( + project: string, + startDate: string, + endDate: string, + category: Programme = 'ELIXIR', + funder: string = category, +): Grant => ({ + id: project, + funder, + project, + recipients: [], + startDate, + endDate, + formattedDuration: '', + category, +}); + +describe('sortGrants', () => { + it('orders by end date (newest first)', () => { + const grants = [ + grant('a', '2020-01-01', '2021-01-01'), + grant('b', '2020-01-01', '2023-01-01'), + grant('c', '2020-01-01', '2022-01-01'), + ]; + expect(sortGrants(grants).map((g) => g.project)).toEqual(['b', 'c', 'a']); + }); + + it('then by start date (oldest first)', () => { + const grants = [ + grant('a', '2022-01-01', '2023-01-01'), + grant('b', '2020-01-01', '2023-01-01'), + grant('c', '2021-01-01', '2023-01-01'), + ]; + expect(sortGrants(grants).map((g) => g.project)).toEqual(['b', 'c', 'a']); + }); + + it('then by name (ascending, alphanumerical)', () => { + const grants = [ + grant('Task 10', '2020-01-01', '2023-01-01'), + grant('task 9', '2020-01-01', '2023-01-01'), + grant('Apps', '2020-01-01', '2023-01-01'), + ]; + expect(sortGrants(grants).map((g) => g.project)).toEqual([ + 'Apps', + 'task 9', + 'Task 10', + ]); + }); + + it('leaves its input unchanged', () => { + const grants = [ + grant('a', '2020-01-01', '2021-01-01'), + grant('b', '2020-01-01', '2023-01-01'), + ]; + sortGrants(grants); + expect(grants.map((g) => g.project)).toEqual(['a', 'b']); + }); +}); + +describe('grantType', () => { + it('is the detailed type when it differs from the programme', () => { + expect( + grantType( + grant( + 'a', + '2020-01-01', + '2021-01-01', + 'ELIXIR', + 'ELIXIR Strategic Implementation Study', + ), + ), + ).toBe('ELIXIR Strategic Implementation Study'); + }); + + it('is null when the type is the programme itself', () => { + expect( + grantType( + grant('a', '2020-01-01', '2021-01-01', 'Google Summer of Code'), + ), + ).toBeNull(); + }); +}); + +describe('checkGrants', () => { + it('accepts grants of known programmes', () => { + const grants = [grant('a', '2020-01-01', '2021-01-01')]; + expect(checkGrants(grants)).toBe(grants); + }); + + it('rejects a grant of an unknown programme', () => { + expect(() => + checkGrants([ + { ...grant('a', '2020-01-01', '2021-01-01'), category: 'Other' }, + ]), + ).toThrow('The grant "a" has the unknown programme "Other"'); + }); +}); diff --git a/src/lib/funding.ts b/src/lib/funding.ts new file mode 100644 index 0000000..c117b36 --- /dev/null +++ b/src/lib/funding.ts @@ -0,0 +1,78 @@ +import fundingData from '@/data/funding.json'; + +export const ELIXIR = 'ELIXIR'; +export const GSOC = 'Google Summer of Code'; + +/** A funding programme: the `category` of a grant. */ +export type Programme = typeof ELIXIR | typeof GSOC; + +// A grant of the Funding page. `category` is the funding programme ("ELIXIR" +// or "Google Summer of Code"): the card's title and filter. `funder` is the +// detailed grant type, e.g. "ELIXIR Strategic Implementation Study", shown +// under the title when it adds to it. +export type Grant = { + id: string; + funder: string; + project: string; + recipients: string[]; + startDate: string; + endDate: string; + formattedDuration: string; + category: Programme; +}; + +// The funding programmes (`category`), in the order of the filters, with +// their logos in `public/`. A new programme needs an entry here. +export const PROGRAMMES: readonly { name: Programme; logo: string }[] = [ + { name: ELIXIR, logo: '/elixir.png' }, + { name: GSOC, logo: '/gsoc.svg' }, +]; + +const compareText = (a: string, b: string) => + a.localeCompare(b, 'en', { numeric: true, sensitivity: 'base' }); + +/** + * Orders grants by end date (newest first), then by start date (oldest + * first), then by project name (ascending, alphanumerical). Returns a new + * array. + */ +export function sortGrants< + T extends Pick, +>(grants: readonly T[]): T[] { + // Dates are ISO 8601 (`YYYY-MM-DD`), so they compare as strings + return [...grants].sort( + (a, b) => + b.endDate.localeCompare(a.endDate) || + a.startDate.localeCompare(b.startDate) || + compareText(a.project, b.project), + ); +} + +/** The detailed grant type to show under the title, if it adds anything. */ +export function grantType(grant: { + funder: string; + category: string; +}): string | null { + return grant.funder !== grant.category ? grant.funder : null; +} + +/** + * Returns the grants after checking that each names a known programme, so + * that none is listed without its logo and filter. Pages are prerendered, + * so an unknown programme fails the build. + */ +export function checkGrants( + data: readonly (Omit & { category: string })[], +): Grant[] { + const known = new Set(PROGRAMMES.map(({ name }) => name)); + data.forEach(({ project, category }) => { + if (!known.has(category)) { + throw new Error( + `The grant "${project}" has the unknown programme "${category}"`, + ); + } + }); + return data as Grant[]; +} + +export const grants: Grant[] = sortGrants(checkGrants(fundingData));
+ {type} +