|
| 1 | +'use client'; |
| 2 | +import { useRef, useState } from 'react'; |
| 3 | +import { ArrowLeft, ArrowRight, ArrowUpRight } from 'lucide-react'; |
| 4 | +const PAGE_SIZE = 10; |
| 5 | +const kindLabel = { journal: 'Journal', accepted: 'Accepted', conference: 'Conference' }; |
| 6 | +// First, last and the pages around the current one. |
| 7 | +const pageWindow = (page, pages) => |
| 8 | + [...new Set([0, page - 1, page, page + 1, pages - 1])] |
| 9 | + .filter((i) => i >= 0 && i < pages) |
| 10 | + .sort((a, b) => a - b); |
| 11 | +function Authors({ authors }) { |
| 12 | + const names = authors.slice(0, 10).map((author) => author.name); |
| 13 | + return names.join('; ') + (authors.length > 10 ? '; et al.' : ''); |
| 14 | +} |
| 15 | +export default function Papers({ papers }) { |
| 16 | + const section = useRef(null); |
| 17 | + const [year, setYear] = useState(''); |
| 18 | + const [page, setPage] = useState(0); |
| 19 | + const years = [...new Set(papers.papers.map((paper) => paper.year))]; |
| 20 | + const filtered = year |
| 21 | + ? papers.papers.filter((paper) => paper.year === Number(year)) |
| 22 | + : papers.papers; |
| 23 | + const pages = Math.ceil(filtered.length / PAGE_SIZE); |
| 24 | + function go(next) { |
| 25 | + setPage(next); |
| 26 | + section.current?.scrollIntoView({ block: 'start' }); |
| 27 | + } |
| 28 | + return ( |
| 29 | + <section id="artigos" className="papers-section" ref={section}> |
| 30 | + <div className="section-heading"> |
| 31 | + <div> |
| 32 | + <div className="kicker">PAPERS / JOURNALS / CONFERENCES</div> |
| 33 | + <h2>Papers</h2> |
| 34 | + </div> |
| 35 | + <select |
| 36 | + className="papers-year" |
| 37 | + value={year} |
| 38 | + onChange={(event) => { |
| 39 | + setYear(event.target.value); |
| 40 | + setPage(0); |
| 41 | + }} |
| 42 | + aria-label="Filter papers by year" |
| 43 | + > |
| 44 | + <option value="">All years</option> |
| 45 | + {years.map((value) => ( |
| 46 | + <option key={value} value={value}> |
| 47 | + {value} |
| 48 | + </option> |
| 49 | + ))} |
| 50 | + </select> |
| 51 | + </div> |
| 52 | + <p className="section-description"> |
| 53 | + Peer-reviewed papers since 2013 · {filtered.length} papers |
| 54 | + {papers.lattesUpdatedAt && |
| 55 | + ` · from Daniel de Oliveira’s Lattes CV, updated on ${new Date( |
| 56 | + `${papers.lattesUpdatedAt}T12:00:00Z`, |
| 57 | + ).toLocaleDateString('en-US')}`} |
| 58 | + </p> |
| 59 | + <ol className="papers-list"> |
| 60 | + {filtered.slice(page * PAGE_SIZE, (page + 1) * PAGE_SIZE).map((paper) => ( |
| 61 | + <li key={`${paper.kind}-${paper.year}-${paper.title}`} className="paper-entry"> |
| 62 | + <span className="paper-year">{paper.year}</span> |
| 63 | + <div> |
| 64 | + <h3> |
| 65 | + {paper.url ? ( |
| 66 | + <a href={paper.url} target="_blank" rel="noreferrer"> |
| 67 | + {paper.title} <ArrowUpRight size={14} /> |
| 68 | + </a> |
| 69 | + ) : ( |
| 70 | + paper.title |
| 71 | + )} |
| 72 | + </h3> |
| 73 | + <p className="paper-authors"> |
| 74 | + <Authors authors={paper.authors} /> |
| 75 | + </p> |
| 76 | + <p className="paper-venue"> |
| 77 | + <span className="paper-kind">{kindLabel[paper.kind]}</span> |
| 78 | + {paper.venue} |
| 79 | + </p> |
| 80 | + </div> |
| 81 | + </li> |
| 82 | + ))} |
| 83 | + </ol> |
| 84 | + {!filtered.length && <p className="no-activity">No papers available.</p>} |
| 85 | + {pages > 1 && ( |
| 86 | + <nav className="papers-pagination" aria-label="Papers pages"> |
| 87 | + <button onClick={() => go(page - 1)} disabled={page === 0} aria-label="Previous page"> |
| 88 | + <ArrowLeft size={15} /> |
| 89 | + </button> |
| 90 | + {pageWindow(page, pages).map((i, index, list) => ( |
| 91 | + <span key={i} className="papers-page"> |
| 92 | + {index > 0 && i - list[index - 1] > 1 && <span aria-hidden="true">…</span>} |
| 93 | + <button |
| 94 | + onClick={() => go(i)} |
| 95 | + className={i === page ? 'active' : ''} |
| 96 | + aria-current={i === page ? 'page' : undefined} |
| 97 | + > |
| 98 | + {i + 1} |
| 99 | + </button> |
| 100 | + </span> |
| 101 | + ))} |
| 102 | + <button onClick={() => go(page + 1)} disabled={page === pages - 1} aria-label="Next page"> |
| 103 | + <ArrowRight size={15} /> |
| 104 | + </button> |
| 105 | + </nav> |
| 106 | + )} |
| 107 | + </section> |
| 108 | + ); |
| 109 | +} |
0 commit comments