diff --git a/src/components/account.jsx b/src/components/account.jsx index 075421fb..7554df00 100644 --- a/src/components/account.jsx +++ b/src/components/account.jsx @@ -3,6 +3,7 @@ import './account.css'; import { useEffect, useState } from 'preact/hooks'; import enhanceContent from '../utils/enhance-content'; +import handleAccountLinks from '../utils/handle-account-links'; import shortenNumber from '../utils/shorten-number'; import store from '../utils/store'; @@ -27,12 +28,29 @@ function Account({ account }) { setInfo(info); setUIState('default'); } catch (e) { - alert(e); - setUIState('error'); + try { + const result = await masto.v2.search({ + q: account, + type: 'accounts', + limit: 1, + resolve: true, + }); + if (result.accounts.length) { + setInfo(result.accounts[0]); + setUIState('default'); + return; + } + alert('Account not found'); + setUIState('error'); + } catch (err) { + alert(err); + console.error(err); + setUIState('error'); + } } })(); } - }, []); + }, [account]); const { acct, @@ -138,6 +156,7 @@ function Account({ account }) { )}
{ - let { target } = e; - if (target.parentNode.tagName.toLowerCase() === 'a') { - target = target.parentNode; - } - if ( - target.tagName.toLowerCase() === 'a' && - target.classList.contains('u-url') - ) { - const targetText = ( - target.querySelector('span') || target - ).innerText.trim(); - const username = targetText.replace(/^@/, ''); - const url = target.getAttribute('href'); - const mention = mentions.find( - (mention) => - mention.username === username || - mention.acct === username || - mention.url === url, - ); - if (mention) { - e.preventDefault(); - e.stopPropagation(); - states.showAccount = mention.acct; - } else if (!/^http/i.test(targetText)) { - console.log('mention not found', targetText); - e.preventDefault(); - e.stopPropagation(); - const href = target.getAttribute('href'); - states.showAccount = href; - } - } - }} + onClick={handleAccountLinks({ mentions })} dangerouslySetInnerHTML={{ __html: enhanceContent(content, { emojis, diff --git a/src/utils/handle-account-links.js b/src/utils/handle-account-links.js new file mode 100644 index 00000000..b7156ffc --- /dev/null +++ b/src/utils/handle-account-links.js @@ -0,0 +1,40 @@ +import states from './states'; + +function handleAccountLinks(opts) { + const { mentions = [] } = opts || {}; + return (e) => { + let { target } = e; + if (target.parentNode.tagName.toLowerCase() === 'a') { + target = target.parentNode; + } + if ( + target.tagName.toLowerCase() === 'a' && + target.classList.contains('u-url') + ) { + const targetText = ( + target.querySelector('span') || target + ).innerText.trim(); + const username = targetText.replace(/^@/, ''); + const url = target.getAttribute('href'); + const mention = mentions.find( + (mention) => + mention.username === username || + mention.acct === username || + mention.url === url, + ); + if (mention) { + e.preventDefault(); + e.stopPropagation(); + states.showAccount = mention.acct; + } else if (!/^http/i.test(targetText)) { + console.log('mention not found', targetText); + e.preventDefault(); + e.stopPropagation(); + const href = target.getAttribute('href'); + states.showAccount = href; + } + } + }; +} + +export default handleAccountLinks;