-
-
Notifications
You must be signed in to change notification settings - Fork 507
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
refactor: Use prerender data API #1192
Changes from 3 commits
5bad0a9
2409f12
fbcdf2a
5474a0c
6803acc
c74d819
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,12 +8,11 @@ import { | |
useMemo, | ||
useCallback | ||
} from 'preact/hooks'; | ||
import { useLocation, useRoute } from 'preact-iso'; | ||
import { useLocation } from 'preact-iso'; | ||
import { TutorialContext, SolutionContext } from './contexts'; | ||
import { ErrorOverlay } from '../repl/error-overlay'; | ||
import { parseStackTrace } from '../repl/errors'; | ||
import cx from '../../../lib/cx'; | ||
import { InjectPrerenderData } from '../../../lib/prerender-data'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a relic of the Preact-CLI setup, |
||
import { useResource } from '../../../lib/use-resource'; | ||
import { useLanguage } from '../../../lib/i18n'; | ||
import { Splitter } from '../../splitter'; | ||
|
@@ -220,11 +219,6 @@ export function Tutorial({ html, meta }) { | |
<ButtonContainer meta={meta} showCode={showCode} help={help} /> | ||
</div> | ||
</Splitter> | ||
|
||
<InjectPrerenderData | ||
name={url} | ||
data={{ html, meta }} | ||
/> | ||
</div> | ||
</TutorialContext.Provider> | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { Component } from 'preact'; | ||
import { localStorageGet, localStorageSet } from '../lib/localstorage'; | ||
import { fallbackData, repoInfo } from '../lib/github'; | ||
import { useEffect, useState } from 'preact/hooks'; | ||
import { repoInfo } from '../lib/github'; | ||
import { usePrerenderData } from '../lib/prerender-data.jsx'; | ||
|
||
const githubStars = repo => | ||
repoInfo(repo).then(d => d.stargazers_count); | ||
|
@@ -10,53 +10,42 @@ const formatNumber = num => (num + '').replace(/(\d{3})$/g, ',$1'); | |
// make available to homepage REPL demo | ||
if (typeof window !== 'undefined') window.githubStars = githubStars; | ||
|
||
export default class GithubStars extends Component { | ||
state = { | ||
stars: localStorageGet('_stars') || fallbackData.preactStargazers | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Getting/setting from/to localstorage here seems unnecessary? I don't see any obvious benefit to doing so. |
||
}; | ||
export default function GitHubStars({ user, repo, simple, children }) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Converted to a function as it simplified consuming the context |
||
const { preactStargazers } = usePrerenderData(); | ||
const [stars, setStars] = useState(preactStargazers); | ||
|
||
setStars = stars => { | ||
if (stars && stars != this.state.stars) { | ||
localStorageSet('_stars', stars); | ||
this.setState({ stars }); | ||
} | ||
}; | ||
|
||
componentDidMount() { | ||
let { user, repo } = this.props; | ||
githubStars(user + '/' + repo).then(this.setStars); | ||
} | ||
|
||
render({ user, repo, simple, children }, { stars }) { | ||
let url = `https://github.com/${user}/${repo}/`; | ||
if (simple) { | ||
return ( | ||
<a href={url} class="stars" target="_blank" rel="noopener noreferrer"> | ||
⭐️ {stars} Stars | ||
</a> | ||
); | ||
} | ||
useEffect(() => { | ||
githubStars(`${user}/${repo}`).then(setStars); | ||
}, []); | ||
|
||
const url = `https://github.com/${user}/${repo}/`; | ||
if (simple) { | ||
return ( | ||
<span class="github-btn"> | ||
<a | ||
class="gh-btn" | ||
href={url} | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
aria-label="Star on GitHub" | ||
> | ||
<span class="gh-ico" /> Star | ||
</a> | ||
<a | ||
class="gh-count" | ||
href={url} | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
> | ||
{stars ? formatNumber(Math.round(stars)) : children || '..'} | ||
</a> | ||
</span> | ||
<a href={url} class="stars" target="_blank" rel="noopener noreferrer"> | ||
⭐️ {stars} Stars | ||
</a> | ||
); | ||
} | ||
|
||
return ( | ||
<span class="github-btn"> | ||
<a | ||
class="gh-btn" | ||
href={url} | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
aria-label="Star on GitHub" | ||
> | ||
<span class="gh-ico" /> Star | ||
</a> | ||
<a | ||
class="gh-count" | ||
href={url} | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
> | ||
{stars ? formatNumber(Math.round(stars)) : children || '..'} | ||
</a> | ||
</span> | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, drive-by issue that I ran into whilst testing:
While our usage example on the home page included the prop, the repl-after here was missing it. This was problematic as it'd attempt to fetch
https://github.com/undefined
if the user tried to open the example in the repl. Must've gotten missed when we added the repl-before & after stuff.