Skip to content

Commit

Permalink
Make it possible to lazy load unpadded urls
Browse files Browse the repository at this point in the history
  • Loading branch information
fallaciousreasoning committed Oct 21, 2022
1 parent bc59f72 commit 1736b4b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export default function Configure () {
}

return (
<Grid>
<Grid id='brave-news-configure'>
<BackButtonContainer>
<Button onClick={() => setCustomizePage(null)}>
{BackArrow}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Flex from '../../../Flex'
import FollowButton from './FollowButton'
import { getCardColor } from './colors'
import { usePublisher, usePublisherFollowed } from './Context'
import { useUnpaddedImageUrl } from '../useUnpaddedImageUrl'
import { useLazyUnpaddedImageUrl } from '../useUnpaddedImageUrl'

interface CardProps {
backgroundColor?: string
Expand Down Expand Up @@ -63,8 +63,13 @@ export default function FeedCard (props: {
const { followed, setFollowed } = usePublisherFollowed(props.publisherId)

const backgroundColor = publisher.backgroundColor || getCardColor(publisher.feedSource?.url || publisher.publisherId)
const coverUrl = useUnpaddedImageUrl(publisher.coverUrl?.url, undefined, /* useCache= */true)
return <Flex direction="column" gap={8}>
const { url: coverUrl, elementRef } = useLazyUnpaddedImageUrl(publisher.coverUrl?.url, {
rootElement: document.getElementById('brave-news-container')!,
rootMargin: '0px 0px 200px 0px',
useCache: true
})

return <Flex direction="column" gap={8} ref={elementRef}>
<Card backgroundColor={backgroundColor} data-feed-card-is-followed={followed}>
{coverUrl && <CoverImage backgroundImage={coverUrl} />}
<StyledFollowButton following={followed} onClick={() => setFollowed(!followed)} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
import * as React from 'react'
import getBraveNewsController from '../../../api/brave_news'

interface Options {
rootElement?: HTMLElement
rootMargin?: string
threshold?: number

useCache?: boolean
onLoaded?: () => any
}

const cache: { [url: string]: string } = {}
export function useUnpaddedImageUrl (paddedUrl: string | undefined, onLoaded?: () => any, useCache?: boolean) {
const [unpaddedUrl, setUnpaddedUrl] = React.useState('')
Expand Down Expand Up @@ -56,3 +65,33 @@ export function useUnpaddedImageUrl (paddedUrl: string | undefined, onLoaded?: (
}, [paddedUrl])
return unpaddedUrl
}

export function useLazyUnpaddedImageUrl (paddedUrl: string | undefined, options: Options) {
const [visible, setVisible] = React.useState(false)
const [elementRef, setElementRef] = React.useState<HTMLElement | null>(null)

React.useEffect(() => {
if (!elementRef) return

const observer = new IntersectionObserver(([intersectionInfo]) => {
if (!intersectionInfo.isIntersecting) {
return
}
setVisible(true)
}, {
root: options.rootElement,
rootMargin: options.rootMargin,
threshold: options.threshold
})

observer.observe(elementRef)
return () => {
observer.disconnect()
}
}, [options.rootElement, elementRef])

return {
url: useUnpaddedImageUrl(visible ? paddedUrl : undefined, options.onLoaded, options.useCache) || cache[paddedUrl!],
elementRef: setElementRef
}
}

0 comments on commit 1736b4b

Please sign in to comment.