Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pubs: image links for akvaplan projects (even if only nva/cristin id in data) #445

Merged
merged 1 commit into from
Dec 5, 2024
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
4 changes: 2 additions & 2 deletions components/news/article_square.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const ArticleSquare = (
href,
hreflang,
keywords,
width,
width = 512,
height,
maxWidth,
type,
Expand All @@ -40,7 +40,7 @@ export const ArticleSquare = (
target={target}
title={desc}
>
<SquareImage w={512} url={img512 ?? img ?? thumb} />
<SquareImage w={width} url={img512 ?? img ?? thumb} />

<p
dangerouslySetInnerHTML={{ __html: title ?? name ?? "" }}
Expand Down
54 changes: 19 additions & 35 deletions components/project_link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,27 @@ import { projectURL, pubsURL } from "akvaplan_fresh/services/nav.ts";
import { ArticleSquare } from "akvaplan_fresh/components/news/article_square.tsx";
import { Card } from "akvaplan_fresh/components/card.tsx";
import { t } from "akvaplan_fresh/text/mod.ts";
import { SquareImage } from "akvaplan_fresh/components/square_image.tsx";

const publicationsUrlForCristinProject = (cristin, lang) =>
pubsURL({ lang }) + `?q=cristin_${cristin}`;

export const ProjectLink = (multiple) => {
const { akvaplan, cristin, lang } = multiple;
const thumb =
`https://mnd-assets.mynewsdesk.com/image/upload/c_fill,dpr_auto,f_auto,g_auto,q_auto:good,w_512,ar_1:1/${""}`;
if (akvaplan) {
const {
id,
name,
cloudinary,
} = akvaplan;
const name_t = name?.[lang] as string ?? id;
const href_t = projectURL({ title: id, lang });
const thumb =
`https://mnd-assets.mynewsdesk.com/image/upload/c_fill,dpr_auto,f_auto,g_auto,q_auto:good,w_256,ar_1:1/${cloudinary}`;
return <ArticleSquare name={name_t} href={href_t} thumb={thumb} />;
}
if (cristin) {
const href = publicationsUrlForCristinProject(
cristin.id.split("/").at(-1),
lang,
);
return (
<li>
<a href={href}>
{cristin.title}
</a>
</li>
);
}
return <span>{JSON.stringify(multiple)}</span>;
export const AkvaplanProjectLink = ({ id, label, cloudinary, lang }) => {
const name_t = label?.[lang] as string ?? id;
const href_t = projectURL({ title: id, lang });
const thumb = cloudinary
? `https://mnd-assets.mynewsdesk.com/image/upload/c_fill,dpr_auto,f_auto,g_auto,q_auto:good,w_256,ar_1:1/${cloudinary}`
: undefined;

return thumb
? <ArticleSquare name={name_t} href={href_t} width={128} thumb={thumb} />
: <li href={href_t}>{name_t ?? id}</li>;
};

//const href = publicationsUrlForCristinProject(cristin.id.split("/").at(-1),lang)
const NvaProject = ({ name, title, id, lang }) => <li>{name ?? title ?? id}
</li>;

export const ProjectsAsImageLinks = ({ projects, lang }) => (
projects?.length > 0
? (
Expand All @@ -46,11 +31,10 @@ export const ProjectsAsImageLinks = ({ projects, lang }) => (
<summary style={{ paddingBottom: "1rem" }}>
{t(projects.length === 1 ? "nav.Project" : "nav.Projects")}
</summary>

{projects.map((p) =>
"cristin" in (p ?? {})
? ProjectLink({ ...p, lang })
: <li>{JSON.stringify(p)}</li>
{projects.map(({ akvaplan, nva }) =>
akvaplan && akvaplan.id
? AkvaplanProjectLink({ ...akvaplan, lang })
: NvaProject({ ...nva, lang })
)}
</details>
</Card>
Expand Down
19 changes: 19 additions & 0 deletions components/pub_article.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { isNvaUrl } from "akvaplan_fresh/services/nva.ts";
import { isHandleUrl } from "akvaplan_fresh/services/handle.ts";
import { CCIcons } from "akvaplan_fresh/components/cc-icons.tsx";
import { Breadcrumbs } from "akvaplan_fresh/components/site_nav.tsx";
import pub from "akvaplan_fresh/routes/pub.tsx";
import { Section } from "akvaplan_fresh/components/section.tsx";

export const PubArticle = ({
pub: {
Expand All @@ -32,6 +34,7 @@ export const PubArticle = ({
pdf,
created,
modified,
abstract,
},
lang,
}: { pub: SlimPublication; lang: string }) => {
Expand Down Expand Up @@ -135,6 +138,22 @@ export const PubArticle = ({
)}
</section>

{abstract
? (
<Section>
<h2>{t("Abstract")}</h2>
<p
dangerouslySetInnerHTML={{ __html: abstract }}
style={{
maxWidth: "120ch",
fontSize: "1rem",
whiteSpace: "pre-wrap",
}}
/>
</Section>
)
: null}

{pdf ||
url?.endsWith(".pdf")
? (
Expand Down
8 changes: 5 additions & 3 deletions components/square_image.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { srcset } from "akvaplan_fresh/services/cloudinary.ts";

export const WIDTHS = [512, 256, 145];
export const SquareImage = ({ w, caption, url, widths = WIDTHS }) => (
export const SquareImage = (
{ w = WIDTHS[0], caption, url, widths = WIDTHS },
) => (
<>
<img
alt={caption ?? ""}
Expand All @@ -10,8 +12,8 @@ export const SquareImage = ({ w, caption, url, widths = WIDTHS }) => (
? widths.map((w) => srcset(url, { w, ar: "1:1" })).join(",\n")
: ""}
sizes="30vw"
width="512"
height="512"
width={w}
height={w}
style={{
// "transition": "2s ease-in-out",
// borderRadius: "6px",
Expand Down
2 changes: 1 addition & 1 deletion data/akvaplanists.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion data/priors.json

Large diffs are not rendered by default.

18 changes: 17 additions & 1 deletion data/projects.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,23 @@
{
"id": "polarfront",
"cristin": 2524794,
"name": { "en": "Polar Front", "no": "Polar Front" },
"label": { "en": "Polar Front", "no": "Polar Front" },
"cloudinary": "f1p8gecba7nuxbduxcou"
},
{
"id": "slice",
"cristin": 2694515,
"label": { "en": "SLICE", "no": "SLICE" },
"cloudinary": "kwy8kuceecjpjoh3yyy5"
},
{
"id": "dokumentar",
"cristin": 2703197,
"label": { "en": "DOKUMENTAR", "no": "DOKUMENTAR" }
},
{
"id": "mxr",
"cristin": 2663173,
"label": { "en": "MxR", "no": "MxR" }
}
]
20 changes: 11 additions & 9 deletions islands/pub_nva_pdf_augment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ export const PubNvaPdfAugment = (
};

const embedPdfs = (publication: NvaPublicationLike, state: Signal) => {
const { entityDescription: { abstract, description } } = publication;
if (abstract) {
state.value.abstract = abstract;
}
if (abstract) {
state.value.description = description;
}
// const { entityDescription: { abstract, description } } = publication;
// if (abstract) {
// state.value.abstract = abstract;
// }
// if (description) {
// state.value.description = description;
// }
publication?.associatedArtifacts?.filter(pdfFilter).map(
async (file) => {
const pdfUrl = await getPresignedPdfUrl(
Expand Down Expand Up @@ -126,7 +126,8 @@ export const PubNvaPdfAugment = (

<div id={nvaPdfWrapperHtmlId} />

{state.value?.abstract && (
{
/* {state.value?.abstract && (
<Section>
<h2>{t("Abstract")}</h2>
<p
Expand All @@ -138,7 +139,8 @@ export const PubNvaPdfAugment = (
}}
/>
</Section>
)}
)} */
}

{state.value?.description &&
(
Expand Down
9 changes: 2 additions & 7 deletions routes/project.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
// FIXME (project.tsx) https://github.com/akvaplan-niva/akvaplan_fresh/issues/232

const cristinMap = new Map([
["polarfront", 2524794],
["dokumentar", 2703197],
["slice", 2694515],
["mxr", 2663173],
]);
import _projects from "akvaplan_fresh/data/projects.json" with { type: "json" };
const cristinMap = new Map(_projects.map(({ id, cristin }) => [id, cristin]));

import {
editHref,
Expand Down
23 changes: 16 additions & 7 deletions routes/pub.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Nice PDF preview layout: https://pubs.acs.org/doi/epdf/10.1021/acs.est.4c04495?ref=article_openPDF

import { longDate } from "../time/intl.ts";
import {
getNvaMetadata,
Expand All @@ -24,9 +25,16 @@ import { defineRoute, type RouteConfig } from "$fresh/server.ts";
import {
ProjectsAsImageLinks,
} from "akvaplan_fresh/components/project_link.tsx";
import { mergeNvaAndCristinProjectsWithAkvaplanProjects } from "akvaplan_fresh/services/projects.ts";

// import {
// getAkvaplanProjectsFromNvaCristinIds,
// mergeNvaAndCristinProjectsWithAkvaplanProjects,
// } from "akvaplan_fresh/services/projects.ts";

import { isHandleUrl } from "akvaplan_fresh/services/handle.ts";
import { pubsURL } from "akvaplan_fresh/services/nav.ts";
import { extractNumericId } from "akvaplan_fresh/services/id.ts";
import { akvaplanProjectsFromNvaProjects } from "akvaplan_fresh/services/projects.ts";

export const config: RouteConfig = {
routeOverride:
Expand Down Expand Up @@ -75,9 +83,11 @@ export default defineRoute(async (_req, ctx) => {
// FIXME? Move NVA fetch to browser island (requires adding CORS to Akvaplan pubs service, or using a CORS proxy)
const nvaPublication = nva ? await getNvaMetadata(nva) : undefined;

const projects = await mergeNvaAndCristinProjectsWithAkvaplanProjects(
nvaPublication?.projects,
);
const nvaProjects = nvaPublication ? nvaPublication?.projects : undefined;

const nvaProjectsWithAkvaplanIds = nvaProjects
? await akvaplanProjectsFromNvaProjects(nvaProjects)
: undefined;

const typeText = t(
isHandleUrl(id) || isNvaUrl(id) ? `nva.${type}` : `type.${type}`,
Expand Down Expand Up @@ -130,21 +140,20 @@ export default defineRoute(async (_req, ctx) => {
? <PubNvaPdfAugment publication={nvaPublication} lang={lang} />
: null}

{projects?.length > 0
{nvaProjectsWithAkvaplanIds?.length > 0
? (
<section
style={{
paddingBottom: ".5rem",
}}
>
<ProjectsAsImageLinks
projects={projects}
projects={nvaProjectsWithAkvaplanIds}
lang={lang}
/>
</section>
)
: null}

{
/* {nvaPublication?.fundings?.length > 0
? (
Expand Down
21 changes: 14 additions & 7 deletions services/akvaplanist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,12 @@ const withNameAndFrom = (a: Akvaplanist) => {
const sortByName = (a: Akvaplanist, b: Akvaplanist) =>
a.name?.localeCompare(b?.name ?? "") ?? 0;

const buildSortReversed = (key: string) => (a: Akvaplanist, b: Akvaplanist) =>
new Date(b?.[key]).getTime() - new Date(a?.[key]).getTime();
const buildSortReversed = (key: string) => (a: Akvaplanist, b: Akvaplanist) => {
const _b = b?.[key] ?? "";
const _a = a?.[key] ?? "";
const { compare } = Intl.Collator("no");
return compare(_b, _a);
};

const buildGrouper = (type: string) =>
type === ""
Expand All @@ -361,23 +365,26 @@ const groupBy = (akva, grouper) =>
[...Map.groupBy(
akva,
grouper,
)].sort((a, b) => b[0].localeCompare(a[0]));
)].sort((a, b) => b[0]?.localeCompare(a[0], "no"));

export const getAkvaplanistsGroupedByYearStartedOrLeft = async () => {
const all = (await getAkvaplanistsFromDenoService("all") ?? [])
.map(withNameAndFrom)
.sort(sortByName);
const _all = (await getAkvaplanistsFromDenoService("all") ?? [])
.map(withNameAndFrom);

const _current = all.filter(({ prior }) => prior !== true).sort(
buildSortReversed("from"),
);

const _prior = all.filter(({ prior }) => prior === true).sort(
buildSortReversed("expired"),
);

const currentGroupedByFromYear = groupBy(
_current,
buildGrouper(""),
);
const priorGroupedByExpiredYear = groupBy(
all.filter(({ prior }) => prior === true),
_prior,
buildGrouper("prior"),
);
return [currentGroupedByFromYear, priorGroupedByExpiredYear];
Expand Down
2 changes: 2 additions & 0 deletions services/id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const extractNumericId = (id: string | number) =>
Number(String(id).split("/").at(-1));
34 changes: 17 additions & 17 deletions services/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { searchMynewsdesk } from "./mynewsdesk.ts";
import { projectURL } from "./nav.ts";
import { AbstractMynewsdeskItem, News } from "../@interfaces/mod.ts";
import _projects from "akvaplan_fresh/data/projects.json" with { type: "json" };
import { extractId } from "akvaplan_fresh/services/extract_id.ts";
import { extractNumericId } from "akvaplan_fresh/services/id.ts";

// https://akvaplan.no/en/news/2016-01-26/the-norwegian-ministry-of-foreign-affair-supports-akvaplan-niva-project-by-nok-14.3-million",
// VEIEN http://localhost:7777/en/news/2023-01-30/hvordan-kan-rognkjeks-bli-en-robust-og-effektiv-rensefisk-i-lakseoppdrett
Expand Down Expand Up @@ -114,9 +114,6 @@ const cristinProjectMap = new Map(
_projects.map((p) => [p.cristin, p]),
);

const extractNumericId = (id: string | number) =>
Number(String(id).split("/").at(-1));

const getAkvaplanProjectByCristinProjectId = async (id: string) =>
await Promise.resolve(
cristinProjectMap.get(extractNumericId(id)),
Expand All @@ -125,16 +122,19 @@ const getAkvaplanProjectByCristinProjectId = async (id: string) =>
export const fetchCristinProject = (id: number | string) =>
fetch(`https://api.nva.unit.no/cristin/project/${extractNumericId(id)}`);

export const mergeNvaAndCristinProjectsWithAkvaplanProjects = async (
projects,
) =>
projects?.length > 0
? await Array.fromAsync(
projects.map(async (nva) => {
const akvaplan = await getAkvaplanProjectByCristinProjectId(nva.id);
const r = await fetchCristinProject(nva.id);
const cristin = r?.ok ? await r.json() : undefined;
return { akvaplan, nva, cristin };
}),
)
: undefined;
const cristinProjectAkvaplanId = new Map(
_projects.map((akvaplan) => [akvaplan.cristin, akvaplan]),
);

export const akvaplanProjectsFromNvaProjects = async (nvaProjects) =>
await Array.fromAsync(
nvaProjects?.map(async (nva, i) => {
await async function () {}();
const { cristin, id } = nva;
const pid = cristin ? cristin : extractNumericId(id);
const akvaplan = cristinProjectAkvaplanId.has(pid)
? cristinProjectAkvaplanId.get(pid)
: undefined;
return { nva, akvaplan };
}),
);
Loading
Loading