@@ -4,14 +4,17 @@ A simple wrapper for embedded Tweet
44Usage:
55
66<Tweet id="20" />
7+ <Tweet url="https://x.com/jack/status/20" />
78-->
89
910<script setup lang="ts">
1011import { onMounted , ref } from ' vue'
1112import { isDark } from ' ../logic/dark'
13+ import { resolveTweetId } from ' ./tweet'
1214
1315const props = defineProps <{
14- id: string | number
16+ id? : string | number
17+ url? : string
1518 scale? : string | number
1619 conversation? : string
1720 cards? : ' hidden' | ' visible'
@@ -23,6 +26,14 @@ const loaded = ref(false)
2326const tweetNotFound = ref (false )
2427
2528async function create(retries = 10 ) {
29+ const tweetId = resolveTweetId (props .id , props .url )
30+ if (! tweetId ) {
31+ loaded .value = true
32+ tweetNotFound .value = true
33+ console .error (' Tweet requires a valid id or X post URL.' )
34+ return
35+ }
36+
2637 // @ts-expect-error global
2738 if (! window .twttr ?.widgets ?.createTweet ) {
2839 if (retries <= 0 )
@@ -32,7 +43,7 @@ async function create(retries = 10) {
3243 }
3344 // @ts-expect-error global
3445 const element = await window .twttr .widgets .createTweet (
35- props . id . toString () ,
46+ tweetId ,
3647 tweet .value ,
3748 {
3849 theme: isDark .value ? ' dark' : ' light' ,
@@ -56,7 +67,7 @@ onMounted(() => {
5667 <div v-if =" !loaded || tweetNotFound" class =" w-30 h-30 my-10px bg-gray-400 bg-opacity-10 rounded-lg flex opacity-50" >
5768 <div class =" m-auto animate-pulse text-4xl" >
5869 <div class =" i-carbon:logo-twitter" />
59- <span v-if =" tweetNotFound" >Could not load tweet with id="{{ props.id }}" </span >
70+ <span v-if =" tweetNotFound" >Could not load tweet</span >
6071 </div >
6172 </div >
6273 </div >
0 commit comments