From da68489886ce5fe845019db1f5667466d01e2f78 Mon Sep 17 00:00:00 2001 From: John Rogers Date: Thu, 8 Oct 2026 12:55:22 +0100 Subject: [PATCH] Credit each source on study pages and stop showing CLOSER's logo Study pages now show the credit line each source's terms require, one per source in extra_data.source, under the data catalogue cards. The wording comes from the source-terms review (SOURCE_ATTRIBUTION_HANDOFF.md in harmonydiscovery); rows marked "required" are used verbatim. The per-source rules live in src/config/sourceAttribution.ts, keyed by source_id. The attribution objects mirror the shape proposed for /discover/sources, so the API can take this over later. Logos: - CLOSER: no logo. Their terms need prior approval to use it. This covers the catalogue card, the source filter chip and link previews of CLOSER pages, which were showing their favicon full size. - Other sources' catalogue cards use the site favicon from Google's favicon service instead of the logo URLs baked into the index, two of which 404. - logo_approved is false for every source until permissions come in. Co-Authored-By: Claude Opus 5.5 --- src/components/DataCatalogCard.tsx | 15 +- src/components/FilterPanel.tsx | 5 +- src/components/LinkPreviewCard.tsx | 12 +- src/components/SourceCredits.tsx | 78 +++++++++++ src/components/StudyDetail.tsx | 15 ++ src/config/sourceAttribution.ts | 211 +++++++++++++++++++++++++++++ 6 files changed, 329 insertions(+), 7 deletions(-) create mode 100644 src/components/SourceCredits.tsx create mode 100644 src/config/sourceAttribution.ts diff --git a/src/components/DataCatalogCard.tsx b/src/components/DataCatalogCard.tsx index 3ddb552d83a..32e5d685e56 100644 --- a/src/components/DataCatalogCard.tsx +++ b/src/components/DataCatalogCard.tsx @@ -3,6 +3,7 @@ import { Box, Card, Typography } from "@mui/material"; import Image from "next/image"; import { findOrganizationLogo } from "@/lib/utils/shared"; +import { findSourceIdForCatalogue, sourceLogo } from "@/config/sourceAttribution"; import { useEffect, useState } from "react"; interface DataCatalogCardProps { @@ -11,9 +12,17 @@ interface DataCatalogCardProps { logo?: string; } +// Known sources follow config/sourceAttribution, not the image URL in the index +function resolveLogo(name: string, url?: string, logo?: string) { + const sourceId = findSourceIdForCatalogue(name, url); + return sourceId ? sourceLogo(sourceId) : findOrganizationLogo(name, logo); +} + export default function DataCatalogCard({ name, url, logo }: DataCatalogCardProps) { // State to track the resolved logo path - const [resolvedLogo, setResolvedLogo] = useState(logo); + const [resolvedLogo, setResolvedLogo] = useState(() => + resolveLogo(name, url, logo) + ); // State to track if there was an error loading the image const [hasImageError, setHasImageError] = useState(false); @@ -21,7 +30,7 @@ export default function DataCatalogCard({ name, url, logo }: DataCatalogCardProp useEffect(() => { // console.log(`Resolving logo for data catalog: "${name}"`); try { - const logoPath = findOrganizationLogo(name, logo); + const logoPath = resolveLogo(name, url, logo); console.log(`Resolved logo for data catalog "${name}":`, logoPath); setResolvedLogo(logoPath); setHasImageError(false); // Reset error state when resolving a new logo @@ -29,7 +38,7 @@ export default function DataCatalogCard({ name, url, logo }: DataCatalogCardProp console.error(`Error resolving logo for data catalog "${name}":`, error); setResolvedLogo(undefined); } - }, [name, logo]); + }, [name, url, logo]); // Handle image load error const handleImageError = () => { diff --git a/src/components/FilterPanel.tsx b/src/components/FilterPanel.tsx index 9e8e09f9027..f4aecf2b250 100644 --- a/src/components/FilterPanel.tsx +++ b/src/components/FilterPanel.tsx @@ -22,6 +22,7 @@ import { } from "@/services/api"; import FancySlider from "@/components/FancySlider"; import { countryCodes } from "@/config/constants"; +import { SOURCES } from "@/config/sourceAttribution"; import { useSearch } from "@/contexts/SearchContext"; // Constants - consolidated at the top level @@ -1059,9 +1060,11 @@ export default function FilterPanel({ const mapping: Record = {}; Object.entries(sourcesData).forEach(([key, sourceInfo]) => { + // Sources excluded from logo use in config/sourceAttribution show their name + const excluded = SOURCES[key]?.faviconDomain === null; mapping[key] = { name: sourceInfo.name || sourceInfo.alternateName || key, - logo: sourceInfo.logo, + logo: excluded ? undefined : sourceInfo.logo, }; }); return mapping; diff --git a/src/components/LinkPreviewCard.tsx b/src/components/LinkPreviewCard.tsx index 5f25250b570..323272f85bd 100644 --- a/src/components/LinkPreviewCard.tsx +++ b/src/components/LinkPreviewCard.tsx @@ -5,6 +5,7 @@ import { Card, Box, Typography, Skeleton, CardContent } from "@mui/material"; import Image from "next/image"; import { ExternalLink } from "lucide-react"; import { fetchOgData } from "@/services/api"; +import { SOURCES, findSourceIdForCatalogue } from "@/config/sourceAttribution"; // Global request throttling mechanism // Keep track of URLs that are currently being fetched @@ -93,6 +94,11 @@ export default function LinkPreviewCard({ const [faviconError, setFaviconError] = useState(false); const [isVisible, setIsVisible] = useState(false); const cardRef = useRef(null); + // Links to sources excluded from logo use (config/sourceAttribution) get no + // image or favicon, since both are usually the source's mark + const linkSourceId = findSourceIdForCatalogue(undefined, url); + const logoExcluded = + linkSourceId !== undefined && SOURCES[linkSourceId].faviconDomain === null; // Don't render if URL is empty or just whitespace if (!url || url.trim() === "") { @@ -444,7 +450,7 @@ export default function LinkPreviewCard({ }} > {/* Image section */} - {ogData.image && !imageError && !compact && ( + {ogData.image && !imageError && !compact && !logoExcluded && ( - {ogData.favicon && ( + {ogData.favicon && !logoExcluded && ( {/* Image section for compact mode */} - {ogData.image && !imageError && compact && ( + {ogData.image && !imageError && compact && !logoExcluded && ( { + const href = + licenceName && licenceUrl && part === licenceName + ? licenceUrl + : /^https?:\/\//.test(part) + ? part + : undefined; + return href ? ( + + {part} + + ) : ( + {part} + ); + }); +} + +// One credit line per source of the record (extra_data.source, else the +// sources of its catalogue cards), as each source's terms require. See +// config/sourceAttribution. +export default function SourceCredits({ + source, + catalogs, + publisher, +}: { + source?: unknown; + catalogs?: { name?: string; url?: string }[]; + publisher?: string; +}) { + let ids = recordSourceIds(source); + if (ids.length === 0) { + ids = (catalogs ?? []) + .map((catalog) => findSourceIdForCatalogue(catalog.name, catalog.url)) + .filter((id): id is string => id !== undefined); + } + ids = Array.from(new Set(ids)); + if (ids.length === 0) return null; + + return ( + + {ids.map((id) => { + const attribution = SOURCES[id]?.attribution; + return ( + + {renderCredit( + creditTextFor(id, { publisher }), + attribution?.licence_name, + attribution?.licence_url + )} + + ); + })} + + ); +} diff --git a/src/components/StudyDetail.tsx b/src/components/StudyDetail.tsx index 94ee8e71083..5c3b8fb0f32 100644 --- a/src/components/StudyDetail.tsx +++ b/src/components/StudyDetail.tsx @@ -13,6 +13,7 @@ import { ChevronDown, ChevronUp, Bookmark, Info } from "lucide-react"; import { useState, memo, useMemo, useEffect, useRef } from "react"; import SquareChip from "@/components/SquareChip"; import DataCatalogCard from "@/components/DataCatalogCard"; +import SourceCredits from "@/components/SourceCredits"; import OrganizationCard from "@/components/OrganizationCard"; import TextWithLinkPreviews from "@/components/TextWithLinkPreviews"; import LinkPreviewCard from "@/components/LinkPreviewCard"; @@ -1001,8 +1002,22 @@ const StudyDetailComponent = ({ ) )} + )} + {/* Source credits on their own when there are no catalogue cards */} + {!study.dataset_schema?.includedInDataCatalog?.length && ( + + + + )} {/* Topics section - only shown if topics exist */} {hasTopics && ( diff --git a/src/config/sourceAttribution.ts b/src/config/sourceAttribution.ts new file mode 100644 index 00000000000..8300956532c --- /dev/null +++ b/src/config/sourceAttribution.ts @@ -0,0 +1,211 @@ +// Per-source credit text and logo rules, keyed by source_id (as used by +// /discover/sources and extra_data.source). +// +// Fallback for when the discovery API doesn't send these fields, which is +// always today. The `attribution` objects mirror the shape proposed for +// /discover/sources; once the API sends them, the API should win. +// +// Wording and statuses come from SOURCE_ATTRIBUTION_HANDOFF.md (harmonydiscovery, +// 2026-10-08). Rows marked "required" are verbatim: don't paraphrase them. + +export interface SourceAttribution { + // {date}, {year}, {publisher} and {publisherName} are filled per record. + // A clause whose placeholder has no value is dropped. + credit_text: string; + // licence_name appears in credit_text and is linked to licence_url + licence_name?: string; + licence_url?: string; + // required: mandated by the source's terms; requested: the source asks for + // it; proposed: no wording prescribed, to confirm with the source + status: "required" | "requested" | "proposed"; + logo_approved: boolean; +} + +export interface SourceConfig { + name: string; + // includedInDataCatalog names and link hosts (subdomains match), to tell + // which source a catalogue card or link belongs to + catalogueNames: string[]; + catalogueHosts: string[]; + // Interim logo until logo_approved: this domain's favicon from Google's + // favicon service. null = no logo. + faviconDomain: string | null; + // Placeholder values that don't come from the record + placeholders?: Record; + attribution: SourceAttribution; +} + +export const SOURCES: Record = { + closer: { + name: "CLOSER", + catalogueNames: ["CLOSER Discovery"], + catalogueHosts: ["closer.ac.uk"], + // Excluded: CLOSER's terms require prior approval to use their logo + faviconDomain: null, + // Harvest year, until the API exposes each record's Colectica VersionDate + placeholders: { date: "2026" }, + attribution: { + credit_text: + "CLOSER {date}. This CLOSER resource is licensed under the Non-Commercial Government Licence v2.0, except where otherwise stated. Our use of this metadata does not imply endorsement by CLOSER.", + licence_name: "Non-Commercial Government Licence v2.0", + licence_url: + "https://www.nationalarchives.gov.uk/doc/non-commercial-government-licence/version/2/", + status: "required", + logo_approved: false, + }, + }, + ukds: { + name: "UK Data Service", + catalogueNames: ["UK Data Service"], + catalogueHosts: ["ukdataservice.ac.uk"], + faviconDomain: "ukdataservice.ac.uk", + attribution: { + credit_text: + "Metadata sourced from the UK Data Service, derived from original data producer submissions. Its reuse does not imply endorsement by the UK Data Service, the original data producers, rights holders or contributing organisations.", + status: "required", + logo_approved: false, + }, + }, + ukllc: { + name: "UK Longitudinal Linkage Collaboration", + catalogueNames: ["UK Longitudinal Linkage Collaboration: UK LLC"], + catalogueHosts: ["ukllc.ac.uk"], + faviconDomain: "ukllc.ac.uk", + attribution: { + credit_text: + "Metadata © 2020–2024 UK Longitudinal Linkage Collaboration and contributors, licensed under CC BY-NC-SA 4.0. Adapted by Harmony; adaptations are shared under the same licence.", + licence_name: "CC BY-NC-SA 4.0", + licence_url: "https://creativecommons.org/licenses/by-nc-sa/4.0/", + status: "required", + logo_approved: false, + }, + }, + hdruk: { + name: "Health Data Research Gateway", + catalogueNames: ["Health Data Research Innovation Gateway"], + catalogueHosts: ["healthdatagateway.org", "hdruk.ac.uk"], + faviconDomain: "hdruk.ac.uk", + // Year accessed + placeholders: { year: "2026" }, + attribution: { + // Wording from HDR UK's Attribution Policy v4, not re-verified + credit_text: + "Data discovery and access was facilitated by the Health Data Research Gateway {year}. https://doi.org/10.57774/jrjz-mn46. Dataset metadata provided by {publisherName}.", + status: "required", + logo_approved: false, + }, + }, + mhc: { + name: "Catalogue of Mental Health Measures", + catalogueNames: ["Catalogue of Mental Health Measures"], + catalogueHosts: ["cataloguementalhealth.ac.uk"], + faviconDomain: "cataloguementalhealth.ac.uk", + attribution: { + credit_text: + "Catalogue of Mental Health Measures (2026). Available at www.cataloguementalhealth.ac.uk", + status: "requested", + logo_approved: false, + }, + }, + cls: { + name: "UCL Centre for Longitudinal Studies", + catalogueNames: ["UCL Centre for Longitudinal Studies"], + catalogueHosts: ["cls.ucl.ac.uk"], + faviconDomain: "cls.ucl.ac.uk", + attribution: { + credit_text: + "Metadata from the UCL Centre for Longitudinal Studies (cls.ucl.ac.uk). © UCL. Reuse does not imply endorsement by UCL or CLS.", + status: "proposed", + logo_approved: false, + }, + }, + adruk: { + name: "ADR UK", + catalogueNames: ["ADR UK"], + catalogueHosts: ["adruk.org"], + faviconDomain: "adruk.org", + attribution: { + credit_text: + "Metadata from the ADR UK Data Catalogue (datacatalogue.adruk.org), originally published by {publisher}. Reuse does not imply endorsement by ADR UK.", + status: "proposed", + logo_approved: false, + }, + }, +}; + +// For a source with no entry above +export function genericCredit(name: string): string { + return `Source: ${name}. Reuse does not imply endorsement by ${name}.`; +} + +export function findSourceIdForCatalogue( + name?: string, + url?: string +): string | undefined { + let host: string | undefined; + try { + host = url ? new URL(url).hostname.toLowerCase() : undefined; + } catch { + host = undefined; + } + return Object.keys(SOURCES).find((id) => { + const source = SOURCES[id]; + return ( + (name !== undefined && source.catalogueNames.includes(name)) || + (host !== undefined && + source.catalogueHosts.some((h) => host === h || host.endsWith(`.${h}`))) + ); + }); +} + +export function sourceLogo(id: string): string | undefined { + const domain = SOURCES[id]?.faviconDomain; + return domain + ? `https://www.google.com/s2/favicons?domain=${domain}&sz=64` + : undefined; +} + +// extra_data.source is a list in the index but typed as a string +export function recordSourceIds(source: unknown): string[] { + if (Array.isArray(source)) return source.filter((s) => typeof s === "string"); + return typeof source === "string" && source ? [source] : []; +} + +// Removes the clause holding `token`: from the preceding comma, or else the +// whole sentence it sits in +function dropClause(text: string, token: string): string { + const start = text.indexOf(token); + const end = start + token.length; + const comma = text.lastIndexOf(",", start); + const sentenceStart = text.lastIndexOf(". ", start); + if (comma > sentenceStart) return text.slice(0, comma) + text.slice(end); + const sentenceEnd = text.indexOf(".", end); + const before = sentenceStart >= 0 ? text.slice(0, sentenceStart + 1) : ""; + const after = sentenceEnd >= 0 ? text.slice(sentenceEnd + 1) : ""; + return (before + after).trim(); +} + +export function fillCreditText( + template: string, + values: Record +): string { + let text = template; + for (const match of template.match(/\{\w+\}/g) ?? []) { + const value = values[match.slice(1, -1)]; + text = value ? text.replace(match, value) : dropClause(text, match); + } + return text; +} + +export function creditTextFor( + id: string, + record: { publisher?: string } +): string { + const source = SOURCES[id]; + if (!source) return genericCredit(id); + return fillCreditText(source.attribution.credit_text, { + ...source.placeholders, + publisher: record.publisher, + publisherName: record.publisher, + }); +}