Skip to content

Commit f493657

Browse files
Improve tweet fetching robustness
1 parent 7fd1530 commit f493657

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

extension/src/content-script/components/modals/PaymentModal.tsx

+39-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export const PaymentModal = ({
5454
const [price, setPrice] = useState<TweetPrice | null>(null)
5555
const [error, setError] = useState<string | null>(null)
5656
const [existingAttempts, setExistingAttempts] = useState<bigint>(0n)
57+
const [tweetText, setTweetText] = useState<string | null>(null)
5758
const [txStatus, setTxStatus] = useState<TransactionStatus>({
5859
approve: 'idle',
5960
payment: 'idle',
@@ -122,7 +123,32 @@ export const PaymentModal = ({
122123
setError(null)
123124
setPrice(null)
124125
setExistingAttempts(0n)
126+
setTweetText(null)
125127
setTxStatus({ approve: 'idle', payment: 'idle' })
128+
129+
// Function to get tweet text with retries
130+
const getTweetText = async (retries = 3, delayMs = 1000): Promise<string> => {
131+
for (let attempt = 0; attempt < retries; attempt++) {
132+
const tweetElement = document.querySelector(`article[data-testid="tweet"] a[href*="/${tweetId}"]`)?.closest('article[data-testid="tweet"]')
133+
const tweetTextElement = tweetElement?.querySelector(SELECTORS.TWEET_TEXT)
134+
const rawTweetText = tweetTextElement?.textContent || ''
135+
const cleanedTweetText = cleanPromptText(rawTweetText)
136+
137+
if (cleanedTweetText.trim()) {
138+
return cleanedTweetText
139+
}
140+
141+
// If not the last attempt, wait before retrying
142+
if (attempt < retries - 1) {
143+
debug.log('PaymentModal', `Attempt ${attempt + 1}: Tweet text empty, retrying in ${delayMs}ms`)
144+
await new Promise(resolve => setTimeout(resolve, delayMs))
145+
}
146+
}
147+
throw new Error('Cannot process empty tweet. Please ensure the tweet contains text.')
148+
}
149+
150+
const cleanedTweetText = await getTweetText()
151+
setTweetText(cleanedTweetText)
126152

127153
// Get agent address
128154
const address = await getAgentAddressByName(agentName)
@@ -209,7 +235,10 @@ export const PaymentModal = ({
209235
}
210236

211237
const handleConfirm = async () => {
212-
if (!account || !tokenContract || !agentContract || !price) return
238+
if (!account || !tokenContract || !agentContract || !price || !tweetText?.trim()) {
239+
setError('Cannot process empty tweet. Please ensure the tweet contains text.')
240+
return
241+
}
213242

214243
try {
215244
setTxStatus((prev) => ({ ...prev, approve: 'loading' }))
@@ -305,6 +334,15 @@ export const PaymentModal = ({
305334
</div>
306335
) : (
307336
<div className="space-y-4">
337+
{/* Tweet Content Preview */}
338+
{tweetText && (
339+
<div className="bg-white/5 p-4 rounded-lg space-y-2">
340+
<div className="text-sm text-muted-foreground">Tweet Content</div>
341+
<div className="text-sm text-white break-all whitespace-pre-wrap border border-white/10 rounded p-3 max-h-[200px] overflow-y-auto" style={{ wordBreak: 'break-word', overflowWrap: 'anywhere' }}>
342+
{tweetText}
343+
</div>
344+
</div>
345+
)}
308346
{existingAttempts > 0n && (
309347
<div className="flex items-center gap-2 p-4 bg-yellow-500/10 rounded-lg text-yellow-500">
310348
<AlertTriangle className="w-5 h-5" />

0 commit comments

Comments
 (0)