Skip to content

Commit

Permalink
✨ set preferSmallFilename in OwidGdocPage
Browse files Browse the repository at this point in the history
  • Loading branch information
ikesau committed Jan 15, 2025
1 parent c75e7ce commit 09c6bcf
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ export type EnrichedBlockImage = {
originalWidth?: number
size: BlockImageSize
hasOutline: boolean
// Not a real ArchieML prop - we set this to true for Data Insights, as a way to migrate
// first generation data insights to only use their small image
// See https://github.com/owid/owid-grapher/issues/4416
preferSmallFilename?: boolean
} & EnrichedBlockWithParseErrors

export type RawBlockVideo = {
Expand Down
4 changes: 1 addition & 3 deletions site/DataInsightsIndexPageContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ export const DataInsightsIndexPageContent = (
}
)
return (
<DocumentContext.Provider
value={{ isPreviewing, documentType: OwidGdocType.DataInsight }}
>
<DocumentContext.Provider value={{ isPreviewing }}>
<AttachmentsContext.Provider
value={{
imageMetadata,
Expand Down
4 changes: 0 additions & 4 deletions site/gdocs/DocumentContext.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { OwidGdocType } from "@ourworldindata/types"
import { createContext } from "react"

export const DocumentContext = createContext<{
isPreviewing: boolean
// Currently used in Image.tsx to always use smallFilename when viewing a data insight
documentType?: OwidGdocType
}>({
isPreviewing: false,
documentType: undefined,
})
4 changes: 1 addition & 3 deletions site/gdocs/OwidGdoc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ export function OwidGdoc({
linkedChartViews: get(props, "linkedChartViews", {}),
}}
>
<DocumentContext.Provider
value={{ isPreviewing, documentType: props.content.type }}
>
<DocumentContext.Provider value={{ isPreviewing }}>
<AdminLinks />
{content}
</DocumentContext.Provider>
Expand Down
5 changes: 5 additions & 0 deletions site/gdocs/OwidGdocPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
import { DATA_INSIGHT_ATOM_FEED_PROPS } from "../SiteConstants.js"
import { Html } from "../Html.js"
import { CLOUDFLARE_IMAGES_URL } from "../../settings/clientSettings.js"
import { addPreferSmallFilenameToDataInsightImages } from "../gdocs/utils.js"

declare global {
interface Window {
Expand Down Expand Up @@ -111,6 +112,10 @@ export default function OwidGdocPage({
}
}

if (gdoc.content.type === OwidGdocType.DataInsight) {
addPreferSmallFilenameToDataInsightImages(gdoc.content)
}

return (
<Html>
<Head
Expand Down
1 change: 1 addition & 0 deletions site/gdocs/components/ArticleBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export default function ArticleBlock({
alt={block.alt}
hasOutline={block.hasOutline}
containerType={containerType as ImageParentContainer}
preferSmallFilename={block.preferSmallFilename}
/>
{block.caption ? (
<figcaption
Expand Down
9 changes: 4 additions & 5 deletions site/gdocs/components/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useCallback, useContext } from "react"
import {
generateSourceProps,
ImageMetadata,
OwidGdocType,
triggerDownloadFromBlob,
} from "@ourworldindata/utils"
import cx from "classnames"
Expand Down Expand Up @@ -72,27 +71,27 @@ export default function Image(props: {
className?: string
containerType?: ImageParentContainer
shouldLightbox?: boolean
preferSmallFilename?: boolean
}) {
const {
filename,
smallFilename,
hasOutline,
containerType = "default",
shouldLightbox = true,
preferSmallFilename,
} = props

const className = cx("image", props.className, {
"image--has-outline": hasOutline,
})

const { isPreviewing, documentType } = useContext(DocumentContext)
const preferSmallimage = documentType === OwidGdocType.DataInsight
const { isPreviewing } = useContext(DocumentContext)
const isSmall = useMediaQuery(SMALL_BREAKPOINT_MEDIA_QUERY)
const image = useImage(filename)
const smallImage = useImage(smallFilename)
const activeImage =
// If we're in a data insight, we always want to use the small image
(isSmall || preferSmallimage) && smallImage ? smallImage : image
(isSmall || preferSmallFilename) && smallImage ? smallImage : image

const renderImageError = (name: string) => (
<BlockErrorFallback
Expand Down
23 changes: 22 additions & 1 deletion site/gdocs/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ import {
CategoryWithEntries,
EntryMeta,
SubNavId,
OwidGdocDataInsightContent,
} from "@ourworldindata/types"
import { formatAuthors, Url } from "@ourworldindata/utils"
import {
formatAuthors,
traverseEnrichedBlock,
Url,
} from "@ourworldindata/utils"
import { AttachmentsContext } from "./AttachmentsContext.js"
import { SiteNavigationStatic, SubnavItem, subnavs } from "../SiteConstants.js"

Expand Down Expand Up @@ -201,3 +206,19 @@ export const getTopSubnavigationParentItem = (
): SubnavItem | undefined => {
return subnavs[subnavId]?.[0]
}

// Always use the smallFilename for old data insights, where two were provided
// Doing this in code was simpler than migrating all the DI gdocs themselves
// See https://github.com/owid/owid-grapher/issues/4416
export function addPreferSmallFilenameToDataInsightImages(
content: OwidGdocDataInsightContent
): OwidGdocDataInsightContent {
content.body.forEach((node) =>
traverseEnrichedBlock(node, (block) => {
if (block.type === "image") {
block.preferSmallFilename = true
}
})
)
return content
}

0 comments on commit 09c6bcf

Please sign in to comment.