Show related affiliation documents

This commit is contained in:
Ananth Venkatesh 2024-02-16 22:24:18 +00:00
parent 8501d13827
commit 17361b092a
3 changed files with 81 additions and 2 deletions

View file

@ -0,0 +1,61 @@
import { Document, documents, authors } from '@/app/db/data'
// Assuming the types Document, DocumentStatus, and reviewer are defined as provided in your question
// Interface for the return output
interface DocumentWithSlug {
slug: string
doc: Document
}
const findDocumentsByAuthor = (
authorId: string
): DocumentWithSlug[] => {
// Filter documents by author
return Object.entries(documents)
.filter(([_, doc]) => doc.manifest.authors.includes(authorId))
.map(([slug, doc]) => ({ slug, doc }))
}
const checkAffiliation = (affiliationStr: string[], affiliationShort: string) => {
for (const affiliation of affiliationStr) {
if (affiliation.split('@')[1] === affiliationShort) {
return true
}
}
return false
}
export default function findDocumentsByAffiliationSorted(
affiliationShort: string
): DocumentWithSlug[] {
// Find authors with the given affiliation
const results: DocumentWithSlug[] = []
for (const [key, value] of Object.entries(authors)) {
if (
checkAffiliation(value.affiliation, affiliationShort)
|| (
value.formerAffiliations
&& checkAffiliation(value.formerAffiliations, affiliationShort)
)
) {
// Check if document is already in results
const additions = findDocumentsByAuthor(key)
for (const addition of additions) {
const existing = results.find((doc) => doc.slug === addition.slug)
if (!existing) {
results.push(addition)
}
}
}
}
// Sort the filtered documents by the latest date in descending order
const sortedDocuments = results.toSorted((a, b) => {
const latestDateA = a.doc.manifest.dates[a.doc.manifest.dates.length - 1]
const latestDateB = b.doc.manifest.dates[b.doc.manifest.dates.length - 1]
return latestDateB - latestDateA // For descending order
})
return sortedDocuments
}

View file

@ -1,6 +1,9 @@
import { affiliations } from '@/app/db/data'
import { notFound } from 'next/navigation'
import { Zilla_Slab } from 'next/font/google'
import findDocumentsByAffiliation from './findDocumentsByAffiliation'
import { Fragment } from 'react'
import DocumentCard from '@/app/components/DocumentCard'
const zillaSlab = Zilla_Slab({ subsets: ['latin'], weight: ['500'] })
@ -18,12 +21,14 @@ export default function Page({
notFound()
}
const affiliationDocuments = findDocumentsByAffiliation(shortName)
const Description = () => {
return (
<>
{description.split('[linebreak]').map((d, i) => (
<>
<div key={i} className='text-lg sm:text-md font-serif'>
<div className='text-lg sm:text-md font-serif'>
{d}
</div>
<br className='m-1' />
@ -56,6 +61,19 @@ export default function Page({
<br />
<Description />
</div>
{affiliationDocuments.length > 0 && (
<>
<hr className='mx-auto w-full h-1 border-0 bg-slate-200 my-2 rounded-md mt-8' />
<h1 className='text-3xl md:my-6 my-4 font-serif'>
Related documents {`(${affiliationDocuments.length})`}
</h1>
{affiliationDocuments.map((d) => (
<Fragment key={d.slug}>
<DocumentCard doc={d.doc} href={`/document/view/${d.slug}`} />
</Fragment>
))}
</>
)}
</div>
)
}

View file

@ -17,7 +17,7 @@ export default function findDocumentsByAuthorSorted(
.map(([slug, doc]) => ({ slug, doc }))
// Sort the filtered documents by the latest date in descending order
const sortedDocuments = filteredDocuments.sort((a, b) => {
const sortedDocuments = filteredDocuments.toSorted((a, b) => {
const latestDateA = a.doc.manifest.dates[a.doc.manifest.dates.length - 1]
const latestDateB = b.doc.manifest.dates[b.doc.manifest.dates.length - 1]
return latestDateB - latestDateA // For descending order