/
swr.tsx
45 lines (40 loc) · 1011 Bytes
/
swr.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use client'
import { type ReactNode } from 'react'
import {
EmbeddedTweet,
TweetNotFound,
TweetSkeleton,
type TwitterComponents,
} from './twitter-theme/components.js'
import { type TweetCoreProps } from './utils.js'
import { useTweet } from './hooks.js'
export type TweetProps = Omit<TweetCoreProps, 'id'> & {
fallback?: ReactNode
components?: TwitterComponents
fetchOptions?: RequestInit
} & (
| {
id: string
apiUrl?: string
}
| {
id?: string
apiUrl: string | undefined
}
)
export const Tweet = ({
id,
apiUrl,
fallback = <TweetSkeleton />,
components,
fetchOptions,
onError,
}: TweetProps) => {
const { data, error, isLoading } = useTweet(id, apiUrl, fetchOptions)
if (isLoading) return fallback
if (error || !data) {
const NotFound = components?.TweetNotFound || TweetNotFound
return <NotFound error={onError ? onError(error) : error} />
}
return <EmbeddedTweet tweet={data} components={components} />
}