Skip to content
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
15 changes: 12 additions & 3 deletions src/components/DataCatalogCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -11,25 +12,33 @@ 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<string | undefined>(logo);
const [resolvedLogo, setResolvedLogo] = useState<string | undefined>(() =>
resolveLogo(name, url, logo)
);
// State to track if there was an error loading the image
const [hasImageError, setHasImageError] = useState(false);

// Use effect to find the appropriate logo when the component mounts
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
} catch (error) {
console.error(`Error resolving logo for data catalog "${name}":`, error);
setResolvedLogo(undefined);
}
}, [name, logo]);
}, [name, url, logo]);

// Handle image load error
const handleImageError = () => {
Expand Down
5 changes: 4 additions & 1 deletion src/components/FilterPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1059,9 +1060,11 @@ export default function FilterPanel({

const mapping: Record<string, { name: string; logo?: string }> = {};
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;
Expand Down
12 changes: 9 additions & 3 deletions src/components/LinkPreviewCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -93,6 +94,11 @@ export default function LinkPreviewCard({
const [faviconError, setFaviconError] = useState(false);
const [isVisible, setIsVisible] = useState(false);
const cardRef = useRef<HTMLDivElement>(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() === "") {
Expand Down Expand Up @@ -444,7 +450,7 @@ export default function LinkPreviewCard({
}}
>
{/* Image section */}
{ogData.image && !imageError && !compact && (
{ogData.image && !imageError && !compact && !logoExcluded && (
<Box
sx={{
position: "relative",
Expand Down Expand Up @@ -517,7 +523,7 @@ export default function LinkPreviewCard({
</Box>

<Box sx={{ display: "flex", alignItems: "center", gap: 1 }}>
{ogData.favicon && (
{ogData.favicon && !logoExcluded && (
<Box
sx={{
width: 16,
Expand Down Expand Up @@ -562,7 +568,7 @@ export default function LinkPreviewCard({
</Box>

{/* Image section for compact mode */}
{ogData.image && !imageError && compact && (
{ogData.image && !imageError && compact && !logoExcluded && (
<Box
sx={{
position: "relative",
Expand Down
78 changes: 78 additions & 0 deletions src/components/SourceCredits.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"use client";

import { Box, Link, Typography } from "@mui/material";
import { Fragment } from "react";
import {
SOURCES,
creditTextFor,
findSourceIdForCatalogue,
recordSourceIds,
} from "@/config/sourceAttribution";

// Links the licence name to the licence and turns bare URLs into links
function renderCredit(text: string, licenceName?: string, licenceUrl?: string) {
const patterns = ["https?://[^\\s]+?(?=\\.?(?:\\s|$))"];
if (licenceName && licenceUrl) {
patterns.unshift(licenceName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
}
return text.split(new RegExp(`(${patterns.join("|")})`, "g")).map((part, index) => {
const href =
licenceName && licenceUrl && part === licenceName
? licenceUrl
: /^https?:\/\//.test(part)
? part
: undefined;
return href ? (
<Link key={index} href={href} target="_blank" rel="noopener noreferrer">
{part}
</Link>
) : (
<Fragment key={index}>{part}</Fragment>
);
});
}

// 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 (
<Box sx={{ mt: 1 }}>
{ids.map((id) => {
const attribution = SOURCES[id]?.attribution;
return (
<Typography
key={id}
variant="caption"
component="p"
color="text.secondary"
sx={{ mb: 0.5 }}
>
{renderCredit(
creditTextFor(id, { publisher }),
attribution?.licence_name,
attribution?.licence_url
)}
</Typography>
);
})}
</Box>
);
}
15 changes: 15 additions & 0 deletions src/components/StudyDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -1001,8 +1002,22 @@ const StudyDetailComponent = ({
)
)}
</Box>
<SourceCredits
source={displayStudy.extra_data?.source}
catalogs={study.dataset_schema.includedInDataCatalog}
publisher={displayStudy.dataset_schema?.publisher?.[0]?.name}
/>
</Box>
)}
{/* Source credits on their own when there are no catalogue cards */}
{!study.dataset_schema?.includedInDataCatalog?.length && (
<Box sx={{ mb: 4 }}>
<SourceCredits
source={displayStudy.extra_data?.source}
publisher={displayStudy.dataset_schema?.publisher?.[0]?.name}
/>
</Box>
)}
{/* Topics section - only shown if topics exist */}
{hasTopics && (
<Box sx={{ mb: 4 }}>
Expand Down
Loading
Loading