From 17361b092a6bd7aa2528131bd0acb923363a7785 Mon Sep 17 00:00:00 2001 From: Ananth Venkatesh <46249765+quantum9Innovation@users.noreply.github.com> Date: Fri, 16 Feb 2024 22:24:18 +0000 Subject: [PATCH] Show related affiliation documents --- .../[shortName]/findDocumentsByAffiliation.ts | 61 +++++++++++++++++++ src/app/affiliation/[shortName]/page.tsx | 20 +++++- .../author/[author]/findDocumentsByAuthor.ts | 2 +- 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 src/app/affiliation/[shortName]/findDocumentsByAffiliation.ts diff --git a/src/app/affiliation/[shortName]/findDocumentsByAffiliation.ts b/src/app/affiliation/[shortName]/findDocumentsByAffiliation.ts new file mode 100644 index 0000000..af90149 --- /dev/null +++ b/src/app/affiliation/[shortName]/findDocumentsByAffiliation.ts @@ -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 +} diff --git a/src/app/affiliation/[shortName]/page.tsx b/src/app/affiliation/[shortName]/page.tsx index 5c2cb16..75793b0 100644 --- a/src/app/affiliation/[shortName]/page.tsx +++ b/src/app/affiliation/[shortName]/page.tsx @@ -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) => ( <> -
+
{d}

@@ -56,6 +61,19 @@ export default function Page({
+ {affiliationDocuments.length > 0 && ( + <> +
+

+ Related documents {`(${affiliationDocuments.length})`} +

+ {affiliationDocuments.map((d) => ( + + + + ))} + + )} ) } diff --git a/src/app/author/[author]/findDocumentsByAuthor.ts b/src/app/author/[author]/findDocumentsByAuthor.ts index 5a2d904..f98a97c 100644 --- a/src/app/author/[author]/findDocumentsByAuthor.ts +++ b/src/app/author/[author]/findDocumentsByAuthor.ts @@ -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