|
| 1 | +import React, { useEffect, useLayoutEffect, useState, useRef } from "react"; |
| 2 | +import './HomepagePRs.css'; |
| 3 | + |
| 4 | +const GITHUB_API_URL = 'https://api.github.com' |
| 5 | + |
| 6 | +const GITHUB_ORGANIZATION = "Sofie-Automation" |
| 7 | +const GITHUB_REPO = "sofie-core" |
| 8 | + |
| 9 | +export function HomepagePRs() { |
| 10 | + const [isReady, setIsReady] = useState("error") |
| 11 | + const [pulls, setPulls] = useState([]) |
| 12 | + |
| 13 | + useEffect(() => { |
| 14 | + let mounted = true |
| 15 | + fetch(`${GITHUB_API_URL}/repos/${GITHUB_ORGANIZATION}/${GITHUB_REPO}/pulls?state=all&sort=updated&direction=desc&per_page=100`, { |
| 16 | + headers: [['Accept', 'application/vnd.github.text+json']], |
| 17 | + }) |
| 18 | + .then((value) => { |
| 19 | + if (value.ok) { |
| 20 | + return value.json() |
| 21 | + } else { |
| 22 | + throw new Error(value.status) |
| 23 | + } |
| 24 | + }) |
| 25 | + .then((data) => { |
| 26 | + if (!mounted) return |
| 27 | + setPulls(data.filter((pull) => !pull.draft && (pull.state === 'closed' && pull.merged_at || pull.state === 'open'))) |
| 28 | + setIsReady("done") |
| 29 | + }) |
| 30 | + .catch((error) => { |
| 31 | + if (!mounted) return |
| 32 | + console.error(error) |
| 33 | + setIsReady("error") |
| 34 | + }) |
| 35 | + |
| 36 | + return () => { |
| 37 | + mounted = false |
| 38 | + } |
| 39 | + }, []) |
| 40 | + |
| 41 | + return ( |
| 42 | + <React.Fragment> |
| 43 | + <div className="container"> |
| 44 | + <h2 className="homepage-prs-title">What's the latest in Sofie?</h2> |
| 45 | + </div> |
| 46 | + {isReady === "error" && <div className="homepage-prs-error">Failed to load recent pull requests from GitHub.</div>} |
| 47 | + {isReady === "loading" && <div className="homepage-prs-loading">Loading recent pull requests...</div>} |
| 48 | + {isReady === "done" && <div className="homepage-pr-gallery"> |
| 49 | + {chunkArray(pulls, 3).map((chunk, chunkIndex) => ( |
| 50 | + <AnimatedRow pulls={chunk} key={chunkIndex} speed={0.025 * (chunkIndex + 1)} /> |
| 51 | + ))} |
| 52 | + </div>} |
| 53 | + </React.Fragment> |
| 54 | + ) |
| 55 | +} |
| 56 | + |
| 57 | +function AnimatedRow(props) { |
| 58 | + const { pulls, speed } = props |
| 59 | + const ref = useRef() |
| 60 | + const pausedRef = useRef(false) |
| 61 | + |
| 62 | + const [elementWidth, setElementWidth] = useState(0) |
| 63 | + const [containerWidth, setContainerWidth] = useState(0) |
| 64 | + |
| 65 | + useLayoutEffect(() => { |
| 66 | + const element = ref.current |
| 67 | + if (!element) return |
| 68 | + |
| 69 | + console.log(elementWidth, containerWidth) |
| 70 | + |
| 71 | + let animationFrameId |
| 72 | + let lastTimestamp = null |
| 73 | + |
| 74 | + let scrollAmount = 0 |
| 75 | + |
| 76 | + function animate(timestamp) { |
| 77 | + if (elementWidth <= containerWidth) return // No need to scroll if content fits |
| 78 | + if (elementWidth - scrollAmount < containerWidth) { |
| 79 | + scrollAmount = 0 // Reset scroll to start when we've scrolled through all content |
| 80 | + } |
| 81 | + if (lastTimestamp !== null && !pausedRef.current) { |
| 82 | + const delta = timestamp - lastTimestamp |
| 83 | + scrollAmount += delta * (speed || 0.05) // Adjust the speed here (0.05 is the default speed factor) |
| 84 | + element.style.transform = `translateX(-${scrollAmount}px)`; |
| 85 | + } |
| 86 | + lastTimestamp = timestamp |
| 87 | + animationFrameId = requestAnimationFrame(animate) |
| 88 | + } |
| 89 | + |
| 90 | + animationFrameId = requestAnimationFrame(animate) |
| 91 | + |
| 92 | + return () => { |
| 93 | + cancelAnimationFrame(animationFrameId) |
| 94 | + } |
| 95 | + }, [speed, elementWidth, containerWidth]) |
| 96 | + |
| 97 | + useLayoutEffect(() => { |
| 98 | + const element = ref.current |
| 99 | + if (!element) return |
| 100 | + |
| 101 | + const updateWidth = () => { |
| 102 | + setElementWidth(element.scrollWidth) |
| 103 | + } |
| 104 | + const updateContainerWidth = () => { |
| 105 | + setContainerWidth(element.clientWidth) |
| 106 | + } |
| 107 | + |
| 108 | + updateWidth() |
| 109 | + updateContainerWidth() |
| 110 | + |
| 111 | + window.addEventListener('resize', updateWidth) |
| 112 | + window.addEventListener('resize', updateContainerWidth) |
| 113 | + return () => { |
| 114 | + window.removeEventListener('resize', updateWidth) |
| 115 | + window.removeEventListener('resize', updateContainerWidth) |
| 116 | + } |
| 117 | + }, [pulls]) |
| 118 | + |
| 119 | + function handleMouseEnter() { |
| 120 | + pausedRef.current = true |
| 121 | + } |
| 122 | + |
| 123 | + function handleMouseLeave() { |
| 124 | + pausedRef.current = false |
| 125 | + } |
| 126 | + |
| 127 | + return ( |
| 128 | + <div className="homepage-pr-gallery-row" onMouseEnter={handleMouseEnter} onMouseLeave={handleMouseLeave}> |
| 129 | + <div className="homepage-pr-gallery-row-inner" ref={ref}> |
| 130 | + {pulls.map((pull) => { |
| 131 | + return ( |
| 132 | + <div className="homepage-pr-gallery-pull" key={String(pull.id)}> |
| 133 | + <div className="homepage-pr-gallery-pull-header"> |
| 134 | + <div className="homepage-pr-gallery-pull-number">#{pull['number']}</div> |
| 135 | + <div className="homepage-pr-gallery-pull-title"> |
| 136 | + <a href={pull.html_url} target="_blank" rel="noopener noreferrer">{pull.title}</a> |
| 137 | + </div> |
| 138 | + </div> |
| 139 | + <SponsorBadge sponsorLabel={pull.labels.find((label) => label.name.match(/contr(\w+) (from|by)/i))} /> |
| 140 | + <div className="homepage-pr-gallery-pull-author"> |
| 141 | + <div className="homepage-pr-gallery-pull-author-avatar" style={{ "--user-avatar": `url("${pull.user.avatar_url}")` }}></div> |
| 142 | + <div className="homepage-pr-gallery-pull-author-name">{pull.user.login}</div> |
| 143 | + </div> |
| 144 | + </div> |
| 145 | + ) |
| 146 | + })} |
| 147 | + </div> |
| 148 | + </div> |
| 149 | + ) |
| 150 | +} |
| 151 | + |
| 152 | +function chunkArray(arr, chunkCount) { |
| 153 | + const arrLength = arr.length; |
| 154 | + const chunkSize = Math.floor(arrLength / chunkCount); |
| 155 | + |
| 156 | + const result = []; |
| 157 | + for (let i = 0; i < chunkCount - 1; i++) { |
| 158 | + result.push(arr.slice(i * chunkSize, (i + 1) * chunkSize)) |
| 159 | + } |
| 160 | + |
| 161 | + // last chunk contains the remainer of stuff |
| 162 | + result.push(arr.slice((chunkCount - 1) * chunkSize)) |
| 163 | + return result |
| 164 | +} |
| 165 | + |
| 166 | +function SponsorBadge(props) { |
| 167 | + if (!props.sponsorLabel) return null |
| 168 | + |
| 169 | + const { sponsorLabel: label } = props |
| 170 | + |
| 171 | + return ( |
| 172 | + <div className="homepage-pr-gallery-pull-sponsor" style={{ '--label-color': `#${label.color}` }}>{label.name}</div> |
| 173 | + ) |
| 174 | +} |
0 commit comments