-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat/refactor: reddit embeds, use dynamic instead of conditional rend…
…er (#103) * feat/refactor: reddit embeds, use dynamic instead of conditional render * chore: format reddit jsx
- Loading branch information
1 parent
31373a9
commit 233878c
Showing
5 changed files
with
66 additions
and
37 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import useDarkMode from '../hooks/useDarkMode'; | ||
import useScript from '../hooks/useScript'; | ||
|
||
interface RedditEmbedProps { | ||
url: string; | ||
title: string; | ||
author: string; | ||
subreddit: string; | ||
} | ||
|
||
export default function RedditPost({ | ||
url, | ||
title, | ||
author, | ||
subreddit, | ||
}: RedditEmbedProps) { | ||
const { isDark } = useDarkMode(); | ||
useScript('https://embed.reddit.com/widgets.js'); | ||
|
||
return ( | ||
<blockquote | ||
className="reddit-embed-bq" | ||
style={{ height: '316px' }} | ||
data-embed-height="316" | ||
data-embed-theme={isDark ? 'dark' : 'light'} | ||
> | ||
<a href={url}>{title}</a> | ||
<br /> | ||
by | ||
<a href={`https://www.reddit.com/user/${author}/`}>u/{author}</a> | ||
in | ||
<a href={`https://www.reddit.com/r/${subreddit}/`}>{subreddit}</a> | ||
</blockquote> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { useEffect } from 'react'; | ||
|
||
// https://stackoverflow.com/questions/34424845/adding-script-tag-to-react-jsx | ||
const useScript = (url: string) => { | ||
useEffect(() => { | ||
const script = document.createElement('script'); | ||
|
||
script.src = url; | ||
script.async = true; | ||
|
||
document.body.appendChild(script); | ||
|
||
return () => { | ||
document.body.removeChild(script); | ||
}; | ||
}, [url]); | ||
}; | ||
|
||
export default useScript; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters