Skip to content

Commit

Permalink
Fixes after PR
Browse files Browse the repository at this point in the history
  • Loading branch information
demchenkoalex committed Jan 27, 2022
1 parent 5ecd937 commit afe2dab
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ return (
| renderMinimizedImage | (PreviewDataImage) => ReactNode | Custom minimised image render prop |
| renderText | (string) => ReactNode | Custom provided text render prop |
| renderTitle | (string) => ReactNode | Custom title render prop |
| requestTimeout | number | Timeout after which request to get preview data should abort |
| textContainerStyle | [ViewStyle](https://reactnative.dev/docs/view-style-props) | Text, title, description and minimized image container style |
| touchableWithoutFeedbackProps | TouchableWithoutFeedbackProps | Top level touchable props |
| requestTimeout | number | Timeout after which request to get preview data should abort |

## License

Expand Down
14 changes: 10 additions & 4 deletions src/LinkPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ export interface LinkPreviewProps {
renderMinimizedImage?: (image: PreviewDataImage) => React.ReactNode
renderText?: (text: string) => React.ReactNode
renderTitle?: (title: string) => React.ReactNode
requestTimeout?: number
text: string
textContainerStyle?: StyleProp<ViewStyle>
touchableWithoutFeedbackProps?: TouchableWithoutFeedbackProps,
requestTimeout?:number
touchableWithoutFeedbackProps?: TouchableWithoutFeedbackProps
}

export const LinkPreview = React.memo(
Expand All @@ -57,10 +57,10 @@ export const LinkPreview = React.memo(
renderMinimizedImage,
renderText,
renderTitle,
requestTimeout = 5000,
text,
textContainerStyle,
touchableWithoutFeedbackProps,
requestTimeout = 5000
}: LinkPreviewProps) => {
const [containerWidth, setContainerWidth] = React.useState(0)
const [data, setData] = React.useState(previewData)
Expand Down Expand Up @@ -95,7 +95,13 @@ export const LinkPreview = React.memo(
return () => {
isCancelled = true
}
}, [enableAnimation, onPreviewDataFetched, previewData, text])
}, [
enableAnimation,
onPreviewDataFetched,
previewData,
requestTimeout,
text,
])

const handleContainerLayout = React.useCallback(
(event: LayoutChangeEvent) => {
Expand Down
18 changes: 10 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const getImageSize = (url: string) => {

// Functions below use functions from the same file and mocks are not working
/* istanbul ignore next */
export const getPreviewData = async (text: string, requestTimeout=5000) => {
export const getPreviewData = async (text: string, requestTimeout = 5000) => {
const previewData: PreviewData = {
description: undefined,
image: undefined,
Expand All @@ -77,23 +77,25 @@ export const getPreviewData = async (text: string, requestTimeout=5000) => {
url = 'https://' + url
}

let abortControllerTimeout: NodeJS.Timeout;
const abortController = new AbortController();
// eslint-disable-next-line no-undef
let abortControllerTimeout: NodeJS.Timeout
const abortController = new AbortController()

const request = fetch(url, {
headers: {
'User-Agent':
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36',
},
signal:abortController.signal
signal: abortController.signal,
})

abortControllerTimeout = setTimeout(() => {
abortController.abort();
}, requestTimeout);
const response = await request;
abortController.abort()
}, requestTimeout)

clearTimeout(abortControllerTimeout);
const response = await request

clearTimeout(abortControllerTimeout)

previewData.link = url

Expand Down

0 comments on commit afe2dab

Please sign in to comment.