Skip to content

Commit

Permalink
fix: add error handling around twttr widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
sajald77 committed Aug 26, 2023
1 parent 74a7cbf commit 0c4ef54
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
38 changes: 23 additions & 15 deletions src/forms/markdown/commands/TweetCommand.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Box } from '@chakra-ui/react'
import { useCommands } from '@remirror/react'
import { BsTwitter } from 'react-icons/bs'

import { useDarkMode } from '../../../utils'
import { useDarkMode, useNotification } from '../../../utils'
import {
InsertTwitterModal,
MarkdownTwitter,
Expand All @@ -13,9 +13,9 @@ import { ToolbarCommandButton } from './ToolbarCommandButton'
export const TweetCommand = ({ isDisabled }: { isDisabled?: boolean }) => {
const commands = useCommands()
const isDarkMode = useDarkMode()
const { toast } = useNotification()

const modal = useInsertTwitterModal(async ({ url }: MarkdownTwitter) => {
if (!commands.addYouTubeVideo) return
commands.insertHardBreak()

const UrlSplit = url.split('/').filter((val) => val && val !== '/')
Expand All @@ -24,21 +24,29 @@ export const TweetCommand = ({ isDisabled }: { isDisabled?: boolean }) => {

if (!tweetId) return

const value = await twttr.widgets.createTweet(
tweetId,
document.getElementById('tweet-container'),
{
width: '350px',
theme: isDarkMode ? 'dark' : 'light',
},
)
try {
const value = await twttr.widgets.createTweet(
tweetId,
document.getElementById('tweet-container'),
{
width: '350px',
theme: isDarkMode ? 'dark' : 'light',
},
)

commands.insertHtml(value.innerHTML, {})
commands.insertHardBreak()
commands.insertHtml(value.innerHTML, {})
commands.insertHardBreak()

const element = document.getElementById('tweet-container')
if (element) {
element.innerHTML = ''
const element = document.getElementById('tweet-container')
if (element) {
element.innerHTML = ''
}
} catch {
toast({
status: 'error',
title: 'Failed to insert tweet',
description: 'Please try again',
})
}

modal.onClose()
Expand Down
11 changes: 8 additions & 3 deletions src/forms/markdown/helpers/StyleProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Box, BoxProps, styled } from '@chakra-ui/react'
import { ThemeProvider } from '@remirror/react-components'
import { AllStyledComponent } from '@remirror/styles/emotion'
import { captureException } from '@sentry/react'
import { useEffect, useMemo } from 'react'
import { RemirrorThemeType } from 'remirror'

Expand Down Expand Up @@ -64,9 +65,13 @@ export const StyleProvider = ({
)

useEffect(() => {
twttr.widgets.load(
document.getElementById(ID.project.story.markdown.container),
)
twttr.widgets
.load(document.getElementById(ID.project.story.markdown.container))
.catch((e: any) =>
captureException(e, {
tags: { area: 'twitter-widgets' },
}),
)
}, [])

return (
Expand Down
3 changes: 2 additions & 1 deletion src/translations/English.json
Original file line number Diff line number Diff line change
Expand Up @@ -666,5 +666,6 @@
"Insert":"Insert",
"Must be a valid twitter URL":"Must be a valid twitter URL",
"URL is required":"URL is required",
"Insert tweet":"Insert tweet"
"Insert tweet":"Insert tweet",
"Failed to insert tweet":"Failed to insert tweet"
}

0 comments on commit 0c4ef54

Please sign in to comment.