Skip to content
This repository was archived by the owner on Feb 27, 2024. It is now read-only.

Commit 8aa4c4c

Browse files
committed
Adding Twitter library using dynamic imports.
1 parent 566bf05 commit 8aa4c4c

File tree

4 files changed

+274
-45
lines changed

4 files changed

+274
-45
lines changed

components/atoms/TwitterEmbed/TwitterEmbed.js

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import RichText from '@/components/atoms/RichText'
12
import cn from 'classnames'
23
import PropTypes from 'prop-types'
4+
import {TwitterTweetEmbed} from 'react-twitter-embed'
35
import styles from './TwitterEmbed.module.css'
4-
import {useEffect, useRef} from 'react'
5-
import RichText from '@/components/atoms/RichText'
66

77
/**
88
* TwitterEmbed Block
@@ -15,39 +15,22 @@ import RichText from '@/components/atoms/RichText'
1515
* @return {Element} The TwitterEmbed component.
1616
*/
1717
export default function TwitterEmbed({className, caption, url}) {
18-
const tweetRef = useRef(null)
19-
20-
useEffect(() => {
21-
if (url === '') {
22-
return
23-
}
24-
// @see https://stackoverflow.com/a/43268098/921927
25-
const proxyUrl = 'https://cors-anywhere.herokuapp.com/',
26-
targetUrl = `https://publish.twitter.com/oembed?url=${url}`
27-
fetch(proxyUrl + targetUrl)
28-
.then((response) => response.json())
29-
.then((data) => {
30-
if (data && data.html) {
31-
// @see https://stackoverflow.com/a/43268098/921927
32-
// Create slot for executing a script file.
33-
const slotHtml = document
34-
.createRange()
35-
.createContextualFragment(data.html) // Create a 'tiny' document and parse the html string.
36-
tweetRef.current.innerHTML = '' // Clear the container.
37-
tweetRef.current.appendChild(slotHtml) // Append the new content.
38-
}
39-
})
40-
}, [])
18+
const tweetURL = url ? url.split('/') : '' // Split URL string into array.
19+
const tweetId = tweetURL ? tweetURL[tweetURL.length - 1] : '' // Get ID from url array.
4120

4221
return (
43-
<div className={cn(styles.twitterEmbed, className)}>
44-
<div ref={tweetRef}></div>
45-
{!!caption && (
46-
<div className={styles.caption}>
47-
<RichText tag="span">{caption}</RichText>
22+
<>
23+
{!!tweetId && Number.isInteger(tweetId) && (
24+
<div className={cn(styles.twitterEmbed, className)}>
25+
<TwitterTweetEmbed tweetId={tweetId} />
26+
{!!caption && (
27+
<div className={styles.caption}>
28+
<RichText tag="span">{caption}</RichText>
29+
</div>
30+
)}
4831
</div>
4932
)}
50-
</div>
33+
</>
5134
)
5235
}
5336

components/blocks/Gutenberg/BlockEmbed/BlockEmbed.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,48 @@
1-
import TwitterEmbed from '@/components/atoms/TwitterEmbed'
21
import VideoEmbed from '@/components/atoms/VideoEmbed'
2+
import dynamic from 'next/dynamic'
33
import PropTypes from 'prop-types'
4-
import React from 'react'
4+
import React, {useEffect, useState} from 'react'
5+
6+
const Tweet = dynamic(() => import('@/components/atoms/TwitterEmbed'))
57

68
/**
79
* Embed Block
810
*
911
* The core Embed block from Gutenberg.
1012
*
1113
* @author WebDevStudios
12-
* @param {string} className Optional classnames.
13-
* @param {string} url The URL of the video.
14-
* @param {string} caption Optional caption.
15-
* @param {string} align Block alignment caption.
16-
* @param {string} providerNameSlug The type of embed.
17-
* @return {Element} The component to embed.
14+
* @param {object} props The component properties.
15+
* @param {string} props.className Optional classnames.
16+
* @param {string} props.url The URL of the video.
17+
* @param {string} props.caption Optional caption.
18+
* @param {string} props.providerNameSlug The type of embed.
19+
* @return {Element} The component to embed.
1820
*/
1921
export default function BlockEmbed({
2022
className,
2123
url,
2224
caption,
2325
providerNameSlug
2426
}) {
27+
const [loadTweet, setLoadTweet] = useState(0)
28+
const supportedVideoTypes = ['youtube', 'vimeo']
29+
30+
useEffect(() => {
31+
// Load Tweet library using Next Dynamic
32+
// @see https://nextjs.org/docs/advanced-features/dynamic-import
33+
if (providerNameSlug === 'twitter') {
34+
setLoadTweet(1)
35+
}
36+
})
37+
2538
return (
2639
<>
2740
{!!url && (
2841
<>
29-
{providerNameSlug === 'twitter' ? (
30-
<TwitterEmbed className={className} url={url} caption={caption} />
31-
) : (
32-
// <div dangerouslySetInnerHTML={{__html: tweetContent}} />
42+
{!!loadTweet && (
43+
<Tweet className={className} url={url} caption={caption} />
44+
)}
45+
{!!supportedVideoTypes.includes(providerNameSlug) && (
3346
<VideoEmbed
3447
className={className}
3548
url={url}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"next": "latest",
4343
"react": "17.0.1",
4444
"react-dom": "17.0.1",
45+
"react-twitter-embed": "^3.0.3",
4546
"yup": "^0.32.8"
4647
},
4748
"devDependencies": {

0 commit comments

Comments
 (0)