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

Fix/homepage images on private repos #1461

Merged
merged 7 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
26 changes: 16 additions & 10 deletions src/templates/homepage/HeroSection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
/* eslint-disable jsx-a11y/anchor-is-valid */

import PropTypes from "prop-types"
import { forwardRef } from "react"
import { useQuery } from "react-query"
import { forwardRef, useState, useEffect } from "react"

import { useGetMediaHook } from "hooks/mediaHooks"

import editorStyles from "styles/isomer-cms/pages/Editor.module.scss"

import { getClassNames } from "templates/utils/stylingUtils"

import { fetchImageURL } from "utils"
import { getImageDetails } from "utils/images"

/* eslint
react/no-array-index-key: 0
Expand Down Expand Up @@ -178,14 +179,19 @@ const TemplateHeroSection = (
{ hero, siteName, dropdownIsActive, toggleDropdown },
ref
) => {
const { data: loadedImageURL } = useQuery(
`${siteName}/${hero.background}`,
() => fetchImageURL(siteName, decodeURI(hero.background)),
{
refetchOnWindowFocus: false,
staleTime: Infinity, // Never automatically refetch image unless query is invalidated
const [loadedImageURL, setLoadedImageURL] = useState("")
const { fileName, imageDirectory } = getImageDetails(hero.background)
const { data: mediaData } = useGetMediaHook({
siteName,
mediaDirectoryName: imageDirectory || "images",
fileName,
})

useEffect(() => {
if (mediaData) {
setLoadedImageURL(mediaData.mediaUrl)
}
)
}, [mediaData])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why can't we just rename data to loadedImageURL here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data consists of a lot of image related data, not just the url!


const heroStyle = {
// See j08691's answer at https://stackoverflow.com/questions/21388712/background-size-doesnt-work
Expand Down
27 changes: 17 additions & 10 deletions src/templates/homepage/InfopicLeftSection.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import PropTypes from "prop-types"
import { forwardRef } from "react"
import { useQuery } from "react-query"
import { forwardRef, useState, useEffect } from "react"

import { useGetMediaHook } from "hooks/mediaHooks"

import editorStyles from "styles/isomer-cms/pages/Editor.module.scss"

import { getClassNames } from "templates/utils/stylingUtils"

import { fetchImageURL } from "utils"
import { getImageDetails } from "utils/images"

/* eslint
react/no-array-index-key: 0
*/
Expand All @@ -28,14 +30,19 @@ const TemplateInfopicLeftSection = (
e.target.src = "/placeholder_no_image.png"
}

const { data: loadedImageURL } = useQuery(
`${siteName}/${imageUrl}`,
() => fetchImageURL(siteName, decodeURI(imageUrl)),
{
refetchOnWindowFocus: false,
staleTime: Infinity, // Never automatically refetch image unless query is invalidated
const [loadedImageURL, setLoadedImageURL] = useState("")
const { fileName, imageDirectory } = getImageDetails(imageUrl)
const { data: mediaData } = useGetMediaHook({
siteName,
mediaDirectoryName: imageDirectory || "images",
fileName,
})

useEffect(() => {
if (mediaData) {
setLoadedImageURL(mediaData.mediaUrl)
}
)
}, [mediaData])

return (
<div ref={ref}>
Expand Down
26 changes: 16 additions & 10 deletions src/templates/homepage/InfopicRightSection.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import PropTypes from "prop-types"
import { forwardRef } from "react"
import { useQuery } from "react-query"
import { forwardRef, useState, useEffect } from "react"

import { useGetMediaHook } from "hooks/mediaHooks"

import editorStyles from "styles/isomer-cms/pages/Editor.module.scss"

import { getClassNames } from "templates/utils/stylingUtils"

import { fetchImageURL } from "utils"
import { getImageDetails } from "utils/images"

/* eslint
react/no-array-index-key: 0
Expand All @@ -29,14 +30,19 @@ const TemplateInfopicRightSection = (
e.target.src = "/placeholder_no_image.png"
}

const { data: loadedImageURL } = useQuery(
`${siteName}/${imageUrl}`,
() => fetchImageURL(siteName, decodeURI(imageUrl)),
{
refetchOnWindowFocus: false,
staleTime: Infinity, // Never automatically refetch image unless query is invalidated
const [loadedImageURL, setLoadedImageURL] = useState("")
const { fileName, imageDirectory } = getImageDetails(imageUrl)
const { data: mediaData } = useGetMediaHook({
siteName,
mediaDirectoryName: imageDirectory || "images",
fileName,
})

useEffect(() => {
if (mediaData) {
setLoadedImageURL(mediaData.mediaUrl)
}
)
}, [mediaData])

return (
<div ref={ref}>
Expand Down
18 changes: 18 additions & 0 deletions src/utils/images.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Util method to retrieve image details from /images folder from the relative file path,
* e.g. "/images/album%/picture$.jpg" -> { imageDirectory: "images%2Falbum%24", fileName: "picture%24.jpg" }
alexanderleegs marked this conversation as resolved.
Show resolved Hide resolved
*/
export const getImageDetails = (imageLink: string) => {
alexanderleegs marked this conversation as resolved.
Show resolved Hide resolved
const cleanImagePath = decodeURI(imageLink).replace(/^\//, "")
const filePathArr = cleanImagePath
.split("/")
.map((segment) => encodeURIComponent(segment))
const fileName = filePathArr[filePathArr.length - 1]
const imageDirectory = filePathArr
.slice(0, filePathArr.length - 1)
.join("%2F")
return {
fileName,
imageDirectory,
}
}